Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
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

@crbroughton/msw-builder

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

  • 1.0.0
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc