@hyperttp/interceptors 1.0.4 → 1.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.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/package.json +12 -4
- package/plugin.d.ts +23 -3
- package/plugin.d.ts.map +1 -1
- package/plugin.js +44 -16
- package/plugin.js.map +1 -1
- package/types/interceptors.d.ts +29 -14
- package/types/interceptors.d.ts.map +1 -1
- package/utils/InterceptorManager.d.ts +67 -5
- package/utils/InterceptorManager.d.ts.map +1 -1
- package/utils/InterceptorManager.js +74 -25
- package/utils/InterceptorManager.js.map +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 IT IF OR
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# hyperttp-interceptors
|
package/package.json
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyperttp/interceptors",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./index.d.ts",
|
|
10
|
+
"import": "./index.js",
|
|
11
|
+
"require": "./index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
7
15
|
"keywords": [
|
|
8
16
|
"hyperttp"
|
|
9
17
|
],
|
|
@@ -14,7 +22,7 @@
|
|
|
14
22
|
"url": "git+https://github.com/IT-IF-OR/hyperttp-interceptors.git"
|
|
15
23
|
},
|
|
16
24
|
"peerDependencies": {
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
25
|
+
"@hyperttp/types": "^0.1.5"
|
|
26
|
+
},
|
|
27
|
+
"private": false
|
|
20
28
|
}
|
package/plugin.d.ts
CHANGED
|
@@ -1,14 +1,34 @@
|
|
|
1
|
-
import type { HyperPlugin } from "@hyperttp/
|
|
1
|
+
import type { HyperPlugin } from "@hyperttp/types";
|
|
2
2
|
import { InterceptorManager } from "./utils/InterceptorManager.js";
|
|
3
3
|
import type { InterceptorOptions } from "./types/interceptors.js";
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* @en Extends PluginContext to include the interceptor manager instance.
|
|
6
|
+
* @ru Расширяет PluginContext, добавляя экземпляр менеджера интерцепторов.
|
|
7
|
+
*/
|
|
8
|
+
declare module "@hyperttp/types" {
|
|
6
9
|
interface PluginContext {
|
|
10
|
+
/**
|
|
11
|
+
* @en The active interceptor manager instance.
|
|
12
|
+
* @ru Активный экземпляр менеджера интерцепторов.
|
|
13
|
+
*/
|
|
7
14
|
interceptors?: InterceptorManager;
|
|
8
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* @en Extends HttpClientOptions to include interceptor configuration.
|
|
18
|
+
* @ru Расширяет HttpClientOptions, добавляя конфигурацию интерцепторов.
|
|
19
|
+
*/
|
|
9
20
|
interface HttpClientOptions {
|
|
21
|
+
/**
|
|
22
|
+
* @en Interceptor plugin configuration options.
|
|
23
|
+
* @ru Опции конфигурации плагина интерцепторов.
|
|
24
|
+
*/
|
|
10
25
|
interceptors?: InterceptorOptions;
|
|
11
26
|
}
|
|
12
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* @en Plugin for integrating custom request and response interceptors (Axios-like Interceptors).
|
|
30
|
+
* @ru Плагин для интеграции кастомных интерцепторов запросов и ответов (Axios-like Interceptors).
|
|
31
|
+
* @returns HyperPlugin object instance.
|
|
32
|
+
*/
|
|
13
33
|
export declare function withInterceptors(): HyperPlugin;
|
|
14
34
|
//# sourceMappingURL=plugin.d.ts.map
|
package/plugin.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EAKZ,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAElE;;;GAGG;AACH,OAAO,QAAQ,iBAAiB,CAAC;IAC/B,UAAU,aAAa;QACrB;;;WAGG;QACH,YAAY,CAAC,EAAE,kBAAkB,CAAC;KACnC;IAED;;;OAGG;IACH,UAAU,iBAAiB;QACzB;;;WAGG;QACH,YAAY,CAAC,EAAE,kBAAkB,CAAC;KACnC;CACF;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,WAAW,CAsD9C"}
|
package/plugin.js
CHANGED
|
@@ -1,27 +1,55 @@
|
|
|
1
1
|
import { InterceptorManager } from "./utils/InterceptorManager.js";
|
|
2
|
+
/**
|
|
3
|
+
* @en Plugin for integrating custom request and response interceptors (Axios-like Interceptors).
|
|
4
|
+
* @ru Плагин для интеграции кастомных интерцепторов запросов и ответов (Axios-like Interceptors).
|
|
5
|
+
* @returns HyperPlugin object instance.
|
|
6
|
+
*/
|
|
2
7
|
export function withInterceptors() {
|
|
3
8
|
let manager;
|
|
4
9
|
return {
|
|
5
10
|
name: "hyperttp-interceptors",
|
|
6
|
-
|
|
11
|
+
/**
|
|
12
|
+
* @en Evaluates plugin activation based on the provided client configuration.
|
|
13
|
+
* @ru Проверяет активацию плагина на основе переданной конфигурации.
|
|
14
|
+
* @param config - The current client configuration.
|
|
15
|
+
* @returns True if interceptors are enabled.
|
|
16
|
+
*/
|
|
7
17
|
enabled: (config) => !!config.interceptors?.enabled,
|
|
8
|
-
|
|
18
|
+
/**
|
|
19
|
+
* @en Initialization hook. Creates the interceptor manager and registers it within the core context.
|
|
20
|
+
* @ru Хук инициализации. Создает менеджер интерцепторов и регистрирует его в контексте ядра.
|
|
21
|
+
* @param ctx - Shared plugin orchestration context.
|
|
22
|
+
*/
|
|
23
|
+
setup(ctx) {
|
|
9
24
|
manager = new InterceptorManager();
|
|
10
|
-
|
|
25
|
+
ctx.interceptors = manager;
|
|
11
26
|
},
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
/**
|
|
28
|
+
* @en Request phase interceptor hook. Sequentially applies all registered interceptors to the request configuration.
|
|
29
|
+
* @ru Перехватчик фазы запроса. Последовательно применяет все зарегистрированные интерцепторы к конфигурации запроса.
|
|
30
|
+
* @param req - Contextual internal request parameters.
|
|
31
|
+
*/
|
|
32
|
+
async onRequest(req) {
|
|
33
|
+
if (!manager.hasRequest)
|
|
34
|
+
return;
|
|
35
|
+
const reqResult = await manager.applyRequest(req);
|
|
36
|
+
if (reqResult) {
|
|
37
|
+
Object.assign(req, reqResult);
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
/**
|
|
41
|
+
* @en Response phase interceptor hook. Passes the response object through the chain of custom modification interceptors.
|
|
42
|
+
* @ru Перехватчик фазы успешного ответа. Пропускает объект ответа через цепочку пользовательских интерцепторов модификации.
|
|
43
|
+
* @param res - Output HTTP client response reference.
|
|
44
|
+
* @param req - Contextual internal request parameters.
|
|
45
|
+
*/
|
|
46
|
+
async onResponse(res) {
|
|
47
|
+
if (!manager.hasResponse)
|
|
48
|
+
return;
|
|
49
|
+
const resResult = await manager.applyResponse(res);
|
|
50
|
+
if (resResult) {
|
|
51
|
+
Object.assign(res, resResult);
|
|
52
|
+
}
|
|
25
53
|
},
|
|
26
54
|
};
|
|
27
55
|
}
|
package/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AA6BnE;;;;GAIG;AACH,MAAM,UAAU,gBAAgB;IAC9B,IAAI,OAA2B,CAAC;IAEhC,OAAO;QACL,IAAI,EAAE,uBAAuB;QAE7B;;;;;WAKG;QACH,OAAO,EAAE,CAAC,MAAyB,EAAW,EAAE,CAC9C,CAAC,CAAC,MAAM,CAAC,YAAY,EAAE,OAAO;QAEhC;;;;WAIG;QACH,KAAK,CAAC,GAAkB;YACtB,OAAO,GAAG,IAAI,kBAAkB,EAAE,CAAC;YACnC,GAAG,CAAC,YAAY,GAAG,OAAO,CAAC;QAC7B,CAAC;QAED;;;;WAIG;QACH,KAAK,CAAC,SAAS,CAAC,GAAoB;YAClC,IAAI,CAAC,OAAO,CAAC,UAAU;gBAAE,OAAO;YAEhC,MAAM,SAAS,GAAG,MAAM,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;YAClD,IAAI,SAAS,EAAE,CAAC;gBACd,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;YAChC,CAAC;QACH,CAAC;QAED;;;;;WAKG;QACH,KAAK,CAAC,UAAU,CAAC,GAAsB;YACrC,IAAI,CAAC,OAAO,CAAC,WAAW;gBAAE,OAAO;YAEjC,MAAM,SAAS,GAAG,MAAM,OAAO,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;YACnD,IAAI,SAAS,EAAE,CAAC;gBACd,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;YAChC,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/types/interceptors.d.ts
CHANGED
|
@@ -1,20 +1,35 @@
|
|
|
1
|
+
import type { InternalRequest, HttpResponse } from "@hyperttp/types";
|
|
2
|
+
/**
|
|
3
|
+
* @en Configuration options for the interceptors plugin.
|
|
4
|
+
* @ru Опции конфигурации для плагина интерцепторов.
|
|
5
|
+
*/
|
|
1
6
|
export interface InterceptorOptions {
|
|
2
7
|
/**
|
|
3
|
-
* @
|
|
4
|
-
* @
|
|
8
|
+
* @en Enable interceptor processing for the client.
|
|
9
|
+
* @ru Включить обработку интерцепторов для клиента.
|
|
5
10
|
*/
|
|
6
11
|
enabled?: boolean;
|
|
7
12
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
/**
|
|
14
|
+
* @en Request interceptor function signature.
|
|
15
|
+
* Accepts the core internal request object. Can return a modified configuration
|
|
16
|
+
* or `void` (if modifications are applied directly to the passed object reference).
|
|
17
|
+
* @ru Перехватчик запроса.
|
|
18
|
+
* Принимает внутренний объект запроса ядра. Может вернуть модифицированный объект
|
|
19
|
+
* или `void` (если изменения вносятся напрямую в переданный объект по ссылке).
|
|
20
|
+
* @param config - The internal request configuration.
|
|
21
|
+
* @returns A modified request object, void, or a Promise resolving to either.
|
|
22
|
+
*/
|
|
23
|
+
export type RequestInterceptor = (config: InternalRequest) => InternalRequest | void | Promise<InternalRequest | void>;
|
|
24
|
+
/**
|
|
25
|
+
* @en Response interceptor function signature.
|
|
26
|
+
* Accepts a response object with any body layout (`HttpResponse<any>`). Can return
|
|
27
|
+
* a transformed response container or `void` (if the object is mutated in-place).
|
|
28
|
+
* @ru Перехватчик ответа.
|
|
29
|
+
* Принимает объект ответа с любым типом тела (`HttpResponse<any>`). Может вернуть
|
|
30
|
+
* трансформированный объект ответа или `void` (если объект мутирует по ссылке).
|
|
31
|
+
* @param response - The HTTP response object.
|
|
32
|
+
* @returns A modified response object, void, or a Promise resolving to either.
|
|
33
|
+
*/
|
|
34
|
+
export type ResponseInterceptor = (response: HttpResponse<any>) => HttpResponse<any> | void | Promise<HttpResponse<any> | void>;
|
|
20
35
|
//# sourceMappingURL=interceptors.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interceptors.d.ts","sourceRoot":"","sources":["../../src/types/interceptors.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,MAAM,kBAAkB,GAAG,
|
|
1
|
+
{"version":3,"file":"interceptors.d.ts","sourceRoot":"","sources":["../../src/types/interceptors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAErE;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;;;;;;;;GASG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAC/B,MAAM,EAAE,eAAe,KACpB,eAAe,GAAG,IAAI,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAAC;AAE9D;;;;;;;;;GASG;AACH,MAAM,MAAM,mBAAmB,GAAG,CAChC,QAAQ,EAAE,YAAY,CAAC,GAAG,CAAC,KACxB,YAAY,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC"}
|
|
@@ -1,22 +1,84 @@
|
|
|
1
|
+
import { InternalRequest, HttpResponse } from "@hyperttp/types";
|
|
1
2
|
import { RequestInterceptor, ResponseInterceptor } from "../types/interceptors.js";
|
|
3
|
+
/**
|
|
4
|
+
* @en Interceptor manager for pipeline execution of requests and responses.
|
|
5
|
+
* Uses a hybrid loop (fast-path): runs synchronously until a Promise is encountered.
|
|
6
|
+
* @ru Менеджер перехватчиков (интерцепторов) для сквозной обработки запросов и ответов.
|
|
7
|
+
* Использует гибридный цикл (fast-path): выполняется синхронно до тех пор, пока не встретит Promise.
|
|
8
|
+
*/
|
|
2
9
|
export declare class InterceptorManager {
|
|
10
|
+
/**
|
|
11
|
+
* @en Array of registered request interceptors.
|
|
12
|
+
* @ru Массив зарегистрированных перехватчиков запроса.
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
3
15
|
private requestInterceptors;
|
|
16
|
+
/**
|
|
17
|
+
* @en Array of registered response interceptors.
|
|
18
|
+
* @ru Массив зарегистрированных перехватчиков ответа.
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
4
21
|
private responseInterceptors;
|
|
22
|
+
/**
|
|
23
|
+
* @en Flag indicating presence of active request interceptors for fast branch short-circuiting.
|
|
24
|
+
* @ru Флаг наличия активных перехватчиков запроса для быстрого ветвления.
|
|
25
|
+
*/
|
|
5
26
|
hasRequest: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* @en Flag indicating presence of active response interceptors for fast branch short-circuiting.
|
|
29
|
+
* @ru Флаг наличия активных перехватчиков ответа для быстрого ветвления.
|
|
30
|
+
*/
|
|
6
31
|
hasResponse: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* @en Registers a new request interceptor in the pipeline.
|
|
34
|
+
* @ru Регистрирует новый перехватчик запроса в конвейере.
|
|
35
|
+
* @param interceptor - Target request interceptor function.
|
|
36
|
+
*/
|
|
7
37
|
addRequest(interceptor: RequestInterceptor): void;
|
|
38
|
+
/**
|
|
39
|
+
* @en Registers a new response interceptor in the pipeline.
|
|
40
|
+
* @ru Регистрирует новый перехватчик ответа в конвейере.
|
|
41
|
+
* @param interceptor - Target response interceptor function.
|
|
42
|
+
*/
|
|
8
43
|
addResponse(interceptor: ResponseInterceptor): void;
|
|
9
44
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
45
|
+
* @en Sequentially applies the chain of interceptors to the request configuration.
|
|
46
|
+
* @ru Последовательно применяет цепочку перехватчиков к конфигурации запроса.
|
|
47
|
+
* @param config - Initial internal request configuration object.
|
|
48
|
+
* @returns Modified configuration or a Promise resolving to it.
|
|
49
|
+
*/
|
|
50
|
+
applyRequest(config: InternalRequest): InternalRequest | Promise<InternalRequest>;
|
|
51
|
+
/**
|
|
52
|
+
* @en Asynchronous fallback to process the remaining request chain after encountering a Promise.
|
|
53
|
+
* @ru Асинхронный фолбек для обработки оставшейся цепочки запросов после обнаружения Promise.
|
|
54
|
+
* @param startIndex - Index of the interceptor that returned a Promise.
|
|
55
|
+
* @param currentPromise - The pending Promise from the current interceptor step.
|
|
56
|
+
* @param currentResult - Cumulative configuration result before the Promise step.
|
|
57
|
+
* @private
|
|
12
58
|
*/
|
|
13
|
-
applyRequest(config: any): any;
|
|
14
59
|
private applyRequestAsync;
|
|
15
60
|
/**
|
|
16
|
-
*
|
|
61
|
+
* @en Sequentially applies the chain of interceptors to the incoming response container.
|
|
62
|
+
* @ru Последовательно применяет цепочку перехватчиков к полученному объекту ответа.
|
|
63
|
+
* @template T - Type mapping descriptor for the expected response payload body.
|
|
64
|
+
* @param response - Target HTTP response context wrapper.
|
|
65
|
+
* @returns Transformed response context or a Promise resolving to it.
|
|
66
|
+
*/
|
|
67
|
+
applyResponse<T = unknown>(response: HttpResponse<T>): HttpResponse<T> | Promise<HttpResponse<T>>;
|
|
68
|
+
/**
|
|
69
|
+
* @en Asynchronous fallback to process the remaining response chain after encountering a Promise.
|
|
70
|
+
* @ru Асинхронный фолбек для обработки оставшейся цепочки ответов после обнаружения Promise.
|
|
71
|
+
* @template T - Type mapping descriptor for the expected response payload body.
|
|
72
|
+
* @param startIndex - Index of the interceptor that returned a Promise.
|
|
73
|
+
* @param currentPromise - The pending Promise from the current interceptor step.
|
|
74
|
+
* @param currentResult - Cumulative response result before the Promise step.
|
|
75
|
+
* @private
|
|
17
76
|
*/
|
|
18
|
-
applyResponse(response: any): any;
|
|
19
77
|
private applyResponseAsync;
|
|
78
|
+
/**
|
|
79
|
+
* @en Completely flushes internal interceptor registers and resets state indicators.
|
|
80
|
+
* @ru Полностью очищает списки зарегистрированных перехватчиков и сбрасывает флаги.
|
|
81
|
+
*/
|
|
20
82
|
clear(): void;
|
|
21
83
|
}
|
|
22
84
|
//# sourceMappingURL=InterceptorManager.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InterceptorManager.d.ts","sourceRoot":"","sources":["../../src/utils/InterceptorManager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,0BAA0B,CAAC;AAElC,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,mBAAmB,CAA4B;
|
|
1
|
+
{"version":3,"file":"InterceptorManager.d.ts","sourceRoot":"","sources":["../../src/utils/InterceptorManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,0BAA0B,CAAC;AAElC;;;;;GAKG;AACH,qBAAa,kBAAkB;IAC7B;;;;OAIG;IACH,OAAO,CAAC,mBAAmB,CAA4B;IAEvD;;;;OAIG;IACH,OAAO,CAAC,oBAAoB,CAA6B;IAEzD;;;OAGG;IACI,UAAU,UAAS;IAE1B;;;OAGG;IACI,WAAW,UAAS;IAE3B;;;;OAIG;IACH,UAAU,CAAC,WAAW,EAAE,kBAAkB,GAAG,IAAI;IAKjD;;;;OAIG;IACH,WAAW,CAAC,WAAW,EAAE,mBAAmB,GAAG,IAAI;IAKnD;;;;;OAKG;IACH,YAAY,CACV,MAAM,EAAE,eAAe,GACtB,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC;IAa7C;;;;;;;OAOG;YACW,iBAAiB;IAc/B;;;;;;OAMG;IACH,aAAa,CAAC,CAAC,GAAG,OAAO,EACvB,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC,GACxB,YAAY,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;IAgB7C;;;;;;;;OAQG;YACW,kBAAkB;IAsBhC;;;OAGG;IACH,KAAK,IAAI,IAAI;CAMd"}
|
|
@@ -1,54 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @en Interceptor manager for pipeline execution of requests and responses.
|
|
3
|
+
* Uses a hybrid loop (fast-path): runs synchronously until a Promise is encountered.
|
|
4
|
+
* @ru Менеджер перехватчиков (интерцепторов) для сквозной обработки запросов и ответов.
|
|
5
|
+
* Использует гибридный цикл (fast-path): выполняется синхронно до тех пор, пока не встретит Promise.
|
|
6
|
+
*/
|
|
1
7
|
export class InterceptorManager {
|
|
8
|
+
/**
|
|
9
|
+
* @en Array of registered request interceptors.
|
|
10
|
+
* @ru Массив зарегистрированных перехватчиков запроса.
|
|
11
|
+
* @private
|
|
12
|
+
*/
|
|
2
13
|
requestInterceptors = [];
|
|
14
|
+
/**
|
|
15
|
+
* @en Array of registered response interceptors.
|
|
16
|
+
* @ru Массив зарегистрированных перехватчиков ответа.
|
|
17
|
+
* @private
|
|
18
|
+
*/
|
|
3
19
|
responseInterceptors = [];
|
|
20
|
+
/**
|
|
21
|
+
* @en Flag indicating presence of active request interceptors for fast branch short-circuiting.
|
|
22
|
+
* @ru Флаг наличия активных перехватчиков запроса для быстрого ветвления.
|
|
23
|
+
*/
|
|
4
24
|
hasRequest = false;
|
|
25
|
+
/**
|
|
26
|
+
* @en Flag indicating presence of active response interceptors for fast branch short-circuiting.
|
|
27
|
+
* @ru Флаг наличия активных перехватчиков ответа для быстрого ветвления.
|
|
28
|
+
*/
|
|
5
29
|
hasResponse = false;
|
|
30
|
+
/**
|
|
31
|
+
* @en Registers a new request interceptor in the pipeline.
|
|
32
|
+
* @ru Регистрирует новый перехватчик запроса в конвейере.
|
|
33
|
+
* @param interceptor - Target request interceptor function.
|
|
34
|
+
*/
|
|
6
35
|
addRequest(interceptor) {
|
|
7
36
|
this.requestInterceptors.push(interceptor);
|
|
8
37
|
this.hasRequest = true;
|
|
9
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* @en Registers a new response interceptor in the pipeline.
|
|
41
|
+
* @ru Регистрирует новый перехватчик ответа в конвейере.
|
|
42
|
+
* @param interceptor - Target response interceptor function.
|
|
43
|
+
*/
|
|
10
44
|
addResponse(interceptor) {
|
|
11
45
|
this.responseInterceptors.push(interceptor);
|
|
12
46
|
this.hasResponse = true;
|
|
13
47
|
}
|
|
14
48
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
49
|
+
* @en Sequentially applies the chain of interceptors to the request configuration.
|
|
50
|
+
* @ru Последовательно применяет цепочку перехватчиков к конфигурации запроса.
|
|
51
|
+
* @param config - Initial internal request configuration object.
|
|
52
|
+
* @returns Modified configuration or a Promise resolving to it.
|
|
17
53
|
*/
|
|
18
54
|
applyRequest(config) {
|
|
19
55
|
let result = config;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return this.applyRequestAsync(i, nextConfig, result);
|
|
56
|
+
for (let i = 0; i < this.requestInterceptors.length; i++) {
|
|
57
|
+
const next = this.requestInterceptors[i](result);
|
|
58
|
+
if (next instanceof Promise) {
|
|
59
|
+
return this.applyRequestAsync(i + 1, next, result);
|
|
25
60
|
}
|
|
26
|
-
if (
|
|
27
|
-
result =
|
|
61
|
+
if (next)
|
|
62
|
+
result = next;
|
|
28
63
|
}
|
|
29
64
|
return result;
|
|
30
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* @en Asynchronous fallback to process the remaining request chain after encountering a Promise.
|
|
68
|
+
* @ru Асинхронный фолбек для обработки оставшейся цепочки запросов после обнаружения Promise.
|
|
69
|
+
* @param startIndex - Index of the interceptor that returned a Promise.
|
|
70
|
+
* @param currentPromise - The pending Promise from the current interceptor step.
|
|
71
|
+
* @param currentResult - Cumulative configuration result before the Promise step.
|
|
72
|
+
* @private
|
|
73
|
+
*/
|
|
31
74
|
async applyRequestAsync(startIndex, currentPromise, currentResult) {
|
|
32
|
-
let result = currentResult;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
result =
|
|
36
|
-
const len = this.requestInterceptors.length;
|
|
37
|
-
for (let i = startIndex + 1; i < len; i++) {
|
|
38
|
-
const nextConfig = this.requestInterceptors[i](result);
|
|
39
|
-
if (nextConfig instanceof Promise) {
|
|
40
|
-
const res = await nextConfig;
|
|
41
|
-
if (res)
|
|
42
|
-
result = res;
|
|
43
|
-
}
|
|
44
|
-
else if (nextConfig) {
|
|
45
|
-
result = nextConfig;
|
|
46
|
-
}
|
|
75
|
+
let result = (await currentPromise) ?? currentResult;
|
|
76
|
+
for (let i = startIndex; i < this.requestInterceptors.length; i++) {
|
|
77
|
+
const next = this.requestInterceptors[i](result);
|
|
78
|
+
result = (next instanceof Promise ? await next : next) ?? result;
|
|
47
79
|
}
|
|
48
80
|
return result;
|
|
49
81
|
}
|
|
50
82
|
/**
|
|
51
|
-
*
|
|
83
|
+
* @en Sequentially applies the chain of interceptors to the incoming response container.
|
|
84
|
+
* @ru Последовательно применяет цепочку перехватчиков к полученному объекту ответа.
|
|
85
|
+
* @template T - Type mapping descriptor for the expected response payload body.
|
|
86
|
+
* @param response - Target HTTP response context wrapper.
|
|
87
|
+
* @returns Transformed response context or a Promise resolving to it.
|
|
52
88
|
*/
|
|
53
89
|
applyResponse(response) {
|
|
54
90
|
let result = response;
|
|
@@ -63,6 +99,15 @@ export class InterceptorManager {
|
|
|
63
99
|
}
|
|
64
100
|
return result;
|
|
65
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* @en Asynchronous fallback to process the remaining response chain after encountering a Promise.
|
|
104
|
+
* @ru Асинхронный фолбек для обработки оставшейся цепочки ответов после обнаружения Promise.
|
|
105
|
+
* @template T - Type mapping descriptor for the expected response payload body.
|
|
106
|
+
* @param startIndex - Index of the interceptor that returned a Promise.
|
|
107
|
+
* @param currentPromise - The pending Promise from the current interceptor step.
|
|
108
|
+
* @param currentResult - Cumulative response result before the Promise step.
|
|
109
|
+
* @private
|
|
110
|
+
*/
|
|
66
111
|
async applyResponseAsync(startIndex, currentPromise, currentResult) {
|
|
67
112
|
let result = currentResult;
|
|
68
113
|
const resolved = await currentPromise;
|
|
@@ -82,6 +127,10 @@ export class InterceptorManager {
|
|
|
82
127
|
}
|
|
83
128
|
return result;
|
|
84
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* @en Completely flushes internal interceptor registers and resets state indicators.
|
|
132
|
+
* @ru Полностью очищает списки зарегистрированных перехватчиков и сбрасывает флаги.
|
|
133
|
+
*/
|
|
85
134
|
clear() {
|
|
86
135
|
this.requestInterceptors = [];
|
|
87
136
|
this.responseInterceptors = [];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InterceptorManager.js","sourceRoot":"","sources":["../../src/utils/InterceptorManager.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"InterceptorManager.js","sourceRoot":"","sources":["../../src/utils/InterceptorManager.ts"],"names":[],"mappings":"AAMA;;;;;GAKG;AACH,MAAM,OAAO,kBAAkB;IAC7B;;;;OAIG;IACK,mBAAmB,GAAyB,EAAE,CAAC;IAEvD;;;;OAIG;IACK,oBAAoB,GAA0B,EAAE,CAAC;IAEzD;;;OAGG;IACI,UAAU,GAAG,KAAK,CAAC;IAE1B;;;OAGG;IACI,WAAW,GAAG,KAAK,CAAC;IAE3B;;;;OAIG;IACH,UAAU,CAAC,WAA+B;QACxC,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAC3C,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;IACzB,CAAC;IAED;;;;OAIG;IACH,WAAW,CAAC,WAAgC;QAC1C,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAC5C,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;IAC1B,CAAC;IAED;;;;;OAKG;IACH,YAAY,CACV,MAAuB;QAEvB,IAAI,MAAM,GAAG,MAAM,CAAC;QACpB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,mBAAmB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACzD,MAAM,IAAI,GAAG,IAAI,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;YAEjD,IAAI,IAAI,YAAY,OAAO,EAAE,CAAC;gBAC5B,OAAO,IAAI,CAAC,iBAAiB,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;YACrD,CAAC;YACD,IAAI,IAAI;gBAAE,MAAM,GAAG,IAAI,CAAC;QAC1B,CAAC;QACD,OAAO,MAAM,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