@darkpatternsdigital/openapi-codegen-typescript-msw
v0.9.0
Published
A typescript code generator for principled development to assist with testing using MSW
Downloads
72
Readme
OpenAPI Codegen for TypeScript-friendly MSW testing
Provides an adapter layer method for @darkpatternsdigital/openapi-codegen-typescript to integrate with msw.
npm i @darkpatternsdigital/openapi-codegen-typescript
npm i -D @darkpatternsdigital/openapi-codegen-typescript-msw
You must also have the .NET 8.0 runtime installed on your machine.
This will provide a corresponding bin to generate the typescript files. (See the @darkpatternsdigital/openapi-codegen-typescript package for command line usage details.)
openapi-codegen-typescript api.yaml api-generated/ -c
You can then create a mock MSW service to handle specific requests:
import { setupServer } from 'msw/node';
import { toMswHandler } from '@darkpatternsdigital/openapi-codegen-typescript-msw';
import operations from './api-generated/operations';
const baseDomain = 'http://localhost/';
const findPets = toMswHandler(operations.findPets, { baseDomain });
const server = setupServer(
findPets(
{ params: { tags: ['dog','cat'], limit: 10 } },
{ statusCode: 200, data: [{ name: 'Fido', tag: 'dog', id: 1234 }], mimeType: 'application/json' }
)
);
This API will use the type safety from OpenAPI along with msw.