Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid including MSW in the bundle - option 1 #77

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading