diff --git a/src/msw.ts b/src/msw.ts index c34bb97..89113fd 100644 --- a/src/msw.ts +++ b/src/msw.ts @@ -29,30 +29,34 @@ const getCollectionHandlers = ({ collectionName: string; server: BaseServer; }) => { - return http.all(`${baseUrl}/${collectionName}`, async ({ request }) => { - const url = new URL(request.url); - const params = Object.fromEntries( - Array.from(new URLSearchParams(url.search).entries()).map( - ([key, value]) => [key, JSON.parse(value)], - ), - ); - let requestJson: Record | undefined = undefined; - try { - const text = await request.text(); - requestJson = JSON.parse(text); - } catch (e) { - // not JSON, no big deal - } - const response = server.handleRequest({ - url: request.url.split('?')[0], - method: request.method, - requestJson, - params, - }); + return http.all( + // Using a regex ensures we match all URLs that start with the collection name + new RegExp(`${baseUrl}/${collectionName}`), + async ({ request }) => { + const url = new URL(request.url); + const params = Object.fromEntries( + Array.from(new URLSearchParams(url.search).entries()).map( + ([key, value]) => [key, JSON.parse(value)], + ), + ); + let requestJson: Record | undefined = undefined; + try { + const text = await request.text(); + requestJson = JSON.parse(text); + } catch (e) { + // not JSON, no big deal + } + const response = server.handleRequest({ + url: request.url.split('?')[0], + method: request.method, + requestJson, + params, + }); - return HttpResponse.json(response.body, { - status: response.status, - headers: response.headers, - }); - }); + return HttpResponse.json(response.body, { + status: response.status, + headers: response.headers, + }); + }, + ); };