msw 2.1.3 → 2.1.5

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.
Files changed (57) hide show
  1. package/lib/browser/index.js +1775 -32
  2. package/lib/browser/index.js.map +1 -1
  3. package/lib/browser/index.mjs +1769 -28
  4. package/lib/browser/index.mjs.map +1 -1
  5. package/lib/core/handlers/GraphQLHandler.js +2 -2
  6. package/lib/core/handlers/GraphQLHandler.js.map +1 -1
  7. package/lib/core/handlers/GraphQLHandler.mjs +2 -2
  8. package/lib/core/handlers/GraphQLHandler.mjs.map +1 -1
  9. package/lib/core/handlers/HttpHandler.js +2 -2
  10. package/lib/core/handlers/HttpHandler.js.map +1 -1
  11. package/lib/core/handlers/HttpHandler.mjs +2 -2
  12. package/lib/core/handlers/HttpHandler.mjs.map +1 -1
  13. package/lib/core/sharedOptions.d.mts +0 -2
  14. package/lib/core/sharedOptions.d.ts +0 -2
  15. package/lib/core/utils/handleRequest.js +1 -1
  16. package/lib/core/utils/handleRequest.js.map +1 -1
  17. package/lib/core/utils/handleRequest.mjs +1 -1
  18. package/lib/core/utils/handleRequest.mjs.map +1 -1
  19. package/lib/core/utils/internal/parseGraphQLRequest.js +2 -2
  20. package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -1
  21. package/lib/core/utils/internal/parseGraphQLRequest.mjs +2 -2
  22. package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +1 -1
  23. package/lib/core/utils/request/onUnhandledRequest.d.mts +1 -4
  24. package/lib/core/utils/request/onUnhandledRequest.d.ts +1 -4
  25. package/lib/core/utils/request/onUnhandledRequest.js +14 -115
  26. package/lib/core/utils/request/onUnhandledRequest.js.map +1 -1
  27. package/lib/core/utils/request/onUnhandledRequest.mjs +14 -107
  28. package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -1
  29. package/lib/core/utils/request/toPublicUrl.d.mts +7 -0
  30. package/lib/core/utils/request/toPublicUrl.d.ts +7 -0
  31. package/lib/core/utils/request/{getPublicUrlFromRequest.js → toPublicUrl.js} +9 -9
  32. package/lib/core/utils/request/toPublicUrl.js.map +1 -0
  33. package/lib/core/utils/request/toPublicUrl.mjs +11 -0
  34. package/lib/core/utils/request/toPublicUrl.mjs.map +1 -0
  35. package/lib/iife/index.js +46 -233
  36. package/lib/iife/index.js.map +1 -1
  37. package/lib/mockServiceWorker.js +1 -1
  38. package/package.json +5 -8
  39. package/src/browser/setupWorker/glossary.ts +1 -0
  40. package/src/browser/setupWorker/setupWorker.ts +1 -0
  41. package/src/browser/setupWorker/start/createRequestListener.ts +9 -0
  42. package/src/browser/setupWorker/start/createResponseListener.ts +9 -7
  43. package/src/core/handlers/GraphQLHandler.ts +2 -2
  44. package/src/core/handlers/HttpHandler.ts +2 -2
  45. package/src/core/utils/handleRequest.ts +1 -1
  46. package/src/core/utils/internal/parseGraphQLRequest.ts +2 -2
  47. package/src/core/utils/request/onUnhandledRequest.test.ts +5 -101
  48. package/src/core/utils/request/onUnhandledRequest.ts +16 -185
  49. package/src/core/utils/request/toPublicUrl.test.ts +18 -0
  50. package/src/core/utils/request/toPublicUrl.ts +15 -0
  51. package/lib/core/utils/request/getPublicUrlFromRequest.d.mts +0 -7
  52. package/lib/core/utils/request/getPublicUrlFromRequest.d.ts +0 -7
  53. package/lib/core/utils/request/getPublicUrlFromRequest.js.map +0 -1
  54. package/lib/core/utils/request/getPublicUrlFromRequest.mjs +0 -11
  55. package/lib/core/utils/request/getPublicUrlFromRequest.mjs.map +0 -1
  56. package/src/core/utils/request/getPublicUrlFromRequest.test.ts +0 -26
  57. package/src/core/utils/request/getPublicUrlFromRequest.ts +0 -15
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,129 +15,27 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
  var onUnhandledRequest_exports = {};
30
20
  __export(onUnhandledRequest_exports, {
31
21
  onUnhandledRequest: () => onUnhandledRequest
32
22
  });
33
23
  module.exports = __toCommonJS(onUnhandledRequest_exports);
34
- var import_js_levenshtein = __toESM(require("@bundled-es-modules/js-levenshtein"));
35
- var import_HttpHandler = require("../../handlers/HttpHandler.js");
36
- var import_GraphQLHandler = require("../../handlers/GraphQLHandler.js");
37
- var import_parseGraphQLRequest = require("../internal/parseGraphQLRequest.js");
38
- var import_getPublicUrlFromRequest = require("./getPublicUrlFromRequest.js");
39
- var import_isStringEqual = require("../internal/isStringEqual.js");
24
+ var import_toPublicUrl = require("./toPublicUrl.js");
40
25
  var import_devUtils = require("../internal/devUtils.js");
41
- const getStringMatchScore = import_js_levenshtein.default;
42
- const MAX_MATCH_SCORE = 3;
43
- const MAX_SUGGESTION_COUNT = 4;
44
- const TYPE_MATCH_DELTA = 0.5;
45
- function groupHandlersByType(handlers) {
46
- return handlers.reduce(
47
- (groups, handler) => {
48
- if (handler instanceof import_HttpHandler.HttpHandler) {
49
- groups.http.push(handler);
50
- }
51
- if (handler instanceof import_GraphQLHandler.GraphQLHandler) {
52
- groups.graphql.push(handler);
53
- }
54
- return groups;
55
- },
56
- {
57
- http: [],
58
- graphql: []
59
- }
60
- );
61
- }
62
- function getHttpHandlerScore() {
63
- return (request, handler) => {
64
- const { path, method } = handler.info;
65
- if (path instanceof RegExp || method instanceof RegExp) {
66
- return Infinity;
67
- }
68
- const hasSameMethod = (0, import_isStringEqual.isStringEqual)(request.method, method);
69
- const methodScoreDelta = hasSameMethod ? TYPE_MATCH_DELTA : 0;
70
- const requestPublicUrl = (0, import_getPublicUrlFromRequest.getPublicUrlFromRequest)(request);
71
- const score = getStringMatchScore(requestPublicUrl, path);
72
- return score - methodScoreDelta;
73
- };
74
- }
75
- function getGraphQLHandlerScore(parsedQuery) {
76
- return (_, handler) => {
77
- if (typeof parsedQuery.operationName === "undefined") {
78
- return Infinity;
79
- }
80
- const { operationType, operationName } = handler.info;
81
- if (typeof operationName !== "string") {
82
- return Infinity;
83
- }
84
- const hasSameOperationType = parsedQuery.operationType === operationType;
85
- const operationTypeScoreDelta = hasSameOperationType ? TYPE_MATCH_DELTA : 0;
86
- const score = getStringMatchScore(parsedQuery.operationName, operationName);
87
- return score - operationTypeScoreDelta;
88
- };
89
- }
90
- function getSuggestedHandler(request, handlers, getScore) {
91
- const suggestedHandlers = handlers.reduce((suggestions, handler) => {
92
- const score = getScore(request, handler);
93
- return suggestions.concat([[score, handler]]);
94
- }, []).sort(([leftScore], [rightScore]) => leftScore - rightScore).filter(([score]) => score <= MAX_MATCH_SCORE).slice(0, MAX_SUGGESTION_COUNT).map(([, handler]) => handler);
95
- return suggestedHandlers;
96
- }
97
- function getSuggestedHandlersMessage(handlers) {
98
- if (handlers.length > 1) {
99
- return `Did you mean to request one of the following resources instead?
26
+ async function onUnhandledRequest(request, strategy = "warn") {
27
+ const url = new URL(request.url);
28
+ const publicUrl = (0, import_toPublicUrl.toPublicUrl)(url);
29
+ const unhandledRequestMessage = `intercepted a request without a matching request handler:
100
30
 
101
- ${handlers.map((handler) => ` \u2022 ${handler.info.header}`).join("\n")}`;
102
- }
103
- return `Did you mean to request "${handlers[0].info.header}" instead?`;
104
- }
105
- async function onUnhandledRequest(request, handlers, strategy = "warn") {
106
- const parsedGraphQLQuery = await (0, import_parseGraphQLRequest.parseGraphQLRequest)(request).catch(
107
- () => null
108
- );
109
- const publicUrl = (0, import_getPublicUrlFromRequest.getPublicUrlFromRequest)(request);
110
- function generateHandlerSuggestion() {
111
- const handlerGroups = groupHandlersByType(handlers);
112
- const relevantHandlers = parsedGraphQLQuery ? handlerGroups.graphql : handlerGroups.http;
113
- const suggestedHandlers = getSuggestedHandler(
114
- request,
115
- relevantHandlers,
116
- parsedGraphQLQuery ? getGraphQLHandlerScore(parsedGraphQLQuery) : getHttpHandlerScore()
117
- );
118
- return suggestedHandlers.length > 0 ? getSuggestedHandlersMessage(suggestedHandlers) : "";
119
- }
120
- function getGraphQLRequestHeader(parsedGraphQLRequest) {
121
- if (!parsedGraphQLRequest?.operationName) {
122
- return `anonymous ${parsedGraphQLRequest?.operationType} (${request.method} ${publicUrl})`;
123
- }
124
- return `${parsedGraphQLRequest.operationType} ${parsedGraphQLRequest.operationName} (${request.method} ${publicUrl})`;
125
- }
126
- function generateUnhandledRequestMessage() {
127
- const requestHeader = parsedGraphQLQuery ? getGraphQLRequestHeader(parsedGraphQLQuery) : `${request.method} ${publicUrl}`;
128
- const handlerSuggestion = generateHandlerSuggestion();
129
- const messageTemplate = [
130
- `intercepted a request without a matching request handler:`,
131
- ` \u2022 ${requestHeader}`,
132
- handlerSuggestion,
133
- `If you still wish to intercept this unhandled request, please create a request handler for it.
134
- Read more: https://mswjs.io/docs/getting-started/mocks`
135
- ].filter(Boolean);
136
- return messageTemplate.join("\n\n");
137
- }
31
+ \u2022 ${request.method} ${publicUrl}
32
+
33
+ If you still wish to intercept this unhandled request, please create a request handler for it.
34
+ Read more: https://mswjs.io/docs/getting-started/mocks`;
138
35
  function applyStrategy(strategy2) {
139
- const message = generateUnhandledRequestMessage();
140
36
  switch (strategy2) {
141
37
  case "error": {
142
- import_devUtils.devUtils.error("Error: %s", message);
38
+ import_devUtils.devUtils.error("Error: %s", unhandledRequestMessage);
143
39
  throw new Error(
144
40
  import_devUtils.devUtils.formatMessage(
145
41
  'Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.'
@@ -147,7 +43,7 @@ Read more: https://mswjs.io/docs/getting-started/mocks`
147
43
  );
148
44
  }
149
45
  case "warn": {
150
- import_devUtils.devUtils.warn("Warning: %s", message);
46
+ import_devUtils.devUtils.warn("Warning: %s", unhandledRequestMessage);
151
47
  break;
152
48
  }
153
49
  case "bypass":
@@ -168,6 +64,9 @@ Read more: https://mswjs.io/docs/getting-started/mocks`
168
64
  });
169
65
  return;
170
66
  }
67
+ if (url.protocol === "file:") {
68
+ return;
69
+ }
171
70
  applyStrategy(strategy);
172
71
  }
173
72
  //# sourceMappingURL=onUnhandledRequest.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/core/utils/request/onUnhandledRequest.ts"],"sourcesContent":["import jsLevenshtein from '@bundled-es-modules/js-levenshtein'\nimport { RequestHandler } from '../../handlers/RequestHandler'\nimport { HttpHandler } from '../../handlers/HttpHandler'\nimport { GraphQLHandler } from '../../handlers/GraphQLHandler'\nimport {\n ParsedGraphQLQuery,\n ParsedGraphQLRequest,\n parseGraphQLRequest,\n} from '../internal/parseGraphQLRequest'\nimport { getPublicUrlFromRequest } from './getPublicUrlFromRequest'\nimport { isStringEqual } from '../internal/isStringEqual'\nimport { devUtils } from '../internal/devUtils'\n\nconst getStringMatchScore = jsLevenshtein\n\nconst MAX_MATCH_SCORE = 3\nconst MAX_SUGGESTION_COUNT = 4\nconst TYPE_MATCH_DELTA = 0.5\n\nexport interface UnhandledRequestPrint {\n warning(): void\n error(): void\n}\n\nexport type UnhandledRequestCallback = (\n request: Request,\n print: UnhandledRequestPrint,\n) => void\n\nexport type UnhandledRequestStrategy =\n | 'bypass'\n | 'warn'\n | 'error'\n | UnhandledRequestCallback\n\ninterface RequestHandlerGroups {\n http: Array<HttpHandler>\n graphql: Array<GraphQLHandler>\n}\n\nfunction groupHandlersByType(\n handlers: Array<RequestHandler>,\n): RequestHandlerGroups {\n return handlers.reduce<RequestHandlerGroups>(\n (groups, handler) => {\n if (handler instanceof HttpHandler) {\n groups.http.push(handler)\n }\n\n if (handler instanceof GraphQLHandler) {\n groups.graphql.push(handler)\n }\n\n return groups\n },\n {\n http: [],\n graphql: [],\n },\n )\n}\n\ntype RequestHandlerSuggestion = [number, RequestHandler]\n\ntype ScoreGetterFn<RequestHandlerType extends RequestHandler> = (\n request: Request,\n handler: RequestHandlerType,\n) => number\n\nfunction getHttpHandlerScore(): ScoreGetterFn<HttpHandler> {\n return (request, handler) => {\n const { path, method } = handler.info\n\n if (path instanceof RegExp || method instanceof RegExp) {\n return Infinity\n }\n\n const hasSameMethod = isStringEqual(request.method, method)\n\n // Always treat a handler with the same method as a more similar one.\n const methodScoreDelta = hasSameMethod ? TYPE_MATCH_DELTA : 0\n const requestPublicUrl = getPublicUrlFromRequest(request)\n const score = getStringMatchScore(requestPublicUrl, path)\n\n return score - methodScoreDelta\n }\n}\n\nfunction getGraphQLHandlerScore(\n parsedQuery: ParsedGraphQLQuery,\n): ScoreGetterFn<GraphQLHandler> {\n return (_, handler) => {\n if (typeof parsedQuery.operationName === 'undefined') {\n return Infinity\n }\n\n const { operationType, operationName } = handler.info\n\n if (typeof operationName !== 'string') {\n return Infinity\n }\n\n const hasSameOperationType = parsedQuery.operationType === operationType\n // Always treat a handler with the same operation type as a more similar one.\n const operationTypeScoreDelta = hasSameOperationType ? TYPE_MATCH_DELTA : 0\n const score = getStringMatchScore(parsedQuery.operationName, operationName)\n\n return score - operationTypeScoreDelta\n }\n}\n\nfunction getSuggestedHandler(\n request: Request,\n handlers: Array<HttpHandler> | Array<GraphQLHandler>,\n getScore: ScoreGetterFn<HttpHandler> | ScoreGetterFn<GraphQLHandler>,\n): Array<RequestHandler> {\n const suggestedHandlers = (handlers as Array<RequestHandler>)\n .reduce<Array<RequestHandlerSuggestion>>((suggestions, handler) => {\n const score = getScore(request, handler as any)\n return suggestions.concat([[score, handler]])\n }, [])\n .sort(([leftScore], [rightScore]) => leftScore - rightScore)\n .filter(([score]) => score <= MAX_MATCH_SCORE)\n .slice(0, MAX_SUGGESTION_COUNT)\n .map(([, handler]) => handler)\n\n return suggestedHandlers\n}\n\nfunction getSuggestedHandlersMessage(handlers: RequestHandler[]) {\n if (handlers.length > 1) {\n return `\\\nDid you mean to request one of the following resources instead?\n\n${handlers.map((handler) => ` • ${handler.info.header}`).join('\\n')}`\n }\n\n return `Did you mean to request \"${handlers[0].info.header}\" instead?`\n}\n\nexport async function onUnhandledRequest(\n request: Request,\n handlers: Array<RequestHandler>,\n strategy: UnhandledRequestStrategy = 'warn',\n): Promise<void> {\n const parsedGraphQLQuery = await parseGraphQLRequest(request).catch(\n () => null,\n )\n const publicUrl = getPublicUrlFromRequest(request)\n\n function generateHandlerSuggestion(): string {\n /**\n * @note Ignore exceptions during GraphQL request parsing because at this point\n * we cannot assume the unhandled request is a valid GraphQL request.\n * If the GraphQL parsing fails, just don't treat it as a GraphQL request.\n */\n const handlerGroups = groupHandlersByType(handlers)\n const relevantHandlers = parsedGraphQLQuery\n ? handlerGroups.graphql\n : handlerGroups.http\n\n const suggestedHandlers = getSuggestedHandler(\n request,\n relevantHandlers,\n parsedGraphQLQuery\n ? getGraphQLHandlerScore(parsedGraphQLQuery)\n : getHttpHandlerScore(),\n )\n\n return suggestedHandlers.length > 0\n ? getSuggestedHandlersMessage(suggestedHandlers)\n : ''\n }\n\n function getGraphQLRequestHeader(\n parsedGraphQLRequest: ParsedGraphQLRequest<any>,\n ): string {\n if (!parsedGraphQLRequest?.operationName) {\n return `anonymous ${parsedGraphQLRequest?.operationType} (${request.method} ${publicUrl})`\n }\n\n return `${parsedGraphQLRequest.operationType} ${parsedGraphQLRequest.operationName} (${request.method} ${publicUrl})`\n }\n\n function generateUnhandledRequestMessage(): string {\n const requestHeader = parsedGraphQLQuery\n ? getGraphQLRequestHeader(parsedGraphQLQuery)\n : `${request.method} ${publicUrl}`\n const handlerSuggestion = generateHandlerSuggestion()\n\n const messageTemplate = [\n `intercepted a request without a matching request handler:`,\n ` \\u2022 ${requestHeader}`,\n handlerSuggestion,\n `\\\nIf you still wish to intercept this unhandled request, please create a request handler for it.\nRead more: https://mswjs.io/docs/getting-started/mocks\\\n`,\n ].filter(Boolean)\n return messageTemplate.join('\\n\\n')\n }\n\n function applyStrategy(strategy: UnhandledRequestStrategy) {\n // Generate handler suggestions only when applying the strategy.\n // This saves bandwidth for scenarios when developers opt-out\n // from the default unhandled request handling strategy.\n const message = generateUnhandledRequestMessage()\n\n switch (strategy) {\n case 'error': {\n // Print a developer-friendly error.\n devUtils.error('Error: %s', message)\n\n // Throw an exception to halt request processing and not perform the original request.\n throw new Error(\n devUtils.formatMessage(\n 'Cannot bypass a request when using the \"error\" strategy for the \"onUnhandledRequest\" option.',\n ),\n )\n }\n\n case 'warn': {\n devUtils.warn('Warning: %s', message)\n break\n }\n\n case 'bypass':\n break\n\n default:\n throw new Error(\n devUtils.formatMessage(\n '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.',\n strategy,\n ),\n )\n }\n }\n\n if (typeof strategy === 'function') {\n strategy(request, {\n warning: applyStrategy.bind(null, 'warn'),\n error: applyStrategy.bind(null, 'error'),\n })\n return\n }\n\n applyStrategy(strategy)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA0B;AAE1B,yBAA4B;AAC5B,4BAA+B;AAC/B,iCAIO;AACP,qCAAwC;AACxC,2BAA8B;AAC9B,sBAAyB;AAEzB,MAAM,sBAAsB,sBAAAA;AAE5B,MAAM,kBAAkB;AACxB,MAAM,uBAAuB;AAC7B,MAAM,mBAAmB;AAuBzB,SAAS,oBACP,UACsB;AACtB,SAAO,SAAS;AAAA,IACd,CAAC,QAAQ,Y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kB;AAAA,MACtB;AAAA,MACA,YAAY,aAAa;AAAA,MACzB;AAAA,MACA;AAAA;AAAA,IAIF,EAAE,OAAO,OAAO;AAChB,WAAO,gBAAgB,KAAK,MAAM;AAAA,EACpC;AAEA,WAAS,cAAcC,WAAoC;AAIzD,UAAM,UAAU,gCAAgC;AAEhD,YAAQA,WAAU;AAAA,MAChB,KAAK,SAAS;AAEZ,iCAAS,MAAM,aAAa,OAAO;AAGnC,cAAM,IAAI;AAAA,UACR,yBAAS;AAAA,YACP;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,MAEA,KAAK,QAAQ;AACX,iCAAS,KAAK,eAAe,OAAO;AACpC;AAAA,MACF;AAAA,MAEA,KAAK;AACH;AAAA,MAEF;AACE,cAAM,IAAI;AAAA,UACR,yBAAS;AAAA,YACP;AAAA,YACAA;AAAA,UACF;AAAA,QACF;AAAA,IACJ;AAAA,EACF;AAEA,MAAI,OAAO,aAAa,YAAY;AAClC,aAAS,SAAS;AAAA,MAChB,SAAS,cAAc,KAAK,MAAM,MAAM;AAAA,MACxC,OAAO,cAAc,KAAK,MAAM,OAAO;AAAA,IACzC,CAAC;AACD;AAAA,EACF;AAEA,gBAAc,QAAQ;AACxB;","names":["jsLevenshtein","strategy"]}
1
+ {"version":3,"sources":["../../../../src/core/utils/request/onUnhandledRequest.ts"],"sourcesContent":["import { toPublicUrl } from './toPublicUrl'\nimport { devUtils } from '../internal/devUtils'\n\nexport interface UnhandledRequestPrint {\n warning(): void\n error(): void\n}\n\nexport type UnhandledRequestCallback = (\n request: Request,\n print: UnhandledRequestPrint,\n) => void\n\nexport type UnhandledRequestStrategy =\n | 'bypass'\n | 'warn'\n | 'error'\n | UnhandledRequestCallback\n\nexport async function onUnhandledRequest(\n request: Request,\n strategy: UnhandledRequestStrategy = 'warn',\n): Promise<void> {\n const url = new URL(request.url)\n const publicUrl = toPublicUrl(url)\n\n const unhandledRequestMessage = `intercepted a request without a matching request handler:\\n\\n \\u2022 ${request.method} ${publicUrl}\\n\\nIf you still wish to intercept this unhandled request, please create a request handler for it.\\nRead more: https://mswjs.io/docs/getting-started/mocks`\n\n function applyStrategy(strategy: UnhandledRequestStrategy) {\n switch (strategy) {\n case 'error': {\n // Print a developer-friendly error.\n devUtils.error('Error: %s', unhandledRequestMessage)\n\n // Throw an exception to halt request processing and not perform the original request.\n throw new Error(\n devUtils.formatMessage(\n 'Cannot bypass a request when using the \"error\" strategy for the \"onUnhandledRequest\" option.',\n ),\n )\n }\n\n case 'warn': {\n devUtils.warn('Warning: %s', unhandledRequestMessage)\n break\n }\n\n case 'bypass':\n break\n\n default:\n throw new Error(\n devUtils.formatMessage(\n '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.',\n strategy,\n ),\n )\n }\n }\n\n if (typeof strategy === 'function') {\n strategy(request, {\n warning: applyStrategy.bind(null, 'warn'),\n error: applyStrategy.bind(null, 'error'),\n })\n return\n }\n\n /**\n * @note Ignore \"file://\" requests.\n * Those often are an implementation detail of modern tooling\n * that fetches modules via HTTP. Developers don't issue those\n * requests and so they mustn't be warned about them.\n */\n if (url.protocol === 'file:') {\n return\n }\n\n applyStrategy(strategy)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAA4B;AAC5B,sBAAyB;AAkBzB,eAAsB,mBACpB,SACA,WAAqC,QACtB;AACf,QAAM,MAAM,IAAI,IAAI,QAAQ,GAAG;AAC/B,QAAM,gBAAY,gCAAY,GAAG;AAEjC,QAAM,0BAA0B;AAAA;AAAA,WAAyE,QAAQ,MAAM,IAAI,SAAS;AAAA;AAAA;AAAA;AAEpI,WAAS,cAAcA,WAAoC;AACzD,YAAQA,WAAU;AAAA,MAChB,KAAK,SAAS;AAEZ,iCAAS,MAAM,aAAa,uBAAuB;AAGnD,cAAM,IAAI;AAAA,UACR,yBAAS;AAAA,YACP;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,MAEA,KAAK,QAAQ;AACX,iCAAS,KAAK,eAAe,uBAAuB;AACpD;AAAA,MACF;AAAA,MAEA,KAAK;AACH;AAAA,MAEF;AACE,cAAM,IAAI;AAAA,UACR,yBAAS;AAAA,YACP;AAAA,YACAA;AAAA,UACF;AAAA,QACF;AAAA,IACJ;AAAA,EACF;AAEA,MAAI,OAAO,aAAa,YAAY;AAClC,aAAS,SAAS;AAAA,MAChB,SAAS,cAAc,KAAK,MAAM,MAAM;AAAA,MACxC,OAAO,cAAc,KAAK,MAAM,OAAO;AAAA,IACzC,CAAC;AACD;AAAA,EACF;AAQA,MAAI,IAAI,aAAa,SAAS;AAC5B;AAAA,EACF;AAEA,gBAAc,QAAQ;AACxB;","names":["strategy"]}
@@ -1,114 +1,18 @@
1
- import jsLevenshtein from "@bundled-es-modules/js-levenshtein";
2
- import { HttpHandler } from '../../handlers/HttpHandler.mjs';
3
- import { GraphQLHandler } from '../../handlers/GraphQLHandler.mjs';
4
- import {
5
- parseGraphQLRequest
6
- } from '../internal/parseGraphQLRequest.mjs';
7
- import { getPublicUrlFromRequest } from './getPublicUrlFromRequest.mjs';
8
- import { isStringEqual } from '../internal/isStringEqual.mjs';
1
+ import { toPublicUrl } from './toPublicUrl.mjs';
9
2
  import { devUtils } from '../internal/devUtils.mjs';
10
- const getStringMatchScore = jsLevenshtein;
11
- const MAX_MATCH_SCORE = 3;
12
- const MAX_SUGGESTION_COUNT = 4;
13
- const TYPE_MATCH_DELTA = 0.5;
14
- function groupHandlersByType(handlers) {
15
- return handlers.reduce(
16
- (groups, handler) => {
17
- if (handler instanceof HttpHandler) {
18
- groups.http.push(handler);
19
- }
20
- if (handler instanceof GraphQLHandler) {
21
- groups.graphql.push(handler);
22
- }
23
- return groups;
24
- },
25
- {
26
- http: [],
27
- graphql: []
28
- }
29
- );
30
- }
31
- function getHttpHandlerScore() {
32
- return (request, handler) => {
33
- const { path, method } = handler.info;
34
- if (path instanceof RegExp || method instanceof RegExp) {
35
- return Infinity;
36
- }
37
- const hasSameMethod = isStringEqual(request.method, method);
38
- const methodScoreDelta = hasSameMethod ? TYPE_MATCH_DELTA : 0;
39
- const requestPublicUrl = getPublicUrlFromRequest(request);
40
- const score = getStringMatchScore(requestPublicUrl, path);
41
- return score - methodScoreDelta;
42
- };
43
- }
44
- function getGraphQLHandlerScore(parsedQuery) {
45
- return (_, handler) => {
46
- if (typeof parsedQuery.operationName === "undefined") {
47
- return Infinity;
48
- }
49
- const { operationType, operationName } = handler.info;
50
- if (typeof operationName !== "string") {
51
- return Infinity;
52
- }
53
- const hasSameOperationType = parsedQuery.operationType === operationType;
54
- const operationTypeScoreDelta = hasSameOperationType ? TYPE_MATCH_DELTA : 0;
55
- const score = getStringMatchScore(parsedQuery.operationName, operationName);
56
- return score - operationTypeScoreDelta;
57
- };
58
- }
59
- function getSuggestedHandler(request, handlers, getScore) {
60
- const suggestedHandlers = handlers.reduce((suggestions, handler) => {
61
- const score = getScore(request, handler);
62
- return suggestions.concat([[score, handler]]);
63
- }, []).sort(([leftScore], [rightScore]) => leftScore - rightScore).filter(([score]) => score <= MAX_MATCH_SCORE).slice(0, MAX_SUGGESTION_COUNT).map(([, handler]) => handler);
64
- return suggestedHandlers;
65
- }
66
- function getSuggestedHandlersMessage(handlers) {
67
- if (handlers.length > 1) {
68
- return `Did you mean to request one of the following resources instead?
3
+ async function onUnhandledRequest(request, strategy = "warn") {
4
+ const url = new URL(request.url);
5
+ const publicUrl = toPublicUrl(url);
6
+ const unhandledRequestMessage = `intercepted a request without a matching request handler:
69
7
 
70
- ${handlers.map((handler) => ` \u2022 ${handler.info.header}`).join("\n")}`;
71
- }
72
- return `Did you mean to request "${handlers[0].info.header}" instead?`;
73
- }
74
- async function onUnhandledRequest(request, handlers, strategy = "warn") {
75
- const parsedGraphQLQuery = await parseGraphQLRequest(request).catch(
76
- () => null
77
- );
78
- const publicUrl = getPublicUrlFromRequest(request);
79
- function generateHandlerSuggestion() {
80
- const handlerGroups = groupHandlersByType(handlers);
81
- const relevantHandlers = parsedGraphQLQuery ? handlerGroups.graphql : handlerGroups.http;
82
- const suggestedHandlers = getSuggestedHandler(
83
- request,
84
- relevantHandlers,
85
- parsedGraphQLQuery ? getGraphQLHandlerScore(parsedGraphQLQuery) : getHttpHandlerScore()
86
- );
87
- return suggestedHandlers.length > 0 ? getSuggestedHandlersMessage(suggestedHandlers) : "";
88
- }
89
- function getGraphQLRequestHeader(parsedGraphQLRequest) {
90
- if (!parsedGraphQLRequest?.operationName) {
91
- return `anonymous ${parsedGraphQLRequest?.operationType} (${request.method} ${publicUrl})`;
92
- }
93
- return `${parsedGraphQLRequest.operationType} ${parsedGraphQLRequest.operationName} (${request.method} ${publicUrl})`;
94
- }
95
- function generateUnhandledRequestMessage() {
96
- const requestHeader = parsedGraphQLQuery ? getGraphQLRequestHeader(parsedGraphQLQuery) : `${request.method} ${publicUrl}`;
97
- const handlerSuggestion = generateHandlerSuggestion();
98
- const messageTemplate = [
99
- `intercepted a request without a matching request handler:`,
100
- ` \u2022 ${requestHeader}`,
101
- handlerSuggestion,
102
- `If you still wish to intercept this unhandled request, please create a request handler for it.
103
- Read more: https://mswjs.io/docs/getting-started/mocks`
104
- ].filter(Boolean);
105
- return messageTemplate.join("\n\n");
106
- }
8
+ \u2022 ${request.method} ${publicUrl}
9
+
10
+ If you still wish to intercept this unhandled request, please create a request handler for it.
11
+ Read more: https://mswjs.io/docs/getting-started/mocks`;
107
12
  function applyStrategy(strategy2) {
108
- const message = generateUnhandledRequestMessage();
109
13
  switch (strategy2) {
110
14
  case "error": {
111
- devUtils.error("Error: %s", message);
15
+ devUtils.error("Error: %s", unhandledRequestMessage);
112
16
  throw new Error(
113
17
  devUtils.formatMessage(
114
18
  'Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.'
@@ -116,7 +20,7 @@ Read more: https://mswjs.io/docs/getting-started/mocks`
116
20
  );
117
21
  }
118
22
  case "warn": {
119
- devUtils.warn("Warning: %s", message);
23
+ devUtils.warn("Warning: %s", unhandledRequestMessage);
120
24
  break;
121
25
  }
122
26
  case "bypass":
@@ -137,6 +41,9 @@ Read more: https://mswjs.io/docs/getting-started/mocks`
137
41
  });
138
42
  return;
139
43
  }
44
+ if (url.protocol === "file:") {
45
+ return;
46
+ }
140
47
  applyStrategy(strategy);
141
48
  }
