@hyperttp/interceptors 1.0.3 → 1.1.0
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 +4 -4
- package/plugin.d.ts +7 -3
- package/plugin.d.ts.map +1 -1
- package/plugin.js +42 -14
- package/plugin.js.map +1 -1
- package/types/interceptors.d.ts +23 -17
- 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 +64 -3
- 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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyperttp/interceptors",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"url": "git+https://github.com/IT-IF-OR/hyperttp-interceptors.git"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
"@hyperttp/types": "^0.1.1"
|
|
18
|
+
},
|
|
19
|
+
"private": false
|
|
20
20
|
}
|
package/plugin.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
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
|
-
declare module "@hyperttp/core" {
|
|
4
|
+
declare module "@hyperttp/types" {
|
|
6
5
|
interface PluginContext {
|
|
7
6
|
interceptors?: InterceptorManager;
|
|
8
7
|
}
|
|
@@ -10,5 +9,10 @@ declare module "@hyperttp/core" {
|
|
|
10
9
|
interceptors?: InterceptorOptions;
|
|
11
10
|
}
|
|
12
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* @ru Плагин для интеграции кастомных интерцепторов запросов и ответов (Axios-like Interceptors).
|
|
14
|
+
* @en Plugin for integrating custom request and response interceptors (Axios-like Interceptors).
|
|
15
|
+
* @returns HyperPlugin object instance.
|
|
16
|
+
*/
|
|
13
17
|
export declare function withInterceptors(): HyperPlugin;
|
|
14
18
|
//# 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,OAAO,QAAQ,iBAAiB,CAAC;IAC/B,UAAU,aAAa;QACrB,YAAY,CAAC,EAAE,kBAAkB,CAAC;KACnC;IAED,UAAU,iBAAiB;QACzB,YAAY,CAAC,EAAE,kBAAkB,CAAC;KACnC;CACF;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,WAAW,CAyD9C"}
|
package/plugin.js
CHANGED
|
@@ -1,27 +1,55 @@
|
|
|
1
1
|
import { InterceptorManager } from "./utils/InterceptorManager.js";
|
|
2
|
+
/**
|
|
3
|
+
* @ru Плагин для интеграции кастомных интерцепторов запросов и ответов (Axios-like Interceptors).
|
|
4
|
+
* @en Plugin for integrating custom request and response interceptors (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
|
+
* @ru Проверяет активацию плагина на основе переданной конфигурации.
|
|
13
|
+
* @en Evaluates plugin activation based on the provided client configuration.
|
|
14
|
+
*/
|
|
7
15
|
enabled: (config) => !!config.interceptors?.enabled,
|
|
16
|
+
/**
|
|
17
|
+
* @ru Хук инициализации. Создает менеджер интерцепторов и регистрирует его в контексте ядра.
|
|
18
|
+
* @en Initialization hook. Creates the interceptor manager and registers it within the core context.
|
|
19
|
+
* @param core - Shared plugin orchestration context.
|
|
20
|
+
*/
|
|
8
21
|
setup(core) {
|
|
9
22
|
manager = new InterceptorManager();
|
|
10
23
|
core.interceptors = manager;
|
|
11
24
|
},
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
+
/**
|
|
26
|
+
* @ru Перехватчик фазы запроса. Последовательно применяет все зарегистрированные интерцепторы к конфигурации запроса.
|
|
27
|
+
* @en Request phase interceptor hook. Sequentially applies all registered interceptors to the request configuration.
|
|
28
|
+
* @param req - Contextual internal request parameters.
|
|
29
|
+
*/
|
|
30
|
+
async onRequest(req) {
|
|
31
|
+
if (!manager.hasRequest)
|
|
32
|
+
return;
|
|
33
|
+
const reqResult = await manager.applyRequest(req);
|
|
34
|
+
if (reqResult) {
|
|
35
|
+
// Накатываем изменения на текущую ссылку запроса, если интерцептор вернул обновленный объект
|
|
36
|
+
Object.assign(req, reqResult);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
/**
|
|
40
|
+
* @ru Перехватчик фазы успешного ответа. Пропускает объект ответа через цепочку пользовательских интерцепторов модификации.
|
|
41
|
+
* @en Response phase interceptor hook. Passes the response object through the chain of custom modification interceptors.
|
|
42
|
+
* @param res - Output HTTP client response reference.
|
|
43
|
+
* @param req - Contextual internal request parameters.
|
|
44
|
+
*/
|
|
45
|
+
async onResponse(res, req) {
|
|
46
|
+
if (!manager.hasResponse)
|
|
47
|
+
return;
|
|
48
|
+
const resResult = await manager.applyResponse(res);
|
|
49
|
+
if (resResult) {
|
|
50
|
+
// Мутируем объект ответа перед передачей в пользовательский код
|
|
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;AAanE;;;;GAIG;AACH,MAAM,UAAU,gBAAgB;IAC9B,IAAI,OAA2B,CAAC;IAEhC,OAAO;QACL,IAAI,EAAE,uBAAuB;QAE7B;;;WAGG;QACH,OAAO,EAAE,CAAC,MAAyB,EAAW,EAAE,CAC9C,CAAC,CAAC,MAAM,CAAC,YAAY,EAAE,OAAO;QAEhC;;;;WAIG;QACH,KAAK,CAAC,IAAmB;YACvB,OAAO,GAAG,IAAI,kBAAkB,EAAE,CAAC;YACnC,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC;QAC9B,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,6FAA6F;gBAC7F,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;YAChC,CAAC;QACH,CAAC;QAED;;;;;WAKG;QACH,KAAK,CAAC,UAAU,CACd,GAAsB,EACtB,GAAoB;YAEpB,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,gEAAgE;gBAChE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;YAChC,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/types/interceptors.d.ts
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
1
|
+
import type { InternalRequest, HttpResponse } from "@hyperttp/types";
|
|
2
|
+
/**
|
|
3
|
+
* @ru Опции конфигурации для плагина интерцепторов.
|
|
4
|
+
* @en Configuration options for the interceptors plugin.
|
|
5
|
+
*/
|
|
1
6
|
export interface InterceptorOptions {
|
|
2
7
|
/**
|
|
3
|
-
* @ru Включить
|
|
4
|
-
* @en Enable
|
|
8
|
+
* @ru Включить обработку интерцепторов для клиента.
|
|
9
|
+
* @en Enable interceptor processing for the client.
|
|
5
10
|
*/
|
|
6
11
|
enabled?: boolean;
|
|
7
12
|
}
|
|
8
|
-
export type RequestInterceptor = (config: {
|
|
9
|
-
url: string;
|
|
10
|
-
method: string;
|
|
11
|
-
headers: Record<string, string | string[]>;
|
|
12
|
-
body?: any;
|
|
13
|
-
}) => any | Promise<any>;
|
|
14
|
-
export type ResponseInterceptor = (response: {
|
|
15
|
-
status: number;
|
|
16
|
-
headers: Record<string, string | string[]>;
|
|
17
|
-
body: any;
|
|
18
|
-
url: string;
|
|
19
|
-
}) => any | Promise<any>;
|
|
20
13
|
/**
|
|
21
|
-
* @ru
|
|
22
|
-
*
|
|
14
|
+
* @ru Перехватчик запроса.
|
|
15
|
+
* Принимает внутренний объект запроса ядра. Может вернуть модифицированный объект
|
|
16
|
+
* или `void` (если изменения вносятся напрямую в переданный объект по ссылке).
|
|
17
|
+
* * @en Request interceptor.
|
|
18
|
+
* Accepts the core internal request object. Can return a modified configuration
|
|
19
|
+
* or `void` (if modifications are applied directly to the passed object reference).
|
|
20
|
+
*/
|
|
21
|
+
export type RequestInterceptor = (config: InternalRequest) => InternalRequest | void | Promise<InternalRequest | void>;
|
|
22
|
+
/**
|
|
23
|
+
* @ru Перехватчик ответа.
|
|
24
|
+
* Принимает объект ответа с любым типом тела (`HttpResponse<any>`). Может вернуть
|
|
25
|
+
* трансформированный объект ответа или `void` (если объект мутирует по ссылке).
|
|
26
|
+
* * @en Response interceptor.
|
|
27
|
+
* Accepts a response object with any body layout (`HttpResponse<any>`). Can return
|
|
28
|
+
* a transformed response container or `void` (if the object is mutated in-place).
|
|
23
29
|
*/
|
|
24
|
-
export type
|
|
30
|
+
export type ResponseInterceptor = (response: HttpResponse<any>) => HttpResponse<any> | void | Promise<HttpResponse<any> | void>;
|
|
25
31
|
//# 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;;;;;;;GAOG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAC/B,MAAM,EAAE,eAAe,KACpB,eAAe,GAAG,IAAI,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAAC;AAE9D;;;;;;;GAOG;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
|
+
* @ru Менеджер перехватчиков (интерцепторов) для сквозной обработки запросов и ответов.
|
|
5
|
+
* Использует гибридный цикл (fast-path): выполняется синхронно до тех пор, пока не встретит Promise.
|
|
6
|
+
* @en Interceptor manager for pipeline execution of requests and responses.
|
|
7
|
+
* Uses a hybrid loop (fast-path): runs synchronously until a Promise is encountered.
|
|
8
|
+
*/
|
|
2
9
|
export declare class InterceptorManager {
|
|
10
|
+
/**
|
|
11
|
+
* @private
|
|
12
|
+
* @ru Массив зарегистрированных перехватчиков запроса.
|
|
13
|
+
* @en Array of registered request interceptors.
|
|
14
|
+
*/
|
|
3
15
|
private requestInterceptors;
|
|
16
|
+
/**
|
|
17
|
+
* @private
|
|
18
|
+
* @ru Массив зарегистрированных перехватчиков ответа.
|
|
19
|
+
* @en Array of registered response interceptors.
|
|
20
|
+
*/
|
|
4
21
|
private responseInterceptors;
|
|
22
|
+
/**
|
|
23
|
+
* @ru Флаг наличия активных перехватчиков запроса для быстрого ветвления.
|
|
24
|
+
* @en Flag indicating presence of active request interceptors for fast branch short-circuiting.
|
|
25
|
+
*/
|
|
5
26
|
hasRequest: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* @ru Флаг наличия активных перехватчиков ответа для быстрого ветвления.
|
|
29
|
+
* @en Flag indicating presence of active response interceptors for fast branch short-circuiting.
|
|
30
|
+
*/
|
|
6
31
|
hasResponse: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* @ru Регистрирует новый перехватчик запроса в конвейере.
|
|
34
|
+
* @en Registers a new request interceptor in the pipeline.
|
|
35
|
+
* @param interceptor - Target request interceptor function.
|
|
36
|
+
*/
|
|
7
37
|
addRequest(interceptor: RequestInterceptor): void;
|
|
38
|
+
/**
|
|
39
|
+
* @ru Регистрирует новый перехватчик ответа в конвейере.
|
|
40
|
+
* @en Registers a new response interceptor in the pipeline.
|
|
41
|
+
* @param interceptor - Target response interceptor function.
|
|
42
|
+
*/
|
|
8
43
|
addResponse(interceptor: ResponseInterceptor): void;
|
|
9
44
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
45
|
+
* @ru Последовательно применяет цепочку перехватчиков к конфигурации запроса.
|
|
46
|
+
* @en Sequentially applies the chain of interceptors to the request configuration.
|
|
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
|
+
* @private
|
|
53
|
+
* @ru Асинхронный фолбек для обработки оставшейся цепочки запросов после обнаружения Promise.
|
|
54
|
+
* @en Asynchronous fallback to process the remaining request chain after encountering a Promise.
|
|
55
|
+
* @param startIndex - Index of the interceptor that returned a Promise.
|
|
56
|
+
* @param currentPromise - The pending Promise from the current interceptor step.
|
|
57
|
+
* @param currentResult - Cumulative configuration result before the Promise step.
|
|
12
58
|
*/
|
|
13
|
-
applyRequest(config: any): any;
|
|
14
59
|
private applyRequestAsync;
|
|
15
60
|
/**
|
|
16
|
-
*
|
|
61
|
+
* @ru Последовательно применяет цепочку перехватчиков к полученному объекту ответа.
|
|
62
|
+
* @en Sequentially applies the chain of interceptors to the incoming response container.
|
|
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
|
+
* @private
|
|
70
|
+
* @ru Асинхронный фолбек для обработки оставшейся цепочки ответов после обнаружения Promise.
|
|
71
|
+
* @en Asynchronous fallback to process the remaining response chain after encountering a Promise.
|
|
72
|
+
* @template T - Type mapping descriptor for the expected response payload body.
|
|
73
|
+
* @param startIndex - Index of the interceptor that returned a Promise.
|
|
74
|
+
* @param currentPromise - The pending Promise from the current interceptor step.
|
|
75
|
+
* @param currentResult - Cumulative response result before the Promise step.
|
|
17
76
|
*/
|
|
18
|
-
applyResponse(response: any): any;
|
|
19
77
|
private applyResponseAsync;
|
|
78
|
+
/**
|
|
79
|
+
* @ru Полностью очищает списки зарегистрированных перехватчиков и сбрасывает флаги.
|
|
80
|
+
* @en Completely flushes internal interceptor registers and resets state indicators.
|
|
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,
|
|
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;IAgB7C;;;;;;;OAOG;YACW,iBAAiB;IAuB/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,19 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @ru Менеджер перехватчиков (интерцепторов) для сквозной обработки запросов и ответов.
|
|
3
|
+
* Использует гибридный цикл (fast-path): выполняется синхронно до тех пор, пока не встретит Promise.
|
|
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
|
+
*/
|
|
1
7
|
export class InterceptorManager {
|
|
8
|
+
/**
|
|
9
|
+
* @private
|
|
10
|
+
* @ru Массив зарегистрированных перехватчиков запроса.
|
|
11
|
+
* @en Array of registered request interceptors.
|
|
12
|
+
*/
|
|
2
13
|
requestInterceptors = [];
|
|
14
|
+
/**
|
|
15
|
+
* @private
|
|
16
|
+
* @ru Массив зарегистрированных перехватчиков ответа.
|
|
17
|
+
* @en Array of registered response interceptors.
|
|
18
|
+
*/
|
|
3
19
|
responseInterceptors = [];
|
|
20
|
+
/**
|
|
21
|
+
* @ru Флаг наличия активных перехватчиков запроса для быстрого ветвления.
|
|
22
|
+
* @en Flag indicating presence of active request interceptors for fast branch short-circuiting.
|
|
23
|
+
*/
|
|
4
24
|
hasRequest = false;
|
|
25
|
+
/**
|
|
26
|
+
* @ru Флаг наличия активных перехватчиков ответа для быстрого ветвления.
|
|
27
|
+
* @en Flag indicating presence of active response interceptors for fast branch short-circuiting.
|
|
28
|
+
*/
|
|
5
29
|
hasResponse = false;
|
|
30
|
+
/**
|
|
31
|
+
* @ru Регистрирует новый перехватчик запроса в конвейере.
|
|
32
|
+
* @en Registers a new request interceptor in the pipeline.
|
|
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
|
+
* @ru Регистрирует новый перехватчик ответа в конвейере.
|
|
41
|
+
* @en Registers a new response interceptor in the pipeline.
|
|
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
|
+
* @ru Последовательно применяет цепочку перехватчиков к конфигурации запроса.
|
|
50
|
+
* @en Sequentially applies the chain of interceptors to the request configuration.
|
|
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;
|
|
@@ -28,6 +64,14 @@ export class InterceptorManager {
|
|
|
28
64
|
}
|
|
29
65
|
return result;
|
|
30
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* @private
|
|
69
|
+
* @ru Асинхронный фолбек для обработки оставшейся цепочки запросов после обнаружения Promise.
|
|
70
|
+
* @en Asynchronous fallback to process the remaining request chain after encountering a Promise.
|
|
71
|
+
* @param startIndex - Index of the interceptor that returned a Promise.
|
|
72
|
+
* @param currentPromise - The pending Promise from the current interceptor step.
|
|
73
|
+
* @param currentResult - Cumulative configuration result before the Promise step.
|
|
74
|
+
*/
|
|
31
75
|
async applyRequestAsync(startIndex, currentPromise, currentResult) {
|
|
32
76
|
let result = currentResult;
|
|
33
77
|
const resolved = await currentPromise;
|
|
@@ -48,7 +92,11 @@ export class InterceptorManager {
|
|
|
48
92
|
return result;
|
|
49
93
|
}
|
|
50
94
|
/**
|
|
51
|
-
*
|
|
95
|
+
* @ru Последовательно применяет цепочку перехватчиков к полученному объекту ответа.
|
|
96
|
+
* @en Sequentially applies the chain of interceptors to the incoming response container.
|
|
97
|
+
* @template T - Type mapping descriptor for the expected response payload body.
|
|
98
|
+
* @param response - Target HTTP response context wrapper.
|
|
99
|
+
* @returns Transformed response context or a Promise resolving to it.
|
|
52
100
|
*/
|
|
53
101
|
applyResponse(response) {
|
|
54
102
|
let result = response;
|
|
@@ -63,6 +111,15 @@ export class InterceptorManager {
|
|
|
63
111
|
}
|
|
64
112
|
return result;
|
|
65
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* @private
|
|
116
|
+
* @ru Асинхронный фолбек для обработки оставшейся цепочки ответов после обнаружения Promise.
|
|
117
|
+
* @en Asynchronous fallback to process the remaining response chain after encountering a Promise.
|
|
118
|
+
* @template T - Type mapping descriptor for the expected response payload body.
|
|
119
|
+
* @param startIndex - Index of the interceptor that returned a Promise.
|
|
120
|
+
* @param currentPromise - The pending Promise from the current interceptor step.
|
|
121
|
+
* @param currentResult - Cumulative response result before the Promise step.
|
|
122
|
+
*/
|
|
66
123
|
async applyResponseAsync(startIndex, currentPromise, currentResult) {
|
|
67
124
|
let result = currentResult;
|
|
68
125
|
const resolved = await currentPromise;
|
|
@@ -82,6 +139,10 @@ export class InterceptorManager {
|
|
|
82
139
|
}
|
|
83
140
|
return result;
|
|
84
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* @ru Полностью очищает списки зарегистрированных перехватчиков и сбрасывает флаги.
|
|
144
|
+
* @en Completely flushes internal interceptor registers and resets state indicators.
|
|
145
|
+
*/
|
|
85
146
|
clear() {
|
|
86
147
|
this.requestInterceptors = [];
|
|
87
148
|
this.responseInterceptors = [];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InterceptorManager.js","sourceRoot":"","sources":["../../src/utils/InterceptorManager.ts"],"names":[],"mappings":"AAMA,MAAM,OAAO,kBAAkB;
|
|
1
|
+
{"version":3,"file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