Gathering detailed insights and metrics for msw-trpc
Gathering detailed insights and metrics for msw-trpc
Gathering detailed insights and metrics for msw-trpc
Gathering detailed insights and metrics for msw-trpc
npm install msw-trpc
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
208 Stars
47 Commits
21 Forks
8 Watching
10 Branches
9 Contributors
Updated on 14 Nov 2024
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
23.8%
7,552
Compared to previous day
Last week
48.8%
40,255
Compared to previous week
Last month
51.5%
115,002
Compared to previous month
Last year
195.1%
813,468
Compared to previous year
[!WARNING] You are looking at a pre-release version of msw-trpc, which adds support for msw v2. Documentation may be out of date and bugs might occur, use at your own risk
As someone who loves MSW and was already using it I wanted to keep using it instead of mocking tRPC. While it is possible to simply write the Rest handlers it felt like it would be great not to lose the full power of tRPC types in the tests.
1. Install msw-trpc
.
1npm i msw-trpc --save-dev
2. build your trpcMsw with createTRPCMsw.
1import { createTRPCMsw } from 'msw-trpc' 2import type { AppRouter } from 'path/to/your/router' 3 4export const trpcMsw = createTRPCMsw<AppRouter>() /* 👈 */
3. Start using it.
1const server = setupServer( 2 trpcMsw.userById.query((req, res, ctx) => { 3 return res(ctx.status(200), ctx.data({ id: '1', name: 'Uncle bob' })) 4 }), 5 trpcMsw.createUser.mutation(async (req, res, ctx) => { 6 return res(ctx.status(200), ctx.data({ id: '2', name: await req.json() })) 7 }) 8)
createTRPCMsw
returns a Proxy that infers types from your AppRouter
1// all queries will expose a query function that accepts a MSW handler 2trpcMsw.myQuery.query((req, res, ctx) => {}) 3 4// all mutations will expose a mutation function that accepts a MSW handler 5trpcMsw.myMutation.mutation((req, res, ctx) => {})
supports merged routers
1// @filename: routers/_app.ts 2// taken from https://trpc.io/docs/merging-routers 3import { userRouter } from './user' 4import { postRouter } from './post' 5 6const appRouter = router({ 7 user: userRouter, // put procedures under "user" namespace 8 post: postRouter, // put procedures under "post" namespace 9}) 10 11// @filename: frontend/test/PostList.tsx 12 13// all nested routers will be infered properly 14trpcMsw.user.list.query((req, res, ctx) => {})
ctx.data
Inspired by MSW GraphQL the context of your handlers now exposes a data function that will transform your data to the tRPC structure
1mswTrpc.userById.query((req, res, ctx) => { 2 console.log(ctx.data({ my: 'data' })) /* 👈 */ 3}) 4 5// return ctx.json with 6{ 7 result: { 8 data: { 9 my: 'data' 10 } 11 } 12}
req.getInput
Returns the parsed input from the request
1//router.ts 2const appRouter = t.router({ 3 userById: t.procedure.input(z.object({ name: z.string() })).query(req => { 4 const { input } = req 5 const user = userList.find(u => u.name === input.name) 6 return user 7 }), 8}) 9 10//test-file.ts 11mswTrpc.userById.query((req, res, ctx) => { 12 console.log(req.getInput()) /* 👈 */ 13}) 14 15mswTrpc.createUser.mutation(async (req, res, ctx) => { 16 console.log(await req.getInput()) /* 👈 in mutation handle getInput returns a promise because it uses req.json() */ 17}) 18 19// outputs 20{ 21 name: 'Pedro' 22} 23 24test('should do something', () => { 25 trpc.userById.query({ name: 'Pedro' }) 26})
Please note:
req.getInput
and req.json in the same mutation handler will failsuperjson
1import { createTRPCMsw } from 'msw-trpc' 2import type { AppRouter } from 'path/to/your/router' 3import superjson from 'superjson'; 4 5export const trpcMsw = createTRPCMsw<AppRouter>({ 6 transformer: { 7 input: superjson, /* 👈 */ 8 output: superjson, /* 👈 */ 9 }, 10})
createTRPCMsw
accepts a 2nd argument:
1type config = { 2 basePath?: string 3 baseUrl?: string 4 transformer?: { 5 input: { 6 serialize(object: any): any 7 deserialize(object: any): any 8 } 9 output: { 10 serialize(object: any): any 11 deserialize(object: any): any 12 } 13 } 14}
property | default | details |
---|---|---|
basePath | 'trpc' | Will match all requests to basePath regardless of host |
baseUrl | undefined | Setting this overrides basePath and will only match requests to this specific baseUrl |
transformer | defaultTransformer | Will transform your output data with transformer.output.serialize when calling ctx.data |
Peer dependencies:
Please note:
1mswTrpc.user.list.query() // this will match /trpc/user/list and /trpc/user.list
No vulnerabilities found.
No security vulnerabilities found.