Socket
Book a DemoInstallSign in
Socket

@crbroughton/msw-builder

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@crbroughton/msw-builder

A small set of helper functions for Mock Service Worker (MSW)

unpublished
latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

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:

// creates a single object from the schema
const { create } = mswBuilder(singleObjSchema)
const result = create({ id: 1 }) // is a single object
// creates an array 
const { createArray } = mswBuilder(singleObjSchema)
const result = createArray(2, { id: 2 }) // is an array of 2 results
// creates an unique array (probably the most helpful for mocking)
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.

Keywords

msw

FAQs

Package last updated on 12 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts