@zimic/interceptor 0.14.0-canary.19 → 0.14.0-canary.21
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/dist/{chunk-3XWHITKR.mjs → chunk-5ESJW32X.mjs} +90 -165
- package/dist/chunk-5ESJW32X.mjs.map +1 -0
- package/dist/{chunk-SWLQ2IF6.js → chunk-JIUQ7G6N.js} +90 -165
- package/dist/chunk-JIUQ7G6N.js.map +1 -0
- package/dist/cli.js +7 -7
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +3 -3
- package/dist/cli.mjs.map +1 -1
- package/dist/http.d.ts +1 -12
- package/dist/http.js +182 -180
- package/dist/http.js.map +1 -1
- package/dist/http.mjs +182 -180
- package/dist/http.mjs.map +1 -1
- package/dist/scripts/postinstall.js.map +1 -1
- package/dist/scripts/postinstall.mjs.map +1 -1
- package/dist/server.js +5 -5
- package/dist/server.mjs +1 -1
- package/package.json +4 -3
- package/src/http/interceptor/HttpInterceptorClient.ts +13 -7
- package/src/http/interceptor/HttpInterceptorStore.ts +4 -6
- package/src/http/interceptor/LocalHttpInterceptor.ts +6 -7
- package/src/http/interceptor/RemoteHttpInterceptor.ts +7 -11
- package/src/http/interceptor/types/options.ts +1 -1
- package/src/http/interceptor/types/requests.ts +1 -2
- package/src/http/interceptorWorker/HttpInterceptorWorker.ts +4 -5
- package/src/http/interceptorWorker/LocalHttpInterceptorWorker.ts +5 -5
- package/src/http/interceptorWorker/RemoteHttpInterceptorWorker.ts +8 -8
- package/src/http/interceptorWorker/types/options.ts +1 -3
- package/src/http/interceptorWorker/types/requests.ts +1 -2
- package/src/http/requestHandler/HttpRequestHandlerClient.ts +3 -2
- package/src/http/requestHandler/LocalHttpRequestHandler.ts +1 -2
- package/src/http/requestHandler/RemoteHttpRequestHandler.ts +1 -2
- package/src/http/requestHandler/errors/TimesCheckError.ts +2 -2
- package/src/http/requestHandler/types/public.ts +1 -2
- package/src/http/requestHandler/types/requests.ts +1 -2
- package/src/http/requestHandler/types/restrictions.ts +1 -2
- package/src/server/InterceptorServer.ts +6 -4
- package/src/utils/console.ts +3 -8
- package/src/utils/crypto.ts +1 -1
- package/src/utils/data.ts +0 -20
- package/src/utils/fetch.ts +0 -20
- package/src/utils/files.ts +5 -18
- package/src/utils/processes.ts +1 -1
- package/src/webSocket/WebSocketClient.ts +3 -5
- package/src/webSocket/WebSocketHandler.ts +1 -1
- package/src/webSocket/types.ts +1 -2
- package/dist/chunk-3XWHITKR.mjs.map +0 -1
- package/dist/chunk-SWLQ2IF6.js.map +0 -1
- package/src/types/arrays.d.ts +0 -4
- package/src/types/objects.d.ts +0 -14
- package/src/types/strings.d.ts +0 -9
- package/src/types/utils.ts +0 -28
- package/src/utils/imports.ts +0 -12
- package/src/utils/time.ts +0 -5
- package/src/utils/urls.ts +0 -138
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../scripts/postinstall.ts"],"names":["path","__require","filesystem","__name"],"mappings":";;;;;;;;;;;
|
|
1
|
+
{"version":3,"sources":["../../scripts/postinstall.ts"],"names":["path","__require","filesystem","__name"],"mappings":";;;;;;;;;;;AAQa,IAAA,kBAAA,GAAqBA,sBAAK,IAAK,CAAAC,0BAAA,CAAQ,QAAQ,KAAK,CAAA,EAAG,IAAM,EAAA,IAAA,EAAM,IAAI;AAC7E,IAAM,gBAAmB,GAAAD,qBAAA,CAAK,IAAK,CAAA,kBAAA,EAAoB,cAAc;AACrE,IAAM,qBAAwB,GAAAA,qBAAA,CAAK,IAAK,CAAA,kBAAA,EAAoB,OAAO,SAAS;AAEnF,eAAe,eAAkB,GAAA;AAC/B,EAAA,MAAM,yBAA4B,GAAA,MAAME,2BAAW,CAAA,QAAA,CAAS,kBAAkB,OAAO,CAAA;AACrF,EAAM,MAAA,iBAAA,GAAoB,IAAK,CAAA,KAAA,CAAM,yBAAyB,CAAA;AAE9D,EAAM,MAAA,cAAA,GAAiB,iBAAkB,CAAA,OAAA,CAAQ,WAAW,CAAA;AAK5D,EAAM,MAAA,WAAA,GAAc,iBAAkB,CAAA,OAAA,CAAQ,QAAQ,CAAA;AAKtD,EAAA,cAAA,CAAe,OAAO,WAAY,CAAA,IAAA;AAClC,EAAA,WAAA,CAAY,UAAU,cAAe,CAAA,OAAA;AAErC,EAAA,MAAM,gCAAmC,GAAA,IAAA,CAAK,SAAU,CAAA,iBAAA,EAAmB,MAAM,CAAC,CAAA;AAClF,EAAM,MAAAA,2BAAA,CAAW,SAAU,CAAA,gBAAA,EAAkB,gCAAgC,CAAA;AAC/E;AAnBeC,uBAAA,CAAA,eAAA,EAAA,iBAAA,CAAA;AAsBf,eAAe,oBAAuB,GAAA;AACpC,EAAA,KAAA,MAAW,aAAiB,IAAA,CAAC,UAAY,EAAA,WAAW,CAAG,EAAA;AACrD,IAAA,MAAM,cAAiB,GAAAH,qBAAA,CAAK,IAAK,CAAA,qBAAA,EAAuB,aAAa,CAAA;AACrE,IAAA,MAAM,iBAAoB,GAAA,MAAME,2BAAW,CAAA,QAAA,CAAS,gBAAgB,OAAO,CAAA;AAE3E,IAAA,MAAM,2BAA2B,iBAAkB,CAAA,OAAA;AAAA,MACjD,CAAA;AAAA;AAAA,KAAA,CAAA;AAAA,MAIA,CAAA;AAAA;AAAA,KAAA;AAAA,KAGF;AAEA,IAAM,MAAAA,2BAAA,CAAW,SAAU,CAAA,cAAA,EAAgB,wBAAwB,CAAA;AAAA;AAEvE;AAjBeC,uBAAA,CAAA,oBAAA,EAAA,sBAAA,CAAA;AAmBf,eAAe,WAAc,GAAA;AAC3B,EAAA,MAAM,eAAgB,EAAA;AACtB,EAAA,MAAM,oBAAqB,EAAA;AAC7B;AAHeA,uBAAA,CAAA,WAAA,EAAA,aAAA,CAAA;AAKR,IAAM,qBAAqB,WAAY","file":"postinstall.js","sourcesContent":["import { Override } from '@zimic/utils/types';\nimport filesystem from 'fs/promises';\nimport type mswPackage from 'msw/package.json';\nimport path from 'path';\n\nexport type MSWPackage = typeof mswPackage;\nexport type MSWExports = MSWPackage['exports'];\n\nexport const MSW_ROOT_DIRECTORY = path.join(require.resolve('msw'), '..', '..', '..');\nexport const MSW_PACKAGE_PATH = path.join(MSW_ROOT_DIRECTORY, 'package.json');\nexport const MSW_BROWSER_DIRECTORY = path.join(MSW_ROOT_DIRECTORY, 'lib', 'browser');\n\nasync function patchMSWExports() {\n const mswPackageContentAsString = await filesystem.readFile(MSW_PACKAGE_PATH, 'utf-8');\n const mswPackageContent = JSON.parse(mswPackageContentAsString) as MSWPackage;\n\n const browserExports = mswPackageContent.exports['./browser'] as Override<\n MSWExports['./browser'],\n { node: MSWExports['./node']['node'] | string | null }\n >;\n\n const nodeExports = mswPackageContent.exports['./node'] as Override<\n MSWExports['./node'],\n { browser: MSWExports['./browser']['browser'] | string | null }\n >;\n\n browserExports.node = nodeExports.node;\n nodeExports.browser = browserExports.browser;\n\n const patchedMSWPackageContentAsString = JSON.stringify(mswPackageContent, null, 2);\n await filesystem.writeFile(MSW_PACKAGE_PATH, patchedMSWPackageContentAsString);\n}\n\n// This is a temporary workaround. Once https://github.com/mswjs/msw/issues/2146 is fixed, we'll be able to remove it.\nasync function patchMSWBrowserEntry() {\n for (const indexFileName of ['index.js', 'index.mjs']) {\n const mswBrowserPath = path.join(MSW_BROWSER_DIRECTORY, indexFileName);\n const mswBrowserContent = await filesystem.readFile(mswBrowserPath, 'utf-8');\n\n const patchedMSWBrowserContent = mswBrowserContent.replace(\n `if (responseJson.type?.includes(\"opaque\")) {\n return;\n }`,\n\n `if (!request || responseJson.type?.includes(\"opaque\")) {\n return;\n }`,\n );\n\n await filesystem.writeFile(mswBrowserPath, patchedMSWBrowserContent);\n }\n}\n\nasync function postinstall() {\n await patchMSWExports();\n await patchMSWBrowserEntry();\n}\n\nexport const postinstallPromise = postinstall();\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../scripts/postinstall.ts"],"names":[],"mappings":";;;;
|
|
1
|
+
{"version":3,"sources":["../../scripts/postinstall.ts"],"names":[],"mappings":";;;;AAQa,IAAA,kBAAA,GAAqB,KAAK,IAAK,CAAA,SAAA,CAAQ,QAAQ,KAAK,CAAA,EAAG,IAAM,EAAA,IAAA,EAAM,IAAI;AAC7E,IAAM,gBAAmB,GAAA,IAAA,CAAK,IAAK,CAAA,kBAAA,EAAoB,cAAc;AACrE,IAAM,qBAAwB,GAAA,IAAA,CAAK,IAAK,CAAA,kBAAA,EAAoB,OAAO,SAAS;AAEnF,eAAe,eAAkB,GAAA;AAC/B,EAAA,MAAM,yBAA4B,GAAA,MAAM,UAAW,CAAA,QAAA,CAAS,kBAAkB,OAAO,CAAA;AACrF,EAAM,MAAA,iBAAA,GAAoB,IAAK,CAAA,KAAA,CAAM,yBAAyB,CAAA;AAE9D,EAAM,MAAA,cAAA,GAAiB,iBAAkB,CAAA,OAAA,CAAQ,WAAW,CAAA;AAK5D,EAAM,MAAA,WAAA,GAAc,iBAAkB,CAAA,OAAA,CAAQ,QAAQ,CAAA;AAKtD,EAAA,cAAA,CAAe,OAAO,WAAY,CAAA,IAAA;AAClC,EAAA,WAAA,CAAY,UAAU,cAAe,CAAA,OAAA;AAErC,EAAA,MAAM,gCAAmC,GAAA,IAAA,CAAK,SAAU,CAAA,iBAAA,EAAmB,MAAM,CAAC,CAAA;AAClF,EAAM,MAAA,UAAA,CAAW,SAAU,CAAA,gBAAA,EAAkB,gCAAgC,CAAA;AAC/E;AAnBe,MAAA,CAAA,eAAA,EAAA,iBAAA,CAAA;AAsBf,eAAe,oBAAuB,GAAA;AACpC,EAAA,KAAA,MAAW,aAAiB,IAAA,CAAC,UAAY,EAAA,WAAW,CAAG,EAAA;AACrD,IAAA,MAAM,cAAiB,GAAA,IAAA,CAAK,IAAK,CAAA,qBAAA,EAAuB,aAAa,CAAA;AACrE,IAAA,MAAM,iBAAoB,GAAA,MAAM,UAAW,CAAA,QAAA,CAAS,gBAAgB,OAAO,CAAA;AAE3E,IAAA,MAAM,2BAA2B,iBAAkB,CAAA,OAAA;AAAA,MACjD,CAAA;AAAA;AAAA,KAAA,CAAA;AAAA,MAIA,CAAA;AAAA;AAAA,KAAA;AAAA,KAGF;AAEA,IAAM,MAAA,UAAA,CAAW,SAAU,CAAA,cAAA,EAAgB,wBAAwB,CAAA;AAAA;AAEvE;AAjBe,MAAA,CAAA,oBAAA,EAAA,sBAAA,CAAA;AAmBf,eAAe,WAAc,GAAA;AAC3B,EAAA,MAAM,eAAgB,EAAA;AACtB,EAAA,MAAM,oBAAqB,EAAA;AAC7B;AAHe,MAAA,CAAA,WAAA,EAAA,aAAA,CAAA;AAKR,IAAM,qBAAqB,WAAY","file":"postinstall.mjs","sourcesContent":["import { Override } from '@zimic/utils/types';\nimport filesystem from 'fs/promises';\nimport type mswPackage from 'msw/package.json';\nimport path from 'path';\n\nexport type MSWPackage = typeof mswPackage;\nexport type MSWExports = MSWPackage['exports'];\n\nexport const MSW_ROOT_DIRECTORY = path.join(require.resolve('msw'), '..', '..', '..');\nexport const MSW_PACKAGE_PATH = path.join(MSW_ROOT_DIRECTORY, 'package.json');\nexport const MSW_BROWSER_DIRECTORY = path.join(MSW_ROOT_DIRECTORY, 'lib', 'browser');\n\nasync function patchMSWExports() {\n const mswPackageContentAsString = await filesystem.readFile(MSW_PACKAGE_PATH, 'utf-8');\n const mswPackageContent = JSON.parse(mswPackageContentAsString) as MSWPackage;\n\n const browserExports = mswPackageContent.exports['./browser'] as Override<\n MSWExports['./browser'],\n { node: MSWExports['./node']['node'] | string | null }\n >;\n\n const nodeExports = mswPackageContent.exports['./node'] as Override<\n MSWExports['./node'],\n { browser: MSWExports['./browser']['browser'] | string | null }\n >;\n\n browserExports.node = nodeExports.node;\n nodeExports.browser = browserExports.browser;\n\n const patchedMSWPackageContentAsString = JSON.stringify(mswPackageContent, null, 2);\n await filesystem.writeFile(MSW_PACKAGE_PATH, patchedMSWPackageContentAsString);\n}\n\n// This is a temporary workaround. Once https://github.com/mswjs/msw/issues/2146 is fixed, we'll be able to remove it.\nasync function patchMSWBrowserEntry() {\n for (const indexFileName of ['index.js', 'index.mjs']) {\n const mswBrowserPath = path.join(MSW_BROWSER_DIRECTORY, indexFileName);\n const mswBrowserContent = await filesystem.readFile(mswBrowserPath, 'utf-8');\n\n const patchedMSWBrowserContent = mswBrowserContent.replace(\n `if (responseJson.type?.includes(\"opaque\")) {\n return;\n }`,\n\n `if (!request || responseJson.type?.includes(\"opaque\")) {\n return;\n }`,\n );\n\n await filesystem.writeFile(mswBrowserPath, patchedMSWBrowserContent);\n }\n}\n\nasync function postinstall() {\n await patchMSWExports();\n await patchMSWBrowserEntry();\n}\n\nexport const postinstallPromise = postinstall();\n"]}
|
package/dist/server.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkJIUQ7G6N_js = require('./chunk-JIUQ7G6N.js');
|
|
4
4
|
require('./chunk-WCQVDF3K.js');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
Object.defineProperty(exports, "DEFAULT_ACCESS_CONTROL_HEADERS", {
|
|
9
9
|
enumerable: true,
|
|
10
|
-
get: function () { return
|
|
10
|
+
get: function () { return chunkJIUQ7G6N_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
|
|
11
11
|
});
|
|
12
12
|
Object.defineProperty(exports, "DEFAULT_PREFLIGHT_STATUS_CODE", {
|
|
13
13
|
enumerable: true,
|
|
14
|
-
get: function () { return
|
|
14
|
+
get: function () { return chunkJIUQ7G6N_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(exports, "NotStartedInterceptorServerError", {
|
|
17
17
|
enumerable: true,
|
|
18
|
-
get: function () { return
|
|
18
|
+
get: function () { return chunkJIUQ7G6N_js.NotStartedInterceptorServerError_default; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "interceptorServer", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunkJIUQ7G6N_js.interceptorServer; }
|
|
23
23
|
});
|
|
24
24
|
//# sourceMappingURL=server.js.map
|
|
25
25
|
//# sourceMappingURL=server.js.map
|
package/dist/server.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotStartedInterceptorServerError_default as NotStartedInterceptorServerError, interceptorServer } from './chunk-
|
|
1
|
+
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotStartedInterceptorServerError_default as NotStartedInterceptorServerError, interceptorServer } from './chunk-5ESJW32X.mjs';
|
|
2
2
|
import './chunk-CGILA3WO.mjs';
|
|
3
3
|
//# sourceMappingURL=server.mjs.map
|
|
4
4
|
//# sourceMappingURL=server.mjs.map
|
package/package.json
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"api",
|
|
15
15
|
"static"
|
|
16
16
|
],
|
|
17
|
-
"version": "0.14.0-canary.
|
|
17
|
+
"version": "0.14.0-canary.21",
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
20
20
|
"url": "https://github.com/zimicjs/zimic.git",
|
|
@@ -97,9 +97,10 @@
|
|
|
97
97
|
"tsup": "^8.3.6",
|
|
98
98
|
"typescript": "^5.7.3",
|
|
99
99
|
"vitest": "^3.0.6",
|
|
100
|
-
"@zimic/lint-staged-config": "0.0.0",
|
|
101
100
|
"@zimic/eslint-config-node": "0.0.0",
|
|
102
|
-
"@zimic/
|
|
101
|
+
"@zimic/lint-staged-config": "0.0.0",
|
|
102
|
+
"@zimic/tsconfig": "0.0.0",
|
|
103
|
+
"@zimic/utils": "0.0.0"
|
|
103
104
|
},
|
|
104
105
|
"peerDependencies": {
|
|
105
106
|
"@zimic/http": "^0.1.0 || ^0.1.0-canary.0",
|
|
@@ -7,9 +7,9 @@ import {
|
|
|
7
7
|
HttpStatusCode,
|
|
8
8
|
HttpSchema,
|
|
9
9
|
} from '@zimic/http';
|
|
10
|
-
|
|
11
|
-
import
|
|
12
|
-
import
|
|
10
|
+
import { Default, PossiblePromise } from '@zimic/utils/types';
|
|
11
|
+
import createRegExpFromURL from '@zimic/utils/url/createRegExpFromURL';
|
|
12
|
+
import joinURL from '@zimic/utils/url/joinURL';
|
|
13
13
|
|
|
14
14
|
import HttpInterceptorWorker from '../interceptorWorker/HttpInterceptorWorker';
|
|
15
15
|
import LocalHttpInterceptorWorker from '../interceptorWorker/LocalHttpInterceptorWorker';
|
|
@@ -31,7 +31,7 @@ class HttpInterceptorClient<
|
|
|
31
31
|
> {
|
|
32
32
|
private worker: HttpInterceptorWorker;
|
|
33
33
|
private store: HttpInterceptorStore;
|
|
34
|
-
private _baseURL:
|
|
34
|
+
private _baseURL: URL;
|
|
35
35
|
private _isRunning = false;
|
|
36
36
|
private _onUnhandledRequest?: UnhandledRequestStrategy;
|
|
37
37
|
private _shouldSaveRequests = false;
|
|
@@ -53,7 +53,7 @@ class HttpInterceptorClient<
|
|
|
53
53
|
constructor(options: {
|
|
54
54
|
worker: HttpInterceptorWorker;
|
|
55
55
|
store: HttpInterceptorStore;
|
|
56
|
-
baseURL:
|
|
56
|
+
baseURL: URL;
|
|
57
57
|
Handler: HandlerConstructor;
|
|
58
58
|
onUnhandledRequest?: UnhandledRequestStrategy;
|
|
59
59
|
saveRequests?: boolean;
|
|
@@ -67,7 +67,13 @@ class HttpInterceptorClient<
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
baseURL() {
|
|
70
|
-
|
|
70
|
+
const baseURL = this._baseURL;
|
|
71
|
+
|
|
72
|
+
if (baseURL.href === `${baseURL.origin}/`) {
|
|
73
|
+
return baseURL.origin;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return baseURL.toString();
|
|
71
77
|
}
|
|
72
78
|
|
|
73
79
|
platform() {
|
|
@@ -184,7 +190,7 @@ class HttpInterceptorClient<
|
|
|
184
190
|
this.handlerClientsByMethod[handler.method()].set(handler.path(), handlerClients);
|
|
185
191
|
|
|
186
192
|
const url = joinURL(this.baseURL(), handler.path());
|
|
187
|
-
const urlRegex =
|
|
193
|
+
const urlRegex = createRegExpFromURL(url);
|
|
188
194
|
|
|
189
195
|
const registrationResult = this.worker.use(this, handler.method(), url, async (context) => {
|
|
190
196
|
const response = await this.handleInterceptedRequest(
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { ExtendedURL } from '@/utils/urls';
|
|
2
|
-
|
|
3
1
|
import { createHttpInterceptorWorker } from '../interceptorWorker/factory';
|
|
4
2
|
import LocalHttpInterceptorWorker from '../interceptorWorker/LocalHttpInterceptorWorker';
|
|
5
3
|
import RemoteHttpInterceptorWorker from '../interceptorWorker/RemoteHttpInterceptorWorker';
|
|
@@ -22,7 +20,7 @@ class HttpInterceptorStore {
|
|
|
22
20
|
return this.class._localWorker;
|
|
23
21
|
}
|
|
24
22
|
|
|
25
|
-
remoteWorker(baseURL:
|
|
23
|
+
remoteWorker(baseURL: URL) {
|
|
26
24
|
return this.class.remoteWorkers.get(baseURL.origin);
|
|
27
25
|
}
|
|
28
26
|
|
|
@@ -30,7 +28,7 @@ class HttpInterceptorStore {
|
|
|
30
28
|
return this.class.runningLocalInterceptors.size;
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
numberOfRunningRemoteInterceptors(baseURL:
|
|
31
|
+
numberOfRunningRemoteInterceptors(baseURL: URL) {
|
|
34
32
|
const runningInterceptors = this.class.runningRemoteInterceptors.get(baseURL.origin);
|
|
35
33
|
return runningInterceptors?.size ?? 0;
|
|
36
34
|
}
|
|
@@ -43,7 +41,7 @@ class HttpInterceptorStore {
|
|
|
43
41
|
}
|
|
44
42
|
}
|
|
45
43
|
|
|
46
|
-
markRemoteInterceptorAsRunning(interceptor: AnyHttpInterceptorClient, isRunning: boolean, baseURL:
|
|
44
|
+
markRemoteInterceptorAsRunning(interceptor: AnyHttpInterceptorClient, isRunning: boolean, baseURL: URL) {
|
|
47
45
|
const runningInterceptors =
|
|
48
46
|
this.class.runningRemoteInterceptors.get(baseURL.origin) ?? this.createRunningInterceptorsSet(baseURL);
|
|
49
47
|
|
|
@@ -54,7 +52,7 @@ class HttpInterceptorStore {
|
|
|
54
52
|
}
|
|
55
53
|
}
|
|
56
54
|
|
|
57
|
-
private createRunningInterceptorsSet(baseURL:
|
|
55
|
+
private createRunningInterceptorsSet(baseURL: URL) {
|
|
58
56
|
const runningInterceptors = new Set<AnyHttpInterceptorClient>();
|
|
59
57
|
this.class.runningRemoteInterceptors.set(baseURL.origin, runningInterceptors);
|
|
60
58
|
return runningInterceptors;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { HttpSchema, HttpSchemaMethod, HttpSchemaPath } from '@zimic/http';
|
|
2
|
-
|
|
3
|
-
import
|
|
2
|
+
import excludeURLParams from '@zimic/utils/url/excludeURLParams';
|
|
3
|
+
import validateURLProtocol from '@zimic/utils/url/validateURLProtocol';
|
|
4
4
|
|
|
5
5
|
import LocalHttpRequestHandler from '../requestHandler/LocalHttpRequestHandler';
|
|
6
6
|
import HttpInterceptorClient, { SUPPORTED_BASE_URL_PROTOCOLS } from './HttpInterceptorClient';
|
|
@@ -18,10 +18,9 @@ class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttp
|
|
|
18
18
|
constructor(options: LocalHttpInterceptorOptions) {
|
|
19
19
|
this.type = options.type;
|
|
20
20
|
|
|
21
|
-
const baseURL =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
});
|
|
21
|
+
const baseURL = new URL(options.baseURL);
|
|
22
|
+
validateURLProtocol(baseURL, SUPPORTED_BASE_URL_PROTOCOLS);
|
|
23
|
+
excludeURLParams(baseURL);
|
|
25
24
|
|
|
26
25
|
const worker = this.store.getOrCreateLocalWorker({});
|
|
27
26
|
|
|
@@ -40,7 +39,7 @@ class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttp
|
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
baseURL() {
|
|
43
|
-
return this._client.baseURL()
|
|
42
|
+
return this._client.baseURL();
|
|
44
43
|
}
|
|
45
44
|
|
|
46
45
|
platform() {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { HttpSchema, HttpSchemaMethod, HttpSchemaPath } from '@zimic/http';
|
|
2
|
-
|
|
3
|
-
import
|
|
2
|
+
import excludeURLParams from '@zimic/utils/url/excludeURLParams';
|
|
3
|
+
import validateURLProtocol from '@zimic/utils/url/validateURLProtocol';
|
|
4
4
|
|
|
5
5
|
import RemoteHttpRequestHandler from '../requestHandler/RemoteHttpRequestHandler';
|
|
6
6
|
import HttpInterceptorClient, { SUPPORTED_BASE_URL_PROTOCOLS } from './HttpInterceptorClient';
|
|
@@ -18,15 +18,11 @@ class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHt
|
|
|
18
18
|
constructor(options: RemoteHttpInterceptorOptions) {
|
|
19
19
|
this.type = options.type;
|
|
20
20
|
|
|
21
|
-
const baseURL =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
});
|
|
21
|
+
const baseURL = new URL(options.baseURL);
|
|
22
|
+
validateURLProtocol(baseURL, SUPPORTED_BASE_URL_PROTOCOLS);
|
|
23
|
+
excludeURLParams(baseURL);
|
|
25
24
|
|
|
26
|
-
const serverURL =
|
|
27
|
-
protocols: SUPPORTED_BASE_URL_PROTOCOLS,
|
|
28
|
-
excludeNonPathParams: true,
|
|
29
|
-
});
|
|
25
|
+
const serverURL = new URL(baseURL.origin);
|
|
30
26
|
|
|
31
27
|
const worker = this.store.getOrCreateRemoteWorker({ serverURL });
|
|
32
28
|
|
|
@@ -45,7 +41,7 @@ class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHt
|
|
|
45
41
|
}
|
|
46
42
|
|
|
47
43
|
baseURL() {
|
|
48
|
-
return this._client.baseURL()
|
|
44
|
+
return this._client.baseURL();
|
|
49
45
|
}
|
|
50
46
|
|
|
51
47
|
platform() {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { HttpBody, HttpSchema, HttpSchemaMethod, HttpSchemaPath } from '@zimic/http';
|
|
2
|
-
|
|
3
|
-
import { Default } from '@/types/utils';
|
|
2
|
+
import { Default } from '@zimic/utils/types';
|
|
4
3
|
|
|
5
4
|
import { HttpResponseFactoryContext } from '../../interceptorWorker/types/requests';
|
|
6
5
|
import { HttpInterceptorRequest } from '../../requestHandler/types/requests';
|
|
@@ -11,15 +11,14 @@ import {
|
|
|
11
11
|
HttpFormData,
|
|
12
12
|
HttpSearchParams,
|
|
13
13
|
} from '@zimic/http';
|
|
14
|
+
import isDefined from '@zimic/utils/data/isDefined';
|
|
15
|
+
import { Default, PossiblePromise } from '@zimic/utils/types';
|
|
14
16
|
import chalk from 'chalk';
|
|
15
17
|
|
|
16
|
-
import { Default, PossiblePromise } from '@/types/utils';
|
|
17
18
|
import { removeArrayElement } from '@/utils/arrays';
|
|
18
19
|
import { formatValueToLog, logWithPrefix } from '@/utils/console';
|
|
19
|
-
import { isDefined } from '@/utils/data';
|
|
20
20
|
import { isClientSide } from '@/utils/environment';
|
|
21
21
|
import { methodCanHaveResponseBody } from '@/utils/http';
|
|
22
|
-
import { createURL } from '@/utils/urls';
|
|
23
22
|
|
|
24
23
|
import HttpInterceptorClient, { AnyHttpInterceptorClient } from '../interceptor/HttpInterceptorClient';
|
|
25
24
|
import { HttpInterceptorPlatform, HttpInterceptorType, UnhandledRequestStrategy } from '../interceptor/types/options';
|
|
@@ -195,7 +194,7 @@ abstract class HttpInterceptorWorker {
|
|
|
195
194
|
|
|
196
195
|
private findInterceptorByRequestBaseURL(request: Request) {
|
|
197
196
|
const interceptor = this.runningInterceptors.findLast((interceptor) => {
|
|
198
|
-
const baseURL = interceptor.baseURL()
|
|
197
|
+
const baseURL = interceptor.baseURL();
|
|
199
198
|
return request.url.startsWith(baseURL);
|
|
200
199
|
});
|
|
201
200
|
|
|
@@ -284,7 +283,7 @@ abstract class HttpInterceptorWorker {
|
|
|
284
283
|
|
|
285
284
|
const pathParams = options.urlRegex ? this.parseRawPathParams<Path>(options.urlRegex, rawRequest) : {};
|
|
286
285
|
|
|
287
|
-
const parsedURL =
|
|
286
|
+
const parsedURL = new URL(rawRequest.url);
|
|
288
287
|
type SearchParamsSchema = Default<Default<MethodSchema['request']>['searchParams']>;
|
|
289
288
|
const searchParams = new HttpSearchParams<SearchParamsSchema>(parsedURL.searchParams);
|
|
290
289
|
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { HttpRequest, HttpResponse, HttpMethod, HttpSchema } from '@zimic/http';
|
|
2
|
+
import excludeURLParams from '@zimic/utils/url/excludeURLParams';
|
|
3
|
+
import validateURLPathParams from '@zimic/utils/url/validateURLPathParams';
|
|
2
4
|
import { HttpHandler as MSWHttpHandler, SharedOptions as MSWWorkerSharedOptions, http, passthrough } from 'msw';
|
|
3
5
|
import * as mswBrowser from 'msw/browser';
|
|
4
6
|
import * as mswNode from 'msw/node';
|
|
5
7
|
|
|
6
8
|
import { removeArrayIndex } from '@/utils/arrays';
|
|
7
|
-
import { createURL } from '@/utils/urls';
|
|
8
9
|
|
|
9
10
|
import NotStartedHttpInterceptorError from '../interceptor/errors/NotStartedHttpInterceptorError';
|
|
10
11
|
import UnknownHttpInterceptorPlatformError from '../interceptor/errors/UnknownHttpInterceptorPlatformError';
|
|
@@ -148,10 +149,9 @@ class LocalHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
148
149
|
const internalWorker = this.internalWorkerOrThrow();
|
|
149
150
|
const lowercaseMethod = method.toLowerCase<typeof method>();
|
|
150
151
|
|
|
151
|
-
const url =
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
});
|
|
152
|
+
const url = new URL(rawURL);
|
|
153
|
+
excludeURLParams(url);
|
|
154
|
+
validateURLPathParams(url);
|
|
155
155
|
|
|
156
156
|
const httpHandler = http[lowercaseMethod](url.toString(), async (context) => {
|
|
157
157
|
const request = context.request as HttpRequest;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { HttpResponse, HttpMethod, HttpSchema } from '@zimic/http';
|
|
2
|
+
import excludeURLParams from '@zimic/utils/url/excludeURLParams';
|
|
3
|
+
import validateURLPathParams from '@zimic/utils/url/validateURLPathParams';
|
|
2
4
|
import * as mswBrowser from 'msw/browser';
|
|
3
5
|
import * as mswNode from 'msw/node';
|
|
4
6
|
|
|
5
7
|
import { HttpHandlerCommit, InterceptorServerWebSocketSchema } from '@/server/types/schema';
|
|
6
8
|
import { importCrypto } from '@/utils/crypto';
|
|
7
9
|
import { deserializeRequest, serializeResponse } from '@/utils/fetch';
|
|
8
|
-
import { createURL, ExtendedURL } from '@/utils/urls';
|
|
9
10
|
import { WebSocket } from '@/webSocket/types';
|
|
10
11
|
import WebSocketClient from '@/webSocket/WebSocketClient';
|
|
11
12
|
|
|
@@ -41,8 +42,8 @@ class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
41
42
|
});
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
private deriveWebSocketServerURL(serverURL:
|
|
45
|
-
const webSocketServerURL =
|
|
45
|
+
private deriveWebSocketServerURL(serverURL: URL) {
|
|
46
|
+
const webSocketServerURL = new URL(serverURL);
|
|
46
47
|
webSocketServerURL.protocol = serverURL.protocol.replace(/^http(s)?:$/, 'ws$1:');
|
|
47
48
|
return webSocketServerURL;
|
|
48
49
|
}
|
|
@@ -141,15 +142,14 @@ class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
141
142
|
|
|
142
143
|
const crypto = await importCrypto();
|
|
143
144
|
|
|
144
|
-
const url =
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
});
|
|
145
|
+
const url = new URL(rawURL);
|
|
146
|
+
excludeURLParams(url);
|
|
147
|
+
validateURLPathParams(url);
|
|
148
148
|
|
|
149
149
|
const handler: HttpHandler = {
|
|
150
150
|
id: crypto.randomUUID(),
|
|
151
151
|
url: {
|
|
152
|
-
base: interceptor.baseURL()
|
|
152
|
+
base: interceptor.baseURL(),
|
|
153
153
|
full: url.toString(),
|
|
154
154
|
},
|
|
155
155
|
method,
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import { ExtendedURL } from '@/utils/urls';
|
|
2
|
-
|
|
3
1
|
export interface LocalHttpInterceptorWorkerOptions {
|
|
4
2
|
type: 'local';
|
|
5
3
|
}
|
|
6
4
|
|
|
7
5
|
export interface RemoteHttpInterceptorWorkerOptions {
|
|
8
6
|
type: 'remote';
|
|
9
|
-
serverURL:
|
|
7
|
+
serverURL: URL;
|
|
10
8
|
}
|
|
11
9
|
|
|
12
10
|
export type HttpInterceptorWorkerOptions = LocalHttpInterceptorWorkerOptions | RemoteHttpInterceptorWorkerOptions;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { HttpResponse, HttpBody, HttpRequest } from '@zimic/http';
|
|
2
|
+
import { PossiblePromise } from '@zimic/utils/types';
|
|
2
3
|
import type { SetupWorker as BrowserMSWWorker } from 'msw/browser';
|
|
3
4
|
import type { SetupServer as NodeMSWWorker } from 'msw/node';
|
|
4
5
|
|
|
5
|
-
import { PossiblePromise } from '@/types/utils';
|
|
6
|
-
|
|
7
6
|
export type HttpWorker = BrowserMSWWorker | NodeMSWWorker;
|
|
8
7
|
export { BrowserMSWWorker as BrowserHttpWorker, NodeMSWWorker as NodeHttpWorker };
|
|
9
8
|
|
|
@@ -9,9 +9,10 @@ import {
|
|
|
9
9
|
HttpRequestSearchParamsSchema,
|
|
10
10
|
HttpRequestHeadersSchema,
|
|
11
11
|
} from '@zimic/http';
|
|
12
|
+
import blobContains from '@zimic/utils/data/blobContains';
|
|
13
|
+
import blobEquals from '@zimic/utils/data/blobEquals';
|
|
14
|
+
import { Default, Range } from '@zimic/utils/types';
|
|
12
15
|
|
|
13
|
-
import { Default, Range } from '@/types/utils';
|
|
14
|
-
import { blobContains, blobEquals } from '@/utils/data';
|
|
15
16
|
import { jsonContains, jsonEquals } from '@/utils/json';
|
|
16
17
|
|
|
17
18
|
import HttpInterceptorClient from '../interceptor/HttpInterceptorClient';
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { HttpSchema, HttpSchemaMethod, HttpSchemaPath, HttpStatusCode } from '@zimic/http';
|
|
2
|
-
|
|
3
|
-
import { Default } from '@/types/utils';
|
|
2
|
+
import { Default } from '@zimic/utils/types';
|
|
4
3
|
|
|
5
4
|
import HttpInterceptorClient from '../interceptor/HttpInterceptorClient';
|
|
6
5
|
import HttpRequestHandlerClient from './HttpRequestHandlerClient';
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { HttpSchema, HttpSchemaMethod, HttpSchemaPath, HttpStatusCode } from '@zimic/http';
|
|
2
|
-
|
|
3
|
-
import { Default, PossiblePromise } from '@/types/utils';
|
|
2
|
+
import { Default, PossiblePromise } from '@zimic/utils/types';
|
|
4
3
|
|
|
5
4
|
import HttpInterceptorClient from '../interceptor/HttpInterceptorClient';
|
|
6
5
|
import HttpRequestHandlerClient from './HttpRequestHandlerClient';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import isNonEmpty from '@zimic/utils/data/isNonEmpty';
|
|
2
|
+
import { Range } from '@zimic/utils/types';
|
|
1
3
|
import chalk from 'chalk';
|
|
2
4
|
|
|
3
|
-
import { Range } from '@/types/utils';
|
|
4
5
|
import { stringifyValueToLog } from '@/utils/console';
|
|
5
|
-
import { isNonEmpty } from '@/utils/data';
|
|
6
6
|
|
|
7
7
|
import { UnmatchedHttpInterceptorRequestGroup } from '../types/restrictions';
|
|
8
8
|
import TimesDeclarationPointer from './TimesDeclarationPointer';
|
|
@@ -5,8 +5,7 @@ import {
|
|
|
5
5
|
HttpSchemaPath,
|
|
6
6
|
HttpStatusCode,
|
|
7
7
|
} from '@zimic/http';
|
|
8
|
-
|
|
9
|
-
import { Default, PossiblePromise } from '@/types/utils';
|
|
8
|
+
import { Default, PossiblePromise } from '@zimic/utils/types';
|
|
10
9
|
|
|
11
10
|
import HttpRequestHandlerClient from '../HttpRequestHandlerClient';
|
|
12
11
|
import {
|
|
@@ -14,8 +14,7 @@ import {
|
|
|
14
14
|
HttpStatusCode,
|
|
15
15
|
InferPathParams,
|
|
16
16
|
} from '@zimic/http';
|
|
17
|
-
|
|
18
|
-
import { Default, PossiblePromise, ReplaceBy } from '@/types/utils';
|
|
17
|
+
import { Default, PossiblePromise, ReplaceBy } from '@zimic/utils/types';
|
|
19
18
|
|
|
20
19
|
export type HttpRequestHandlerResponseWithBody<ResponseSchema extends HttpResponseSchema> =
|
|
21
20
|
unknown extends ResponseSchema['body']
|
|
@@ -12,8 +12,7 @@ import {
|
|
|
12
12
|
HttpSearchParams,
|
|
13
13
|
HttpSearchParamsSchema,
|
|
14
14
|
} from '@zimic/http';
|
|
15
|
-
|
|
16
|
-
import { IfNever, Default, DeepPartial, PossiblePromise } from '@/types/utils';
|
|
15
|
+
import { IfNever, Default, DeepPartial, PossiblePromise } from '@zimic/utils/types';
|
|
17
16
|
|
|
18
17
|
import { HttpInterceptorRequest } from './requests';
|
|
19
18
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { normalizeNodeRequest, sendNodeResponse } from '@whatwg-node/server';
|
|
2
2
|
import { HttpRequest, HttpMethod } from '@zimic/http';
|
|
3
|
+
import createRegExpFromURL from '@zimic/utils/url/createRegExpFromURL';
|
|
4
|
+
import excludeURLParams from '@zimic/utils/url/excludeURLParams';
|
|
3
5
|
import { createServer, Server as HttpServer, IncomingMessage, ServerResponse } from 'http';
|
|
4
6
|
import type { WebSocket as Socket } from 'isomorphic-ws';
|
|
5
7
|
|
|
@@ -7,7 +9,6 @@ import HttpInterceptorWorker from '@/http/interceptorWorker/HttpInterceptorWorke
|
|
|
7
9
|
import { removeArrayIndex } from '@/utils/arrays';
|
|
8
10
|
import { deserializeResponse, SerializedHttpRequest, serializeRequest } from '@/utils/fetch';
|
|
9
11
|
import { getHttpServerPort, startHttpServer, stopHttpServer } from '@/utils/http';
|
|
10
|
-
import { createRegexFromURL, createURL } from '@/utils/urls';
|
|
11
12
|
import { WebSocketMessageAbortError } from '@/utils/webSocket';
|
|
12
13
|
import { WebSocket } from '@/webSocket/types';
|
|
13
14
|
import WebSocketServer from '@/webSocket/WebSocketServer';
|
|
@@ -175,13 +176,14 @@ class InterceptorServer implements PublicInterceptorServer {
|
|
|
175
176
|
private registerHttpHandler({ id, url, method }: HttpHandlerCommit, socket: Socket) {
|
|
176
177
|
const handlerGroups = this.httpHandlerGroups[method];
|
|
177
178
|
|
|
178
|
-
const fullURL =
|
|
179
|
+
const fullURL = new URL(url.full);
|
|
180
|
+
excludeURLParams(fullURL);
|
|
179
181
|
|
|
180
182
|
handlerGroups.push({
|
|
181
183
|
id,
|
|
182
184
|
url: {
|
|
183
185
|
base: url.base,
|
|
184
|
-
fullRegex:
|
|
186
|
+
fullRegex: createRegExpFromURL(fullURL.toString()),
|
|
185
187
|
},
|
|
186
188
|
socket,
|
|
187
189
|
});
|
|
@@ -279,7 +281,7 @@ class InterceptorServer implements PublicInterceptorServer {
|
|
|
279
281
|
const webSocketServer = this.webSocketServer();
|
|
280
282
|
const methodHandlers = this.httpHandlerGroups[request.method as HttpMethod];
|
|
281
283
|
|
|
282
|
-
const requestURL =
|
|
284
|
+
const requestURL = excludeURLParams(new URL(request.url)).toString();
|
|
283
285
|
|
|
284
286
|
let matchedSomeInterceptor = false;
|
|
285
287
|
|
package/src/utils/console.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { HttpFormData, HttpHeaders, HttpSearchParams } from '@zimic/http';
|
|
2
|
+
import createCachedDynamicImport from '@zimic/utils/import/createCachedDynamicImport';
|
|
2
3
|
import chalk from 'chalk';
|
|
3
4
|
|
|
4
5
|
import { isClientSide } from './environment';
|
|
5
6
|
import { isGlobalFileAvailable } from './files';
|
|
6
|
-
import { createCachedDynamicImport } from './imports';
|
|
7
7
|
|
|
8
8
|
function stringifyJSONToLog(value: unknown): string {
|
|
9
9
|
return JSON.stringify(
|
|
@@ -80,14 +80,9 @@ export async function formatValueToLog(value: unknown, options: { colors?: boole
|
|
|
80
80
|
});
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
export function logWithPrefix(
|
|
84
|
-
messageOrMessages: unknown,
|
|
85
|
-
options: {
|
|
86
|
-
method?: 'log' | 'warn' | 'error';
|
|
87
|
-
} = {},
|
|
88
|
-
) {
|
|
83
|
+
export function logWithPrefix(messageOrMessages: unknown, options: { method?: 'log' | 'warn' | 'error' } = {}) {
|
|
89
84
|
const { method = 'log' } = options;
|
|
90
85
|
|
|
91
86
|
const messages = Array.isArray(messageOrMessages) ? messageOrMessages : [messageOrMessages];
|
|
92
|
-
console[method](chalk.cyan('[zimic]'), ...messages);
|
|
87
|
+
console[method](chalk.cyan('[@zimic/interceptor]'), ...messages);
|
|
93
88
|
}
|
package/src/utils/crypto.ts
CHANGED
package/src/utils/data.ts
CHANGED
|
@@ -1,17 +1,5 @@
|
|
|
1
1
|
import { isClientSide } from './environment';
|
|
2
2
|
|
|
3
|
-
export async function blobEquals(blob: Blob, otherBlob: Blob) {
|
|
4
|
-
return (
|
|
5
|
-
blob.type === otherBlob.type && blob.size === otherBlob.size && (await blob.text()) === (await otherBlob.text())
|
|
6
|
-
);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export async function blobContains(blob: Blob, otherBlob: Blob) {
|
|
10
|
-
return (
|
|
11
|
-
blob.type === otherBlob.type && blob.size >= otherBlob.size && (await blob.text()).includes(await otherBlob.text())
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
3
|
export function convertArrayBufferToBase64(buffer: ArrayBuffer) {
|
|
16
4
|
if (isClientSide()) {
|
|
17
5
|
const bufferBytes = new Uint8Array(buffer);
|
|
@@ -38,11 +26,3 @@ export function convertBase64ToArrayBuffer(base64Value: string) {
|
|
|
38
26
|
return Buffer.from(base64Value, 'base64');
|
|
39
27
|
}
|
|
40
28
|
}
|
|
41
|
-
|
|
42
|
-
export function isDefined<Value>(value: Value): value is NonNullable<Value> {
|
|
43
|
-
return value !== undefined && value !== null;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function isNonEmpty<Value>(value: Value): value is Exclude<NonNullable<Value>, ''> {
|
|
47
|
-
return isDefined(value) && value !== '';
|
|
48
|
-
}
|
package/src/utils/fetch.ts
CHANGED
|
@@ -2,26 +2,6 @@ import { JSONValue } from '@zimic/http';
|
|
|
2
2
|
|
|
3
3
|
import { convertArrayBufferToBase64, convertBase64ToArrayBuffer } from './data';
|
|
4
4
|
|
|
5
|
-
export async function fetchWithTimeout(url: URL | RequestInfo, options: RequestInit & { timeout: number }) {
|
|
6
|
-
const { timeout: timeoutDuration, ...fetchOptions } = options;
|
|
7
|
-
|
|
8
|
-
const abort = new AbortController();
|
|
9
|
-
|
|
10
|
-
const timeout =
|
|
11
|
-
timeoutDuration > 0
|
|
12
|
-
? setTimeout(() => {
|
|
13
|
-
abort.abort();
|
|
14
|
-
}, timeoutDuration)
|
|
15
|
-
: undefined;
|
|
16
|
-
|
|
17
|
-
try {
|
|
18
|
-
const result = await fetch(url, { ...fetchOptions, signal: abort.signal });
|
|
19
|
-
return result;
|
|
20
|
-
} finally {
|
|
21
|
-
clearTimeout(timeout);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
5
|
export type SerializedHttpRequest = JSONValue<{
|
|
26
6
|
url: string;
|
|
27
7
|
method: string;
|