0.0.2 • Published 5 months ago

mock-service-builder v0.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

mock-service-builder

This is a small wrapper around msw. The purpose of this utility is to build a temporary mocking layer that intercepts requests and returns data in the right format. This is an implementation of Design By Contract.

Usage

import { CreatePetResponse } from "@mycorp/pet-store-client";
import { MockServiceBuilder } from "mock-builder";

export const worker = () => {
    const builder = new MockServiceBuilder("https://pet.api.example.com/v1");

    /*
    By setting the return value to Promise<CreatePetResponse>,
    typescript will enforce the correct return values.
    */
    builder.post("pets", async (req): Promise<CreatePetResponse> => {
        const { name, species } = await req.json();

        const id = crypto.randomUUID();
        const created = Date.UTC()

        // dunno, store it somehow in indexedb?
        await idb.set(id, {name, species, created})

        return { id, created , name, species, birthdate };
    });

    return builder.start();
};

// call this somewhere top level in your app
worker();
0.0.2

5 months ago

0.0.1

5 months ago