@gravity-ui/gateway 2.1.0 → 2.3.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/README.md CHANGED
@@ -72,6 +72,7 @@ type ProxyHeadersFunction = (
72
72
  type: ControllerType,
73
73
  ) => IncomingHttpHeaders;
74
74
  type ProxyHeaders = string[] | ProxyHeadersFunction;
75
+ type ResponseContentType = AxiosResponse['headers']['Content-Type'];
75
76
 
76
77
  interface GatewayConfig {
77
78
  // Gateway Installation (external/internal/...). If the configuration is not provided, it is determined from process.env.APP_INSTALLATION.
@@ -114,10 +115,12 @@ interface GatewayConfig {
114
115
  // Validation schema for parameters used when no schema is present in the action. Documentation: https://ajv.js.org/json-schema.html#json-data-type
115
116
  // You can use DEFAULT_VALIDATION_SCHEMA from lib/constants.ts.
116
117
  validationSchema?: object;
117
- // Enables encoding of REST path arguments.
118
+ // Enables encoding of REST path arguments (default is true).
118
119
  encodePathArgs?: boolean;
119
120
  // Configuration for automatic connection re-establishment upon connection error through L3 load balancer (default is true).
120
121
  grpcRecreateService?: boolean;
122
+ // Enable verification of response contentType header. Actual only for REST actions. This value can be set / redefined the in action confg.
123
+ expectedResponseContentType?: ResponseContentType | ResponseContentType[];
121
124
  }
122
125
  ```
123
126
 
@@ -39,7 +39,7 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
39
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);
40
40
  /* eslint-disable complexity */
41
41
  return async function action(actionConfig) {
42
- var _a, _b, _c, _d, _e, _f;
42
+ var _a, _b, _c, _d, _e, _f, _g;
43
43
  const { args, requestId, headers: requestHeaders, ctx: parentCtx, authArgs } = actionConfig;
44
44
  const debugHeaders = {};
45
45
  const lang = requestHeaders[constants_1.DEFAULT_LANG_HEADER] || constants_1.Lang.Ru; // header might be empty string
@@ -223,6 +223,37 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
223
223
  const responseHeaders = {};
224
224
  const endRequestTime = Date.now();
225
225
  requestData.requestTime = endRequestTime - startRequestTime;
226
+ const actualResponseContentType = (_f = response.headers) === null || _f === void 0 ? void 0 : _f['Content-Type'];
227
+ const expectedResponseContentType = config.expectedResponseContentType || options.expectedResponseContentType;
228
+ if (actualResponseContentType && expectedResponseContentType) {
229
+ let isInvalidResponseContentType;
230
+ if (Array.isArray(expectedResponseContentType)) {
231
+ isInvalidResponseContentType = !expectedResponseContentType.includes(String(actualResponseContentType));
232
+ }
233
+ else {
234
+ isInvalidResponseContentType =
235
+ expectedResponseContentType !== actualResponseContentType;
236
+ }
237
+ if (isInvalidResponseContentType) {
238
+ ctx.log('Invalid response content type', {
239
+ expectedResponseContentType,
240
+ actualResponseContentType,
241
+ });
242
+ ctx.end();
243
+ return Promise.reject({
244
+ error: {
245
+ status: 415,
246
+ message: 'Response content type validation failed',
247
+ code: 'INVALID_RESPONSE_CONTENT_TYPE',
248
+ details: {
249
+ title: 'Invalid response content type',
250
+ description: `Expected to get ${expectedResponseContentType} but got ${actualResponseContentType}`,
251
+ },
252
+ },
253
+ debugHeaders,
254
+ });
255
+ }
256
+ }
226
257
  if (config.transformResponseData) {
227
258
  try {
228
259
  response.data = await config.transformResponseData(response.data, {
@@ -289,7 +320,7 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
289
320
  }
290
321
  const responseStatus = lodash_1.default.get(parsedError, 'status') || lodash_1.default.get(error, 'status', 500);
291
322
  if (options === null || options === void 0 ? void 0 : options.sendStats) {
292
- options.sendStats(Object.assign(Object.assign({}, requestData), { responseSize: getRestResponseSize((_f = error === null || error === void 0 ? void 0 : error.response) === null || _f === void 0 ? void 0 : _f.data, ctx, ErrorConstructor), restStatus: responseStatus }), (0, redact_sensitive_headers_1.redactSensitiveHeaders)(parentCtx, headers), parentCtx, { debugHeaders: (0, common_1.sanitizeDebugHeaders)(debugHeaders) });
323
+ options.sendStats(Object.assign(Object.assign({}, requestData), { responseSize: getRestResponseSize((_g = error === null || error === void 0 ? void 0 : error.response) === null || _g === void 0 ? void 0 : _g.data, ctx, ErrorConstructor), restStatus: responseStatus }), (0, redact_sensitive_headers_1.redactSensitiveHeaders)(parentCtx, headers), parentCtx, { debugHeaders: (0, common_1.sanitizeDebugHeaders)(debugHeaders) });
293
324
  }
294
325
  else {
295
326
  ctx.stats(Object.assign(Object.assign({}, requestData), { responseStatus }));
@@ -71,6 +71,7 @@ export type GetAuthHeadersParams<AuthArgs = Record<string, unknown>> = {
71
71
  authArgs: AuthArgs | undefined;
72
72
  };
73
73
  export type GetAuthHeaders<AuthArgs = Record<string, unknown>> = (params: GetAuthHeadersParams<AuthArgs>) => Record<string, string> | undefined;
74
+ export type ResponseContentType = AxiosResponse['headers']['Content-Type'];
74
75
  export interface GatewayApiOptions<Context extends GatewayContext> {
75
76
  serviceName: string;
76
77
  timeout?: number;
@@ -81,6 +82,7 @@ export interface GatewayApiOptions<Context extends GatewayContext> {
81
82
  proxyHeaders?: ProxyHeaders;
82
83
  validationSchema?: object;
83
84
  encodePathArgs?: boolean;
85
+ expectedResponseContentType?: ResponseContentType | ResponseContentType[];
84
86
  getAuthHeaders: GetAuthHeaders;
85
87
  }
86
88
  export interface ParamsOutput {
@@ -124,6 +126,7 @@ export interface ApiServiceRestActionConfig<Context extends GatewayContext, TOut
124
126
  path: (args: TParams) => string;
125
127
  paramsSerializer?: AxiosRequestConfig['paramsSerializer'];
126
128
  responseType?: AxiosRequestConfig['responseType'];
129
+ expectedResponseContentType?: ResponseContentType | ResponseContentType[];
127
130
  maxRedirects?: number;
128
131
  }
129
132
  export interface ApiServiceBaseGrpcActionConfig<Context extends GatewayContext, TOutput, TParams = undefined, TTransformed = TOutput> extends ApiServiceBaseActionConfig<Context, TOutput, TParams, TTransformed> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/gateway",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "main": "build/index.js",