142
49
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/core/utils/request/onUnhandledRequest.ts"],"sourcesContent":["import jsLevenshtein from '@bundled-es-modules/js-levenshtein'\nimport { RequestHandler } from '../../handlers/RequestHandler'\nimport { HttpHandler } from '../../handlers/HttpHandler'\nimport { GraphQLHandler } from '../../handlers/GraphQLHandler'\nimport {\n ParsedGraphQLQuery,\n ParsedGraphQLRequest,\n parseGraphQLRequest,\n} from '../internal/parseGraphQLRequest'\nimport { getPublicUrlFromRequest } from './getPublicUrlFromRequest'\nimport { isStringEqual } from '../internal/isStringEqual'\nimport { devUtils } from '../internal/devUtils'\n\nconst getStringMatchScore = jsLevenshtein\n\nconst MAX_MATCH_SCORE = 3\nconst MAX_SUGGESTION_COUNT = 4\nconst TYPE_MATCH_DELTA = 0.5\n\nexport interface UnhandledRequestPrint {\n warning(): void\n error(): void\n}\n\nexport type UnhandledRequestCallback = (\n request: Request,\n print: UnhandledRequestPrint,\n) => void\n\nexport type UnhandledRequestStrategy =\n | 'bypass'\n | 'warn'\n | 'error'\n | UnhandledRequestCallback\n\ninterface RequestHandlerGroups {\n http: Array<HttpHandler>\n graphql: Array<GraphQLHandler>\n}\n\nfunction groupHandlersByType(\n handlers: Array<RequestHandler>,\n): RequestHandlerGroups {\n return handlers.reduce<RequestHandlerGroups>(\n (groups, handler) => {\n if (handler instanceof HttpHandler) {\n groups.http.push(handler)\n }\n\n if (handler instanceof GraphQLHandler) {\n groups.graphql.push(handler)\n }\n\n return groups\n },\n {\n http: [],\n graphql: [],\n },\n )\n}\n\ntype RequestHandlerSuggestion = [number, RequestHandler]\n\ntype ScoreGetterFn<RequestHandlerType extends RequestHandler> = (\n request: Request,\n handler: RequestHandlerType,\n) => number\n\nfunction getHttpHandlerScore(): ScoreGetterFn<HttpHandler> {\n return (request, handler) => {\n const { path, method } = handler.info\n\n if (path instanceof RegExp || method instanceof RegExp) {\n return Infinity\n }\n\n const hasSameMethod = isStringEqual(request.method, method)\n\n // Always treat a handler with the same method as a more similar one.\n const methodScoreDelta = hasSameMethod ? TYPE_MATCH_DELTA : 0\n const requestPublicUrl = getPublicUrlFromRequest(request)\n const score = getStringMatchScore(requestPublicUrl, path)\n\n return score - methodScoreDelta\n }\n}\n\nfunction getGraphQLHandlerScore(\n parsedQuery: ParsedGraphQLQuery,\n): ScoreGetterFn<GraphQLHandler> {\n return (_, handler) => {\n if (typeof parsedQuery.operationName === 'undefined') {\n return Infinity\n }\n\n const { operationType, operationName } = handler.info\n\n if (typeof operationName !== 'string') {\n return Infinity\n }\n\n const hasSameOperationType = parsedQuery.operationType === operationType\n // Always treat a handler with the same operation type as a more similar one.\n const operationTypeScoreDelta = hasSameOperationType ? TYPE_MATCH_DELTA : 0\n const score = getStringMatchScore(parsedQuery.operationName, operationName)\n\n return score - operationTypeScoreDelta\n }\n}\n\nfunction getSuggestedHandler(\n request: Request,\n handlers: Array<HttpHandler> | Array<GraphQLHandler>,\n getScore: ScoreGetterFn<HttpHandler> | ScoreGetterFn<GraphQLHandler>,\n): Array<RequestHandler> {\n const suggestedHandlers = (handlers as Array<RequestHandler>)\n .reduce<Array<RequestHandlerSuggestion>>((suggestions, handler) => {\n const score = getScore(request, handler as any)\n return suggestions.concat([[score, handler]])\n }, [])\n .sort(([leftScore], [rightScore]) => leftScore - rightScore)\n .filter(([score]) => score <= MAX_MATCH_SCORE)\n .slice(0, MAX_SUGGESTION_COUNT)\n .map(([, handler]) => handler)\n\n return suggestedHandlers\n}\n\nfunction getSuggestedHandlersMessage(handlers: RequestHandler[]) {\n if (handlers.length > 1) {\n return `\\\nDid you mean to request one of the following resources instead?\n\n${handlers.map((handler) => ` • ${handler.info.header}`).join('\\n')}`\n }\n\n return `Did you mean to request \"${handlers[0].info.header}\" instead?`\n}\n\nexport async function onUnhandledRequest(\n request: Request,\n handlers: Array<RequestHandler>,\n strategy: UnhandledRequestStrategy = 'warn',\n): Promise<void> {\n const parsedGraphQLQuery = await parseGraphQLRequest(request).catch(\n () => null,\n )\n const publicUrl = getPublicUrlFromRequest(request)\n\n function generateHandlerSuggestion(): string {\n /**\n * @note Ignore exceptions during GraphQL request parsing because at this point\n * we cannot assume the unhandled request is a valid GraphQL request.\n * If the GraphQL parsing fails, just don't treat it as a GraphQL request.\n */\n const handlerGroups = groupHandlersByType(handlers)\n const relevantHandlers = parsedGraphQLQuery\n ? handlerGroups.graphql\n : handlerGroups.http\n\n const suggestedHandlers = getSuggestedHandler(\n request,\n relevantHandlers,\n parsedGraphQLQuery\n ? getGraphQLHandlerScore(parsedGraphQLQuery)\n : getHttpHandlerScore(),\n )\n\n return suggestedHandlers.length > 0\n ? getSuggestedHandlersMessage(suggestedHandlers)\n : ''\n }\n\n function getGraphQLRequestHeader(\n parsedGraphQLRequest: ParsedGraphQLRequest<any>,\n ): string {\n if (!parsedGraphQLRequest?.operationName) {\n return `anonymous ${parsedGraphQLRequest?.operationType} (${request.method} ${publicUrl})`\n }\n\n return `${parsedGraphQLRequest.operationType} ${parsedGraphQLRequest.operationName} (${request.method} ${publicUrl})`\n }\n\n function generateUnhandledRequestMessage(): string {\n const requestHeader = parsedGraphQLQuery\n ? getGraphQLRequestHeader(parsedGraphQLQuery)\n : `${request.method} ${publicUrl}`\n const handlerSuggestion = generateHandlerSuggestion()\n\n const messageTemplate = [\n `intercepted a request without a matching request handler:`,\n ` \\u2022 ${requestHeader}`,\n handlerSuggestion,\n `\\\nIf you still wish to intercept this unhandled request, please create a request handler for it.\nRead more: https://mswjs.io/docs/getting-started/mocks\\\n`,\n ].filter(Boolean)\n return messageTemplate.join('\\n\\n')\n }\n\n function applyStrategy(strategy: UnhandledRequestStrategy) {\n // Generate handler suggestions only when applying the strategy.\n // This saves bandwidth for scenarios when developers opt-out\n // from the default unhandled request handling strategy.\n const message = generateUnhandledRequestMessage()\n\n switch (strategy) {\n case 'error': {\n // Print a developer-friendly error.\n devUtils.error('Error: %s', message)\n\n // Throw an exception to halt request processing and not perform the original request.\n throw new Error(\n devUtils.formatMessage(\n 'Cannot bypass a request when using the \"error\" strategy for the \"onUnhandledRequest\" option.',\n ),\n )\n }\n\n case 'warn': {\n devUtils.warn('Warning: %s', message)\n break\n }\n\n case 'bypass':\n break\n\n default:\n throw new Error(\n devUtils.formatMessage(\n '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.',\n strategy,\n ),\n )\n }\n }\n\n if (typeof strategy === 'function') {\n strategy(request, {\n warning: applyStrategy.bind(null, 'warn'),\n error: applyStrategy.bind(null, 'error'),\n })\n return\n }\n\n applyStrategy(strategy)\n}\n"],"mappings":"AAAA,OAAO,mBAAmB;AAE1B,SAAS,mBAAmB;AAC5B,SAAS,sBAAsB;AAC/B;AAAA,EAGE;AAAA,OACK;AACP,SAAS,+BAA+B;AACxC,SAAS,qBAAqB;AAC9B,SAAS,gBAAgB;AAEzB,MAAM,sBAAsB;AAE5B,MAAM,kBAAkB;AACxB,MAAM,uBAAuB;AAC7B,MAAM,mBAAmB;AAuBzB,SAAS,oBACP,UACsB;AACtB,SAAO,SAAS;AAAA,IACd,CAAC,QAAQ,YAAY;AACnB,UAAI,mBAAmB,aAAa;AAClC,eAAO,KAAK,KAAK,OAAO;AAAA,MAC1B;AAEA,UAAI,mBAAmB,gBAAgB;AACrC,eAAO,QAAQ,KAAK,OAAO;AAAA,MAC7B;AAEA,aAAO;AAAA,IACT;AAAA,IACA;AAAA,MACE,MAAM,CAAC;AAAA,MACP,SAAS,CAAC;AAAA,IACZ;AAAA,EACF;AACF;AASA,SAAS,sBAAkD;A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,cAAM,IAAI;AAAA,UACR,SAAS;AAAA,YACP;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,MAEA,KAAK,QAAQ;AACX,iBAAS,KAAK,eAAe,OAAO;AACpC;AAAA,MACF;AAAA,MAEA,KAAK;AACH;AAAA,MAEF;AACE,cAAM,IAAI;AAAA,UACR,SAAS;AAAA,YACP;AAAA,YACAA;AAAA,UACF;AAAA,QACF;AAAA,IACJ;AAAA,EACF;AAEA,MAAI,OAAO,aAAa,YAAY;AAClC,aAAS,SAAS;AAAA,MAChB,SAAS,cAAc,KAAK,MAAM,MAAM;AAAA,MACxC,OAAO,cAAc,KAAK,MAAM,OAAO;AAAA,IACzC,CAAC;AACD;AAAA,EACF;AAEA,gBAAc,QAAQ;AACxB;","names":["strategy"]}
1
+ {"version":3,"sources":["../../../../src/core/utils/request/onUnhandledRequest.ts"],"sourcesContent":["import { toPublicUrl } from './toPublicUrl'\nimport { devUtils } from '../internal/devUtils'\n\nexport interface UnhandledRequestPrint {\n warning(): void\n error(): void\n}\n\nexport type UnhandledRequestCallback = (\n request: Request,\n print: UnhandledRequestPrint,\n) => void\n\nexport type UnhandledRequestStrategy =\n | 'bypass'\n | 'warn'\n | 'error'\n | UnhandledRequestCallback\n\nexport async function onUnhandledRequest(\n request: Request,\n strategy: UnhandledRequestStrategy = 'warn',\n): Promise<void> {\n const url = new URL(request.url)\n const publicUrl = toPublicUrl(url)\n\n const unhandledRequestMessage = `intercepted a request without a matching request handler:\\n\\n \\u2022 ${request.method} ${publicUrl}\\n\\nIf you still wish to intercept this unhandled request, please create a request handler for it.\\nRead more: https://mswjs.io/docs/getting-started/mocks`\n\n function applyStrategy(strategy: UnhandledRequestStrategy) {\n switch (strategy) {\n case 'error': {\n // Print a developer-friendly error.\n devUtils.error('Error: %s', unhandledRequestMessage)\n\n // Throw an exception to halt request processing and not perform the original request.\n throw new Error(\n devUtils.formatMessage(\n 'Cannot bypass a request when using the \"error\" strategy for the \"onUnhandledRequest\" option.',\n ),\n )\n }\n\n case 'warn': {\n devUtils.warn('Warning: %s', unhandledRequestMessage)\n break\n }\n\n case 'bypass':\n break\n\n default:\n throw new Error(\n devUtils.formatMessage(\n '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.',\n strategy,\n ),\n )\n }\n }\n\n if (typeof strategy === 'function') {\n strategy(request, {\n warning: applyStrategy.bind(null, 'warn'),\n error: applyStrategy.bind(null, 'error'),\n })\n return\n }\n\n /**\n * @note Ignore \"file://\" requests.\n * Those often are an implementation detail of modern tooling\n * that fetches modules via HTTP. Developers don't issue those\n * requests and so they mustn't be warned about them.\n */\n if (url.protocol === 'file:') {\n return\n }\n\n applyStrategy(strategy)\n}\n"],"mappings":"AAAA,SAAS,mBAAmB;AAC5B,SAAS,gBAAgB;AAkBzB,eAAsB,mBACpB,SACA,WAAqC,QACtB;AACf,QAAM,MAAM,IAAI,IAAI,QAAQ,GAAG;AAC/B,QAAM,YAAY,YAAY,GAAG;AAEjC,QAAM,0BAA0B;AAAA;AAAA,WAAyE,QAAQ,MAAM,IAAI,SAAS;AAAA;AAAA;AAAA;AAEpI,WAAS,cAAcA,WAAoC;AACzD,YAAQA,WAAU;AAAA,MAChB,KAAK,SAAS;AAEZ,iBAAS,MAAM,aAAa,uBAAuB;AAGnD,cAAM,IAAI;AAAA,UACR,SAAS;AAAA,YACP;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,MAEA,KAAK,QAAQ;AACX,iBAAS,KAAK,eAAe,uBAAuB;AACpD;AAAA,MACF;AAAA,MAEA,KAAK;AACH;AAAA,MAEF;AACE,cAAM,IAAI;AAAA,UACR,SAAS;AAAA,YACP;AAAA,YACAA;AAAA,UACF;AAAA,QACF;AAAA,IACJ;AAAA,EACF;AAEA,MAAI,OAAO,aAAa,YAAY;AAClC,aAAS,SAAS;AAAA,MAChB,SAAS,cAAc,KAAK,MAAM,MAAM;AAAA,MACxC,OAAO,cAAc,KAAK,MAAM,OAAO;AAAA,IACzC,CAAC;AACD;AAAA,EACF;AAQA,MAAI,IAAI,aAAa,SAAS;AAC5B;AAAA,EACF;AAEA,gBAAc,QAAQ;AACxB;","names":["strategy"]}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Returns a relative URL if the given request URL is relative
3
+ * to the current origin. Otherwise returns an absolute URL.
4
+ */
5
+ declare function toPublicUrl(url: string | URL): string;
6
+
7
+ export { toPublicUrl };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Returns a relative URL if the given request URL is relative
3
+ * to the current origin. Otherwise returns an absolute URL.
4
+ */
5
+ declare function toPublicUrl(url: string | URL): string;
6
+
7
+ export { toPublicUrl };
@@ -16,16 +16,16 @@ var __copyProps = (to, from, except, desc) => {
16
16
  return to;
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var getPublicUrlFromRequest_exports = {};
20
- __export(getPublicUrlFromRequest_exports, {
21
- getPublicUrlFromRequest: () => getPublicUrlFromRequest
19
+ var toPublicUrl_exports = {};
20
+ __export(toPublicUrl_exports, {
21
+ toPublicUrl: () => toPublicUrl
22
22
  });
23
- module.exports = __toCommonJS(getPublicUrlFromRequest_exports);
24
- function getPublicUrlFromRequest(request) {
23
+ module.exports = __toCommonJS(toPublicUrl_exports);
24
+ function toPublicUrl(url) {
25
25
  if (typeof location === "undefined") {
26
- return request.url;
26
+ return url.toString();
27
27
  }
28
- const url = new URL(request.url);
29
- return url.origin === location.origin ? url.pathname : url.origin + url.pathname;
28
+ const urlInstance = url instanceof URL ? url : new URL(url);
29
+ return urlInstance.origin === location.origin ? urlInstance.pathname : urlInstance.origin + urlInstance.pathname;
30
30
  }
31
- //# sourceMappingURL=getPublicUrlFromRequest.js.map
31
+ //# sourceMappingURL=toPublicUrl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/core/utils/request/toPublicUrl.ts"],"sourcesContent":["/**\n * Returns a relative URL if the given request URL is relative\n * to the current origin. Otherwise returns an absolute URL.\n */\nexport function toPublicUrl(url: string | URL): string {\n if (typeof location === 'undefined') {\n return url.toString()\n }\n\n const urlInstance = url instanceof URL ? url : new URL(url)\n\n return urlInstance.origin === location.origin\n ? urlInstance.pathname\n : urlInstance.origin + urlInstance.pathname\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIO,SAAS,YAAY,KAA2B;AACrD,MAAI,OAAO,aAAa,aAAa;AACnC,WAAO,IAAI,SAAS;AAAA,EACtB;AAEA,QAAM,cAAc,eAAe,MAAM,MAAM,IAAI,IAAI,GAAG;AAE1D,SAAO,YAAY,WAAW,SAAS,SACnC,YAAY,WACZ,YAAY,SAAS,YAAY;AACvC;","names":[]}
@@ -0,0 +1,11 @@
1
+ function toPublicUrl(url) {
2
+ if (typeof location === "undefined") {
3
+ return url.toString();
4
+ }
5
+ const urlInstance = url instanceof URL ? url : new URL(url);
6
+ return urlInstance.origin === location.origin ? urlInstance.pathname : urlInstance.origin + urlInstance.pathname;
7
+ }
8
+ export {
9
+ toPublicUrl
10
+ };
11
+ //# sourceMappingURL=toPublicUrl.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/core/utils/request/toPublicUrl.ts"],"sourcesContent":["/**\n * Returns a relative URL if the given request URL is relative\n * to the current origin. Otherwise returns an absolute URL.\n */\nexport function toPublicUrl(url: string | URL): string {\n if (typeof location === 'undefined') {\n return url.toString()\n }\n\n const urlInstance = url instanceof URL ? url : new URL(url)\n\n return urlInstance.origin === location.origin\n ? urlInstance.pathname\n : urlInstance.origin + urlInstance.pathname\n}\n"],"mappings":"AAIO,SAAS,YAAY,KAA2B;AACrD,MAAI,OAAO,aAAa,aAAa;AACnC,WAAO,IAAI,SAAS;AAAA,EACtB;AAEA,QAAM,cAAc,eAAe,MAAM,MAAM,IAAI,IAAI,GAAG;AAE1D,SAAO,YAAY,WAAW,SAAS,SACnC,YAAY,WACZ,YAAY,SAAS,YAAY;AACvC;","names":[]}