@gravity-ui/gateway 3.1.4-alpha.0 → 4.0.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 +4 -4
- package/build/components/grpc.js +8 -9
- package/build/index.d.ts +1 -1
- package/build/index.js +2 -1
- package/build/models/common.d.ts +0 -2
- package/build/utils/grpc.d.ts +1 -1
- package/build/utils/grpc.js +5 -5
- package/build/utils/parse-error.d.ts +1 -1
- package/build/utils/parse-error.js +4 -4
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm install --save-dev @gravity-ui/gateway
|
|
|
13
13
|
First of all, you need to create a controller where you will import Gateway and Schema, and then return the initialized gateway controller:
|
|
14
14
|
|
|
15
15
|
```javascript
|
|
16
|
-
import getGatewayControllers from '@gravity-ui/gateway';
|
|
16
|
+
import {getGatewayControllers} from '@gravity-ui/gateway';
|
|
17
17
|
import Schema from '<schemas package>';
|
|
18
18
|
|
|
19
19
|
const config = {
|
|
@@ -134,7 +134,7 @@ If the parameter value does not pass validation, the `GATEWAY_INVALID_PARAM_VALU
|
|
|
134
134
|
Upon gateway initialization, in addition to exporting the controller, it also exports an `api` object, which represents the core for executing requests to the backend.
|
|
135
135
|
|
|
136
136
|
```javascript
|
|
137
|
-
import getGatewayControllers from '@gravity-ui/gateway';
|
|
137
|
+
import {getGatewayControllers} from '@gravity-ui/gateway';
|
|
138
138
|
import Schema from '<schemas package>';
|
|
139
139
|
|
|
140
140
|
const config = {
|
|
@@ -180,7 +180,7 @@ const schemasByScopes = {scope1: schema1, scope2: schema2};
|
|
|
180
180
|
Example with two scope namespaces: `root` and `anotherScope`.
|
|
181
181
|
|
|
182
182
|
```javascript
|
|
183
|
-
import getGatewayControllers from '@gravity-ui/gateway';
|
|
183
|
+
import {getGatewayControllers} from '@gravity-ui/gateway';
|
|
184
184
|
|
|
185
185
|
const {
|
|
186
186
|
controller, // Controller
|
|
@@ -215,7 +215,7 @@ If the scope parameter is not specified, the default scope is assumed to be `roo
|
|
|
215
215
|
When initializing the `gateway`, there is an option to explicitly specify the actions that need to be connected from the schemas. To do this, provide a list of available client-side actions in the `actions` field in the config. If `actions` are not provided, all actions from the schemas are connected by default.
|
|
216
216
|
|
|
217
217
|
```typescript
|
|
218
|
-
import getGatewayControllers from '@gravity-ui/gateway';
|
|
218
|
+
import {getGatewayControllers} from '@gravity-ui/gateway';
|
|
219
219
|
import rootSchema from '<schemas package>';
|
|
220
220
|
import localSchema from '../shared/schemas';
|
|
221
221
|
|
package/build/components/grpc.js
CHANGED
|
@@ -71,14 +71,14 @@ function getCredentialsMap(caCertificatePath) {
|
|
|
71
71
|
};
|
|
72
72
|
}
|
|
73
73
|
exports.getCredentialsMap = getCredentialsMap;
|
|
74
|
-
function decodeResponse(response, packageRoot, ctx, encodedFields = [], ErrorConstructor
|
|
74
|
+
function decodeResponse(response, packageRoot, ctx, encodedFields = [], ErrorConstructor) {
|
|
75
75
|
const systemFields = ['metadata', 'response', 'error.details'];
|
|
76
76
|
[...systemFields, ...encodedFields].forEach((fieldName) => {
|
|
77
77
|
try {
|
|
78
78
|
const parsedFieldName = fieldName.replace(/\.\*$/, '');
|
|
79
79
|
const fieldValue = lodash_1.default.get(response, parsedFieldName);
|
|
80
80
|
if (fieldValue) {
|
|
81
|
-
lodash_1.default.set(response, parsedFieldName, (0, grpc_1.decodeAnyMessageRecursively)(packageRoot, fieldValue
|
|
81
|
+
lodash_1.default.set(response, parsedFieldName, (0, grpc_1.decodeAnyMessageRecursively)(packageRoot, fieldValue));
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
catch (error) {
|
|
@@ -301,15 +301,15 @@ async function getServiceInstance(root, config, endpointData, grpcOptions, crede
|
|
|
301
301
|
}
|
|
302
302
|
return serviceInstance;
|
|
303
303
|
}
|
|
304
|
-
async function getResponseData({ config, response, ctx, packageRoot, args, ErrorConstructor,
|
|
304
|
+
async function getResponseData({ config, response, ctx, packageRoot, args, ErrorConstructor, }) {
|
|
305
305
|
// Handle operation's runtime protocol buffers
|
|
306
306
|
if (response) {
|
|
307
307
|
const encodedFields = config.encodedFields;
|
|
308
308
|
if (Array.isArray(response)) {
|
|
309
|
-
response.forEach((responseItem) => decodeResponse(responseItem, packageRoot, ctx, encodedFields, ErrorConstructor
|
|
309
|
+
response.forEach((responseItem) => decodeResponse(responseItem, packageRoot, ctx, encodedFields, ErrorConstructor));
|
|
310
310
|
}
|
|
311
311
|
else if (typeof response === 'object') {
|
|
312
|
-
decodeResponse(response, packageRoot, ctx, encodedFields, ErrorConstructor
|
|
312
|
+
decodeResponse(response, packageRoot, ctx, encodedFields, ErrorConstructor);
|
|
313
313
|
}
|
|
314
314
|
}
|
|
315
315
|
let responseData = response;
|
|
@@ -526,7 +526,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
526
526
|
ctx.log('ServerStream error', {
|
|
527
527
|
debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders),
|
|
528
528
|
});
|
|
529
|
-
processError(new parse_error_1.GrpcError('ClientReadableStream error', (0, parse_error_1.parseGrpcError)(error, root, lang
|
|
529
|
+
processError(new parse_error_1.GrpcError('ClientReadableStream error', (0, parse_error_1.parseGrpcError)(error, root, lang), error));
|
|
530
530
|
});
|
|
531
531
|
stream.on('status', (status) => {
|
|
532
532
|
ctx.log('ServerStream status changed', status);
|
|
@@ -566,7 +566,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
566
566
|
ctx.log('BidiStream error', {
|
|
567
567
|
debugHeaders: (0, common_2.sanitizeDebugHeaders)(debugHeaders),
|
|
568
568
|
});
|
|
569
|
-
processError(new parse_error_1.GrpcError('BidiStream error', (0, parse_error_1.parseGrpcError)(error, root, lang
|
|
569
|
+
processError(new parse_error_1.GrpcError('BidiStream error', (0, parse_error_1.parseGrpcError)(error, root, lang), error));
|
|
570
570
|
});
|
|
571
571
|
stream.on('status', (status) => {
|
|
572
572
|
ctx.log('BidiStream status changed', status);
|
|
@@ -618,7 +618,7 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
618
618
|
return;
|
|
619
619
|
}
|
|
620
620
|
if (error) {
|
|
621
|
-
reject(new parse_error_1.GrpcError('gRPC request error', (0, parse_error_1.parseGrpcError)(error, root, lang
|
|
621
|
+
reject(new parse_error_1.GrpcError('gRPC request error', (0, parse_error_1.parseGrpcError)(error, root, lang), error));
|
|
622
622
|
return;
|
|
623
623
|
}
|
|
624
624
|
const responseData = await getResponseData({
|
|
@@ -628,7 +628,6 @@ function createGrpcAction({ root, credentials }, endpoints, config, serviceKey,
|
|
|
628
628
|
args,
|
|
629
629
|
packageRoot: root,
|
|
630
630
|
ErrorConstructor,
|
|
631
|
-
decodeAnyMessageProtoLoaderOptions: config.decodeAnyMessageProtoLoaderOptions,
|
|
632
631
|
});
|
|
633
632
|
const responseHeaders = {};
|
|
634
633
|
if (config.proxyResponseHeaders) {
|
package/build/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export * from './utils/grpc-reflection';
|
|
|
5
5
|
export * from './models/common';
|
|
6
6
|
export * from './models/context';
|
|
7
7
|
export * from './models/error';
|
|
8
|
-
export
|
|
8
|
+
export declare function getGatewayControllers<TSchema extends SchemasByScope, Context extends GatewayContext, Req extends GatewayRequest<Context>, Res extends GatewayResponse>(schemasByScope: TSchema, config: GatewayConfig<Context, Req, Res>): {
|
|
9
9
|
controller: (req: Req, res: Res) => Promise<any>;
|
|
10
10
|
api: ApiWithRoot<TSchema, Context, Req, Res>;
|
|
11
11
|
};
|
package/build/index.js
CHANGED
|
@@ -29,6 +29,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
29
29
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
30
|
};
|
|
31
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.getGatewayControllers = void 0;
|
|
32
33
|
const lodash_1 = __importDefault(require("lodash"));
|
|
33
34
|
const grpc_1 = __importStar(require("./components/grpc"));
|
|
34
35
|
const mixed_1 = require("./components/mixed");
|
|
@@ -292,4 +293,4 @@ function getGatewayControllers(schemasByScope, config) {
|
|
|
292
293
|
api,
|
|
293
294
|
};
|
|
294
295
|
}
|
|
295
|
-
exports.
|
|
296
|
+
exports.getGatewayControllers = getGatewayControllers;
|
package/build/models/common.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ import { ClientDuplexStream, ClientReadableStream, ClientWritableStream } from '
|
|
|
4
4
|
import { HandlerType } from '@grpc/grpc-js/build/src/server-call';
|
|
5
5
|
import { AxiosInterceptorManager, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
|
6
6
|
import type { Request, Response } from 'express';
|
|
7
|
-
import * as protobufjs from 'protobufjs';
|
|
8
7
|
import type { GrpcContext } from '../components/grpc';
|
|
9
8
|
import { Lang } from '../constants';
|
|
10
9
|
import { GatewayContext } from './context';
|
|
@@ -142,7 +141,6 @@ export interface ApiServiceBaseGrpcActionConfig<Context extends GatewayContext,
|
|
|
142
141
|
secureWithoutRootCert?: boolean;
|
|
143
142
|
encodedFields?: string[];
|
|
144
143
|
type?: HandlerType;
|
|
145
|
-
decodeAnyMessageProtoLoaderOptions?: protobufjs.IConversionOptions;
|
|
146
144
|
}
|
|
147
145
|
export interface ApiServiceFileGrpcActionConfig<Context extends GatewayContext, TOutput, TParams, TTransformed> extends ApiServiceBaseGrpcActionConfig<Context, TOutput, TParams, TTransformed> {
|
|
148
146
|
protoPath: string;
|
package/build/utils/grpc.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as grpc from '@grpc/grpc-js';
|
|
2
2
|
import * as protobufjs from 'protobufjs';
|
|
3
|
-
export declare function decodeAnyMessageRecursively(root: protobufjs.Root, message?: unknown
|
|
3
|
+
export declare function decodeAnyMessageRecursively(root: protobufjs.Root, message?: unknown): unknown;
|
|
4
4
|
export declare function isRetryableError(error?: grpc.ServiceError): boolean;
|
|
5
5
|
export declare function isRecreateServiceError(error?: grpc.ServiceError): boolean;
|
package/build/utils/grpc.js
CHANGED
|
@@ -6,16 +6,16 @@ const constants_1 = require("../constants");
|
|
|
6
6
|
function isEncodedMessage(message) {
|
|
7
7
|
return Boolean(message.type_url && message.value);
|
|
8
8
|
}
|
|
9
|
-
function decodeAnyMessageRecursively(root, message
|
|
9
|
+
function decodeAnyMessageRecursively(root, message) {
|
|
10
10
|
if (!message || typeof message !== 'object') {
|
|
11
11
|
return message;
|
|
12
12
|
}
|
|
13
13
|
if (Array.isArray(message)) {
|
|
14
|
-
return message.map((innerMessage) => decodeAnyMessageRecursively(root, innerMessage
|
|
14
|
+
return message.map((innerMessage) => decodeAnyMessageRecursively(root, innerMessage));
|
|
15
15
|
}
|
|
16
16
|
if (typeof message === 'object' && !isEncodedMessage(message)) {
|
|
17
17
|
return Object.entries(message).reduce((res, [key, value]) => {
|
|
18
|
-
res[key] = decodeAnyMessageRecursively(root, value
|
|
18
|
+
res[key] = decodeAnyMessageRecursively(root, value);
|
|
19
19
|
return res;
|
|
20
20
|
}, {});
|
|
21
21
|
}
|
|
@@ -25,8 +25,8 @@ function decodeAnyMessageRecursively(root, message, decodeAnyMessageProtoLoaderO
|
|
|
25
25
|
}
|
|
26
26
|
const typeName = message.type_url.substring(lastSlashIndex + 1);
|
|
27
27
|
const type = root.lookupType(typeName);
|
|
28
|
-
const decodedMessage = type.toObject(type.decode(message.value),
|
|
29
|
-
return decodeAnyMessageRecursively(root, decodedMessage
|
|
28
|
+
const decodedMessage = type.toObject(type.decode(message.value), constants_1.DEFAULT_PROTO_LOADER_OPTIONS);
|
|
29
|
+
return decodeAnyMessageRecursively(root, decodedMessage);
|
|
30
30
|
}
|
|
31
31
|
exports.decodeAnyMessageRecursively = decodeAnyMessageRecursively;
|
|
32
32
|
function isRetryableError(error) {
|
|
@@ -17,7 +17,7 @@ export declare function parseMixedError(e: Error & {
|
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
19
|
export declare function parseRestError(error: any, lang?: string): GatewayError;
|
|
20
|
-
export declare function parseGrpcError(error: grpc.ServiceError, packageRoot: protobufjs.Root, lang?: Lang
|
|
20
|
+
export declare function parseGrpcError(error: grpc.ServiceError, packageRoot: protobufjs.Root, lang?: Lang): GatewayError;
|
|
21
21
|
export declare class GrpcError extends Error {
|
|
22
22
|
private parsedError;
|
|
23
23
|
private rawError;
|
|
@@ -135,7 +135,7 @@ function parseRestError(error, lang) {
|
|
|
135
135
|
};
|
|
136
136
|
}
|
|
137
137
|
exports.parseRestError = parseRestError;
|
|
138
|
-
function decodeGrpcStatusMessage(metadata, packageRoot
|
|
138
|
+
function decodeGrpcStatusMessage(metadata, packageRoot) {
|
|
139
139
|
const statusMessageBin = metadata.get('grpc-status-details-bin')[0];
|
|
140
140
|
if (!statusMessageBin) {
|
|
141
141
|
return undefined;
|
|
@@ -143,9 +143,9 @@ function decodeGrpcStatusMessage(metadata, packageRoot, decodeAnyMessageProtoLoa
|
|
|
143
143
|
return (0, grpc_1.decodeAnyMessageRecursively)(packageRoot, {
|
|
144
144
|
type_url: 'type.googleapis.com/google.rpc.Status',
|
|
145
145
|
value: statusMessageBin,
|
|
146
|
-
}
|
|
146
|
+
});
|
|
147
147
|
}
|
|
148
|
-
function parseGrpcError(error, packageRoot, lang = constants_1.Lang.Ru
|
|
148
|
+
function parseGrpcError(error, packageRoot, lang = constants_1.Lang.Ru) {
|
|
149
149
|
let title = lang === constants_1.Lang.Ru ? 'Ошибка' : 'Error';
|
|
150
150
|
if (error.code === 7) {
|
|
151
151
|
// Always redefine title for Access denied errors
|
|
@@ -156,7 +156,7 @@ function parseGrpcError(error, packageRoot, lang = constants_1.Lang.Ru, decodeAn
|
|
|
156
156
|
let details;
|
|
157
157
|
if (error.metadata) {
|
|
158
158
|
try {
|
|
159
|
-
const statusMessage = decodeGrpcStatusMessage(error.metadata, packageRoot
|
|
159
|
+
const statusMessage = decodeGrpcStatusMessage(error.metadata, packageRoot);
|
|
160
160
|
if (statusMessage) {
|
|
161
161
|
code = statusMessage.code;
|
|
162
162
|
description = statusMessage.message;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gravity-ui/gateway",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "build/index.js",
|
|
@@ -29,7 +29,9 @@
|
|
|
29
29
|
"start-integration-server": "node build-integration/integration-test/server",
|
|
30
30
|
"build-and-run-integration": "npm run build-integration && npm run start-integration-server",
|
|
31
31
|
"clean": "rm -rf build && rm -rf build-integration",
|
|
32
|
-
"lint": "
|
|
32
|
+
"lint": "npm run lint:code && npm run lint:exports",
|
|
33
|
+
"lint:code": "eslint \"lib/**/*.{js,ts}\" --quiet",
|
|
34
|
+
"lint:exports": "npm run build && attw --pack .",
|
|
33
35
|
"typecheck": "tsc --noEmit",
|
|
34
36
|
"prepublishOnly": "npm run build",
|
|
35
37
|
"test": "jest --colors",
|
|
@@ -48,6 +50,7 @@
|
|
|
48
50
|
"uuid": "^9.0.0"
|
|
49
51
|
},
|
|
50
52
|
"devDependencies": {
|
|
53
|
+
"@arethetypeswrong/cli": "^0.17.3",
|
|
51
54
|
"@commitlint/cli": "^17.7.1",
|
|
52
55
|
"@commitlint/config-conventional": "^17.7.0",
|
|
53
56
|
"@gravity-ui/eslint-config": "^2.0.0",
|