2.0.0 • Published 6 months ago

msw-symlinked v2.0.0

Weekly downloads
-
License
SEE LICENCE IN LI...
Repository
-
Last release
6 months ago

Installation

npm install msw-symlinked

Stubbing Responses with MSW and msw-symlinked

When working with MSW (Mock Service Worker), you can stub responses for your API requests using the stubResponse function. This function allows you to provide file-based stubs and optional response variants.

Code Example

Create real response files, for example exampleapi.200, exampleapi.500

Then create a symlink examplejson, pointing to one of the response files.

import { rest } from 'msw';
import { stubResponse } from 'msw-symlinked';

// Define your MSW request handler
const exampleRequestHandler = rest.get('/api/example', async (req, res, ctx) => stubResponse('/home/user/exampleapi'));

// Start mocking the API using MSW with the request handler
worker.start(exampleRequestHandler);

msw-symlinked will check the symlinked file, and will gather HTTP code and variant name.

If you need to add custom logic for the specific variant, create a file with variant name (exampleapi.200.EXAMPLE_VARIANT) and pass variants map to the stubResponse:

import { rest } from 'msw';
import { stubResponse } from 'msw-symlinked';

// Define your MSW request handler
const exampleRequestHandler = rest.get('/api/example', async (req, res, ctx) => {
  // Stub the response using the stubResponse function
  const response = await stubResponse('/home/user/example', {
    EXAMPLE_VARIANT: [ctx.set('Header', 'Heder-value')]
  });
});

// Start mocking the API using MSW with the request handler
worker.start(exampleRequestHandler);

In this example when symlink will be pointing to the exampleapi.200.EXAMPLE_VARIANT stub, additional headers will be added to the response.

2.0.0

6 months ago

1.1.6

9 months ago

1.1.5

10 months ago

1.1.4

10 months ago

1.1.3

10 months ago

1.1.2

10 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.0

10 months ago