@gravity-ui/gateway 4.1.2-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 +19 -29
- 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 +18 -31
- 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/package-root.d.ts +0 -1
- package/dist/commonjs/utils/package-root.js +0 -44
- 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 -82
- 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 -72
- 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/package-root.d.ts +0 -1
- package/dist/esm/utils/package-root.js +0 -8
- 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
package/dist/esm/index.js
DELETED
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
import { createGrpcAction, createRoot, getCredentialsMap } from './components/grpc.js';
|
|
3
|
-
import { createMixedAction } from './components/mixed.js';
|
|
4
|
-
import { createRestAction } from './components/rest.js';
|
|
5
|
-
import { ANY_ACTION_SYMBOL } from './constants.js';
|
|
6
|
-
import { getKeys, handleError } from './utils/common.js';
|
|
7
|
-
import { overrideEndpoints } from './utils/overrideEndpoints/overrideEndpoints.js';
|
|
8
|
-
export * from './utils/typed-api.js';
|
|
9
|
-
export * from './utils/grpc-reflection.js';
|
|
10
|
-
export * from './models/common.js';
|
|
11
|
-
export * from './models/context.js';
|
|
12
|
-
export * from './models/error.js';
|
|
13
|
-
function isMixedActionConfig(actionConfig) {
|
|
14
|
-
return typeof actionConfig === 'function';
|
|
15
|
-
}
|
|
16
|
-
function isRestActionConfig(actionConfig) {
|
|
17
|
-
return Boolean(actionConfig.method);
|
|
18
|
-
}
|
|
19
|
-
function createApiAction(schema, config, serviceKey, actionName, api, grpcContext) {
|
|
20
|
-
var _a;
|
|
21
|
-
const serviceSchema = schema[serviceKey];
|
|
22
|
-
if (!serviceSchema) {
|
|
23
|
-
throw new config.ErrorConstructor(`Gateway config error. Service "${serviceKey}" have been not found.`, {
|
|
24
|
-
code: 'SERVICE_NOT_FOUND',
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
const action = serviceSchema.actions[actionName];
|
|
28
|
-
if (!action) {
|
|
29
|
-
throw new config.ErrorConstructor(`Gateway config error. Action "${serviceKey}.${actionName}" have been not found.`, {
|
|
30
|
-
code: 'ACTION_NOT_FOUND',
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
const serviceName = serviceSchema.serviceName || '';
|
|
34
|
-
const installation = config.installation || '';
|
|
35
|
-
const env = config.env || '';
|
|
36
|
-
if (isMixedActionConfig(action)) {
|
|
37
|
-
const resultServiceName = serviceName || serviceKey;
|
|
38
|
-
return createMixedAction(action, api, resultServiceName, actionName, { config, grpcContext }, config.ErrorConstructor);
|
|
39
|
-
}
|
|
40
|
-
const endpointsConfig = _.get(serviceSchema.endpoints, [installation, env]);
|
|
41
|
-
if (isRestActionConfig(action)) {
|
|
42
|
-
return createRestAction(endpointsConfig, action, serviceKey, actionName, {
|
|
43
|
-
serviceName,
|
|
44
|
-
timeout: config.timeout,
|
|
45
|
-
sendStats: config.sendStats,
|
|
46
|
-
proxyHeaders: config.proxyHeaders,
|
|
47
|
-
proxyDebugHeaders: config.proxyDebugHeaders,
|
|
48
|
-
axiosConfig: config.axiosConfig,
|
|
49
|
-
axiosInterceptors: config.axiosInterceptors,
|
|
50
|
-
validationSchema: config.validationSchema,
|
|
51
|
-
encodePathArgs: config.encodePathArgs,
|
|
52
|
-
getAuthHeaders: config.getAuthHeaders,
|
|
53
|
-
}, config.ErrorConstructor);
|
|
54
|
-
}
|
|
55
|
-
const grpcRecreateService = (_a = config.grpcRecreateService) !== null && _a !== void 0 ? _a : true;
|
|
56
|
-
return createGrpcAction(grpcContext, endpointsConfig, action, serviceKey, actionName, {
|
|
57
|
-
serviceName,
|
|
58
|
-
timeout: config.timeout,
|
|
59
|
-
sendStats: config.sendStats,
|
|
60
|
-
proxyHeaders: config.proxyHeaders,
|
|
61
|
-
proxyDebugHeaders: config.proxyDebugHeaders,
|
|
62
|
-
grpcOptions: config.grpcOptions,
|
|
63
|
-
grpcRecreateService,
|
|
64
|
-
getAuthHeaders: config.getAuthHeaders,
|
|
65
|
-
}, config.ErrorConstructor);
|
|
66
|
-
}
|
|
67
|
-
function generateGatewayApi(schema, config, grpcContext, baseApi) {
|
|
68
|
-
const { installation, env } = config;
|
|
69
|
-
if (!installation) {
|
|
70
|
-
throw new config.ErrorConstructor('Gateway config error', {
|
|
71
|
-
code: 'EMPTY_GATEWAY_INSTALLATION',
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
if (!env) {
|
|
75
|
-
throw new config.ErrorConstructor('Gateway config error', { code: 'EMPTY_GATEWAY_ENV' });
|
|
76
|
-
}
|
|
77
|
-
return Object.keys(schema).reduce((api, serviceKey) => {
|
|
78
|
-
return _.set(api, serviceKey, Object.keys(schema[serviceKey].actions).reduce((serviceActions, actionName) => (Object.assign(Object.assign({}, serviceActions), { [actionName]: createApiAction(schema, config, serviceKey, actionName, baseApi, grpcContext) })), {}));
|
|
79
|
-
}, {});
|
|
80
|
-
}
|
|
81
|
-
function generateGatewayApiController(schemasByScope, Api, config, controllerActions) {
|
|
82
|
-
// eslint-disable-next-line complexity
|
|
83
|
-
return async function gateway(req, res) {
|
|
84
|
-
var _a, _b, _c;
|
|
85
|
-
const { userId } = res.locals || {};
|
|
86
|
-
const { service, action, scope = 'root' } = req.params;
|
|
87
|
-
const withDebugHeaders = typeof config.withDebugHeaders === 'function'
|
|
88
|
-
? config.withDebugHeaders(req, res)
|
|
89
|
-
: Boolean(config.withDebugHeaders);
|
|
90
|
-
const onUnknownAction = config === null || config === void 0 ? void 0 : config.onUnknownAction;
|
|
91
|
-
const onBeforeAction = config === null || config === void 0 ? void 0 : config.onBeforeAction;
|
|
92
|
-
const onRequestSuccess = config === null || config === void 0 ? void 0 : config.onRequestSuccess;
|
|
93
|
-
const onRequestFailed = config === null || config === void 0 ? void 0 : config.onRequestFailed;
|
|
94
|
-
if (!Api || !Api[scope] || !Api[scope][service]) {
|
|
95
|
-
if (onUnknownAction) {
|
|
96
|
-
return onUnknownAction(req, res, {
|
|
97
|
-
service,
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
return res.status(404).send({
|
|
102
|
-
status: 404,
|
|
103
|
-
code: 'UNKNOWN_SERVICE',
|
|
104
|
-
message: 'Unknown service',
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
if (!Api[scope][service][action]) {
|
|
109
|
-
if (onUnknownAction) {
|
|
110
|
-
return onUnknownAction(req, res, {
|
|
111
|
-
service,
|
|
112
|
-
action,
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
return res.status(404).send({
|
|
117
|
-
status: 404,
|
|
118
|
-
code: 'UNKNOWN_SERVICE_ACTION',
|
|
119
|
-
message: 'Unknown service action',
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
// Validation of private endpoints, preventing their invocation through the controller.
|
|
124
|
-
if (_.startsWith(action, '_')) {
|
|
125
|
-
return res.status(404).send({
|
|
126
|
-
status: 404,
|
|
127
|
-
code: 'UNKNOWN_SERVICE_ACTION',
|
|
128
|
-
message: 'Unknown service action',
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
if (controllerActions &&
|
|
132
|
-
_.get(controllerActions, [scope]) !== ANY_ACTION_SYMBOL &&
|
|
133
|
-
_.get(controllerActions, [scope, service]) !== ANY_ACTION_SYMBOL &&
|
|
134
|
-
!_.get(controllerActions, [scope, service, action])) {
|
|
135
|
-
return res.status(404).send({
|
|
136
|
-
status: 404,
|
|
137
|
-
code: 'UNKNOWN_SERVICE_ACTION',
|
|
138
|
-
message: 'Unknown service action',
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
const args = req.method === 'GET' ? req.query : req.body;
|
|
142
|
-
try {
|
|
143
|
-
const apiAction = Api[scope][service][action];
|
|
144
|
-
if (onBeforeAction) {
|
|
145
|
-
const actionConfig = (_c = (_b = (_a = schemasByScope[scope]) === null || _a === void 0 ? void 0 : _a[service]) === null || _b === void 0 ? void 0 : _b.actions) === null || _c === void 0 ? void 0 : _c[action];
|
|
146
|
-
try {
|
|
147
|
-
await onBeforeAction(req, res, scope, service, action, actionConfig);
|
|
148
|
-
}
|
|
149
|
-
catch (error) {
|
|
150
|
-
handleError(config.ErrorConstructor, error, req.ctx, 'Before action handler error');
|
|
151
|
-
throw { error, debugHeaders: {} };
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
const { responseData, responseHeaders, debugHeaders } = await apiAction({
|
|
155
|
-
requestId: req.id,
|
|
156
|
-
headers: req.headers,
|
|
157
|
-
ctx: req.ctx,
|
|
158
|
-
args,
|
|
159
|
-
authArgs: config.getAuthArgs(req, res),
|
|
160
|
-
userId,
|
|
161
|
-
});
|
|
162
|
-
if (withDebugHeaders) {
|
|
163
|
-
res.set(debugHeaders);
|
|
164
|
-
}
|
|
165
|
-
if (responseHeaders) {
|
|
166
|
-
res.set(responseHeaders);
|
|
167
|
-
}
|
|
168
|
-
if (onRequestSuccess) {
|
|
169
|
-
return onRequestSuccess(req, res, responseData);
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
return res.send(responseData);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
catch (respError) {
|
|
176
|
-
const { error, debugHeaders } = respError;
|
|
177
|
-
let responseError = error;
|
|
178
|
-
if (withDebugHeaders) {
|
|
179
|
-
res.set(debugHeaders);
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
responseError = _.omit(error, ['debug']);
|
|
183
|
-
// Remove DebugInfo
|
|
184
|
-
if (responseError.details) {
|
|
185
|
-
_.forEach(responseError.details, function (value, key) {
|
|
186
|
-
const DEBUG_INFO_TYPE = 'type.googleapis.com/google.rpc.DebugInfo';
|
|
187
|
-
if ((value === null || value === void 0 ? void 0 : value['@type']) === DEBUG_INFO_TYPE) {
|
|
188
|
-
responseError.details[key] = undefined;
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
if (onRequestFailed) {
|
|
194
|
-
return onRequestFailed(req, res, error);
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
return res.status(_.get(error, 'status', 500)).send(responseError);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
export function getGatewayControllers(schemasByScope, config) {
|
|
203
|
-
var _a;
|
|
204
|
-
const apiByScope = {};
|
|
205
|
-
config.installation = config.installation || process.env.APP_INSTALLATION;
|
|
206
|
-
config.env = config.env || process.env.APP_ENV;
|
|
207
|
-
if (process.env.GATEWAY_ENDPOINTS_OVERRIDES) {
|
|
208
|
-
try {
|
|
209
|
-
// eslint-disable-next-line no-param-reassign
|
|
210
|
-
schemasByScope = overrideEndpoints(schemasByScope, JSON.parse(process.env.GATEWAY_ENDPOINTS_OVERRIDES), config.installation, config.env);
|
|
211
|
-
}
|
|
212
|
-
catch (err) {
|
|
213
|
-
console.warn('Error when parse GATEWAY_ENDPOINTS_OVERRIDES', err);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
const credentials = getCredentialsMap(config.caCertificatePath);
|
|
217
|
-
for (const scope of getKeys(schemasByScope)) {
|
|
218
|
-
apiByScope[scope] = generateGatewayApi(schemasByScope[scope], config, { root: createRoot(config.includeProtoRoots), credentials }, apiByScope);
|
|
219
|
-
}
|
|
220
|
-
const api = Object.assign({}, apiByScope);
|
|
221
|
-
const rootScope = apiByScope.root;
|
|
222
|
-
if (rootScope) {
|
|
223
|
-
for (const rootService of getKeys(rootScope)) {
|
|
224
|
-
const curScope = (_a = api[rootService]) !== null && _a !== void 0 ? _a : {};
|
|
225
|
-
for (const rootAction of getKeys(rootScope[rootService])) {
|
|
226
|
-
const rootServiceFunc = rootScope[rootService][rootAction];
|
|
227
|
-
if (curScope[rootAction]) {
|
|
228
|
-
for (const curScopeAction of getKeys(curScope[rootAction])) {
|
|
229
|
-
rootServiceFunc[curScopeAction] = curScope[rootAction][curScopeAction];
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
curScope[rootAction] = rootServiceFunc;
|
|
233
|
-
}
|
|
234
|
-
api[rootService] = curScope;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
const controllerActions = config.actions
|
|
238
|
-
? config.actions.reduce((acc, item) => {
|
|
239
|
-
const [scope, service, action] = item.split('.');
|
|
240
|
-
// Ignore option '*' for scopes
|
|
241
|
-
if (scope === ANY_ACTION_SYMBOL) {
|
|
242
|
-
return acc;
|
|
243
|
-
}
|
|
244
|
-
if (service === ANY_ACTION_SYMBOL) {
|
|
245
|
-
_.set(acc, [scope], ANY_ACTION_SYMBOL);
|
|
246
|
-
}
|
|
247
|
-
else if (action === ANY_ACTION_SYMBOL) {
|
|
248
|
-
if (acc[scope] !== ANY_ACTION_SYMBOL) {
|
|
249
|
-
_.set(acc, [scope, service], ANY_ACTION_SYMBOL);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
_.set(acc, [scope, service, action], true);
|
|
254
|
-
}
|
|
255
|
-
return acc;
|
|
256
|
-
}, {})
|
|
257
|
-
: undefined;
|
|
258
|
-
const controller = generateGatewayApiController(schemasByScope, api, config, controllerActions);
|
|
259
|
-
return {
|
|
260
|
-
controller,
|
|
261
|
-
api,
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
export default getGatewayControllers;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export interface AppErrorArgs {
|
|
2
|
-
code?: string | number;
|
|
3
|
-
details?: object;
|
|
4
|
-
debug?: object;
|
|
5
|
-
}
|
|
6
|
-
export interface AppErrorWrapArgs extends AppErrorArgs {
|
|
7
|
-
message?: string;
|
|
8
|
-
}
|
|
9
|
-
export interface AppErrorConstructor {
|
|
10
|
-
new (message?: string, args?: AppErrorArgs): Error;
|
|
11
|
-
wrap: (error: Error, args?: AppErrorWrapArgs) => Error;
|
|
12
|
-
}
|
package/dist/esm/models/error.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/esm/package.json
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { AxiosRequestConfig } from 'axios';
|
|
2
|
-
import { AxiosInterceptorsConfig } from '../models/common.js';
|
|
3
|
-
export declare function getAxiosClient(timeout?: number, retries?: number, axiosConfig?: AxiosRequestConfig, { request: reqInterceptors, response: resInterceptors }?: AxiosInterceptorsConfig): import("axios").AxiosInstance;
|
package/dist/esm/utils/axios.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import axiosRetry from 'axios-retry';
|
|
3
|
-
import _ from 'lodash';
|
|
4
|
-
import { DEFAULT_AXIOS_OPTIONS, DEFAULT_TIMEOUT } from '../constants.js';
|
|
5
|
-
export function getAxiosClient(timeout = DEFAULT_TIMEOUT, retries = 0, axiosConfig = DEFAULT_AXIOS_OPTIONS, { request: reqInterceptors, response: resInterceptors } = {}) {
|
|
6
|
-
const client = axios.create(Object.assign(Object.assign({}, axiosConfig), { timeout }));
|
|
7
|
-
reqInterceptors === null || reqInterceptors === void 0 ? void 0 : reqInterceptors.forEach(({ callback, errorCallback }) => client.interceptors.request.use(callback, errorCallback));
|
|
8
|
-
resInterceptors === null || resInterceptors === void 0 ? void 0 : resInterceptors.forEach(({ callback, errorCallback }) => client.interceptors.response.use(callback, errorCallback));
|
|
9
|
-
axiosRetry(client, {
|
|
10
|
-
retries,
|
|
11
|
-
retryDelay: axiosRetry.exponentialDelay,
|
|
12
|
-
retryCondition: (error) => {
|
|
13
|
-
if (!error.config) {
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
return axiosRetry.isNetworkError(error) || axiosRetry.isRetryableError(error);
|
|
17
|
-
},
|
|
18
|
-
onRetry: (retryCount, _error, requestConfig) => {
|
|
19
|
-
_.set(requestConfig, ['headers', 'x-request-attempt'], retryCount);
|
|
20
|
-
},
|
|
21
|
-
});
|
|
22
|
-
return client;
|
|
23
|
-
}
|
package/dist/esm/utils/common.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
export function isExtendedActionEndpoint(endpoint) {
|
|
3
|
-
return (endpoint === null || endpoint === void 0 ? void 0 : endpoint.path) !== undefined;
|
|
4
|
-
}
|
|
5
|
-
export function isExtendedGrpcActionEndpoint(endpoint) {
|
|
6
|
-
return ((endpoint === null || endpoint === void 0 ? void 0 : endpoint.grpcOptions) !== undefined ||
|
|
7
|
-
(endpoint === null || endpoint === void 0 ? void 0 : endpoint.insecure) !== undefined ||
|
|
8
|
-
(endpoint === null || endpoint === void 0 ? void 0 : endpoint.secureWithoutRootCert) !== undefined);
|
|
9
|
-
}
|
|
10
|
-
export function isExtendedRestActionEndpoint(endpoint) {
|
|
11
|
-
return (endpoint === null || endpoint === void 0 ? void 0 : endpoint.axiosConfig) !== undefined;
|
|
12
|
-
}
|
|
13
|
-
export function getKeys(obj) {
|
|
14
|
-
return Object.keys(obj);
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* This function should only use to sanitize debugHeaders that are creating in our code
|
|
18
|
-
*/
|
|
19
|
-
export function sanitizeDebugHeaders(debugHeaders) {
|
|
20
|
-
return _.omit(debugHeaders, ['x-api-request-body']);
|
|
21
|
-
}
|
|
22
|
-
export function getHeadersFromMetadata(metadata, prefix = '') {
|
|
23
|
-
return Object.entries(metadata).reduce((headers, [key, values]) => {
|
|
24
|
-
headers[`${prefix}${key}`] = values.filter((value) => typeof value === 'string').join(' ');
|
|
25
|
-
return headers;
|
|
26
|
-
}, {});
|
|
27
|
-
}
|
|
28
|
-
export function handleError(ErrorConstructor, error, ctx, message, extra) {
|
|
29
|
-
if (error instanceof Error) {
|
|
30
|
-
ctx.logError(message, ErrorConstructor.wrap(error), extra);
|
|
31
|
-
}
|
|
32
|
-
else if (typeof error === 'string') {
|
|
33
|
-
ctx.logError(message, { error }, extra);
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
ctx.logError(message, error, extra);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { ApiActionConfig, ApiByScope, ContextApiWithRoot, GatewayRequest, GatewayResponse, SchemasByScope } from '../models/common.js';
|
|
2
|
-
import { GatewayContext } from '../models/context.js';
|
|
3
|
-
export type RequestContext<Context extends GatewayContext> = Omit<ApiActionConfig<Context, never>, 'args'>;
|
|
4
|
-
export declare function generateContextApi<TFullSchema extends SchemasByScope, Context extends GatewayContext, Req extends GatewayRequest<Context>, Res extends GatewayResponse>(baseApi: ApiByScope<TFullSchema, Context, Req, Res>, requestContext: RequestContext<Context>): ContextApiWithRoot<TFullSchema>;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
import { getKeys } from './common.js';
|
|
3
|
-
function createContextApiForScope(api, requestContext) {
|
|
4
|
-
return _.reduce(api, (acc, service, serviceName) => {
|
|
5
|
-
acc[serviceName] = _.reduce(service, (accService, action, actionName) => {
|
|
6
|
-
accService[actionName] = (args) => action(Object.assign(Object.assign({}, requestContext), { args })).then((response) => response.responseData ||
|
|
7
|
-
response
|
|
8
|
-
.stream);
|
|
9
|
-
return accService;
|
|
10
|
-
}, {});
|
|
11
|
-
return acc;
|
|
12
|
-
}, {});
|
|
13
|
-
}
|
|
14
|
-
export function generateContextApi(baseApi, requestContext) {
|
|
15
|
-
var _a;
|
|
16
|
-
const contextApi = _.reduce(baseApi, (contextApi, scope, scopeName) => {
|
|
17
|
-
contextApi[scopeName] = createContextApiForScope(scope, requestContext);
|
|
18
|
-
return contextApi;
|
|
19
|
-
}, {});
|
|
20
|
-
const api = contextApi;
|
|
21
|
-
const rootScope = contextApi.root;
|
|
22
|
-
if (rootScope) {
|
|
23
|
-
for (const rootService of getKeys(rootScope)) {
|
|
24
|
-
const curScope = (_a = api[rootService]) !== null && _a !== void 0 ? _a : {};
|
|
25
|
-
for (const rootAction of getKeys(rootScope[rootService])) {
|
|
26
|
-
const rootServiceFunc = rootScope[rootService][rootAction];
|
|
27
|
-
if (curScope[rootAction]) {
|
|
28
|
-
for (const curScopeAction of getKeys(curScope[rootAction])) {
|
|
29
|
-
rootServiceFunc[curScopeAction] = curScope[rootAction][curScopeAction];
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
curScope[rootAction] = rootServiceFunc;
|
|
33
|
-
}
|
|
34
|
-
api[rootService] = curScope;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return api;
|
|
38
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { ChannelCredentials } from '@grpc/grpc-js';
|
|
2
|
-
type DescriptorExtensionProto = string[] | {
|
|
3
|
-
includeProtoRoots: string[];
|
|
4
|
-
filenames: string[];
|
|
5
|
-
};
|
|
6
|
-
/**
|
|
7
|
-
* @param actionEndpoint
|
|
8
|
-
* @param protoKey
|
|
9
|
-
* @param credentials
|
|
10
|
-
* @param grpcOptions
|
|
11
|
-
* @param descriptorExtensionProto
|
|
12
|
-
* @returns Promise<protobufjs.Root>.
|
|
13
|
-
* use toDescriptor for use with protoLoader.
|
|
14
|
-
* use toJSON for get a JSON descriptor.
|
|
15
|
-
*/
|
|
16
|
-
export declare function getCachedReflectionRoot(actionEndpoint: string, protoKey: string, credentials: ChannelCredentials, grpcOptions?: object, descriptorExtensionProto?: DescriptorExtensionProto): Promise<any>;
|
|
17
|
-
/**
|
|
18
|
-
* @param actionEndpoint
|
|
19
|
-
* @param protoKey
|
|
20
|
-
* @param credentials
|
|
21
|
-
* @param grpcOptions
|
|
22
|
-
* @param addToCache
|
|
23
|
-
* @returns Promise<protobufjs.Root>.
|
|
24
|
-
* use toDescriptor for use with protoLoader.
|
|
25
|
-
* use toJSON for get a JSON descriptor.
|
|
26
|
-
*/
|
|
27
|
-
export declare function getReflectionRoot(actionEndpoint: string, protoKey: string, credentials: ChannelCredentials, grpcOptions?: object, addToCache?: boolean): Promise<any>;
|
|
28
|
-
export {};
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
import * as protobufjs from 'protobufjs';
|
|
3
|
-
import { patchProtoPathResolver } from './proto-path-resolver.js';
|
|
4
|
-
const reflectionClientsMap = {};
|
|
5
|
-
async function getCachedClient(actionEndpoint, credentials, grpcOptions, descriptorExtensionProto) {
|
|
6
|
-
const cacheKey = [actionEndpoint, JSON.stringify([grpcOptions, descriptorExtensionProto])];
|
|
7
|
-
let clientWithCache = _.get(reflectionClientsMap, cacheKey);
|
|
8
|
-
if (!clientWithCache) {
|
|
9
|
-
const grpcReflection = await import('grpc-reflection-js');
|
|
10
|
-
let descriptorRoot;
|
|
11
|
-
if (descriptorExtensionProto) {
|
|
12
|
-
descriptorRoot = protobufjs.Root.fromJSON(
|
|
13
|
-
// @ts-expect-error no typings for default export
|
|
14
|
-
(await import('protobufjs/ext/descriptor/index.js')).default);
|
|
15
|
-
if (Array.isArray(descriptorExtensionProto)) {
|
|
16
|
-
descriptorRoot.loadSync(descriptorExtensionProto);
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
patchProtoPathResolver(descriptorRoot, descriptorExtensionProto.includeProtoRoots);
|
|
20
|
-
descriptorRoot.loadSync(descriptorExtensionProto.filenames);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
const client = new grpcReflection.Client(actionEndpoint, credentials, grpcOptions, undefined,
|
|
24
|
-
// @ts-ignore this parameter is present only in the patched version
|
|
25
|
-
descriptorRoot);
|
|
26
|
-
clientWithCache = { client, reflectionRootPromiseMap: {} };
|
|
27
|
-
_.set(reflectionClientsMap, cacheKey, clientWithCache);
|
|
28
|
-
}
|
|
29
|
-
return clientWithCache;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* @param actionEndpoint
|
|
33
|
-
* @param protoKey
|
|
34
|
-
* @param credentials
|
|
35
|
-
* @param grpcOptions
|
|
36
|
-
* @param descriptorExtensionProto
|
|
37
|
-
* @returns Promise<protobufjs.Root>.
|
|
38
|
-
* use toDescriptor for use with protoLoader.
|
|
39
|
-
* use toJSON for get a JSON descriptor.
|
|
40
|
-
*/
|
|
41
|
-
export async function getCachedReflectionRoot(actionEndpoint, protoKey, credentials, grpcOptions, descriptorExtensionProto) {
|
|
42
|
-
const { client, reflectionRootPromiseMap } = await getCachedClient(actionEndpoint, credentials, grpcOptions, descriptorExtensionProto);
|
|
43
|
-
const cacheKey = [actionEndpoint, protoKey];
|
|
44
|
-
let cachedRootPromise = _.get(reflectionRootPromiseMap, cacheKey);
|
|
45
|
-
if (!cachedRootPromise) {
|
|
46
|
-
cachedRootPromise = client.fileContainingSymbol(protoKey);
|
|
47
|
-
_.set(reflectionRootPromiseMap, cacheKey, cachedRootPromise);
|
|
48
|
-
cachedRootPromise.catch(() => {
|
|
49
|
-
_.set(reflectionRootPromiseMap, cacheKey, undefined);
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
const loadedRoot = await cachedRootPromise;
|
|
53
|
-
return loadedRoot;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* @param actionEndpoint
|
|
57
|
-
* @param protoKey
|
|
58
|
-
* @param credentials
|
|
59
|
-
* @param grpcOptions
|
|
60
|
-
* @param addToCache
|
|
61
|
-
* @returns Promise<protobufjs.Root>.
|
|
62
|
-
* use toDescriptor for use with protoLoader.
|
|
63
|
-
* use toJSON for get a JSON descriptor.
|
|
64
|
-
*/
|
|
65
|
-
export async function getReflectionRoot(actionEndpoint, protoKey, credentials, grpcOptions, addToCache) {
|
|
66
|
-
const { client, reflectionRootPromiseMap } = await getCachedClient(actionEndpoint, credentials, grpcOptions);
|
|
67
|
-
const loadedRoot = await client.fileContainingSymbol(protoKey);
|
|
68
|
-
if (addToCache) {
|
|
69
|
-
_.set(reflectionRootPromiseMap, [actionEndpoint, protoKey], Promise.resolve(loadedRoot));
|
|
70
|
-
}
|
|
71
|
-
return loadedRoot;
|
|
72
|
-
}
|
package/dist/esm/utils/grpc.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import * as grpc from '@grpc/grpc-js';
|
|
2
|
-
import * as protobufjs from 'protobufjs';
|
|
3
|
-
export declare function decodeAnyMessageRecursively(root: protobufjs.Root, message?: unknown, decodeAnyMessageProtoLoaderOptions?: protobufjs.IConversionOptions): unknown;
|
|
4
|
-
export declare function isRetryableError(error?: grpc.ServiceError): boolean;
|
|
5
|
-
export declare function isRecreateServiceError(error?: grpc.ServiceError): boolean;
|
package/dist/esm/utils/grpc.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/* eslint-disable camelcase */
|
|
2
|
-
import { DEFAULT_PROTO_LOADER_OPTIONS, RECREATE_SERVICE_CODES, RETRYABLE_STATUS_CODES, } from '../constants.js';
|
|
3
|
-
function isEncodedMessage(message) {
|
|
4
|
-
return Boolean(message.type_url && message.value);
|
|
5
|
-
}
|
|
6
|
-
export function decodeAnyMessageRecursively(root, message, decodeAnyMessageProtoLoaderOptions) {
|
|
7
|
-
if (!message || typeof message !== 'object') {
|
|
8
|
-
return message;
|
|
9
|
-
}
|
|
10
|
-
if (Array.isArray(message)) {
|
|
11
|
-
return message.map((innerMessage) => decodeAnyMessageRecursively(root, innerMessage, decodeAnyMessageProtoLoaderOptions));
|
|
12
|
-
}
|
|
13
|
-
if (typeof message === 'object' && !isEncodedMessage(message)) {
|
|
14
|
-
return Object.entries(message).reduce((res, [key, value]) => {
|
|
15
|
-
res[key] = decodeAnyMessageRecursively(root, value, decodeAnyMessageProtoLoaderOptions);
|
|
16
|
-
return res;
|
|
17
|
-
}, {});
|
|
18
|
-
}
|
|
19
|
-
const lastSlashIndex = message.type_url.lastIndexOf('/');
|
|
20
|
-
if (lastSlashIndex < 0) {
|
|
21
|
-
return message;
|
|
22
|
-
}
|
|
23
|
-
const typeName = message.type_url.substring(lastSlashIndex + 1);
|
|
24
|
-
const type = root.lookupType(typeName);
|
|
25
|
-
const decodedMessage = type.toObject(type.decode(message.value), Object.assign(Object.assign({}, DEFAULT_PROTO_LOADER_OPTIONS), decodeAnyMessageProtoLoaderOptions));
|
|
26
|
-
return decodeAnyMessageRecursively(root, decodedMessage, decodeAnyMessageProtoLoaderOptions);
|
|
27
|
-
}
|
|
28
|
-
export function isRetryableError(error) {
|
|
29
|
-
if (!error) {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
return RETRYABLE_STATUS_CODES.includes(error.code);
|
|
33
|
-
}
|
|
34
|
-
export function isRecreateServiceError(error) {
|
|
35
|
-
if (!error) {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
return RECREATE_SERVICE_CODES.includes(error.code);
|
|
39
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { ActionEndpoint, SchemasByScope } from '../../index.js';
|
|
2
|
-
export interface OverrideParams {
|
|
3
|
-
[serviceKeyScope: string]: {
|
|
4
|
-
[endpointName: string]: ActionEndpoint;
|
|
5
|
-
};
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Overrides endpoints in the schema according to overrideParams
|
|
9
|
-
* @param originalSchema Schema for the gateway
|
|
10
|
-
* @param overrideParams Definitions of endpoints for services in the format {[<scope>.<service key>]: {[<endpointName>]: ActionEndpoint}}
|
|
11
|
-
* If <scope> is not specified, root is assumed.
|
|
12
|
-
* Example: '{serviceName: {endpoint: "https://example.com"}}'
|
|
13
|
-
* @param installation
|
|
14
|
-
* @param env
|
|
15
|
-
* @return new overridden schema
|
|
16
|
-
*/
|
|
17
|
-
export declare function overrideEndpoints<T extends SchemasByScope>(originalSchema: T, overrideParams: OverrideParams, installation: string | undefined, env: string | undefined): T;
|