@gravity-ui/gateway 3.0.4-alpha.0 → 3.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/build/components/rest.js +5 -16
- package/build/index.js +1 -0
- package/build/models/common.d.ts +12 -1
- package/build/utils/axios.d.ts +2 -1
- package/build/utils/axios.js +7 -1
- package/package.json +1 -1
package/build/components/rest.js
CHANGED
|
@@ -36,7 +36,7 @@ function getConfigSerializerFunction(config) {
|
|
|
36
36
|
function createRestAction(endpoints, config, serviceKey, actionName, options, ErrorConstructor) {
|
|
37
37
|
var _a, _b, _c;
|
|
38
38
|
const timeout = (_c = (_a = config === null || config === void 0 ? void 0 : config.timeout) !== null && _a !== void 0 ? _a : (_b = options === null || options === void 0 ? void 0 : options.axiosConfig) === null || _b === void 0 ? void 0 : _b.timeout) !== null && _c !== void 0 ? _c : options === null || options === void 0 ? void 0 : options.timeout;
|
|
39
|
-
const defaultAxiosClient = (0, axios_1.getAxiosClient)(timeout, config === null || config === void 0 ? void 0 : config.retries, options === null || options === void 0 ? void 0 : options.axiosConfig);
|
|
39
|
+
const defaultAxiosClient = (0, axios_1.getAxiosClient)(timeout, config === null || config === void 0 ? void 0 : config.retries, options === null || options === void 0 ? void 0 : options.axiosConfig, options === null || options === void 0 ? void 0 : options.axiosInterceptors);
|
|
40
40
|
/* eslint-disable complexity */
|
|
41
41
|
return async function action(actionConfig) {
|
|
42
42
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
@@ -96,8 +96,8 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
|
|
|
96
96
|
? endpointData.path
|
|
97
97
|
: endpointData;
|
|
98
98
|
const endpointAxiosConfig = (0, common_1.isExtendedRestActionEndpoint)(endpointData)
|
|
99
|
-
? endpointData.axiosConfig
|
|
100
|
-
:
|
|
99
|
+
? endpointData.axiosConfig
|
|
100
|
+
: undefined;
|
|
101
101
|
const pathArgs = config.validationSchema
|
|
102
102
|
? (0, validate_1.encodePathParams)(args)
|
|
103
103
|
: (0, validate_1.getPathArgsProxy)(args, options.encodePathArgs);
|
|
@@ -196,10 +196,10 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
|
|
|
196
196
|
}
|
|
197
197
|
const startRequestTime = Date.now();
|
|
198
198
|
let axiosClient = defaultAxiosClient;
|
|
199
|
-
const customActionTimeout = (_e = (_d = (_c = actionConfig.timeout) !== null && _c !== void 0 ? _c : config.timeout) !== null && _d !== void 0 ? _d : endpointAxiosConfig.timeout) !== null && _e !== void 0 ? _e : timeout;
|
|
199
|
+
const customActionTimeout = (_e = (_d = (_c = actionConfig.timeout) !== null && _c !== void 0 ? _c : config.timeout) !== null && _d !== void 0 ? _d : endpointAxiosConfig === null || endpointAxiosConfig === void 0 ? void 0 : endpointAxiosConfig.timeout) !== null && _e !== void 0 ? _e : timeout;
|
|
200
200
|
if (actionConfig.timeout || endpointAxiosConfig) {
|
|
201
201
|
const customActionAxiosConfig = Object.assign(Object.assign({}, ((options === null || options === void 0 ? void 0 : options.axiosConfig) || {})), (endpointAxiosConfig || {}));
|
|
202
|
-
axiosClient = (0, axios_1.getAxiosClient)(customActionTimeout, config === null || config === void 0 ? void 0 : config.retries, customActionAxiosConfig);
|
|
202
|
+
axiosClient = (0, axios_1.getAxiosClient)(customActionTimeout, config === null || config === void 0 ? void 0 : config.retries, customActionAxiosConfig, options === null || options === void 0 ? void 0 : options.axiosInterceptors);
|
|
203
203
|
}
|
|
204
204
|
headers['x-request-timeout'] = customActionTimeout !== null && customActionTimeout !== void 0 ? customActionTimeout : constants_1.DEFAULT_TIMEOUT;
|
|
205
205
|
ctx.log('Starting request', { debugHeaders: (0, common_1.sanitizeDebugHeaders)(debugHeaders) });
|
|
@@ -231,17 +231,6 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
|
|
|
231
231
|
responseType: config.responseType,
|
|
232
232
|
});
|
|
233
233
|
}
|
|
234
|
-
axiosClient.interceptors.request.use((requestConfig) => {
|
|
235
|
-
var _a;
|
|
236
|
-
requestConfig.headers.set('x-request-attempt',
|
|
237
|
-
// according this issue https://github.com/softonic/axios-retry/issues/167
|
|
238
|
-
// 'axios-retry' doesn`t define retryCount field in exported type
|
|
239
|
-
// however in this issue https://github.com/softonic/axios-retry/issues/75#issuecomment-502151719
|
|
240
|
-
// people use it
|
|
241
|
-
// @ts-ignore
|
|
242
|
-
((_a = requestConfig[constants_1.AXIOS_RETRY_NAMESPACE]) === null || _a === void 0 ? void 0 : _a.retryCount) || 0);
|
|
243
|
-
return requestConfig;
|
|
244
|
-
});
|
|
245
234
|
try {
|
|
246
235
|
const response = await axiosClient.request(requestConfig);
|
|
247
236
|
const responseHeaders = {};
|
package/build/index.js
CHANGED
|
@@ -77,6 +77,7 @@ function createApiAction(schema, config, serviceKey, actionName, api, grpcContex
|
|
|
77
77
|
proxyHeaders: config.proxyHeaders,
|
|
78
78
|
proxyDebugHeaders: config.proxyDebugHeaders,
|
|
79
79
|
axiosConfig: config.axiosConfig,
|
|
80
|
+
axiosInterceptors: config.axiosInterceptors,
|
|
80
81
|
validationSchema: config.validationSchema,
|
|
81
82
|
encodePathArgs: config.encodePathArgs,
|
|
82
83
|
getAuthHeaders: config.getAuthHeaders,
|
package/build/models/common.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { IncomingHttpHeaders } from 'http';
|
|
3
3
|
import { ClientDuplexStream, ClientReadableStream, ClientWritableStream } from '@grpc/grpc-js';
|
|
4
4
|
import { HandlerType } from '@grpc/grpc-js/build/src/server-call';
|
|
5
|
-
import { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
5
|
+
import { AxiosInterceptorManager, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
|
6
6
|
import type { Request, Response } from 'express';
|
|
7
7
|
import type { GrpcContext } from '../components/grpc';
|
|
8
8
|
import { Lang } from '../constants';
|
|
@@ -82,6 +82,7 @@ export interface GatewayApiOptions<Context extends GatewayContext> {
|
|
|
82
82
|
grpcOptions?: object;
|
|
83
83
|
grpcRecreateService?: boolean;
|
|
84
84
|
axiosConfig?: AxiosRequestConfig;
|
|
85
|
+
axiosInterceptors?: AxiosInterceptorsConfig;
|
|
85
86
|
proxyHeaders?: ProxyHeaders;
|
|
86
87
|
proxyDebugHeaders?: ProxyHeaders;
|
|
87
88
|
validationSchema?: object;
|
|
@@ -227,6 +228,15 @@ interface OnUnknownActionData {
|
|
|
227
228
|
service?: string;
|
|
228
229
|
action?: string;
|
|
229
230
|
}
|
|
231
|
+
type AxiosInterceptorUseParams<T> = Parameters<AxiosInterceptorManager<T>['use']>;
|
|
232
|
+
interface AxiosInterceptorConfig<T> {
|
|
233
|
+
callback: AxiosInterceptorUseParams<T>[0];
|
|
234
|
+
errorCallback?: AxiosInterceptorUseParams<T>[1];
|
|
235
|
+
}
|
|
236
|
+
export interface AxiosInterceptorsConfig {
|
|
237
|
+
request?: AxiosInterceptorConfig<InternalAxiosRequestConfig<any>>[];
|
|
238
|
+
response?: AxiosInterceptorConfig<AxiosResponse<any>>[];
|
|
239
|
+
}
|
|
230
240
|
export interface GatewayConfig<Context extends GatewayContext, Req extends GatewayRequest<Context>, Res extends GatewayResponse> {
|
|
231
241
|
installation?: string;
|
|
232
242
|
env?: string;
|
|
@@ -235,6 +245,7 @@ export interface GatewayConfig<Context extends GatewayContext, Req extends Gatew
|
|
|
235
245
|
grpcOptions?: object;
|
|
236
246
|
grpcRecreateService?: boolean;
|
|
237
247
|
axiosConfig?: AxiosRequestConfig;
|
|
248
|
+
axiosInterceptors?: AxiosInterceptorsConfig;
|
|
238
249
|
onUnknownAction?: (req: Req, res: Res, data: OnUnknownActionData) => any;
|
|
239
250
|
onBeforeAction?: (req: Req, res: Res, scope: string, service: string, action: string, config?: ApiServiceActionConfig<Context, Req, Res, unknown>) => any;
|
|
240
251
|
onRequestSuccess?: (req: Req, res: Res, data: any) => any;
|
package/build/utils/axios.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { AxiosRequestConfig } from 'axios';
|
|
2
|
-
|
|
2
|
+
import { AxiosInterceptorsConfig } from '../models/common';
|
|
3
|
+
export declare function getAxiosClient(timeout?: number, retries?: number, axiosConfig?: AxiosRequestConfig, { request: reqInterceptors, response: resInterceptors }?: AxiosInterceptorsConfig): import("axios").AxiosInstance;
|
package/build/utils/axios.js
CHANGED
|
@@ -6,9 +6,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.getAxiosClient = void 0;
|
|
7
7
|
const axios_1 = __importDefault(require("axios"));
|
|
8
8
|
const axios_retry_1 = __importDefault(require("axios-retry"));
|
|
9
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
9
10
|
const constants_1 = require("../constants");
|
|
10
|
-
function getAxiosClient(timeout = constants_1.DEFAULT_TIMEOUT, retries = 0, axiosConfig = constants_1.DEFAULT_AXIOS_OPTIONS) {
|
|
11
|
+
function getAxiosClient(timeout = constants_1.DEFAULT_TIMEOUT, retries = 0, axiosConfig = constants_1.DEFAULT_AXIOS_OPTIONS, { request: reqInterceptors, response: resInterceptors } = {}) {
|
|
11
12
|
const client = axios_1.default.create(Object.assign(Object.assign({}, axiosConfig), { timeout }));
|
|
13
|
+
reqInterceptors === null || reqInterceptors === void 0 ? void 0 : reqInterceptors.forEach(({ callback, errorCallback }) => client.interceptors.request.use(callback, errorCallback));
|
|
14
|
+
resInterceptors === null || resInterceptors === void 0 ? void 0 : resInterceptors.forEach(({ callback, errorCallback }) => client.interceptors.response.use(callback, errorCallback));
|
|
12
15
|
(0, axios_retry_1.default)(client, {
|
|
13
16
|
retries,
|
|
14
17
|
retryDelay: axios_retry_1.default.exponentialDelay,
|
|
@@ -18,6 +21,9 @@ function getAxiosClient(timeout = constants_1.DEFAULT_TIMEOUT, retries = 0, axio
|
|
|
18
21
|
}
|
|
19
22
|
return axios_retry_1.default.isNetworkError(error) || axios_retry_1.default.isRetryableError(error);
|
|
20
23
|
},
|
|
24
|
+
onRetry: (retryCount, _error, requestConfig) => {
|
|
25
|
+
lodash_1.default.set(requestConfig, ['headers', 'x-request-attempt'], retryCount);
|
|
26
|
+
},
|
|
21
27
|
});
|
|
22
28
|
return client;
|
|
23
29
|
}
|