@zimic/interceptor 0.16.0-canary.11 → 0.16.0-canary.3
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/README.md +2 -1
- package/dist/{chunk-NTRC2S4I.mjs → chunk-2RNFR6TF.mjs} +267 -249
- package/dist/chunk-2RNFR6TF.mjs.map +1 -0
- package/dist/{chunk-O6ZIPCUJ.js → chunk-D7CDDSQE.js} +268 -250
- package/dist/chunk-D7CDDSQE.js.map +1 -0
- package/dist/cli.js +9 -9
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +5 -5
- package/dist/cli.mjs.map +1 -1
- package/dist/http.d.ts +141 -180
- package/dist/http.js +69 -102
- package/dist/http.js.map +1 -1
- package/dist/http.mjs +69 -101
- package/dist/http.mjs.map +1 -1
- package/dist/server.d.ts +46 -20
- package/dist/server.js +7 -7
- package/dist/server.mjs +1 -1
- package/package.json +11 -11
- package/src/cli/browser/init.ts +2 -2
- package/src/cli/server/start.ts +2 -2
- package/src/http/index.ts +2 -5
- package/src/http/interceptor/HttpInterceptorClient.ts +15 -30
- package/src/http/interceptor/LocalHttpInterceptor.ts +8 -8
- package/src/http/interceptor/RemoteHttpInterceptor.ts +8 -8
- package/src/http/interceptor/types/options.ts +10 -3
- package/src/http/interceptor/types/public.ts +12 -44
- package/src/http/interceptorWorker/HttpInterceptorWorker.ts +2 -2
- package/src/http/requestHandler/HttpRequestHandlerClient.ts +5 -15
- package/src/http/requestHandler/errors/DisabledRequestSavingError.ts +1 -1
- package/src/http/requestHandler/errors/TimesCheckError.ts +14 -15
- package/src/http/requestHandler/types/public.ts +8 -16
- package/src/server/index.ts +11 -1
- package/src/server/namespace/InterceptorServerNamespace.ts +21 -0
- package/src/utils/console.ts +2 -2
- package/dist/chunk-NTRC2S4I.mjs.map +0 -1
- package/dist/chunk-O6ZIPCUJ.js.map +0 -1
- package/src/http/interceptor/errors/RequestSavingSafeLimitExceededError.ts +0 -22
package/dist/server.d.ts
CHANGED
|
@@ -1,17 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* An error thrown when the interceptor server is running and some operation requires it to be stopped first.
|
|
3
|
-
*
|
|
4
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐server `@zimic/interceptor/server` - API reference}
|
|
5
|
-
*/
|
|
6
|
-
declare class RunningInterceptorServerError extends Error {
|
|
7
|
-
constructor(additionalMessage: string);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/** An error thrown when the interceptor server is not running. */
|
|
11
|
-
declare class NotRunningInterceptorServerError extends Error {
|
|
12
|
-
constructor();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
1
|
/**
|
|
16
2
|
* The options to create an
|
|
17
3
|
* {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server interceptor server}.
|
|
@@ -87,6 +73,48 @@ interface InterceptorServer {
|
|
|
87
73
|
stop: () => Promise<void>;
|
|
88
74
|
}
|
|
89
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Creates an {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server interceptor server}.
|
|
78
|
+
*
|
|
79
|
+
* @param options The options to create the server.
|
|
80
|
+
* @returns The created server.
|
|
81
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server-programmatic-usage `zimic-interceptor server` programmatic usage}
|
|
82
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/getting‐started#remote-http-interceptors Remote HTTP Interceptors} .
|
|
83
|
+
*/
|
|
84
|
+
declare function createInterceptorServer(options?: InterceptorServerOptions): InterceptorServer;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A namespace of interceptor server resources for handling HTTP requests.
|
|
88
|
+
*
|
|
89
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server-programmatic-usage `zimic-interceptor server` programmatic usage}
|
|
90
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/getting‐started#remote-http-interceptors Remote HTTP Interceptors} .
|
|
91
|
+
*/
|
|
92
|
+
declare class InterceptorServerNamespace {
|
|
93
|
+
/**
|
|
94
|
+
* Creates an {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server interceptor server}.
|
|
95
|
+
*
|
|
96
|
+
* @param options The options to create the server.
|
|
97
|
+
* @returns The created server.
|
|
98
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server-programmatic-usage `zimic-interceptor server` programmatic usage}
|
|
99
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/getting‐started#remote-http-interceptors Remote HTTP Interceptors} .
|
|
100
|
+
*/
|
|
101
|
+
create: typeof createInterceptorServer;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* An error thrown when the interceptor server is running and some operation requires it to be stopped first.
|
|
106
|
+
*
|
|
107
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐server `@zimic/interceptor/server` - API reference}
|
|
108
|
+
*/
|
|
109
|
+
declare class RunningInterceptorServerError extends Error {
|
|
110
|
+
constructor(additionalMessage: string);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** An error thrown when the interceptor server is not running. */
|
|
114
|
+
declare class NotRunningInterceptorServerError extends Error {
|
|
115
|
+
constructor();
|
|
116
|
+
}
|
|
117
|
+
|
|
90
118
|
/** The default access control headers for the server. */
|
|
91
119
|
declare const DEFAULT_ACCESS_CONTROL_HEADERS: Readonly<{
|
|
92
120
|
'access-control-allow-origin': "*";
|
|
@@ -99,13 +127,11 @@ declare const DEFAULT_ACCESS_CONTROL_HEADERS: Readonly<{
|
|
|
99
127
|
declare const DEFAULT_PREFLIGHT_STATUS_CODE = 204;
|
|
100
128
|
|
|
101
129
|
/**
|
|
102
|
-
*
|
|
130
|
+
* A namespace of interceptor server resources for handling HTTP requests.
|
|
103
131
|
*
|
|
104
|
-
* @
|
|
105
|
-
* @returns The created server.
|
|
106
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server-programmatic-usage `zimic-interceptor server` programmatic usage}
|
|
132
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#zimic-server `zimic-interceptor server` API reference}
|
|
107
133
|
* @see {@link https://github.com/zimicjs/zimic/wiki/getting‐started#remote-http-interceptors Remote HTTP Interceptors} .
|
|
108
134
|
*/
|
|
109
|
-
declare
|
|
135
|
+
declare const interceptorServer: Readonly<InterceptorServerNamespace>;
|
|
110
136
|
|
|
111
|
-
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, type InterceptorServer, type InterceptorServerOptions, NotRunningInterceptorServerError, RunningInterceptorServerError,
|
|
137
|
+
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, type InterceptorServer, InterceptorServerNamespace, type InterceptorServerOptions, NotRunningInterceptorServerError, RunningInterceptorServerError, interceptorServer };
|
package/dist/server.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkD7CDDSQE_js = require('./chunk-D7CDDSQE.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 chunkD7CDDSQE_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 chunkD7CDDSQE_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(exports, "NotRunningInterceptorServerError", {
|
|
17
17
|
enumerable: true,
|
|
18
|
-
get: function () { return
|
|
18
|
+
get: function () { return chunkD7CDDSQE_js.NotRunningInterceptorServerError_default; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "RunningInterceptorServerError", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunkD7CDDSQE_js.RunningInterceptorServerError_default; }
|
|
23
23
|
});
|
|
24
|
-
Object.defineProperty(exports, "
|
|
24
|
+
Object.defineProperty(exports, "interceptorServer", {
|
|
25
25
|
enumerable: true,
|
|
26
|
-
get: function () { return
|
|
26
|
+
get: function () { return chunkD7CDDSQE_js.interceptorServer; }
|
|
27
27
|
});
|
|
28
28
|
//# sourceMappingURL=server.js.map
|
|
29
29
|
//# sourceMappingURL=server.js.map
|
package/dist/server.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default as NotRunningInterceptorServerError, RunningInterceptorServerError_default as RunningInterceptorServerError,
|
|
1
|
+
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default as NotRunningInterceptorServerError, RunningInterceptorServerError_default as RunningInterceptorServerError, interceptorServer } from './chunk-2RNFR6TF.mjs';
|
|
2
2
|
import './chunk-CGILA3WO.mjs';
|
|
3
3
|
//# sourceMappingURL=server.mjs.map
|
|
4
4
|
//# sourceMappingURL=server.mjs.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zimic/interceptor",
|
|
3
|
-
"description": "
|
|
3
|
+
"description": "TypeScript-first HTTP intercepting and mocking",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"zimic",
|
|
6
6
|
"typescript",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"api",
|
|
15
15
|
"static"
|
|
16
16
|
],
|
|
17
|
-
"version": "0.16.0-canary.
|
|
17
|
+
"version": "0.16.0-canary.3",
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
20
20
|
"url": "https://github.com/zimicjs/zimic.git",
|
|
@@ -75,10 +75,10 @@
|
|
|
75
75
|
},
|
|
76
76
|
"dependencies": {
|
|
77
77
|
"@whatwg-node/server": "0.10.1",
|
|
78
|
+
"chalk": "4.1.2",
|
|
78
79
|
"execa": "9.5.2",
|
|
79
80
|
"isomorphic-ws": "5.0.0",
|
|
80
81
|
"msw": "2.7.3",
|
|
81
|
-
"picocolors": "^1.1.1",
|
|
82
82
|
"ws": "8.18.1",
|
|
83
83
|
"yargs": "17.7.2"
|
|
84
84
|
},
|
|
@@ -89,21 +89,21 @@
|
|
|
89
89
|
"@types/node": "^22.13.10",
|
|
90
90
|
"@types/ws": "^8.18.0",
|
|
91
91
|
"@types/yargs": "^17.0.33",
|
|
92
|
-
"@vitest/browser": "^3.0.
|
|
93
|
-
"@vitest/coverage-istanbul": "^3.0.
|
|
92
|
+
"@vitest/browser": "^3.0.8",
|
|
93
|
+
"@vitest/coverage-istanbul": "^3.0.8",
|
|
94
94
|
"dotenv-cli": "^8.0.0",
|
|
95
95
|
"eslint": "^9.22.0",
|
|
96
|
-
"playwright": "^1.51.
|
|
96
|
+
"playwright": "^1.51.0",
|
|
97
97
|
"tsup": "^8.4.0",
|
|
98
98
|
"typescript": "^5.8.2",
|
|
99
|
-
"vitest": "^3.0.
|
|
100
|
-
"@zimic/eslint-config-node": "0.0.0",
|
|
101
|
-
"@zimic/tsconfig": "0.0.0",
|
|
99
|
+
"vitest": "^3.0.8",
|
|
102
100
|
"@zimic/lint-staged-config": "0.0.0",
|
|
103
|
-
"@zimic/
|
|
101
|
+
"@zimic/tsconfig": "0.0.0",
|
|
102
|
+
"@zimic/utils": "0.0.0",
|
|
103
|
+
"@zimic/eslint-config-node": "0.0.0"
|
|
104
104
|
},
|
|
105
105
|
"peerDependencies": {
|
|
106
|
-
"@zimic/http": "^0.
|
|
106
|
+
"@zimic/http": "^0.1.0 || ^0.1.0-canary.0",
|
|
107
107
|
"typescript": ">=4.8.0"
|
|
108
108
|
},
|
|
109
109
|
"peerDependenciesMeta": {
|
package/src/cli/browser/init.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
1
2
|
import filesystem from 'fs/promises';
|
|
2
3
|
import path from 'path';
|
|
3
|
-
import color from 'picocolors';
|
|
4
4
|
|
|
5
5
|
import { logWithPrefix } from '@/utils/console';
|
|
6
6
|
|
|
@@ -20,7 +20,7 @@ async function initializeBrowserServiceWorker({ publicDirectory }: BrowserServic
|
|
|
20
20
|
const destinationPath = path.join(absolutePublicDirectory, SERVICE_WORKER_FILE_NAME);
|
|
21
21
|
await filesystem.copyFile(MOCK_SERVICE_WORKER_PATH, destinationPath);
|
|
22
22
|
|
|
23
|
-
logWithPrefix(`Service worker script saved to ${
|
|
23
|
+
logWithPrefix(`Service worker script saved to ${chalk.green(destinationPath)}!`);
|
|
24
24
|
logWithPrefix('You can now use browser interceptors!');
|
|
25
25
|
}
|
|
26
26
|
|
package/src/cli/server/start.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { InterceptorServer,
|
|
1
|
+
import { InterceptorServer, interceptorServer } from '@/server';
|
|
2
2
|
import { InterceptorServerOptions } from '@/server/types/options';
|
|
3
3
|
import { logWithPrefix } from '@/utils/console';
|
|
4
4
|
import {
|
|
@@ -26,7 +26,7 @@ async function startInterceptorServer({
|
|
|
26
26
|
logUnhandledRequests,
|
|
27
27
|
onReady,
|
|
28
28
|
}: InterceptorServerStartOptions) {
|
|
29
|
-
const server =
|
|
29
|
+
const server = interceptorServer.create({
|
|
30
30
|
hostname,
|
|
31
31
|
port,
|
|
32
32
|
logUnhandledRequests,
|
package/src/http/index.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
+
export { default as InvalidJSONError } from './interceptorWorker/errors/InvalidJSONError';
|
|
2
|
+
export { default as InvalidFormDataError } from './interceptorWorker/errors/InvalidFormDataError';
|
|
1
3
|
export { default as RunningHttpInterceptorError } from './interceptor/errors/RunningHttpInterceptorError';
|
|
2
4
|
export { default as NotRunningHttpInterceptorError } from './interceptor/errors/NotRunningHttpInterceptorError';
|
|
3
5
|
export { default as UnknownHttpInterceptorPlatformError } from './interceptor/errors/UnknownHttpInterceptorPlatformError';
|
|
4
6
|
export { default as UnknownHttpInterceptorTypeError } from './interceptor/errors/UnknownHttpInterceptorTypeError';
|
|
5
|
-
export { default as RequestSavingSafeLimitExceededError } from './interceptor/errors/RequestSavingSafeLimitExceededError';
|
|
6
|
-
|
|
7
|
-
export { default as InvalidFormDataError } from './interceptorWorker/errors/InvalidFormDataError';
|
|
8
|
-
export { default as InvalidJSONError } from './interceptorWorker/errors/InvalidJSONError';
|
|
9
7
|
export { default as UnregisteredBrowserServiceWorkerError } from './interceptorWorker/errors/UnregisteredBrowserServiceWorkerError';
|
|
10
|
-
|
|
11
8
|
export { default as DisabledRequestSavingError } from './requestHandler/errors/DisabledRequestSavingError';
|
|
12
9
|
export { default as TimesCheckError } from './requestHandler/errors/TimesCheckError';
|
|
13
10
|
|
|
@@ -23,17 +23,13 @@ import RemoteHttpRequestHandler from '../requestHandler/RemoteHttpRequestHandler
|
|
|
23
23
|
import { HttpRequestHandler, InternalHttpRequestHandler } from '../requestHandler/types/public';
|
|
24
24
|
import { HttpInterceptorRequest } from '../requestHandler/types/requests';
|
|
25
25
|
import NotRunningHttpInterceptorError from './errors/NotRunningHttpInterceptorError';
|
|
26
|
-
import RequestSavingSafeLimitExceededError from './errors/RequestSavingSafeLimitExceededError';
|
|
27
26
|
import RunningHttpInterceptorError from './errors/RunningHttpInterceptorError';
|
|
28
27
|
import HttpInterceptorStore from './HttpInterceptorStore';
|
|
29
28
|
import { UnhandledRequestStrategy } from './types/options';
|
|
30
|
-
import { HttpInterceptorRequestSaving } from './types/public';
|
|
31
29
|
import { HttpInterceptorRequestContext } from './types/requests';
|
|
32
30
|
|
|
33
31
|
export const SUPPORTED_BASE_URL_PROTOCOLS = Object.freeze(['http', 'https']);
|
|
34
32
|
|
|
35
|
-
export const DEFAULT_REQUEST_SAVING_SAFE_LIMIT = 1000;
|
|
36
|
-
|
|
37
33
|
class HttpInterceptorClient<
|
|
38
34
|
Schema extends HttpSchema,
|
|
39
35
|
HandlerConstructor extends HttpRequestHandlerConstructor = HttpRequestHandlerConstructor,
|
|
@@ -42,9 +38,7 @@ class HttpInterceptorClient<
|
|
|
42
38
|
private store: HttpInterceptorStore;
|
|
43
39
|
|
|
44
40
|
private _baseURL!: URL;
|
|
45
|
-
|
|
46
|
-
requestSaving: HttpInterceptorRequestSaving;
|
|
47
|
-
private numberOfSavedRequests = 0;
|
|
41
|
+
private _saveRequests?: boolean;
|
|
48
42
|
|
|
49
43
|
onUnhandledRequest?: HandlerConstructor extends typeof LocalHttpRequestHandler
|
|
50
44
|
? UnhandledRequestStrategy.Local
|
|
@@ -70,7 +64,7 @@ class HttpInterceptorClient<
|
|
|
70
64
|
worker: HttpInterceptorWorker;
|
|
71
65
|
store: HttpInterceptorStore;
|
|
72
66
|
baseURL: URL;
|
|
73
|
-
|
|
67
|
+
saveRequests?: boolean;
|
|
74
68
|
onUnhandledRequest?: UnhandledRequestStrategy;
|
|
75
69
|
Handler: HandlerConstructor;
|
|
76
70
|
}) {
|
|
@@ -78,12 +72,7 @@ class HttpInterceptorClient<
|
|
|
78
72
|
this.store = options.store;
|
|
79
73
|
|
|
80
74
|
this.baseURL = options.baseURL;
|
|
81
|
-
|
|
82
|
-
this.requestSaving = {
|
|
83
|
-
enabled: options.requestSaving?.enabled ?? this.getDefaultRequestSavingEnabled(),
|
|
84
|
-
safeLimit: options.requestSaving?.safeLimit ?? DEFAULT_REQUEST_SAVING_SAFE_LIMIT,
|
|
85
|
-
};
|
|
86
|
-
|
|
75
|
+
this._saveRequests = options.saveRequests;
|
|
87
76
|
this.onUnhandledRequest = options.onUnhandledRequest satisfies
|
|
88
77
|
| UnhandledRequestStrategy
|
|
89
78
|
| undefined as this['onUnhandledRequest'];
|
|
@@ -91,10 +80,6 @@ class HttpInterceptorClient<
|
|
|
91
80
|
this.Handler = options.Handler;
|
|
92
81
|
}
|
|
93
82
|
|
|
94
|
-
private getDefaultRequestSavingEnabled(): boolean {
|
|
95
|
-
return isServerSide() ? process.env.NODE_ENV === 'test' : false;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
83
|
get baseURL() {
|
|
99
84
|
return this._baseURL;
|
|
100
85
|
}
|
|
@@ -118,6 +103,17 @@ class HttpInterceptorClient<
|
|
|
118
103
|
return this.baseURL.href;
|
|
119
104
|
}
|
|
120
105
|
|
|
106
|
+
get saveRequests() {
|
|
107
|
+
if (this._saveRequests === undefined) {
|
|
108
|
+
return isServerSide() ? process.env.NODE_ENV === 'test' : false;
|
|
109
|
+
}
|
|
110
|
+
return this._saveRequests;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
set saveRequests(saveRequests: boolean) {
|
|
114
|
+
this._saveRequests = saveRequests;
|
|
115
|
+
}
|
|
116
|
+
|
|
121
117
|
get platform() {
|
|
122
118
|
return this.worker.platform;
|
|
123
119
|
}
|
|
@@ -255,7 +251,7 @@ class HttpInterceptorClient<
|
|
|
255
251
|
const responseDeclaration = await matchedHandler.applyResponseDeclaration(parsedRequest);
|
|
256
252
|
const response = HttpInterceptorWorker.createResponseFromDeclaration(request, responseDeclaration);
|
|
257
253
|
|
|
258
|
-
if (this.
|
|
254
|
+
if (this.saveRequests) {
|
|
259
255
|
const responseClone = response.clone();
|
|
260
256
|
|
|
261
257
|
const parsedResponse = await HttpInterceptorWorker.parseRawResponse<
|
|
@@ -269,17 +265,6 @@ class HttpInterceptorClient<
|
|
|
269
265
|
return response;
|
|
270
266
|
}
|
|
271
267
|
|
|
272
|
-
incrementNumberOfSavedRequests(increment: number) {
|
|
273
|
-
this.numberOfSavedRequests = Math.max(this.numberOfSavedRequests + increment, 0);
|
|
274
|
-
|
|
275
|
-
const exceedsSafeLimit = this.numberOfSavedRequests > this.requestSaving.safeLimit;
|
|
276
|
-
|
|
277
|
-
if (increment > 0 && exceedsSafeLimit) {
|
|
278
|
-
const error = new RequestSavingSafeLimitExceededError(this.numberOfSavedRequests, this.requestSaving.safeLimit);
|
|
279
|
-
console.warn(error);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
268
|
private async findMatchedHandler<
|
|
284
269
|
Method extends HttpSchemaMethod<Schema>,
|
|
285
270
|
Path extends HttpSchemaPath<Schema, Method>,
|
|
@@ -4,8 +4,8 @@ import LocalHttpRequestHandler from '../requestHandler/LocalHttpRequestHandler';
|
|
|
4
4
|
import HttpInterceptorClient from './HttpInterceptorClient';
|
|
5
5
|
import HttpInterceptorStore from './HttpInterceptorStore';
|
|
6
6
|
import { SyncHttpInterceptorMethodHandler } from './types/handlers';
|
|
7
|
-
import { LocalHttpInterceptorOptions
|
|
8
|
-
import {
|
|
7
|
+
import { LocalHttpInterceptorOptions } from './types/options';
|
|
8
|
+
import { LocalHttpInterceptor as PublicLocalHttpInterceptor } from './types/public';
|
|
9
9
|
|
|
10
10
|
class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttpInterceptor<Schema> {
|
|
11
11
|
private store = new HttpInterceptorStore();
|
|
@@ -23,7 +23,7 @@ class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttp
|
|
|
23
23
|
baseURL,
|
|
24
24
|
Handler: LocalHttpRequestHandler,
|
|
25
25
|
onUnhandledRequest: options.onUnhandledRequest,
|
|
26
|
-
|
|
26
|
+
saveRequests: options.saveRequests,
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -39,19 +39,19 @@ class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttp
|
|
|
39
39
|
this.client.baseURL = new URL(baseURL);
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
get
|
|
43
|
-
return this.client.
|
|
42
|
+
get saveRequests() {
|
|
43
|
+
return this.client.saveRequests;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
set
|
|
47
|
-
this.client.
|
|
46
|
+
set saveRequests(saveRequests: NonNullable<LocalHttpInterceptorOptions['saveRequests']>) {
|
|
47
|
+
this.client.saveRequests = saveRequests;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
get onUnhandledRequest() {
|
|
51
51
|
return this.client.onUnhandledRequest;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
set onUnhandledRequest(onUnhandledRequest:
|
|
54
|
+
set onUnhandledRequest(onUnhandledRequest: LocalHttpInterceptorOptions['onUnhandledRequest']) {
|
|
55
55
|
this.client.onUnhandledRequest = onUnhandledRequest;
|
|
56
56
|
}
|
|
57
57
|
|
|
@@ -4,8 +4,8 @@ import RemoteHttpRequestHandler from '../requestHandler/RemoteHttpRequestHandler
|
|
|
4
4
|
import HttpInterceptorClient from './HttpInterceptorClient';
|
|
5
5
|
import HttpInterceptorStore from './HttpInterceptorStore';
|
|
6
6
|
import { AsyncHttpInterceptorMethodHandler } from './types/handlers';
|
|
7
|
-
import { RemoteHttpInterceptorOptions
|
|
8
|
-
import {
|
|
7
|
+
import { RemoteHttpInterceptorOptions } from './types/options';
|
|
8
|
+
import { RemoteHttpInterceptor as PublicRemoteHttpInterceptor } from './types/public';
|
|
9
9
|
|
|
10
10
|
class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHttpInterceptor<Schema> {
|
|
11
11
|
private store = new HttpInterceptorStore();
|
|
@@ -24,7 +24,7 @@ class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHt
|
|
|
24
24
|
baseURL,
|
|
25
25
|
Handler: RemoteHttpRequestHandler,
|
|
26
26
|
onUnhandledRequest: options.onUnhandledRequest,
|
|
27
|
-
|
|
27
|
+
saveRequests: options.saveRequests,
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -40,19 +40,19 @@ class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHt
|
|
|
40
40
|
this.client.baseURL = new URL(baseURL);
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
get
|
|
44
|
-
return this.client.
|
|
43
|
+
get saveRequests() {
|
|
44
|
+
return this.client.saveRequests;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
set
|
|
48
|
-
this.client.
|
|
47
|
+
set saveRequests(saveRequests: NonNullable<RemoteHttpInterceptorOptions['saveRequests']>) {
|
|
48
|
+
this.client.saveRequests = saveRequests;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
get onUnhandledRequest() {
|
|
52
52
|
return this.client.onUnhandledRequest;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
set onUnhandledRequest(onUnhandledRequest:
|
|
55
|
+
set onUnhandledRequest(onUnhandledRequest: RemoteHttpInterceptorOptions['onUnhandledRequest']) {
|
|
56
56
|
this.client.onUnhandledRequest = onUnhandledRequest;
|
|
57
57
|
}
|
|
58
58
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { PossiblePromise } from '@zimic/utils/types';
|
|
2
2
|
|
|
3
|
-
import { HttpInterceptorRequestSaving } from './public';
|
|
4
3
|
import { UnhandledHttpInterceptorRequest } from './requests';
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -120,12 +119,20 @@ export interface SharedHttpInterceptorOptions {
|
|
|
120
119
|
baseURL: string;
|
|
121
120
|
|
|
122
121
|
/**
|
|
123
|
-
*
|
|
122
|
+
* Whether {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#httprequesthandler request handlers}
|
|
123
|
+
* should save their intercepted requests in memory and make them accessible through
|
|
124
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests`}.
|
|
124
125
|
*
|
|
126
|
+
* **Important**: If `saveRequests` is true, make sure to regularly clear the interceptor to avoid that the requests
|
|
127
|
+
* accumulate in memory. A common practice is to call
|
|
128
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorclear `interceptor.clear()`}
|
|
129
|
+
* after each test.
|
|
130
|
+
*
|
|
131
|
+
* @default false
|
|
125
132
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
126
133
|
* @see {@link https://github.com/zimicjs/zimic/wiki/guides‐testing‐interceptor Testing}
|
|
127
134
|
*/
|
|
128
|
-
|
|
135
|
+
saveRequests?: boolean;
|
|
129
136
|
}
|
|
130
137
|
|
|
131
138
|
/**
|
|
@@ -3,45 +3,6 @@ import { HttpSchema } from '@zimic/http';
|
|
|
3
3
|
import { SyncHttpInterceptorMethodHandler, AsyncHttpInterceptorMethodHandler } from './handlers';
|
|
4
4
|
import { HttpInterceptorPlatform, UnhandledRequestStrategy } from './options';
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* Configures if the intercepted requests are saved and how they are handled.
|
|
8
|
-
*
|
|
9
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
10
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/guides‐testing‐interceptor Testing}
|
|
11
|
-
*/
|
|
12
|
-
export interface HttpInterceptorRequestSaving {
|
|
13
|
-
/**
|
|
14
|
-
* Whether {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#httprequesthandler request handlers}
|
|
15
|
-
* should save their intercepted requests in memory and make them accessible through
|
|
16
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests`}.
|
|
17
|
-
*
|
|
18
|
-
* If you are using
|
|
19
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorchecktimes `interceptor.checkTimes()`}
|
|
20
|
-
* or
|
|
21
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerchecktimes `handler.checkTimes()`}
|
|
22
|
-
* during tests, consider enabling this option to get more detailed information in `TimesCheckError` errors.
|
|
23
|
-
*
|
|
24
|
-
* **Important**: If `requestSaving.enabled` is `true`, make sure to regularly clear the interceptor to avoid requests
|
|
25
|
-
* accumulating in memory. A common practice is to call
|
|
26
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorclear `interceptor.clear()`}
|
|
27
|
-
* after each test.
|
|
28
|
-
*
|
|
29
|
-
* @default process.env.NODE_ENV === 'test'
|
|
30
|
-
*/
|
|
31
|
-
enabled: boolean;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* The safe number of requests to save in memory before logging warnings in the console. If `requestSaving.enabled` is
|
|
35
|
-
* `true` and the interceptor is not regularly cleared with
|
|
36
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorclear `interceptor.clear()`},
|
|
37
|
-
* the requests may accumulate in memory and cause performance issues. This option does not limit the number of
|
|
38
|
-
* requests saved in memory, only when to log warnings.
|
|
39
|
-
*
|
|
40
|
-
* @default 1000
|
|
41
|
-
*/
|
|
42
|
-
safeLimit: number;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
6
|
/**
|
|
46
7
|
* An interceptor to handle HTTP requests and return mock responses. The methods, paths, status codes, parameters, and
|
|
47
8
|
* responses are statically-typed based on the provided service schema.
|
|
@@ -59,12 +20,20 @@ export interface HttpInterceptor<_Schema extends HttpSchema> {
|
|
|
59
20
|
baseURL: string;
|
|
60
21
|
|
|
61
22
|
/**
|
|
62
|
-
*
|
|
23
|
+
* Whether {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#httprequesthandler request handlers}
|
|
24
|
+
* should save their intercepted requests in memory and make them accessible through
|
|
25
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests`}.
|
|
26
|
+
*
|
|
27
|
+
* **Important**: If `saveRequests` is true, make sure to regularly clear the interceptor to avoid that the requests
|
|
28
|
+
* accumulate in memory. A common practice is to call
|
|
29
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorclear `interceptor.clear()`}
|
|
30
|
+
* after each test.
|
|
63
31
|
*
|
|
32
|
+
* @default process.env.NODE_ENV === 'test'
|
|
64
33
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
65
34
|
* @see {@link https://github.com/zimicjs/zimic/wiki/guides‐testing‐interceptor Testing}
|
|
66
35
|
*/
|
|
67
|
-
|
|
36
|
+
saveRequests: boolean;
|
|
68
37
|
|
|
69
38
|
/**
|
|
70
39
|
* The platform the interceptor is running on.
|
|
@@ -117,9 +86,8 @@ export interface HttpInterceptor<_Schema extends HttpSchema> {
|
|
|
117
86
|
* of each test.
|
|
118
87
|
*
|
|
119
88
|
* When
|
|
120
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#
|
|
121
|
-
*
|
|
122
|
-
* `true` in your interceptor, the `TimesCheckError` errors will also list each unmatched request with diff of the
|
|
89
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#createhttpinterceptoroptions `saveRequests: true`}
|
|
90
|
+
* is enabled in your interceptor, the `TimesCheckError` errors will also list each unmatched request with diff of the
|
|
123
91
|
* expected and received data. This is useful for debugging requests that did not match a handler with
|
|
124
92
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerwithrestriction restrictions}.
|
|
125
93
|
*
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
} from '@zimic/http';
|
|
14
14
|
import isDefined from '@zimic/utils/data/isDefined';
|
|
15
15
|
import { Default, PossiblePromise } from '@zimic/utils/types';
|
|
16
|
-
import
|
|
16
|
+
import chalk from 'chalk';
|
|
17
17
|
|
|
18
18
|
import { removeArrayElement } from '@/utils/arrays';
|
|
19
19
|
import { formatValueToLog, logWithPrefix } from '@/utils/console';
|
|
@@ -477,7 +477,7 @@ abstract class HttpInterceptorWorker {
|
|
|
477
477
|
logWithPrefix(
|
|
478
478
|
[
|
|
479
479
|
`${action === 'bypass' ? 'Warning:' : 'Error:'} Request was not handled and was ` +
|
|
480
|
-
`${action === 'bypass' ?
|
|
480
|
+
`${action === 'bypass' ? chalk.yellow('bypassed') : chalk.red('rejected')}.\n\n `,
|
|
481
481
|
`${request.method} ${request.url}`,
|
|
482
482
|
'\n Headers:',
|
|
483
483
|
formattedHeaders,
|
|
@@ -91,8 +91,7 @@ class HttpRequestHandlerClient<
|
|
|
91
91
|
|
|
92
92
|
newThis.numberOfMatchedRequests = 0;
|
|
93
93
|
newThis.unmatchedRequestGroups.length = 0;
|
|
94
|
-
|
|
95
|
-
newThis.clearInterceptedRequests();
|
|
94
|
+
newThis._requests.length = 0;
|
|
96
95
|
|
|
97
96
|
this.interceptor.registerRequestHandler(this.handler);
|
|
98
97
|
|
|
@@ -133,7 +132,7 @@ class HttpRequestHandlerClient<
|
|
|
133
132
|
declarationPointer: this.timesDeclarationPointer,
|
|
134
133
|
unmatchedRequestGroups: this.unmatchedRequestGroups,
|
|
135
134
|
hasRestrictions: this.restrictions.length > 0,
|
|
136
|
-
|
|
135
|
+
hasSavedRequests: this.interceptor.saveRequests,
|
|
137
136
|
});
|
|
138
137
|
}
|
|
139
138
|
}
|
|
@@ -149,8 +148,7 @@ class HttpRequestHandlerClient<
|
|
|
149
148
|
|
|
150
149
|
this.numberOfMatchedRequests = 0;
|
|
151
150
|
this.unmatchedRequestGroups.length = 0;
|
|
152
|
-
|
|
153
|
-
this.clearInterceptedRequests();
|
|
151
|
+
this._requests.length = 0;
|
|
154
152
|
|
|
155
153
|
this.createResponseDeclaration = undefined;
|
|
156
154
|
|
|
@@ -170,9 +168,7 @@ class HttpRequestHandlerClient<
|
|
|
170
168
|
this.numberOfMatchedRequests++;
|
|
171
169
|
} else {
|
|
172
170
|
const shouldSaveUnmatchedGroup =
|
|
173
|
-
this.interceptor.
|
|
174
|
-
this.restrictions.length > 0 &&
|
|
175
|
-
this.timesDeclarationPointer !== undefined;
|
|
171
|
+
this.interceptor.saveRequests && this.restrictions.length > 0 && this.timesDeclarationPointer !== undefined;
|
|
176
172
|
|
|
177
173
|
if (shouldSaveUnmatchedGroup) {
|
|
178
174
|
this.unmatchedRequestGroups.push({ request, diff: restrictionsMatch.diff });
|
|
@@ -382,12 +378,6 @@ class HttpRequestHandlerClient<
|
|
|
382
378
|
) {
|
|
383
379
|
const interceptedRequest = this.createInterceptedRequest(request, response);
|
|
384
380
|
this._requests.push(interceptedRequest);
|
|
385
|
-
this.interceptor.incrementNumberOfSavedRequests(1);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
private clearInterceptedRequests() {
|
|
389
|
-
this.interceptor.incrementNumberOfSavedRequests(-this._requests.length);
|
|
390
|
-
this._requests.length = 0;
|
|
391
381
|
}
|
|
392
382
|
|
|
393
383
|
private createInterceptedRequest(
|
|
@@ -411,7 +401,7 @@ class HttpRequestHandlerClient<
|
|
|
411
401
|
}
|
|
412
402
|
|
|
413
403
|
get requests(): readonly InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[] {
|
|
414
|
-
if (!this.interceptor.
|
|
404
|
+
if (!this.interceptor.saveRequests) {
|
|
415
405
|
throw new DisabledRequestSavingError();
|
|
416
406
|
}
|
|
417
407
|
|
|
@@ -7,7 +7,7 @@ class DisabledRequestSavingError extends TypeError {
|
|
|
7
7
|
constructor() {
|
|
8
8
|
super(
|
|
9
9
|
'Intercepted requests are not being saved. ' +
|
|
10
|
-
'Did you forget to use `
|
|
10
|
+
'Did you forget to use `saveRequests: true` when creating the interceptor?\n\n' +
|
|
11
11
|
'Learn more: https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests',
|
|
12
12
|
);
|
|
13
13
|
this.name = 'DisabledRequestSavingError';
|