1.0.7 • Published 11 months ago

tmsw v1.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Contributors Forks Stargazers Issues MIT License LinkedIn

About The Project

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

Getting Started

Installation

npm install tmsw --save-dev

or

yarn add tmsw --dev

or

pnpm add tmsw --save-dev

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;
}
propertydefaultdetails
basePath'trpc'Defines a basepath to match all handlers against
transformerdefaultTransformerWill transform your output data with transformer.output.serialize when calling ctx.data

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

License

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

Contact

Jake Bottrall - @jakebottrall

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

Acknowledgments

Inspired & built on msw-trpc.

1.1.0

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

12 months ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago