@gravity-ui/gateway 2.2.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.
@@ -119,7 +120,7 @@ interface GatewayConfig {
119
120
  // Configuration for automatic connection re-establishment upon connection error through L3 load balancer (default is true).
120
121
  grpcRecreateService?: boolean;
121
122
  // Enable verification of response contentType header. Actual only for REST actions. This value can be set / redefined the in action confg.
122
- expectedResponseContentType?: AxiosResponse['headers']['Content-Type'];
123
+ expectedResponseContentType?: ResponseContentType | ResponseContentType[];
123
124
  }
124
125
  ```
125
126
 
@@ -225,26 +225,34 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
225
225
  requestData.requestTime = endRequestTime - startRequestTime;
226
226
  const actualResponseContentType = (_f = response.headers) === null || _f === void 0 ? void 0 : _f['Content-Type'];
227
227
  const expectedResponseContentType = config.expectedResponseContentType || options.expectedResponseContentType;
228
- if (actualResponseContentType &&
229
- expectedResponseContentType &&
230
- actualResponseContentType !== expectedResponseContentType) {
231
- ctx.log('Invalid response content type', {
232
- expectedResponseContentType,
233
- actualResponseContentType,
234
- });
235
- ctx.end();
236
- return Promise.reject({
237
- error: {
238
- status: 415,
239
- message: 'Response content type validation failed',
240
- code: 'INVALID_RESPONSE_CONTENT_TYPE',
241
- details: {
242
- title: 'Invalid response content type',
243
- description: `Expected to get ${expectedResponseContentType} but got ${actualResponseContentType}`,
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
+ },
244
252
  },
245
- },
246
- debugHeaders,
247
- });
253
+ debugHeaders,
254
+ });
255
+ }
248
256
  }
249
257
  if (config.transformResponseData) {
250
258
  try {
@@ -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,7 +82,7 @@ export interface GatewayApiOptions<Context extends GatewayContext> {
81
82
  proxyHeaders?: ProxyHeaders;
82
83
  validationSchema?: object;
83
84
  encodePathArgs?: boolean;
84
- expectedResponseContentType?: AxiosResponse['headers']['Content-Type'];
85
+ expectedResponseContentType?: ResponseContentType | ResponseContentType[];
85
86
  getAuthHeaders: GetAuthHeaders;
86
87
  }
87
88
  export interface ParamsOutput {
@@ -125,7 +126,7 @@ export interface ApiServiceRestActionConfig<Context extends GatewayContext, TOut
125
126
  path: (args: TParams) => string;
126
127
  paramsSerializer?: AxiosRequestConfig['paramsSerializer'];
127
128
  responseType?: AxiosRequestConfig['responseType'];
128
- expectedResponseContentType?: AxiosResponse['headers']['Content-Type'];
129
+ expectedResponseContentType?: ResponseContentType | ResponseContentType[];
129
130
  maxRedirects?: number;
130
131
  }
131
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.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "main": "build/index.js",