Skip to content

Commit

Permalink
Avoid including MSW in the bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Jun 9, 2024
1 parent 0343b00 commit 65acf90
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Then configure an MSW worker:

```js
// in ./src/fakeServer.js
import { http } from 'msw';
import { setupWorker } from "msw/browser";
import { getMswHandler } from "fakerest";

Expand All @@ -51,7 +52,10 @@ const handler = getMswHandler({
}
}
});
export const worker = setupWorker(handler);
export const worker = setupWorker(
// Make sure you use a RegExp to target all calls to the API
http.all(/http:\/\/localhost:3000/, handler)
);
```

Finally, call the `worker.start()` method before rendering your application. For instance, in a Vite React application:
Expand Down
3 changes: 2 additions & 1 deletion example/msw.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { http } from 'msw';
import { setupWorker } from 'msw/browser';
import { getMswHandler } from '../src';
import { data } from './data';
Expand All @@ -10,7 +11,7 @@ export const initializeMsw = async () => {
data,
middlewares,
});
const worker = setupWorker(handler);
const worker = setupWorker(http.all(/http:\/\/localhost:3000/, handler));
return worker.start({
quiet: true, // Instruct MSW to not log requests in the console
onUnhandledRequest: 'bypass', // Instruct MSW to ignore requests we don't handle
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 8 additions & 14 deletions src/adapters/MswAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { http, HttpResponse } from 'msw';
import { SimpleRestServer } from '../SimpleRestServer.js';
import type { BaseServerOptions } from '../SimpleRestServer.js';
import type { APIServer, NormalizedRequest } from '../types.js';
Expand All @@ -11,19 +10,14 @@ export class MswAdapter {
}

getHandler() {
return http.all(
// Using a regex ensures we match all URLs that start with the collection name
new RegExp(`${this.server.baseUrl}`),
async ({ request }) => {
const normalizedRequest =
await this.getNormalizedRequest(request);
const response = await this.server.handle(normalizedRequest);
return HttpResponse.json(response.body, {
status: response.status,
headers: response.headers,
});
},
);
return async ({ request }: { request: Request }) => {
const normalizedRequest = await this.getNormalizedRequest(request);
const response = await this.server.handle(normalizedRequest);
return new Response(JSON.stringify(response.body), {
status: response.status,
headers: response.headers,
});
};
}

async getNormalizedRequest(request: Request): Promise<NormalizedRequest> {
Expand Down

0 comments on commit 65acf90

Please sign in to comment.