Skip to content

Commit

Permalink
feat(client): add ability to call TRPCProxyClient-methods with `.…
Browse files Browse the repository at this point in the history
…apply()` (#3973)
  • Loading branch information
atoy40 committed May 25, 2023
1 parent fd3f409 commit 7aaf4aa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/server/src/shared/createProxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ function createInnerProxy(callback: ProxyCallback, path: string[]) {
return createInnerProxy(callback, [...path, key]);
},
apply(_1, _2, args) {
const isApply = path[path.length - 1] === 'apply';
return callback({
args,
path,
args: isApply ? (args.length >= 2 ? args[1] : []) : args,
path: isApply ? path.slice(0, -1) : path,
});
},
});
Expand Down
26 changes: 26 additions & 0 deletions packages/tests/server/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,29 @@ test('regression: JSON.stringify([undefined]) gives [null] causes wrong type to
expect(await proxy.q.query()).toMatchInlineSnapshot(`Object {}`);
await close();
});

describe('apply()', () => {
test('query without input', async () => {
const t = initTRPC.create();
const router = t.router({
hello: t.procedure.query(() => 'world'),
});
const { close, proxy } = routerToServerAndClientNew(router);
expect(await proxy.hello.query.apply(undefined)).toBe('world');
await close();
});

test('query with input', async () => {
const t = initTRPC.create();
const router = t.router({
helloinput: t.procedure
.input(z.string())
.query(({ input }) => `hello ${input}`),
});
const { close, proxy } = routerToServerAndClientNew(router);
expect(await proxy.helloinput.query.apply(undefined, ['world'])).toBe(
'hello world',
);
await close();
});
});

2 comments on commit 7aaf4aa

@vercel
Copy link

@vercel vercel bot commented on 7aaf4aa May 25, 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-three-neon.vercel.app
og-image-trpc.vercel.app
og-image.trpc.io

@vercel
Copy link

@vercel vercel bot commented on 7aaf4aa May 25, 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

Please sign in to comment.