@gravity-ui/gateway 4.1.1-alpha.0 → 4.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/{dist/commonjs → build}/components/grpc.d.ts +4 -4
- package/{dist/commonjs → build}/components/grpc.js +78 -85
- package/{dist/esm → build}/components/mixed.d.ts +4 -4
- package/{dist/commonjs → build}/components/mixed.js +12 -11
- package/{dist/commonjs → build}/components/rest.d.ts +5 -5
- package/{dist/commonjs → build}/components/rest.js +32 -32
- package/{dist/esm → build}/constants.d.ts +2 -2
- package/{dist/commonjs → build}/constants.js +18 -25
- package/{dist/commonjs → build}/index.d.ts +7 -8
- package/{dist/commonjs → build}/index.js +46 -32
- package/{dist/esm → build}/models/common.d.ts +14 -12
- package/{dist/esm → build}/models/context.d.ts +1 -0
- package/build/utils/axios.d.ts +4 -0
- package/{dist/commonjs → build}/utils/axios.js +6 -4
- package/{dist/esm → build}/utils/common.d.ts +3 -3
- package/{dist/commonjs → build}/utils/common.js +8 -7
- package/{dist/commonjs → build}/utils/create-context-api.d.ts +2 -2
- package/{dist/commonjs → build}/utils/create-context-api.js +6 -5
- package/{dist/commonjs → build}/utils/grpc-reflection.js +12 -21
- package/{dist/commonjs → build}/utils/grpc.js +8 -7
- package/build/utils/overrideEndpoints/index.d.ts +2 -0
- package/build/utils/overrideEndpoints/index.js +4 -0
- package/{dist/commonjs → build}/utils/overrideEndpoints/overrideEndpoints.d.ts +1 -1
- package/{dist/commonjs → build}/utils/overrideEndpoints/overrideEndpoints.js +2 -1
- package/{dist/esm → build}/utils/parse-error.d.ts +4 -4
- package/{dist/commonjs → build}/utils/parse-error.js +18 -18
- package/{dist/commonjs → build}/utils/proto-path-resolver.js +17 -23
- package/{dist/commonjs → build}/utils/redact-sensitive-headers.d.ts +2 -1
- package/{dist/commonjs → build}/utils/redact-sensitive-headers.js +2 -1
- package/{dist/commonjs → build}/utils/typed-api.d.ts +1 -1
- package/{dist/commonjs → build}/utils/typed-api.js +2 -1
- package/{dist/commonjs → build}/utils/validate.js +10 -6
- package/package.json +16 -41
- package/dist/commonjs/components/mixed.d.ts +0 -11
- package/dist/commonjs/constants.d.ts +0 -53
- package/dist/commonjs/models/common.d.ts +0 -272
- package/dist/commonjs/models/context.d.ts +0 -22
- package/dist/commonjs/package.json +0 -3
- package/dist/commonjs/utils/axios.d.ts +0 -3
- package/dist/commonjs/utils/common.d.ts +0 -15
- package/dist/commonjs/utils/overrideEndpoints/index.d.ts +0 -2
- package/dist/commonjs/utils/overrideEndpoints/index.js +0 -4
- package/dist/commonjs/utils/parse-error.d.ts +0 -30
- package/dist/commonjs/utils/source-dir.d.ts +0 -1
- package/dist/commonjs/utils/source-dir.js +0 -41
- package/dist/esm/components/grpc.d.ts +0 -24
- package/dist/esm/components/grpc.js +0 -641
- package/dist/esm/components/mixed.js +0 -62
- package/dist/esm/components/rest.d.ts +0 -8
- package/dist/esm/components/rest.js +0 -349
- package/dist/esm/constants.js +0 -79
- package/dist/esm/index.d.ts +0 -12
- package/dist/esm/index.js +0 -264
- package/dist/esm/models/common.js +0 -5
- package/dist/esm/models/context.js +0 -1
- package/dist/esm/models/error.d.ts +0 -12
- package/dist/esm/models/error.js +0 -1
- package/dist/esm/package.json +0 -3
- package/dist/esm/utils/axios.d.ts +0 -3
- package/dist/esm/utils/axios.js +0 -23
- package/dist/esm/utils/common.js +0 -38
- package/dist/esm/utils/create-context-api.d.ts +0 -4
- package/dist/esm/utils/create-context-api.js +0 -38
- package/dist/esm/utils/grpc-reflection.d.ts +0 -28
- package/dist/esm/utils/grpc-reflection.js +0 -68
- package/dist/esm/utils/grpc.d.ts +0 -5
- package/dist/esm/utils/grpc.js +0 -39
- package/dist/esm/utils/overrideEndpoints/index.d.ts +0 -2
- package/dist/esm/utils/overrideEndpoints/index.js +0 -2
- package/dist/esm/utils/overrideEndpoints/overrideEndpoints.d.ts +0 -17
- package/dist/esm/utils/overrideEndpoints/overrideEndpoints.js +0 -96
- package/dist/esm/utils/parse-error.js +0 -210
- package/dist/esm/utils/proto-path-resolver.d.ts +0 -2
- package/dist/esm/utils/proto-path-resolver.js +0 -23
- package/dist/esm/utils/redact-sensitive-headers.d.ts +0 -3
- package/dist/esm/utils/redact-sensitive-headers.js +0 -12
- package/dist/esm/utils/source-dir.d.ts +0 -1
- package/dist/esm/utils/source-dir.js +0 -4
- package/dist/esm/utils/typed-api.d.ts +0 -2
- package/dist/esm/utils/typed-api.js +0 -3
- package/dist/esm/utils/validate.d.ts +0 -4
- package/dist/esm/utils/validate.js +0 -47
- /package/bin/{patch.cjs → patch.js} +0 -0
- /package/{dist/commonjs → build}/models/common.js +0 -0
- /package/{dist/commonjs → build}/models/context.js +0 -0
- /package/{dist/commonjs → build}/models/error.d.ts +0 -0
- /package/{dist/commonjs → build}/models/error.js +0 -0
- /package/{dist/commonjs → build}/utils/grpc-reflection.d.ts +0 -0
- /package/{dist/commonjs → build}/utils/grpc.d.ts +0 -0
- /package/{dist/commonjs → build}/utils/proto-path-resolver.d.ts +0 -0
- /package/{dist/commonjs → build}/utils/validate.d.ts +0 -0
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
function parseOverrideEndpointsParams(params) {
|
|
3
|
-
const parsedParams = [];
|
|
4
|
-
for (const [serviceKeyScope, serviceKeyValues] of Object.entries(params)) {
|
|
5
|
-
for (const [endpointName, config] of Object.entries(serviceKeyValues)) {
|
|
6
|
-
let scope = 'root';
|
|
7
|
-
let serviceKey = serviceKeyScope;
|
|
8
|
-
const pointInd = serviceKey.indexOf('.');
|
|
9
|
-
if (pointInd >= 0) {
|
|
10
|
-
scope = serviceKey.slice(0, pointInd);
|
|
11
|
-
serviceKey = serviceKey.slice(pointInd + 1);
|
|
12
|
-
}
|
|
13
|
-
parsedParams.push({
|
|
14
|
-
scope,
|
|
15
|
-
serviceKey,
|
|
16
|
-
endpointName,
|
|
17
|
-
config,
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return parsedParams;
|
|
22
|
-
}
|
|
23
|
-
function getKeys(obj) {
|
|
24
|
-
return Object.keys(obj);
|
|
25
|
-
}
|
|
26
|
-
function deepCopy(obj) {
|
|
27
|
-
const res = Object.assign({}, obj);
|
|
28
|
-
getKeys(res).forEach((key) => {
|
|
29
|
-
if (typeof res[key] === 'object' && !Array.isArray(res[key]) && res[key] !== null) {
|
|
30
|
-
res[key] = deepCopy(res[key]);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
return res;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Overrides endpoints in the schema according to overrideParams
|
|
37
|
-
* @param originalSchema Schema for the gateway
|
|
38
|
-
* @param overrideParams Definitions of endpoints for services in the format {[<scope>.<service key>]: {[<endpointName>]: ActionEndpoint}}
|
|
39
|
-
* If <scope> is not specified, root is assumed.
|
|
40
|
-
* Example: '{serviceName: {endpoint: "https://example.com"}}'
|
|
41
|
-
* @param installation
|
|
42
|
-
* @param env
|
|
43
|
-
* @return new overridden schema
|
|
44
|
-
*/
|
|
45
|
-
export function overrideEndpoints(originalSchema, overrideParams, installation, env) {
|
|
46
|
-
if (!installation) {
|
|
47
|
-
console.warn('overrideEndpoints: installation empty');
|
|
48
|
-
return originalSchema;
|
|
49
|
-
}
|
|
50
|
-
if (!env) {
|
|
51
|
-
console.warn('overrideEndpoints: env empty');
|
|
52
|
-
return originalSchema;
|
|
53
|
-
}
|
|
54
|
-
const schema = deepCopy(originalSchema);
|
|
55
|
-
const parsedParams = parseOverrideEndpointsParams(overrideParams);
|
|
56
|
-
parsedParams.forEach((overrideConfig) => {
|
|
57
|
-
var _a;
|
|
58
|
-
const curService = (_a = schema[overrideConfig.scope]) === null || _a === void 0 ? void 0 : _a[overrideConfig.serviceKey];
|
|
59
|
-
if (!curService || !curService.actions || !curService.endpoints) {
|
|
60
|
-
console.warn('overrideEndpoints: incorrect service ' +
|
|
61
|
-
overrideConfig.scope +
|
|
62
|
-
'.' +
|
|
63
|
-
overrideConfig.serviceKey);
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
const endpoints = curService.endpoints;
|
|
67
|
-
const envEndpoints = _.get(endpoints, [installation, env]);
|
|
68
|
-
if (!envEndpoints) {
|
|
69
|
-
console.warn('overrideEndpoints: ' + [installation, env] + ' not exists in envEndpoints');
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
if (overrideConfig.endpointName) {
|
|
73
|
-
if (!envEndpoints[overrideConfig.endpointName]) {
|
|
74
|
-
console.warn('overrideEndpoints: incorrect endpointName ' + overrideConfig.endpointName);
|
|
75
|
-
}
|
|
76
|
-
overrideEndpoint(envEndpoints, overrideConfig.endpointName, overrideConfig);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
return schema;
|
|
80
|
-
}
|
|
81
|
-
function isExtendedEndpointConfig(config) {
|
|
82
|
-
return typeof config === 'object';
|
|
83
|
-
}
|
|
84
|
-
function overrideEndpoint(envEndpoints, endpointName, overrideConfig) {
|
|
85
|
-
const config = envEndpoints[endpointName];
|
|
86
|
-
if (isExtendedEndpointConfig(config)) {
|
|
87
|
-
if (typeof overrideConfig.config === 'string') {
|
|
88
|
-
config.path = overrideConfig.config;
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
Object.assign(config, overrideConfig.config);
|
|
92
|
-
}
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
envEndpoints[endpointName] = overrideConfig.config;
|
|
96
|
-
}
|
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
import { Lang } from '../constants.js';
|
|
3
|
-
import { decodeAnyMessageRecursively } from './grpc.js';
|
|
4
|
-
const DEFAULT_GATEWAY_CODE = 'GATEWAY_REQUEST_ERROR';
|
|
5
|
-
const DEFAULT_GATEWAY_MESSAGE = 'Gateway request error';
|
|
6
|
-
export function parseMixedError(e) {
|
|
7
|
-
return {
|
|
8
|
-
status: 500,
|
|
9
|
-
message: String(e.message || DEFAULT_GATEWAY_MESSAGE),
|
|
10
|
-
code: String(e.code || DEFAULT_GATEWAY_CODE),
|
|
11
|
-
debug: {
|
|
12
|
-
originalError: e,
|
|
13
|
-
stack: e.stack,
|
|
14
|
-
},
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
function getErrorTitle(error) {
|
|
18
|
-
const prop = _.propertyOf(error);
|
|
19
|
-
const details = prop('details[0]');
|
|
20
|
-
if (details) {
|
|
21
|
-
return prop('details[0].type') || prop('details[0].code') || prop('code');
|
|
22
|
-
}
|
|
23
|
-
return prop('type') || prop('code');
|
|
24
|
-
}
|
|
25
|
-
function getErrorMessage(error) {
|
|
26
|
-
const prop = _.propertyOf(error);
|
|
27
|
-
const message = prop('message');
|
|
28
|
-
const detail = prop('detail');
|
|
29
|
-
const detailsMessage = prop('details[0].message');
|
|
30
|
-
if (message) {
|
|
31
|
-
return message;
|
|
32
|
-
}
|
|
33
|
-
if (detailsMessage) {
|
|
34
|
-
return detailsMessage;
|
|
35
|
-
}
|
|
36
|
-
if (detail) {
|
|
37
|
-
return prop('detail.Description') || prop('detail.Body') || detail;
|
|
38
|
-
}
|
|
39
|
-
return '';
|
|
40
|
-
}
|
|
41
|
-
function getStatusByGrpcCode(code) {
|
|
42
|
-
switch (code) {
|
|
43
|
-
case 0:
|
|
44
|
-
return 200;
|
|
45
|
-
case 1:
|
|
46
|
-
return 499;
|
|
47
|
-
case 2:
|
|
48
|
-
return 500;
|
|
49
|
-
case 3:
|
|
50
|
-
return 400;
|
|
51
|
-
case 4:
|
|
52
|
-
return 504;
|
|
53
|
-
case 5:
|
|
54
|
-
return 404;
|
|
55
|
-
case 6:
|
|
56
|
-
return 409;
|
|
57
|
-
case 7:
|
|
58
|
-
return 403;
|
|
59
|
-
case 8:
|
|
60
|
-
return 429;
|
|
61
|
-
case 9:
|
|
62
|
-
return 400;
|
|
63
|
-
case 10:
|
|
64
|
-
return 409;
|
|
65
|
-
case 11:
|
|
66
|
-
return 400;
|
|
67
|
-
case 12:
|
|
68
|
-
return 501;
|
|
69
|
-
case 13:
|
|
70
|
-
return 500;
|
|
71
|
-
case 14:
|
|
72
|
-
return 503;
|
|
73
|
-
case 15:
|
|
74
|
-
return 500;
|
|
75
|
-
case 16:
|
|
76
|
-
return 401;
|
|
77
|
-
default:
|
|
78
|
-
return 500;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
export function parseRestError(error, lang) {
|
|
82
|
-
const prop = _.propertyOf(error);
|
|
83
|
-
let status = 500;
|
|
84
|
-
let title;
|
|
85
|
-
let description;
|
|
86
|
-
let details;
|
|
87
|
-
let type;
|
|
88
|
-
let code;
|
|
89
|
-
let debug;
|
|
90
|
-
if (prop('response')) {
|
|
91
|
-
const responseData = prop('response.data');
|
|
92
|
-
const statusTitle = `${prop('response.status')} ${prop('response.statusText')}`;
|
|
93
|
-
const errorTitle = getErrorTitle(responseData);
|
|
94
|
-
status = prop('response.status');
|
|
95
|
-
code = prop('response.data.code');
|
|
96
|
-
type = prop('response.data.type');
|
|
97
|
-
details = prop('response.data.details');
|
|
98
|
-
debug = prop('response.data.debug');
|
|
99
|
-
title = errorTitle ? errorTitle : statusTitle;
|
|
100
|
-
description = getErrorMessage(responseData);
|
|
101
|
-
if (status === 403) {
|
|
102
|
-
title = lang === Lang.Ru ? 'Доступ запрещен' : 'Access denied';
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
code = prop('code');
|
|
107
|
-
title = lang === Lang.Ru ? 'Ошибка' : 'Error';
|
|
108
|
-
if (code === 'ECONNABORTED' || code === 'ETIMEDOUT') {
|
|
109
|
-
status = 504;
|
|
110
|
-
description = lang === Lang.Ru ? 'Превышено время ожидания ответа' : 'Timeout exceeded';
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
status = 500;
|
|
114
|
-
description =
|
|
115
|
-
lang === Lang.Ru
|
|
116
|
-
? 'Произошла непредвиденная ошибка. Попробуйте обновить страницу через некоторое время.'
|
|
117
|
-
: 'An unexpected error has occurred. Try to refresh the page in a few moments.';
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return {
|
|
121
|
-
status,
|
|
122
|
-
message: String(description || DEFAULT_GATEWAY_MESSAGE),
|
|
123
|
-
code: String(code || DEFAULT_GATEWAY_CODE),
|
|
124
|
-
details: Object.assign({ type,
|
|
125
|
-
title,
|
|
126
|
-
description }, (typeof details === 'object' && !Array.isArray(details) ? details : { details })),
|
|
127
|
-
debug,
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
function decodeGrpcStatusMessage(metadata, packageRoot, decodeAnyMessageProtoLoaderOptions) {
|
|
131
|
-
const statusMessageBin = metadata.get('grpc-status-details-bin')[0];
|
|
132
|
-
if (!statusMessageBin) {
|
|
133
|
-
return undefined;
|
|
134
|
-
}
|
|
135
|
-
return decodeAnyMessageRecursively(packageRoot, {
|
|
136
|
-
type_url: 'type.googleapis.com/google.rpc.Status',
|
|
137
|
-
value: statusMessageBin,
|
|
138
|
-
}, decodeAnyMessageProtoLoaderOptions);
|
|
139
|
-
}
|
|
140
|
-
export function parseGrpcError(error, packageRoot, lang = Lang.Ru, decodeAnyMessageProtoLoaderOptions) {
|
|
141
|
-
let title = lang === Lang.Ru ? 'Ошибка' : 'Error';
|
|
142
|
-
if (error.code === 7) {
|
|
143
|
-
// Always redefine title for Access denied errors
|
|
144
|
-
title = lang === Lang.Ru ? 'Доступ запрещен' : 'Access denied';
|
|
145
|
-
}
|
|
146
|
-
let code = error.code;
|
|
147
|
-
let description = error.details;
|
|
148
|
-
let details;
|
|
149
|
-
if (error.metadata) {
|
|
150
|
-
try {
|
|
151
|
-
const statusMessage = decodeGrpcStatusMessage(error.metadata, packageRoot, decodeAnyMessageProtoLoaderOptions);
|
|
152
|
-
if (statusMessage) {
|
|
153
|
-
code = statusMessage.code;
|
|
154
|
-
description = statusMessage.message;
|
|
155
|
-
details = statusMessage.details;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
catch (e) { }
|
|
159
|
-
}
|
|
160
|
-
// Always redefine description for Timeout exceeded errors
|
|
161
|
-
if (error.code === 4) {
|
|
162
|
-
description = lang === Lang.Ru ? 'Превышено время ожидания ответа' : 'Timeout exceeded';
|
|
163
|
-
}
|
|
164
|
-
// Use default description if description is undefined, but not for Access denied errors
|
|
165
|
-
if (!description) {
|
|
166
|
-
description =
|
|
167
|
-
lang === Lang.Ru
|
|
168
|
-
? 'Произошла непредвиденная ошибка. Попробуйте обновить страницу через некоторое время.'
|
|
169
|
-
: 'An unexpected error has occurred. Try to refresh the page in a few moments.';
|
|
170
|
-
}
|
|
171
|
-
const status = getStatusByGrpcCode(error.code);
|
|
172
|
-
return {
|
|
173
|
-
status,
|
|
174
|
-
message: String(description || DEFAULT_GATEWAY_MESSAGE),
|
|
175
|
-
code: DEFAULT_GATEWAY_CODE,
|
|
176
|
-
details: Object.assign({ title,
|
|
177
|
-
description, grpcCode: code }, (typeof details === 'object' && !Array.isArray(details) ? details : { details })),
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
export class GrpcError extends Error {
|
|
181
|
-
constructor(message, parsedError, rawError) {
|
|
182
|
-
super(message);
|
|
183
|
-
this.parsedError = parsedError;
|
|
184
|
-
this.rawError = rawError;
|
|
185
|
-
}
|
|
186
|
-
getGatewayError() {
|
|
187
|
-
return this.parsedError;
|
|
188
|
-
}
|
|
189
|
-
getRawError() {
|
|
190
|
-
return this.rawError;
|
|
191
|
-
}
|
|
192
|
-
getAppError(ErrorConstructor) {
|
|
193
|
-
return new ErrorConstructor(this.parsedError.message, {
|
|
194
|
-
code: this.parsedError.code,
|
|
195
|
-
details: this.parsedError.details,
|
|
196
|
-
debug: this.parsedError.debug,
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
export function isGrpcError(error) {
|
|
201
|
-
return error.getGatewayError !== undefined;
|
|
202
|
-
}
|
|
203
|
-
export function grpcErrorFactory(error) {
|
|
204
|
-
return new GrpcError(error.message, {
|
|
205
|
-
status: 500,
|
|
206
|
-
code: '',
|
|
207
|
-
message: error.message,
|
|
208
|
-
details: {},
|
|
209
|
-
});
|
|
210
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
import * as protobufjs from 'protobufjs';
|
|
4
|
-
export function patchProtoPathResolver(root, includeDirs) {
|
|
5
|
-
const originalResolvePath = root.resolvePath;
|
|
6
|
-
root.resolvePath = function (origin, target) {
|
|
7
|
-
if (target in protobufjs.common || path.isAbsolute(target)) {
|
|
8
|
-
return target;
|
|
9
|
-
}
|
|
10
|
-
for (let i = 0; i < includeDirs.length; i++) {
|
|
11
|
-
const directory = includeDirs[i];
|
|
12
|
-
const fullPath = path.join(directory, target);
|
|
13
|
-
try {
|
|
14
|
-
fs.accessSync(fullPath, fs.constants.R_OK);
|
|
15
|
-
return fullPath;
|
|
16
|
-
}
|
|
17
|
-
catch (err) {
|
|
18
|
-
continue;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return originalResolvePath(origin, target);
|
|
22
|
-
};
|
|
23
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export function redactSensitiveHeaders(ctx, headers) {
|
|
2
|
-
var _a, _b;
|
|
3
|
-
if ((_a = ctx.utils) === null || _a === void 0 ? void 0 : _a.redactSensitiveHeaders) {
|
|
4
|
-
return ctx.utils.redactSensitiveHeaders(headers);
|
|
5
|
-
}
|
|
6
|
-
else if ((_b = ctx.utils) === null || _b === void 0 ? void 0 : _b.redactSensitiveKeys) {
|
|
7
|
-
return ctx.utils.redactSensitiveKeys(headers);
|
|
8
|
-
}
|
|
9
|
-
else {
|
|
10
|
-
return headers;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const sourceDir: string;
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export declare function validateArgs<TParams>(args: TParams, schema: object): string | false;
|
|
2
|
-
export declare function encodePathParams<TParams extends {}>(params: TParams): Record<string, any>;
|
|
3
|
-
export declare function getPathParam(value: string): string;
|
|
4
|
-
export declare function getPathArgsProxy<TParams extends {}>(args: TParams, encodePathArgs?: boolean): TParams;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { Ajv } from 'ajv';
|
|
2
|
-
export function validateArgs(args, schema) {
|
|
3
|
-
const ajv = new Ajv();
|
|
4
|
-
const validate = ajv.compile(schema);
|
|
5
|
-
return validate(args) ? false : ajv.errorsText(validate.errors);
|
|
6
|
-
}
|
|
7
|
-
export function encodePathParams(params) {
|
|
8
|
-
const encodedParams = {};
|
|
9
|
-
Object.keys(params).forEach((key) => {
|
|
10
|
-
const value = params[key];
|
|
11
|
-
if (value instanceof Buffer) {
|
|
12
|
-
encodedParams[key] = value;
|
|
13
|
-
}
|
|
14
|
-
else if (typeof value === 'object' && value !== null) {
|
|
15
|
-
encodedParams[key] = encodePathParams(value);
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
encodedParams[key] = encodeURIComponent(value);
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
return encodedParams;
|
|
22
|
-
}
|
|
23
|
-
export function getPathParam(value) {
|
|
24
|
-
return /^((?!(\.\.|\?|#|\\|\/)).)*$/i.test(value) ? value : 'GATEWAY_INVALID_PARAM_VALUE';
|
|
25
|
-
}
|
|
26
|
-
export function getPathArgsProxy(args, encodePathArgs) {
|
|
27
|
-
const encodePathArgsVal = encodePathArgs !== null && encodePathArgs !== void 0 ? encodePathArgs : true;
|
|
28
|
-
if (!args) {
|
|
29
|
-
return args;
|
|
30
|
-
}
|
|
31
|
-
return new Proxy(args, {
|
|
32
|
-
get: (object, key) => {
|
|
33
|
-
const value = object[key];
|
|
34
|
-
if (value instanceof Buffer) {
|
|
35
|
-
return value;
|
|
36
|
-
}
|
|
37
|
-
if (typeof value === 'object' && value !== null) {
|
|
38
|
-
return getPathArgsProxy(value);
|
|
39
|
-
}
|
|
40
|
-
if (typeof value === 'string') {
|
|
41
|
-
const pathParam = getPathParam(value);
|
|
42
|
-
return encodePathArgsVal ? encodeURIComponent(pathParam) : pathParam;
|
|
43
|
-
}
|
|
44
|
-
return value; // TODO return error INVALID_PARAMS
|
|
45
|
-
},
|
|
46
|
-
});
|
|
47
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|