Table of Contents
tRPC integration for MSW. Inspired & built on msw-trpc. A fantastic package, all tmsw
does is add support for TRPCError
.
npm install tmsw --save-dev
or
yarn add tmsw --dev
or
pnpm add tmsw --save-dev
Use createTMSW
& your apps AppRouter
to build typesafe msw handlers, like so:
import { setupServer } from "msw/node";
import { createTMSW } from "tmsw";
const tmsw = createTMSW<AppRouter>();
const server = setupServer(
tmsw.getManyCountries.query((req, res, ctx) => {
return res(
ctx.status(200),
ctx.data([{ name: "Tanzania", continent: "Africa" }])
);
}),
tmsw.addOneCountry.mutation((req, res, ctx) => {
return res(
ctx.status(200),
ctx.data({ name: "Tanzania", continent: "Africa" })
);
}),
tmsw.nested.getManyCities.query((req, res, ctx) => {
return res(
ctx.status(200),
ctx.data([{ name: "Dodoma", country: "Tanzania" }])
);
}),
tmsw.nested.addOneCity.mutation((req, res, ctx) => {
return res(
ctx.status(200),
ctx.data({ name: "Dodoma", country: "Tanzania" })
);
})
);
tmsw
also supports TRPCError
. Simply throw them like you would in a procedure:
server.use(
tmsw.getManyCountries.query(() => {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Oops. Something went wrong",
});
})
);
createTMSW
accepts a 2nd argument:
interface CreateTMSWConfig {
basePath?: string;
transformer?: CombinedDataTransformer;
}
property | default | details |
---|---|---|
basePath | 'trpc' | Defines a basepath to match all handlers against |
transformer | defaultTransformer | Will transform your output data with transformer.output.serialize when calling ctx.data |
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE for more information.
Jake Bottrall - @jakebottrall
Project Link: https://github.com/jakebottrall/tmsw
Inspired & built on msw-trpc.