@gravity-ui/gateway 2.1.0 → 2.2.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 +3 -1
- package/build/components/rest.js +25 -2
- package/build/models/common.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -114,10 +114,12 @@ interface GatewayConfig {
|
|
|
114
114
|
// 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
115
|
// You can use DEFAULT_VALIDATION_SCHEMA from lib/constants.ts.
|
|
116
116
|
validationSchema?: object;
|
|
117
|
-
// Enables encoding of REST path arguments.
|
|
117
|
+
// Enables encoding of REST path arguments (default is true).
|
|
118
118
|
encodePathArgs?: boolean;
|
|
119
119
|
// Configuration for automatic connection re-establishment upon connection error through L3 load balancer (default is true).
|
|
120
120
|
grpcRecreateService?: boolean;
|
|
121
|
+
// 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'];
|
|
121
123
|
}
|
|
122
124
|
```
|
|
123
125
|
|
package/build/components/rest.js
CHANGED
|
@@ -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,29 @@ 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 &&
|
|
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}`,
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
debugHeaders,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
226
249
|
if (config.transformResponseData) {
|
|
227
250
|
try {
|
|
228
251
|
response.data = await config.transformResponseData(response.data, {
|
|
@@ -289,7 +312,7 @@ function createRestAction(endpoints, config, serviceKey, actionName, options, Er
|
|
|
289
312
|
}
|
|
290
313
|
const responseStatus = lodash_1.default.get(parsedError, 'status') || lodash_1.default.get(error, 'status', 500);
|
|
291
314
|
if (options === null || options === void 0 ? void 0 : options.sendStats) {
|
|
292
|
-
options.sendStats(Object.assign(Object.assign({}, requestData), { responseSize: getRestResponseSize((
|
|
315
|
+
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
316
|
}
|
|
294
317
|
else {
|
|
295
318
|
ctx.stats(Object.assign(Object.assign({}, requestData), { responseStatus }));
|
package/build/models/common.d.ts
CHANGED
|
@@ -81,6 +81,7 @@ export interface GatewayApiOptions<Context extends GatewayContext> {
|
|
|
81
81
|
proxyHeaders?: ProxyHeaders;
|
|
82
82
|
validationSchema?: object;
|
|
83
83
|
encodePathArgs?: boolean;
|
|
84
|
+
expectedResponseContentType?: AxiosResponse['headers']['Content-Type'];
|
|
84
85
|
getAuthHeaders: GetAuthHeaders;
|
|
85
86
|
}
|
|
86
87
|
export interface ParamsOutput {
|
|
@@ -124,6 +125,7 @@ export interface ApiServiceRestActionConfig<Context extends GatewayContext, TOut
|
|
|
124
125
|
path: (args: TParams) => string;
|
|
125
126
|
paramsSerializer?: AxiosRequestConfig['paramsSerializer'];
|
|
126
127
|
responseType?: AxiosRequestConfig['responseType'];
|
|
128
|
+
expectedResponseContentType?: AxiosResponse['headers']['Content-Type'];
|
|
127
129
|
maxRedirects?: number;
|
|
128
130
|
}
|
|
129
131
|
export interface ApiServiceBaseGrpcActionConfig<Context extends GatewayContext, TOutput, TParams = undefined, TTransformed = TOutput> extends ApiServiceBaseActionConfig<Context, TOutput, TParams, TTransformed> {
|