Skip to content

Commit

Permalink
feat(server): deprecate .createCaller() in favor of `t.createCaller…
Browse files Browse the repository at this point in the history
…Factory()` (#5213)
  • Loading branch information
KATT committed Dec 26, 2023
1 parent b005dc6 commit 50cf164
Show file tree
Hide file tree
Showing 19 changed files with 325 additions and 87 deletions.
4 changes: 2 additions & 2 deletions examples/.experimental/next-formdata/src/server/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* - We export only the functionality that we use so we can enforce which base procedures should be used
*
* Learn how to create protected base procedures and other things below:
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/v10/procedures
* @see https://trpc.io/docs/router
* @see https://trpc.io/docs/procedures
*/
import { initTRPC } from '@trpc/server';
import * as trpcNext from '@trpc/server/adapters/next';
Expand Down
2 changes: 1 addition & 1 deletion examples/.test/internal-types-export/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const someMiddleware = t.middleware(({ next }) => {
return next();
});

export function genericRouter<S extends (value: any) => unknown>(schema: S) {
export function genericRouter<TSchema extends (value: any) => unknown>(schema: TSchema) {
return t.router({
foo: t.procedure.output(schema).query(() => 'bar'),
});
Expand Down
12 changes: 6 additions & 6 deletions examples/.test/ssg/src/server/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@
* - We export only the functionality that we use so we can enforce which base procedures should be used
*
* Learn how to create protected base procedures and other things below:
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/v10/procedures
* @see https://trpc.io/docs/router
* @see https://trpc.io/docs/procedures
*/
import { initTRPC } from '@trpc/server';
import superjson from 'superjson';

const t = initTRPC.create({
/**
* @see https://trpc.io/docs/v10/data-transformers
* @see https://trpc.io/docs/data-transformers
*/
transformer: superjson,
});

/**
* Create a router
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/router
*/
export const router = t.router;

/**
* Create an unprotected procedure
* @see https://trpc.io/docs/v10/procedures
* @see https://trpc.io/docs/procedures
**/
export const publicProcedure = t.procedure;

/**
* @see https://trpc.io/docs/v10/middlewares
* @see https://trpc.io/docs/middlewares
*/
export const middleware = t.middleware;
4 changes: 2 additions & 2 deletions examples/next-edge-runtime/src/server/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* - We export only the functionality that we use so we can enforce which base procedures should be used
*
* Learn how to create protected base procedures and other things below:
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/v10/procedures
* @see https://trpc.io/docs/router
* @see https://trpc.io/docs/procedures
*/
import { initTRPC } from '@trpc/server';

Expand Down
4 changes: 2 additions & 2 deletions examples/next-minimal-starter/src/server/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* - We export only the functionality that we use so we can enforce which base procedures should be used
*
* Learn how to create protected base procedures and other things below:
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/v10/procedures
* @see https://trpc.io/docs/router
* @see https://trpc.io/docs/procedures
*/
import { initTRPC } from '@trpc/server';

Expand Down
3 changes: 3 additions & 0 deletions examples/next-prisma-starter/.github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ jobs:
- run: pnpm build
- run: pnpm test-e2e

- name: Check types
run: pnpm tsc

- name: Upload test results
if: ${{ always() }}
uses: actions/upload-artifact@v2
Expand Down
4 changes: 3 additions & 1 deletion examples/next-prisma-starter/src/server/routers/_app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This file contains the root router of your tRPC-backend
*/
import { publicProcedure, router } from '../trpc';
import { createCallerFactory, publicProcedure, router } from '../trpc';
import { postRouter } from './post';

export const appRouter = router({
Expand All @@ -10,4 +10,6 @@ export const appRouter = router({
post: postRouter,
});

export const createCaller = createCallerFactory(appRouter);

export type AppRouter = typeof appRouter;
6 changes: 3 additions & 3 deletions examples/next-prisma-starter/src/server/routers/post.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Integration test example for the `post` router
*/
import { createContextInner } from '../context';
import { AppRouter, appRouter } from './_app';
import { inferProcedureInput } from '@trpc/server';
import { createContextInner } from '../context';
import { AppRouter, createCaller } from './_app';

test('add and get post', async () => {
const ctx = await createContextInner({});
const caller = appRouter.createCaller(ctx);
const caller = createCaller(ctx);

const input: inferProcedureInput<AppRouter['post']['add']> = {
text: 'hello test',
Expand Down
23 changes: 15 additions & 8 deletions examples/next-prisma-starter/src/server/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* - We export only the functionality that we use so we can enforce which base procedures should be used
*
* Learn how to create protected base procedures and other things below:
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/v10/procedures
* @see https://trpc.io/docs/router
* @see https://trpc.io/docs/procedures
*/

import { initTRPC } from '@trpc/server';
Expand All @@ -14,11 +14,11 @@ import { Context } from './context';

const t = initTRPC.context<Context>().create({
/**
* @see https://trpc.io/docs/v10/data-transformers
* @see https://trpc.io/docs/data-transformers
*/
transformer,
/**
* @see https://trpc.io/docs/v10/error-formatting
* @see https://trpc.io/docs/error-formatting
*/
errorFormatter({ shape }) {
return shape;
Expand All @@ -27,22 +27,29 @@ const t = initTRPC.context<Context>().create({

/**
* Create a router
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/router
*/
export const router = t.router;

/**
* Create an unprotected procedure
* @see https://trpc.io/docs/v10/procedures
* @see https://trpc.io/docs/procedures
**/
export const publicProcedure = t.procedure;

/**
* @see https://trpc.io/docs/v10/middlewares
* @see https://trpc.io/docs/middlewares
*/
export const middleware = t.middleware;

/**
* @see https://trpc.io/docs/v10/merging-routers
* Merge multiple routers together
* @see https://trpc.io/docs/merging-routers
*/
export const mergeRouters = t.mergeRouters;

/**
* Create a server-side caller
* @see https://trpc.io/docs/server/server-side-calls
*/
export const createCallerFactory = t.createCallerFactory;
16 changes: 8 additions & 8 deletions examples/next-prisma-websockets-starter/src/server/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* - We export only the functionality that we use so we can enforce which base procedures should be used
*
* Learn how to create protected base procedures and other things below:
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/v10/procedures
* @see https://trpc.io/docs/router
* @see https://trpc.io/docs/procedures
*/

import { Context } from './context';
Expand All @@ -14,11 +14,11 @@ import superjson from 'superjson';

const t = initTRPC.context<Context>().create({
/**
* @see https://trpc.io/docs/v10/data-transformers
* @see https://trpc.io/docs/data-transformers
*/
transformer: superjson,
/**
* @see https://trpc.io/docs/v10/error-formatting
* @see https://trpc.io/docs/error-formatting
*/
errorFormatter({ shape }) {
return shape;
Expand All @@ -27,23 +27,23 @@ const t = initTRPC.context<Context>().create({

/**
* Create a router
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/router
*/
export const router = t.router;

/**
* Create an unprotected procedure
* @see https://trpc.io/docs/v10/procedures
* @see https://trpc.io/docs/procedures
**/
export const publicProcedure = t.procedure;

/**
* @see https://trpc.io/docs/v10/middlewares
* @see https://trpc.io/docs/middlewares
*/
export const middleware = t.middleware;

/**
* @see https://trpc.io/docs/v10/merging-routers
* @see https://trpc.io/docs/merging-routers
*/
export const mergeRouters = t.mergeRouters;

Expand Down
8 changes: 4 additions & 4 deletions examples/soa/server-lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ const t = initTRPC.context<Context>().create();

/**
* Create a router
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/router
*/
export const router = t.router;

/**
* Create an unprotected procedure
* @see https://trpc.io/docs/v10/procedures
* @see https://trpc.io/docs/procedures
**/
export const publicProcedure = t.procedure;

/**
* @see https://trpc.io/docs/v10/middlewares
* @see https://trpc.io/docs/middlewares
*/
export const middleware = t.middleware;

/**
* @see https://trpc.io/docs/v10/merging-routers
* @see https://trpc.io/docs/merging-routers
*/
export const mergeRouters = t.mergeRouters;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"current-branch": "echo \"$(git rev-parse --abbrev-ref HEAD)\" | sed -E 's/refs\\/heads\\///' | sed -E 's/\\W|_/-/g' | sed -e 's/\\//-/'",
"publish-canary": "lerna publish --force-publish --canary --preid alpha-$(pnpm --silent canary-preid) --dist-tag $(pnpm --silent current-branch)",
"publish-prerelease": "pnpm install && lerna publish prerelease --force-publish --dist-tag next --no-push && sleep 60 && git push --follow-tags",
"merge-main": "git checkout main && git pull && git checkout next && git pull && git checkout -b next-merge-$(date '+%Y-%m-%d--%H-%M') next && git merge --ff main"
"merge-main": "git checkout main && git pull && git checkout next && git pull && git checkout -b next-merge-$(date '+%Y-%m-%d--%H-%M') next && git merge --ff-only main"
},
"dependencies": {
"@changesets/changelog-github": "^0.5.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/server/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ export type {
ProcedureRouterRecord,
CreateRouterInner,
Router,
RouterCaller,
AnyRouterDef,
} from './router';
export { callProcedure } from './router';
export { callProcedure, createCallerFactory } from './router';
export type {
Procedure,
AnyProcedure,
Expand Down
11 changes: 10 additions & 1 deletion packages/server/src/core/initTRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { mergeRouters } from './internals/mergeRouters';
import { createBuilder } from './internals/procedureBuilder';
import { Overwrite, PickFirstDefined, ValidateShape } from './internals/utils';
import { createMiddlewareFactory } from './middleware';
import { createRouterFactory } from './router';
import { createCallerFactory, createRouterFactory } from './router';

type PartialRootConfigTypes = Partial<RootConfigTypes>;

Expand Down Expand Up @@ -148,22 +148,31 @@ function createTRPCInner<TParams extends PartialRootConfigTypes>() {
_config: config,
/**
* Builder object for creating procedures
* @see https://trpc.io/docs/server/procedures
*/
procedure: createBuilder<$Config>({
meta: runtime?.defaultMeta,
}),
/**
* Create reusable middlewares
* @see https://trpc.io/docs/server/middlewares
*/
middleware: createMiddlewareFactory<$Config>(),
/**
* Create a router
* @see https://trpc.io/docs/server/routers
*/
router: createRouterFactory<$Config>(config),
/**
* Merge Routers
* @see https://trpc.io/docs/server/merging-routers
*/
mergeRouters,
/**
* Create a server-side caller for a router
* @see https://trpc.io/docs/server/server-side-calls
*/
createCallerFactory: createCallerFactory<$Config>(),
};
};
}
8 changes: 1 addition & 7 deletions packages/server/src/core/internals/procedureBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,8 @@ export interface ProcedureCallOptions {
}

const codeblock = `
If you want to call this function on the server, you do the following:
This is a client-only function.
const caller = appRouter.createCaller({
/* ... your context */
});
const result = await caller.call('myProcedure', input);
If you want to call this function on the server, see https://trpc.io/docs/server/server-side-calls
`.trim();

function createProcedureCaller(_def: AnyProcedureBuilderDef): AnyProcedure {
Expand Down

3 comments on commit 50cf164

@vercel
Copy link

@vercel vercel bot commented on 50cf164 Dec 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-prisma-starter – ./examples/next-prisma-starter

next-prisma-starter-trpc.vercel.app
next-prisma-starter-git-main-trpc.vercel.app
nextjs.trpc.io

@vercel
Copy link

@vercel vercel bot commented on 50cf164 Dec 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

og-image – ./www/og-image

og-image-git-main-trpc.vercel.app
og-image.trpc.io
og-image-three-neon.vercel.app
og-image-trpc.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 50cf164 Dec 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

www – ./www

www-git-main-trpc.vercel.app
trpc.io
beta.trpc.io
www-trpc.vercel.app
www.trpc.io
alpha.trpc.io

Please sign in to comment.