tmsw
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

Contributors Forks Stargazers Issues MIT License LinkedIn


tmsw

tRPC integration for MSW.
Explore the docs »

Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Contributing
  4. License
  5. Contact
  6. Acknowledgments

About The Project

tRPC integration for MSW. Inspired & built on msw-trpc. A fantastic package, all tmsw does is add support for TRPCError.

(back to top)

Getting Started

Installation

npm install tmsw --save-dev

or

yarn add tmsw --dev

or

pnpm add tmsw --save-dev

(back to top)

Usage

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",
    });
  })
);

Config

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

(back to top)

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

Jake Bottrall - @jakebottrall

Project Link: https://github.com/jakebottrall/tmsw

(back to top)

Acknowledgments

Inspired & built on msw-trpc.

(back to top)

Readme

Keywords

Package Sidebar

Install

npm i tmsw

Weekly Downloads

1

Version

1.0.7

License

MIT

Unpacked Size

14.3 kB

Total Files

9

Last publish

Collaborators

  • jakebottrall