msw-builder
msw-builder is a helper library that exposes small functions when
working with Mock Service Worker (MSW) & Zod. These helper functions
utilise ts-deepmerge
. The supplied schema is used as a base,
then merged with whatever overrides you wish to supply.
The goal of this library is to simplfy MSW mocking patterns,
especially copy-pasta mocking of JSON responses.
To use, simply define your zod schema (ensure you parse at the end):
const singleObjSchema = z.object({
id: z.number().default(1),
}).parse({})
Once defined, you can use any of the following functions:
const { create } = mswBuilder(singleObjSchema)
const result = create({ id: 1 })
const { createArray } = mswBuilder(singleObjSchema)
const result = createArray(2, { id: 2 })
const { createUniArray } = mswBuilder(singleObjSchema)
const result = createUniArray([
{
id: 2,
},
{
id: 3,
},
])
To install dependencies:
bun install
This project was created using bun init
in bun v1.0.0. Bun is a fast all-in-one JavaScript runtime.