msw 2.0.12 → 2.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser/index.d.mts +104 -0
- package/lib/browser/index.d.ts +1 -1
- package/lib/browser/index.js +1 -0
- package/lib/browser/index.js.map +1 -0
- package/lib/browser/index.mjs +1 -0
- package/lib/browser/index.mjs.map +1 -0
- package/lib/core/{GraphQLHandler-da09c680.d.ts → GraphQLHandler-SHlRCcy4.d.ts} +2 -2
- package/lib/core/GraphQLHandler-zlmUDnN6.d.mts +97 -0
- package/lib/core/HttpResponse.d.mts +2 -0
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/HttpResponse.js +1 -0
- package/lib/core/HttpResponse.js.map +1 -0
- package/lib/core/HttpResponse.mjs +1 -0
- package/lib/core/HttpResponse.mjs.map +1 -0
- package/lib/core/{RequestHandler-25f9cfd1.d.ts → RequestHandler-rBDJQrEf.d.ts} +1 -1
- package/lib/core/RequestHandler-vxZdj6Tw.d.mts +179 -0
- package/lib/core/SetupApi.d.mts +26 -0
- package/lib/core/SetupApi.d.ts +1 -1
- package/lib/core/SetupApi.js +1 -0
- package/lib/core/SetupApi.js.map +1 -0
- package/lib/core/SetupApi.mjs +1 -0
- package/lib/core/SetupApi.mjs.map +1 -0
- package/lib/core/bypass.d.mts +16 -0
- package/lib/core/bypass.d.ts +1 -1
- package/lib/core/bypass.js +1 -0
- package/lib/core/bypass.js.map +1 -0
- package/lib/core/bypass.mjs +1 -0
- package/lib/core/bypass.mjs.map +1 -0
- package/lib/core/delay.d.mts +18 -0
- package/lib/core/delay.d.ts +1 -1
- package/lib/core/delay.js +1 -0
- package/lib/core/delay.js.map +1 -0
- package/lib/core/delay.mjs +1 -0
- package/lib/core/delay.mjs.map +1 -0
- package/lib/core/graphql.d.mts +109 -0
- package/lib/core/graphql.d.ts +3 -3
- package/lib/core/graphql.js +1 -0
- package/lib/core/graphql.js.map +1 -0
- package/lib/core/graphql.mjs +1 -0
- package/lib/core/graphql.mjs.map +1 -0
- package/lib/core/handlers/GraphQLHandler.d.mts +5 -0
- package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
- package/lib/core/handlers/GraphQLHandler.js +3 -2
- package/lib/core/handlers/GraphQLHandler.js.map +1 -0
- package/lib/core/handlers/GraphQLHandler.mjs +3 -2
- package/lib/core/handlers/GraphQLHandler.mjs.map +1 -0
- package/lib/core/handlers/HttpHandler.d.mts +62 -0
- package/lib/core/handlers/HttpHandler.d.ts +2 -2
- package/lib/core/handlers/HttpHandler.js +1 -0
- package/lib/core/handlers/HttpHandler.js.map +1 -0
- package/lib/core/handlers/HttpHandler.mjs +1 -0
- package/lib/core/handlers/HttpHandler.mjs.map +1 -0
- package/lib/core/handlers/RequestHandler.d.mts +2 -0
- package/lib/core/handlers/RequestHandler.d.ts +1 -1
- package/lib/core/handlers/RequestHandler.js +3 -2
- package/lib/core/handlers/RequestHandler.js.map +1 -0
- package/lib/core/handlers/RequestHandler.mjs +3 -2
- package/lib/core/handlers/RequestHandler.mjs.map +1 -0
- package/lib/core/http.d.mts +26 -0
- package/lib/core/http.d.ts +1 -1
- package/lib/core/http.js +1 -0
- package/lib/core/http.js.map +1 -0
- package/lib/core/http.mjs +1 -0
- package/lib/core/http.mjs.map +1 -0
- package/lib/core/index.d.mts +18 -0
- package/lib/core/index.d.ts +2 -2
- package/lib/core/index.js +1 -0
- package/lib/core/index.js.map +1 -0
- package/lib/core/index.mjs +1 -0
- package/lib/core/index.mjs.map +1 -0
- package/lib/core/passthrough.d.mts +17 -0
- package/lib/core/passthrough.js +1 -0
- package/lib/core/passthrough.js.map +1 -0
- package/lib/core/passthrough.mjs +1 -0
- package/lib/core/passthrough.mjs.map +1 -0
- package/lib/core/sharedOptions.d.mts +66 -0
- package/lib/core/sharedOptions.d.ts +2 -2
- package/lib/core/sharedOptions.js +1 -0
- package/lib/core/sharedOptions.js.map +1 -0
- package/lib/core/sharedOptions.mjs +1 -0
- package/lib/core/sharedOptions.mjs.map +1 -0
- package/lib/core/typeUtils.d.mts +7 -0
- package/lib/core/typeUtils.d.ts +1 -1
- package/lib/core/typeUtils.js +1 -0
- package/lib/core/typeUtils.js.map +1 -0
- package/lib/core/typeUtils.mjs +1 -0
- package/lib/core/typeUtils.mjs.map +1 -0
- package/lib/core/utils/HttpResponse/decorators.d.mts +12 -0
- package/lib/core/utils/HttpResponse/decorators.d.ts +2 -2
- package/lib/core/utils/HttpResponse/decorators.js +1 -0
- package/lib/core/utils/HttpResponse/decorators.js.map +1 -0
- package/lib/core/utils/HttpResponse/decorators.mjs +1 -0
- package/lib/core/utils/HttpResponse/decorators.mjs.map +1 -0
- package/lib/core/utils/getResponse.d.mts +2 -0
- package/lib/core/utils/getResponse.d.ts +1 -1
- package/lib/core/utils/getResponse.js +1 -0
- package/lib/core/utils/getResponse.js.map +1 -0
- package/lib/core/utils/getResponse.mjs +1 -0
- package/lib/core/utils/getResponse.mjs.map +1 -0
- package/lib/core/utils/handleRequest.d.mts +37 -0
- package/lib/core/utils/handleRequest.d.ts +2 -2
- package/lib/core/utils/handleRequest.js +1 -0
- package/lib/core/utils/handleRequest.js.map +1 -0
- package/lib/core/utils/handleRequest.mjs +1 -0
- package/lib/core/utils/handleRequest.mjs.map +1 -0
- package/lib/core/utils/internal/Disposable.d.mts +7 -0
- package/lib/core/utils/internal/Disposable.d.ts +1 -1
- package/lib/core/utils/internal/Disposable.js +1 -0
- package/lib/core/utils/internal/Disposable.js.map +1 -0
- package/lib/core/utils/internal/Disposable.mjs +1 -0
- package/lib/core/utils/internal/Disposable.mjs.map +1 -0
- package/lib/core/utils/internal/checkGlobals.d.mts +3 -0
- package/lib/core/utils/internal/checkGlobals.js +1 -0
- package/lib/core/utils/internal/checkGlobals.js.map +1 -0
- package/lib/core/utils/internal/checkGlobals.mjs +1 -0
- package/lib/core/utils/internal/checkGlobals.mjs.map +1 -0
- package/lib/core/utils/internal/devUtils.d.mts +19 -0
- package/lib/core/utils/internal/devUtils.js +1 -0
- package/lib/core/utils/internal/devUtils.js.map +1 -0
- package/lib/core/utils/internal/devUtils.mjs +1 -0
- package/lib/core/utils/internal/devUtils.mjs.map +1 -0
- package/lib/core/utils/internal/getCallFrame.d.mts +6 -0
- package/lib/core/utils/internal/getCallFrame.js +1 -0
- package/lib/core/utils/internal/getCallFrame.js.map +1 -0
- package/lib/core/utils/internal/getCallFrame.mjs +1 -0
- package/lib/core/utils/internal/getCallFrame.mjs.map +1 -0
- package/lib/core/utils/internal/isIterable.d.mts +6 -0
- package/lib/core/utils/internal/isIterable.js +1 -0
- package/lib/core/utils/internal/isIterable.js.map +1 -0
- package/lib/core/utils/internal/isIterable.mjs +1 -0
- package/lib/core/utils/internal/isIterable.mjs.map +1 -0
- package/lib/core/utils/internal/isObject.d.mts +6 -0
- package/lib/core/utils/internal/isObject.js +1 -0
- package/lib/core/utils/internal/isObject.js.map +1 -0
- package/lib/core/utils/internal/isObject.mjs +1 -0
- package/lib/core/utils/internal/isObject.mjs.map +1 -0
- package/lib/core/utils/internal/isStringEqual.d.mts +6 -0
- package/lib/core/utils/internal/isStringEqual.js +1 -0
- package/lib/core/utils/internal/isStringEqual.js.map +1 -0
- package/lib/core/utils/internal/isStringEqual.mjs +1 -0
- package/lib/core/utils/internal/isStringEqual.mjs.map +1 -0
- package/lib/core/utils/internal/jsonParse.d.mts +7 -0
- package/lib/core/utils/internal/jsonParse.js +1 -0
- package/lib/core/utils/internal/jsonParse.js.map +1 -0
- package/lib/core/utils/internal/jsonParse.mjs +1 -0
- package/lib/core/utils/internal/jsonParse.mjs.map +1 -0
- package/lib/core/utils/internal/mergeRight.d.mts +7 -0
- package/lib/core/utils/internal/mergeRight.js +1 -0
- package/lib/core/utils/internal/mergeRight.js.map +1 -0
- package/lib/core/utils/internal/mergeRight.mjs +1 -0
- package/lib/core/utils/internal/mergeRight.mjs.map +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.d.mts +5 -0
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
- package/lib/core/utils/internal/parseGraphQLRequest.js +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.mjs +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +1 -0
- package/lib/core/utils/internal/parseMultipartData.d.mts +10 -0
- package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
- package/lib/core/utils/internal/parseMultipartData.js +1 -0
- package/lib/core/utils/internal/parseMultipartData.js.map +1 -0
- package/lib/core/utils/internal/parseMultipartData.mjs +1 -0
- package/lib/core/utils/internal/parseMultipartData.mjs.map +1 -0
- package/lib/core/utils/internal/pipeEvents.d.mts +8 -0
- package/lib/core/utils/internal/pipeEvents.js +1 -0
- package/lib/core/utils/internal/pipeEvents.js.map +1 -0
- package/lib/core/utils/internal/pipeEvents.mjs +1 -0
- package/lib/core/utils/internal/pipeEvents.mjs.map +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.d.mts +8 -0
- package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.js +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.js.map +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.mjs +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.mjs.map +1 -0
- package/lib/core/utils/internal/toReadonlyArray.d.mts +6 -0
- package/lib/core/utils/internal/toReadonlyArray.js +1 -0
- package/lib/core/utils/internal/toReadonlyArray.js.map +1 -0
- package/lib/core/utils/internal/toReadonlyArray.mjs +1 -0
- package/lib/core/utils/internal/toReadonlyArray.mjs.map +1 -0
- package/lib/core/utils/internal/tryCatch.d.mts +3 -0
- package/lib/core/utils/internal/tryCatch.js +1 -0
- package/lib/core/utils/internal/tryCatch.js.map +1 -0
- package/lib/core/utils/internal/tryCatch.mjs +1 -0
- package/lib/core/utils/internal/tryCatch.mjs.map +1 -0
- package/lib/core/utils/internal/uuidv4.d.mts +3 -0
- package/lib/core/utils/internal/uuidv4.js +1 -0
- package/lib/core/utils/internal/uuidv4.js.map +1 -0
- package/lib/core/utils/internal/uuidv4.mjs +1 -0
- package/lib/core/utils/internal/uuidv4.mjs.map +1 -0
- package/lib/core/utils/logging/getStatusCodeColor.d.mts +11 -0
- package/lib/core/utils/logging/getStatusCodeColor.js +1 -0
- package/lib/core/utils/logging/getStatusCodeColor.js.map +1 -0
- package/lib/core/utils/logging/getStatusCodeColor.mjs +1 -0
- package/lib/core/utils/logging/getStatusCodeColor.mjs.map +1 -0
- package/lib/core/utils/logging/getTimestamp.d.mts +6 -0
- package/lib/core/utils/logging/getTimestamp.js +1 -0
- package/lib/core/utils/logging/getTimestamp.js.map +1 -0
- package/lib/core/utils/logging/getTimestamp.mjs +1 -0
- package/lib/core/utils/logging/getTimestamp.mjs.map +1 -0
- package/lib/core/utils/logging/serializeRequest.d.mts +12 -0
- package/lib/core/utils/logging/serializeRequest.d.ts +1 -1
- package/lib/core/utils/logging/serializeRequest.js +1 -0
- package/lib/core/utils/logging/serializeRequest.js.map +1 -0
- package/lib/core/utils/logging/serializeRequest.mjs +1 -0
- package/lib/core/utils/logging/serializeRequest.mjs.map +1 -0
- package/lib/core/utils/logging/serializeResponse.d.mts +9 -0
- package/lib/core/utils/logging/serializeResponse.d.ts +1 -1
- package/lib/core/utils/logging/serializeResponse.js +1 -0
- package/lib/core/utils/logging/serializeResponse.js.map +1 -0
- package/lib/core/utils/logging/serializeResponse.mjs +1 -0
- package/lib/core/utils/logging/serializeResponse.mjs.map +1 -0
- package/lib/core/utils/matching/matchRequestUrl.d.mts +19 -0
- package/lib/core/utils/matching/matchRequestUrl.d.ts +1 -1
- package/lib/core/utils/matching/matchRequestUrl.js +1 -0
- package/lib/core/utils/matching/matchRequestUrl.js.map +1 -0
- package/lib/core/utils/matching/matchRequestUrl.mjs +1 -0
- package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -0
- package/lib/core/utils/matching/normalizePath.d.mts +12 -0
- package/lib/core/utils/matching/normalizePath.js +1 -0
- package/lib/core/utils/matching/normalizePath.js.map +1 -0
- package/lib/core/utils/matching/normalizePath.mjs +1 -0
- package/lib/core/utils/matching/normalizePath.mjs.map +1 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.d.mts +7 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.js +1 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.js.map +1 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.mjs +1 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.mjs.map +1 -0
- package/lib/core/utils/request/getRequestCookies.d.mts +8 -0
- package/lib/core/utils/request/getRequestCookies.js +1 -0
- package/lib/core/utils/request/getRequestCookies.js.map +1 -0
- package/lib/core/utils/request/getRequestCookies.mjs +1 -0
- package/lib/core/utils/request/getRequestCookies.mjs.map +1 -0
- package/lib/core/utils/request/onUnhandledRequest.d.mts +12 -0
- package/lib/core/utils/request/onUnhandledRequest.d.ts +2 -2
- package/lib/core/utils/request/onUnhandledRequest.js +1 -0
- package/lib/core/utils/request/onUnhandledRequest.js.map +1 -0
- package/lib/core/utils/request/onUnhandledRequest.mjs +1 -0
- package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -0
- package/lib/core/utils/request/readResponseCookies.d.mts +3 -0
- package/lib/core/utils/request/readResponseCookies.js +1 -0
- package/lib/core/utils/request/readResponseCookies.js.map +1 -0
- package/lib/core/utils/request/readResponseCookies.mjs +1 -0
- package/lib/core/utils/request/readResponseCookies.mjs.map +1 -0
- package/lib/core/utils/toResponseInit.d.mts +3 -0
- package/lib/core/utils/toResponseInit.js +1 -0
- package/lib/core/utils/toResponseInit.js.map +1 -0
- package/lib/core/utils/toResponseInit.mjs +1 -0
- package/lib/core/utils/toResponseInit.mjs.map +1 -0
- package/lib/core/utils/url/cleanUrl.d.mts +7 -0
- package/lib/core/utils/url/cleanUrl.js +1 -0
- package/lib/core/utils/url/cleanUrl.js.map +1 -0
- package/lib/core/utils/url/cleanUrl.mjs +1 -0
- package/lib/core/utils/url/cleanUrl.mjs.map +1 -0
- package/lib/core/utils/url/getAbsoluteUrl.d.mts +6 -0
- package/lib/core/utils/url/getAbsoluteUrl.js +1 -0
- package/lib/core/utils/url/getAbsoluteUrl.js.map +1 -0
- package/lib/core/utils/url/getAbsoluteUrl.mjs +1 -0
- package/lib/core/utils/url/getAbsoluteUrl.mjs.map +1 -0
- package/lib/core/utils/url/isAbsoluteUrl.d.mts +6 -0
- package/lib/core/utils/url/isAbsoluteUrl.js +1 -0
- package/lib/core/utils/url/isAbsoluteUrl.js.map +1 -0
- package/lib/core/utils/url/isAbsoluteUrl.mjs +1 -0
- package/lib/core/utils/url/isAbsoluteUrl.mjs.map +1 -0
- package/lib/iife/index.js +20 -19
- package/lib/iife/index.js.map +1 -0
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.d.mts +75 -0
- package/lib/native/index.js +1 -0
- package/lib/native/index.js.map +1 -0
- package/lib/native/index.mjs +1 -0
- package/lib/native/index.mjs.map +1 -0
- package/lib/node/index.d.mts +75 -0
- package/lib/node/index.d.ts +1 -1
- package/lib/node/index.js +1 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/index.mjs +1 -0
- package/lib/node/index.mjs.map +1 -0
- package/package.json +5 -4
- package/src/browser/global.browser.d.ts +1 -0
- package/src/browser/index.ts +3 -0
- package/src/browser/setupWorker/glossary.ts +259 -0
- package/src/browser/setupWorker/setupWorker.node.test.ts +10 -0
- package/src/browser/setupWorker/setupWorker.ts +199 -0
- package/src/browser/setupWorker/start/createFallbackRequestListener.ts +67 -0
- package/src/browser/setupWorker/start/createFallbackStart.ts +21 -0
- package/src/browser/setupWorker/start/createRequestListener.ts +127 -0
- package/src/browser/setupWorker/start/createResponseListener.ts +58 -0
- package/src/browser/setupWorker/start/createStartHandler.ts +143 -0
- package/src/browser/setupWorker/start/utils/createMessageChannel.ts +32 -0
- package/src/browser/setupWorker/start/utils/enableMocking.ts +32 -0
- package/src/browser/setupWorker/start/utils/getWorkerByRegistration.ts +25 -0
- package/src/browser/setupWorker/start/utils/getWorkerInstance.ts +94 -0
- package/src/browser/setupWorker/start/utils/prepareStartHandler.test.ts +59 -0
- package/src/browser/setupWorker/start/utils/prepareStartHandler.ts +44 -0
- package/src/browser/setupWorker/start/utils/printStartMessage.test.ts +84 -0
- package/src/browser/setupWorker/start/utils/printStartMessage.ts +40 -0
- package/src/browser/setupWorker/start/utils/validateWorkerScope.ts +18 -0
- package/src/browser/setupWorker/stop/createFallbackStop.ts +11 -0
- package/src/browser/setupWorker/stop/createStop.ts +29 -0
- package/src/browser/setupWorker/stop/utils/printStopMessage.test.ts +26 -0
- package/src/browser/setupWorker/stop/utils/printStopMessage.ts +12 -0
- package/src/browser/tsconfig.browser.build.json +6 -0
- package/src/browser/tsconfig.browser.json +9 -0
- package/src/browser/utils/deferNetworkRequestsUntil.test.ts +48 -0
- package/src/browser/utils/deferNetworkRequestsUntil.ts +29 -0
- package/src/browser/utils/getAbsoluteWorkerUrl.test.ts +31 -0
- package/src/browser/utils/getAbsoluteWorkerUrl.ts +7 -0
- package/src/browser/utils/parseWorkerRequest.ts +15 -0
- package/src/browser/utils/pruneGetRequestBody.test.ts +53 -0
- package/src/browser/utils/pruneGetRequestBody.ts +21 -0
- package/src/browser/utils/requestIntegrityCheck.ts +23 -0
- package/src/browser/utils/supportsReadableStreamTransfer.ts +17 -0
- package/src/core/HttpResponse.test.ts +200 -0
- package/src/core/HttpResponse.ts +134 -0
- package/src/core/SetupApi.ts +95 -0
- package/src/core/bypass.test.ts +47 -0
- package/src/core/bypass.ts +36 -0
- package/src/core/delay.ts +70 -0
- package/src/core/graphql.test.ts +11 -0
- package/src/core/graphql.ts +138 -0
- package/src/core/handlers/GraphQLHandler.test.ts +820 -0
- package/src/core/handlers/GraphQLHandler.ts +263 -0
- package/src/core/handlers/HttpHandler.test.ts +218 -0
- package/src/core/handlers/HttpHandler.ts +169 -0
- package/src/core/handlers/RequestHandler.ts +338 -0
- package/src/core/http.test.ts +15 -0
- package/src/core/http.ts +51 -0
- package/src/core/index.ts +56 -0
- package/src/core/passthrough.test.ts +13 -0
- package/src/core/passthrough.ts +23 -0
- package/src/core/sharedOptions.ts +66 -0
- package/src/core/typeUtils.ts +20 -0
- package/src/core/utils/HttpResponse/decorators.ts +56 -0
- package/src/core/utils/getResponse.ts +55 -0
- package/src/core/utils/handleRequest.test.ts +554 -0
- package/src/core/utils/handleRequest.ts +132 -0
- package/src/core/utils/internal/Disposable.ts +9 -0
- package/src/core/utils/internal/checkGlobals.ts +17 -0
- package/src/core/utils/internal/devUtils.ts +31 -0
- package/src/core/utils/internal/getCallFrame.test.ts +154 -0
- package/src/core/utils/internal/getCallFrame.ts +35 -0
- package/src/core/utils/internal/isIterable.test.ts +23 -0
- package/src/core/utils/internal/isIterable.ts +12 -0
- package/src/core/utils/internal/isObject.test.ts +20 -0
- package/src/core/utils/internal/isObject.ts +6 -0
- package/src/core/utils/internal/isStringEqual.test.ts +45 -0
- package/src/core/utils/internal/isStringEqual.ts +6 -0
- package/src/core/utils/internal/jsonParse.test.ts +13 -0
- package/src/core/utils/internal/jsonParse.ts +13 -0
- package/src/core/utils/internal/mergeRight.test.ts +43 -0
- package/src/core/utils/internal/mergeRight.ts +27 -0
- package/src/core/utils/internal/parseGraphQLRequest.test.ts +99 -0
- package/src/core/utils/internal/parseGraphQLRequest.ts +205 -0
- package/src/core/utils/internal/parseMultipartData.test.ts +76 -0
- package/src/core/utils/internal/parseMultipartData.ts +104 -0
- package/src/core/utils/internal/pipeEvents.test.ts +14 -0
- package/src/core/utils/internal/pipeEvents.ts +25 -0
- package/src/core/utils/internal/requestHandlerUtils.ts +21 -0
- package/src/core/utils/internal/toReadonlyArray.test.ts +30 -0
- package/src/core/utils/internal/toReadonlyArray.ts +8 -0
- package/src/core/utils/internal/tryCatch.test.ts +29 -0
- package/src/core/utils/internal/tryCatch.ts +11 -0
- package/src/core/utils/internal/uuidv4.ts +3 -0
- package/src/core/utils/logging/getStatusCodeColor.test.ts +22 -0
- package/src/core/utils/logging/getStatusCodeColor.ts +20 -0
- package/src/core/utils/logging/getTimestamp.test.ts +18 -0
- package/src/core/utils/logging/getTimestamp.ts +12 -0
- package/src/core/utils/logging/serializeRequest.test.ts +23 -0
- package/src/core/utils/logging/serializeRequest.ts +23 -0
- package/src/core/utils/logging/serializeResponse.test.ts +77 -0
- package/src/core/utils/logging/serializeResponse.ts +31 -0
- package/src/core/utils/matching/matchRequestUrl.test.ts +114 -0
- package/src/core/utils/matching/matchRequestUrl.ts +73 -0
- package/src/core/utils/matching/normalizePath.node.test.ts +44 -0
- package/src/core/utils/matching/normalizePath.test.ts +50 -0
- package/src/core/utils/matching/normalizePath.ts +21 -0
- package/src/core/utils/request/getPublicUrlFromRequest.test.ts +26 -0
- package/src/core/utils/request/getPublicUrlFromRequest.ts +15 -0
- package/src/core/utils/request/getRequestCookies.node.test.ts +29 -0
- package/src/core/utils/request/getRequestCookies.test.ts +64 -0
- package/src/core/utils/request/getRequestCookies.ts +75 -0
- package/src/core/utils/request/onUnhandledRequest.test.ts +215 -0
- package/src/core/utils/request/onUnhandledRequest.ts +247 -0
- package/src/core/utils/request/readResponseCookies.ts +9 -0
- package/src/core/utils/toResponseInit.ts +7 -0
- package/src/core/utils/url/cleanUrl.test.ts +17 -0
- package/src/core/utils/url/cleanUrl.ts +12 -0
- package/src/core/utils/url/getAbsoluteUrl.node.test.ts +19 -0
- package/src/core/utils/url/getAbsoluteUrl.test.ts +29 -0
- package/src/core/utils/url/getAbsoluteUrl.ts +26 -0
- package/src/core/utils/url/isAbsoluteUrl.test.ts +32 -0
- package/src/core/utils/url/isAbsoluteUrl.ts +6 -0
- package/src/iife/index.ts +2 -0
- package/src/mockServiceWorker.js +287 -0
- package/src/native/index.ts +17 -0
- package/src/node/SetupServerApi.ts +113 -0
- package/src/node/glossary.ts +62 -0
- package/src/node/index.ts +3 -0
- package/src/node/setupServer.ts +21 -0
- package/src/node/utils/isNodeExceptionLike.ts +14 -0
- package/src/tsconfig.core.build.json +6 -0
- package/src/tsconfig.node.build.json +6 -0
- package/src/tsconfig.node.json +8 -0
- package/src/tsconfig.src.json +15 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
onUnhandledRequest,
|
|
6
|
+
UnhandledRequestCallback,
|
|
7
|
+
} from './onUnhandledRequest'
|
|
8
|
+
import { HttpHandler, HttpMethods } from '../../handlers/HttpHandler'
|
|
9
|
+
import { ResponseResolver } from '../../handlers/RequestHandler'
|
|
10
|
+
|
|
11
|
+
const resolver: ResponseResolver = () => void 0
|
|
12
|
+
|
|
13
|
+
const fixtures = {
|
|
14
|
+
warningWithoutSuggestions: `\
|
|
15
|
+
[MSW] Warning: intercepted a request without a matching request handler:
|
|
16
|
+
|
|
17
|
+
• GET /api
|
|
18
|
+
|
|
19
|
+
If you still wish to intercept this unhandled request, please create a request handler for it.
|
|
20
|
+
Read more: https://mswjs.io/docs/getting-started/mocks`,
|
|
21
|
+
|
|
22
|
+
errorWithoutSuggestions: `\
|
|
23
|
+
[MSW] Error: intercepted a request without a matching request handler:
|
|
24
|
+
|
|
25
|
+
• GET /api
|
|
26
|
+
|
|
27
|
+
If you still wish to intercept this unhandled request, please create a request handler for it.
|
|
28
|
+
Read more: https://mswjs.io/docs/getting-started/mocks`,
|
|
29
|
+
|
|
30
|
+
warningWithSuggestions: (suggestions: string) => `\
|
|
31
|
+
[MSW] Warning: intercepted a request without a matching request handler:
|
|
32
|
+
|
|
33
|
+
• GET /api
|
|
34
|
+
|
|
35
|
+
Did you mean to request one of the following resources instead?
|
|
36
|
+
|
|
37
|
+
${suggestions}
|
|
38
|
+
|
|
39
|
+
If you still wish to intercept this unhandled request, please create a request handler for it.
|
|
40
|
+
Read more: https://mswjs.io/docs/getting-started/mocks`,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
vi.spyOn(console, 'warn').mockImplementation(() => void 0)
|
|
45
|
+
vi.spyOn(console, 'error').mockImplementation(() => void 0)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
afterEach(() => {
|
|
49
|
+
vi.restoreAllMocks()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('supports the "bypass" request strategy', async () => {
|
|
53
|
+
await onUnhandledRequest(
|
|
54
|
+
new Request(new URL('http://localhost/api')),
|
|
55
|
+
[],
|
|
56
|
+
'bypass',
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
expect(console.warn).not.toHaveBeenCalled()
|
|
60
|
+
expect(console.error).not.toHaveBeenCalled()
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('supports the "warn" request strategy', async () => {
|
|
64
|
+
await onUnhandledRequest(
|
|
65
|
+
new Request(new URL('http://localhost/api')),
|
|
66
|
+
[],
|
|
67
|
+
'warn',
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('supports the "error" request strategy', async () => {
|
|
74
|
+
await expect(
|
|
75
|
+
onUnhandledRequest(
|
|
76
|
+
new Request(new URL('http://localhost/api')),
|
|
77
|
+
[],
|
|
78
|
+
'error',
|
|
79
|
+
),
|
|
80
|
+
).rejects.toThrow(
|
|
81
|
+
'[MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.',
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
expect(console.error).toHaveBeenCalledWith(fixtures.errorWithoutSuggestions)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('supports a custom callback function', async () => {
|
|
88
|
+
const callback = vi.fn<Parameters<UnhandledRequestCallback>>((request) => {
|
|
89
|
+
console.warn(`callback: ${request.method} ${request.url}`)
|
|
90
|
+
})
|
|
91
|
+
const request = new Request(new URL('/user', 'http://localhost:3000'))
|
|
92
|
+
await onUnhandledRequest(request, [], callback)
|
|
93
|
+
|
|
94
|
+
expect(callback).toHaveBeenCalledTimes(1)
|
|
95
|
+
expect(callback).toHaveBeenCalledWith(request, {
|
|
96
|
+
warning: expect.any(Function),
|
|
97
|
+
error: expect.any(Function),
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
// Check that the custom logic in the callback was called.
|
|
101
|
+
expect(console.warn).toHaveBeenCalledWith(
|
|
102
|
+
`callback: GET http://localhost:3000/user`,
|
|
103
|
+
)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('supports calling default strategies from the custom callback function', async () => {
|
|
107
|
+
const callback = vi.fn<Parameters<UnhandledRequestCallback>>(
|
|
108
|
+
(request, print) => {
|
|
109
|
+
// Call the default "error" strategy.
|
|
110
|
+
print.error()
|
|
111
|
+
},
|
|
112
|
+
)
|
|
113
|
+
const request = new Request(new URL('http://localhost/api'))
|
|
114
|
+
await expect(onUnhandledRequest(request, [], callback)).rejects.toThrow(
|
|
115
|
+
`[MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.`,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
expect(callback).toHaveBeenCalledTimes(1)
|
|
119
|
+
expect(callback).toHaveBeenCalledWith(request, {
|
|
120
|
+
warning: expect.any(Function),
|
|
121
|
+
error: expect.any(Function),
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
// Check that the default strategy was called.
|
|
125
|
+
expect(console.error).toHaveBeenCalledWith(fixtures.errorWithoutSuggestions)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('does not print any suggestions given no handlers to suggest', async () => {
|
|
129
|
+
await onUnhandledRequest(
|
|
130
|
+
new Request(new URL('http://localhost/api')),
|
|
131
|
+
[],
|
|
132
|
+
'warn',
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('does not print any suggestions given no handlers are similar', async () => {
|
|
139
|
+
await onUnhandledRequest(
|
|
140
|
+
new Request(new URL('http://localhost/api')),
|
|
141
|
+
[
|
|
142
|
+
// None of the defined request handlers match the actual request URL
|
|
143
|
+
// to be used as suggestions.
|
|
144
|
+
new HttpHandler(HttpMethods.GET, 'https://api.github.com', resolver),
|
|
145
|
+
new HttpHandler(HttpMethods.GET, 'https://api.stripe.com', resolver),
|
|
146
|
+
],
|
|
147
|
+
'warn',
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
test('respects RegExp as a request handler method', async () => {
|
|
154
|
+
await onUnhandledRequest(
|
|
155
|
+
new Request(new URL('http://localhost/api')),
|
|
156
|
+
[new HttpHandler(/^GE/, 'http://localhost/api', resolver)],
|
|
157
|
+
'warn',
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
test('sorts the suggestions by relevance', async () => {
|
|
164
|
+
await onUnhandledRequest(
|
|
165
|
+
new Request(new URL('http://localhost/api')),
|
|
166
|
+
[
|
|
167
|
+
new HttpHandler(HttpMethods.GET, '/', resolver),
|
|
168
|
+
new HttpHandler(HttpMethods.GET, 'https://api.example.com/api', resolver),
|
|
169
|
+
new HttpHandler(HttpMethods.POST, '/api', resolver),
|
|
170
|
+
],
|
|
171
|
+
'warn',
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
expect(console.warn).toHaveBeenCalledWith(
|
|
175
|
+
fixtures.warningWithSuggestions(`\
|
|
176
|
+
• POST /api
|
|
177
|
+
• GET /`),
|
|
178
|
+
)
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
test('does not print more than 4 suggestions', async () => {
|
|
182
|
+
await onUnhandledRequest(
|
|
183
|
+
new Request(new URL('http://localhost/api')),
|
|
184
|
+
[
|
|
185
|
+
new HttpHandler(HttpMethods.GET, '/ap', resolver),
|
|
186
|
+
new HttpHandler(HttpMethods.GET, '/api', resolver),
|
|
187
|
+
new HttpHandler(HttpMethods.GET, '/api-1', resolver),
|
|
188
|
+
new HttpHandler(HttpMethods.GET, '/api-2', resolver),
|
|
189
|
+
new HttpHandler(HttpMethods.GET, '/api-3', resolver),
|
|
190
|
+
new HttpHandler(HttpMethods.GET, '/api-4', resolver),
|
|
191
|
+
],
|
|
192
|
+
'warn',
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
expect(console.warn).toHaveBeenCalledWith(
|
|
196
|
+
fixtures.warningWithSuggestions(`\
|
|
197
|
+
• GET /api
|
|
198
|
+
• GET /ap
|
|
199
|
+
• GET /api-1
|
|
200
|
+
• GET /api-2`),
|
|
201
|
+
)
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
test('throws an exception given unknown request strategy', async () => {
|
|
205
|
+
await expect(
|
|
206
|
+
onUnhandledRequest(
|
|
207
|
+
new Request(new URL('http://localhost/api')),
|
|
208
|
+
[],
|
|
209
|
+
// @ts-expect-error Intentional unknown strategy.
|
|
210
|
+
'invalid-strategy',
|
|
211
|
+
),
|
|
212
|
+
).rejects.toThrow(
|
|
213
|
+
'[MSW] Failed to react to an unhandled request: unknown strategy "invalid-strategy". Please provide one of the supported strategies ("bypass", "warn", "error") or a custom callback function as the value of the "onUnhandledRequest" option.',
|
|
214
|
+
)
|
|
215
|
+
})
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import jsLevenshtein from '@bundled-es-modules/js-levenshtein'
|
|
2
|
+
import { RequestHandler, HttpHandler, GraphQLHandler } from '../..'
|
|
3
|
+
import {
|
|
4
|
+
ParsedGraphQLQuery,
|
|
5
|
+
ParsedGraphQLRequest,
|
|
6
|
+
parseGraphQLRequest,
|
|
7
|
+
} from '../internal/parseGraphQLRequest'
|
|
8
|
+
import { getPublicUrlFromRequest } from './getPublicUrlFromRequest'
|
|
9
|
+
import { isStringEqual } from '../internal/isStringEqual'
|
|
10
|
+
import { devUtils } from '../internal/devUtils'
|
|
11
|
+
|
|
12
|
+
const getStringMatchScore = jsLevenshtein
|
|
13
|
+
|
|
14
|
+
const MAX_MATCH_SCORE = 3
|
|
15
|
+
const MAX_SUGGESTION_COUNT = 4
|
|
16
|
+
const TYPE_MATCH_DELTA = 0.5
|
|
17
|
+
|
|
18
|
+
export interface UnhandledRequestPrint {
|
|
19
|
+
warning(): void
|
|
20
|
+
error(): void
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type UnhandledRequestCallback = (
|
|
24
|
+
request: Request,
|
|
25
|
+
print: UnhandledRequestPrint,
|
|
26
|
+
) => void
|
|
27
|
+
|
|
28
|
+
export type UnhandledRequestStrategy =
|
|
29
|
+
| 'bypass'
|
|
30
|
+
| 'warn'
|
|
31
|
+
| 'error'
|
|
32
|
+
| UnhandledRequestCallback
|
|
33
|
+
|
|
34
|
+
interface RequestHandlerGroups {
|
|
35
|
+
http: Array<HttpHandler>
|
|
36
|
+
graphql: Array<GraphQLHandler>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function groupHandlersByType(
|
|
40
|
+
handlers: Array<RequestHandler>,
|
|
41
|
+
): RequestHandlerGroups {
|
|
42
|
+
return handlers.reduce<RequestHandlerGroups>(
|
|
43
|
+
(groups, handler) => {
|
|
44
|
+
if (handler instanceof HttpHandler) {
|
|
45
|
+
groups.http.push(handler)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (handler instanceof GraphQLHandler) {
|
|
49
|
+
groups.graphql.push(handler)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return groups
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
http: [],
|
|
56
|
+
graphql: [],
|
|
57
|
+
},
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
type RequestHandlerSuggestion = [number, RequestHandler]
|
|
62
|
+
|
|
63
|
+
type ScoreGetterFn<RequestHandlerType extends RequestHandler> = (
|
|
64
|
+
request: Request,
|
|
65
|
+
handler: RequestHandlerType,
|
|
66
|
+
) => number
|
|
67
|
+
|
|
68
|
+
function getHttpHandlerScore(): ScoreGetterFn<HttpHandler> {
|
|
69
|
+
return (request, handler) => {
|
|
70
|
+
const { path, method } = handler.info
|
|
71
|
+
|
|
72
|
+
if (path instanceof RegExp || method instanceof RegExp) {
|
|
73
|
+
return Infinity
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const hasSameMethod = isStringEqual(request.method, method)
|
|
77
|
+
|
|
78
|
+
// Always treat a handler with the same method as a more similar one.
|
|
79
|
+
const methodScoreDelta = hasSameMethod ? TYPE_MATCH_DELTA : 0
|
|
80
|
+
const requestPublicUrl = getPublicUrlFromRequest(request)
|
|
81
|
+
const score = getStringMatchScore(requestPublicUrl, path)
|
|
82
|
+
|
|
83
|
+
return score - methodScoreDelta
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getGraphQLHandlerScore(
|
|
88
|
+
parsedQuery: ParsedGraphQLQuery,
|
|
89
|
+
): ScoreGetterFn<GraphQLHandler> {
|
|
90
|
+
return (_, handler) => {
|
|
91
|
+
if (typeof parsedQuery.operationName === 'undefined') {
|
|
92
|
+
return Infinity
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const { operationType, operationName } = handler.info
|
|
96
|
+
|
|
97
|
+
if (typeof operationName !== 'string') {
|
|
98
|
+
return Infinity
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const hasSameOperationType = parsedQuery.operationType === operationType
|
|
102
|
+
// Always treat a handler with the same operation type as a more similar one.
|
|
103
|
+
const operationTypeScoreDelta = hasSameOperationType ? TYPE_MATCH_DELTA : 0
|
|
104
|
+
const score = getStringMatchScore(parsedQuery.operationName, operationName)
|
|
105
|
+
|
|
106
|
+
return score - operationTypeScoreDelta
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function getSuggestedHandler(
|
|
111
|
+
request: Request,
|
|
112
|
+
handlers: Array<HttpHandler> | Array<GraphQLHandler>,
|
|
113
|
+
getScore: ScoreGetterFn<HttpHandler> | ScoreGetterFn<GraphQLHandler>,
|
|
114
|
+
): Array<RequestHandler> {
|
|
115
|
+
const suggestedHandlers = (handlers as Array<RequestHandler>)
|
|
116
|
+
.reduce<Array<RequestHandlerSuggestion>>((suggestions, handler) => {
|
|
117
|
+
const score = getScore(request, handler as any)
|
|
118
|
+
return suggestions.concat([[score, handler]])
|
|
119
|
+
}, [])
|
|
120
|
+
.sort(([leftScore], [rightScore]) => leftScore - rightScore)
|
|
121
|
+
.filter(([score]) => score <= MAX_MATCH_SCORE)
|
|
122
|
+
.slice(0, MAX_SUGGESTION_COUNT)
|
|
123
|
+
.map(([, handler]) => handler)
|
|
124
|
+
|
|
125
|
+
return suggestedHandlers
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function getSuggestedHandlersMessage(handlers: RequestHandler[]) {
|
|
129
|
+
if (handlers.length > 1) {
|
|
130
|
+
return `\
|
|
131
|
+
Did you mean to request one of the following resources instead?
|
|
132
|
+
|
|
133
|
+
${handlers.map((handler) => ` • ${handler.info.header}`).join('\n')}`
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return `Did you mean to request "${handlers[0].info.header}" instead?`
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export async function onUnhandledRequest(
|
|
140
|
+
request: Request,
|
|
141
|
+
handlers: Array<RequestHandler>,
|
|
142
|
+
strategy: UnhandledRequestStrategy = 'warn',
|
|
143
|
+
): Promise<void> {
|
|
144
|
+
const parsedGraphQLQuery = await parseGraphQLRequest(request).catch(
|
|
145
|
+
() => null,
|
|
146
|
+
)
|
|
147
|
+
const publicUrl = getPublicUrlFromRequest(request)
|
|
148
|
+
|
|
149
|
+
function generateHandlerSuggestion(): string {
|
|
150
|
+
/**
|
|
151
|
+
* @note Ignore exceptions during GraphQL request parsing because at this point
|
|
152
|
+
* we cannot assume the unhandled request is a valid GraphQL request.
|
|
153
|
+
* If the GraphQL parsing fails, just don't treat it as a GraphQL request.
|
|
154
|
+
*/
|
|
155
|
+
const handlerGroups = groupHandlersByType(handlers)
|
|
156
|
+
const relevantHandlers = parsedGraphQLQuery
|
|
157
|
+
? handlerGroups.graphql
|
|
158
|
+
: handlerGroups.http
|
|
159
|
+
|
|
160
|
+
const suggestedHandlers = getSuggestedHandler(
|
|
161
|
+
request,
|
|
162
|
+
relevantHandlers,
|
|
163
|
+
parsedGraphQLQuery
|
|
164
|
+
? getGraphQLHandlerScore(parsedGraphQLQuery)
|
|
165
|
+
: getHttpHandlerScore(),
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
return suggestedHandlers.length > 0
|
|
169
|
+
? getSuggestedHandlersMessage(suggestedHandlers)
|
|
170
|
+
: ''
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function getGraphQLRequestHeader(
|
|
174
|
+
parsedGraphQLRequest: ParsedGraphQLRequest<any>,
|
|
175
|
+
): string {
|
|
176
|
+
if (!parsedGraphQLRequest?.operationName) {
|
|
177
|
+
return `anonymous ${parsedGraphQLRequest?.operationType} (${request.method} ${publicUrl})`
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return `${parsedGraphQLRequest.operationType} ${parsedGraphQLRequest.operationName} (${request.method} ${publicUrl})`
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function generateUnhandledRequestMessage(): string {
|
|
184
|
+
const requestHeader = parsedGraphQLQuery
|
|
185
|
+
? getGraphQLRequestHeader(parsedGraphQLQuery)
|
|
186
|
+
: `${request.method} ${publicUrl}`
|
|
187
|
+
const handlerSuggestion = generateHandlerSuggestion()
|
|
188
|
+
|
|
189
|
+
const messageTemplate = [
|
|
190
|
+
`intercepted a request without a matching request handler:`,
|
|
191
|
+
` \u2022 ${requestHeader}`,
|
|
192
|
+
handlerSuggestion,
|
|
193
|
+
`\
|
|
194
|
+
If you still wish to intercept this unhandled request, please create a request handler for it.
|
|
195
|
+
Read more: https://mswjs.io/docs/getting-started/mocks\
|
|
196
|
+
`,
|
|
197
|
+
].filter(Boolean)
|
|
198
|
+
return messageTemplate.join('\n\n')
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function applyStrategy(strategy: UnhandledRequestStrategy) {
|
|
202
|
+
// Generate handler suggestions only when applying the strategy.
|
|
203
|
+
// This saves bandwidth for scenarios when developers opt-out
|
|
204
|
+
// from the default unhandled request handling strategy.
|
|
205
|
+
const message = generateUnhandledRequestMessage()
|
|
206
|
+
|
|
207
|
+
switch (strategy) {
|
|
208
|
+
case 'error': {
|
|
209
|
+
// Print a developer-friendly error.
|
|
210
|
+
devUtils.error('Error: %s', message)
|
|
211
|
+
|
|
212
|
+
// Throw an exception to halt request processing and not perform the original request.
|
|
213
|
+
throw new Error(
|
|
214
|
+
devUtils.formatMessage(
|
|
215
|
+
'Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.',
|
|
216
|
+
),
|
|
217
|
+
)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
case 'warn': {
|
|
221
|
+
devUtils.warn('Warning: %s', message)
|
|
222
|
+
break
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
case 'bypass':
|
|
226
|
+
break
|
|
227
|
+
|
|
228
|
+
default:
|
|
229
|
+
throw new Error(
|
|
230
|
+
devUtils.formatMessage(
|
|
231
|
+
'Failed to react to an unhandled request: unknown strategy "%s". Please provide one of the supported strategies ("bypass", "warn", "error") or a custom callback function as the value of the "onUnhandledRequest" option.',
|
|
232
|
+
strategy,
|
|
233
|
+
),
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (typeof strategy === 'function') {
|
|
239
|
+
strategy(request, {
|
|
240
|
+
warning: applyStrategy.bind(null, 'warn'),
|
|
241
|
+
error: applyStrategy.bind(null, 'error'),
|
|
242
|
+
})
|
|
243
|
+
return
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
applyStrategy(strategy)
|
|
247
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { cleanUrl } from './cleanUrl'
|
|
2
|
+
|
|
3
|
+
test('removes query parameters from a URL string', () => {
|
|
4
|
+
expect(cleanUrl('/user?id=123')).toEqual('/user')
|
|
5
|
+
expect(cleanUrl('/user?id=123&id=456')).toEqual('/user')
|
|
6
|
+
expect(cleanUrl('/user?id=123&role=admin')).toEqual('/user')
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
test('removes hashes from a URL string', () => {
|
|
10
|
+
expect(cleanUrl('/user#hash')).toEqual('/user')
|
|
11
|
+
expect(cleanUrl('/user#hash-with-dashes')).toEqual('/user')
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('removes both query parameters and hashes from a URL string', () => {
|
|
15
|
+
expect(cleanUrl('/user?id=123#some')).toEqual('/user')
|
|
16
|
+
expect(cleanUrl('/user?id=123&role=admin#some')).toEqual('/user')
|
|
17
|
+
})
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const REDUNDANT_CHARACTERS_EXP = /[\?|#].*$/g
|
|
2
|
+
|
|
3
|
+
export function getSearchParams(path: string) {
|
|
4
|
+
return new URL(`/${path}`, 'http://localhost').searchParams
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Removes query parameters and hashes from a given URL string.
|
|
9
|
+
*/
|
|
10
|
+
export function cleanUrl(path: string): string {
|
|
11
|
+
return path.replace(REDUNDANT_CHARACTERS_EXP, '')
|
|
12
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import { getAbsoluteUrl } from './getAbsoluteUrl'
|
|
5
|
+
|
|
6
|
+
test('returns a given relative URL as-is', () => {
|
|
7
|
+
expect(getAbsoluteUrl('/reviews')).toBe('/reviews')
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test('rebases a relative URL against a custom base URL', () => {
|
|
11
|
+
expect(getAbsoluteUrl('/user', 'https://api.github.com')).toEqual(
|
|
12
|
+
'https://api.github.com/user',
|
|
13
|
+
)
|
|
14
|
+
})
|
|
15
|
+
test('returns a given absolute URL as-is', () => {
|
|
16
|
+
expect(getAbsoluteUrl('https://api.mswjs.io/users')).toBe(
|
|
17
|
+
'https://api.mswjs.io/users',
|
|
18
|
+
)
|
|
19
|
+
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import { getAbsoluteUrl } from './getAbsoluteUrl'
|
|
5
|
+
|
|
6
|
+
test('rebases a relative URL against the current "baseURI" (default)', () => {
|
|
7
|
+
expect(getAbsoluteUrl('/reviews')).toEqual('http://localhost/reviews')
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test('rebases a relative URL against a custom base URL', () => {
|
|
11
|
+
expect(getAbsoluteUrl('/user', 'https://api.github.com')).toEqual(
|
|
12
|
+
'https://api.github.com/user',
|
|
13
|
+
)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('returns a given absolute URL as-is', () => {
|
|
17
|
+
expect(getAbsoluteUrl('https://api.mswjs.io/users')).toEqual(
|
|
18
|
+
'https://api.mswjs.io/users',
|
|
19
|
+
)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('returns an absolute URL given a relative path without a leading slash', () => {
|
|
23
|
+
expect(getAbsoluteUrl('users')).toEqual('http://localhost/users')
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('returns a path with a pattern as-is', () => {
|
|
27
|
+
expect(getAbsoluteUrl(':api/user')).toEqual('http://localhost/:api/user')
|
|
28
|
+
expect(getAbsoluteUrl('*/resource/*')).toEqual('*/resource/*')
|
|
29
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { isAbsoluteUrl } from './isAbsoluteUrl'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns an absolute URL based on the given path.
|
|
5
|
+
*/
|
|
6
|
+
export function getAbsoluteUrl(path: string, baseUrl?: string): string {
|
|
7
|
+
// already absolute URL
|
|
8
|
+
if (isAbsoluteUrl(path)) {
|
|
9
|
+
return path
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Ignore path with pattern start with *
|
|
13
|
+
if (path.startsWith('*')) {
|
|
14
|
+
return path
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Resolve a relative request URL against a given custom "baseUrl"
|
|
18
|
+
// or the document baseURI (in the case of browser/browser-like environments).
|
|
19
|
+
const origin =
|
|
20
|
+
baseUrl || (typeof document !== 'undefined' && document.baseURI)
|
|
21
|
+
|
|
22
|
+
return origin
|
|
23
|
+
? // Encode and decode the path to preserve escaped characters.
|
|
24
|
+
decodeURI(new URL(encodeURI(path), origin).href)
|
|
25
|
+
: path
|
|
26
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import { isAbsoluteUrl } from './isAbsoluteUrl'
|
|
5
|
+
|
|
6
|
+
test('returns true for the "http" scheme', () => {
|
|
7
|
+
expect(isAbsoluteUrl('http://www.domain.com')).toEqual(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test('returns true for the "https" scheme', () => {
|
|
11
|
+
expect(isAbsoluteUrl('https://www.domain.com')).toEqual(true)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('returns true for the "ws" scheme', () => {
|
|
15
|
+
expect(isAbsoluteUrl('ws://www.domain.com')).toEqual(true)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
test('returns true for the "ftp" scheme', () => {
|
|
19
|
+
expect(isAbsoluteUrl('ftp://www.domain.com')).toEqual(true)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('returns true for the custom scheme', () => {
|
|
23
|
+
expect(isAbsoluteUrl('web+my://www.example.com')).toEqual(true)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('returns false for the relative URL', () => {
|
|
27
|
+
expect(isAbsoluteUrl('/test')).toEqual(false)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('returns false for the relative URL without a leading slash', () => {
|
|
31
|
+
expect(isAbsoluteUrl('test')).toEqual(false)
|
|
32
|
+
})
|