-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
msw@next #26
base: main
Are you sure you want to change the base?
msw@next #26
Conversation
test('throwing error works', async () => { | ||
server.use( | ||
mswTrpc.userById.query(() => { | ||
throw new TRPCError({ code: 'BAD_REQUEST' }) | ||
}) | ||
) | ||
await expect(async () => { | ||
await trpc.userById.query('1') | ||
}).rejects.toThrow(new TRPCClientError('BAD_REQUEST')) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the API for returning 404, to throw a NOT_FOUND
TRPCError?
Hey thanks for this! I'll try to review this tomorrow. Sorry I have been pretty busy lately. |
No worries. Latest commit actually changes it to the "raw data" option and catches TRPCError. Been in use at my company since without issue |
I believe the PR must be updated to depend on [email protected] |
@@ -43,35 +44,35 @@ const createUntypedTRPCMsw = ( | |||
{}, | |||
{ | |||
get(_target: unknown, procedureKey) { | |||
if (procedureKey === 'query') { | |||
if (procedureKey === 'query' || procedureKey === 'mutation') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would you think of creating an helper for this to clean up the code a bit:
const httpMethodByProcedureKey = {
query: http.get,
post: http.post
}
const getInputByProcedureKey = {
query: getQueryInput,
mutation: getMutationInput
}
const createHandler(procedureKey: 'query' | 'mutation') => {
const httpMethod = httpMethodByProcedureKey[procedureKey]
const getInput = getInputByProcedureKey[procedureKey]
return handler => (httpMethod)(
buildUrlFromPathParts(pathParts),
async (params): Promise<any> => {
try {
const body = await handler(await getInput(params.request, transformer))
return HttpResponse.json({ result: { data: transformer.input.serialize(body) } })
} catch (e) {
if (e instanceof TRPCError) {
const status = getHTTPStatusCodeFromError(e)
return HttpResponse.json(
{
error: {
message: e.message,
code: TRPC_ERROR_CODES_BY_KEY[e.code],
data: { code: e.code, httpStatus: status },
},
},
{ status }
)
} else {
throw e
}
}
}
)
}
Do you mean the PR in itself or the package dependencies? |
This PR seems to adress this feature request: #13 |
The package dependencies must be changed from msw@^1.0.1 to msw@^2.0.10. This PR currently sets it to msw@next. Of course, that would be a breaking change. (Also for dropping support for Node <18) |
Oh you're right, I thought I had seen msw@next on their website |
"zod": "^3.20.2" | ||
}, | ||
"peerDependencies": { | ||
"@trpc/server": ">=10.9.0", | ||
"msw": ">=0.49.3" | ||
"msw": "next" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set dependency to ^2.0.0
We also need to update the doc |
Anything I can do to help unblock and get this shipped? Our team also has a prerequisite for msw v2. If it's just documentation or a breaking change hesitation could it be shipped as a pre-release version? |
I wouldn't mind that, I'm not sure I'll have the time this week but I'm open to review the PR for that! |
For publishing a pre-release version? Would need access to npm for that, but I'll happily bump the package.json |
yes sorry that is what I was referring to! |
🎯 Changes
This makes msw-trpc work with the breaking changes in MSW 2, currently released under
msw@next
. I needed this because my project is on Node 18, which is not supported in msw v1, so I was forced to upgrade msw, and thus this package too.API Change
MSW 2 changes the API a bit: https://github.com/mswjs/msw/blob/feat/standard-api/MIGRATING.md
With no context object, and response construction being done with
HttpResponse.json
I wasn't sure what the best design for this library would be. I have implemented adata()
andgetInput()
function that just gets passed in the params object.So this:
becomes
Can also use
HttpResponse
from msw and type-safety still works:Other API proposals:
Would love some feedback on the API! Thanks.