@gravity-ui/gateway 3.0.4 → 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.
@@ -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;
@@ -199,7 +199,7 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
199
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) });
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,
@@ -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;
@@ -1,2 +1,3 @@
1
1
  import { AxiosRequestConfig } from 'axios';
2
- export declare function getAxiosClient(timeout?: number, retries?: number, axiosConfig?: AxiosRequestConfig): import("axios").AxiosInstance;
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;
@@ -8,8 +8,10 @@ const axios_1 = __importDefault(require("axios"));
8
8
  const axios_retry_1 = __importDefault(require("axios-retry"));
9
9
  const lodash_1 = __importDefault(require("lodash"));
10
10
  const constants_1 = require("../constants");
11
- 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 } = {}) {
12
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));
13
15
  (0, axios_retry_1.default)(client, {
14
16
  retries,
15
17
  retryDelay: axios_retry_1.default.exponentialDelay,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/gateway",
3
- "version": "3.0.4",
3
+ "version": "3.1.0",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "main": "build/index.js",