@russ-b/nestjs-common-tools 1.0.0 → 1.1.3
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 +87 -0
- package/dist/common/grpc/base-client-grpc.proxy.d.ts +5 -0
- package/dist/common/grpc/base-client-grpc.proxy.js +58 -0
- package/dist/common/grpc/base-client-grpc.proxy.js.map +1 -0
- package/dist/common/grpc/client-grpc.interface.d.ts +34 -0
- package/dist/common/grpc/client-grpc.interface.js +3 -0
- package/dist/common/grpc/client-grpc.interface.js.map +1 -0
- package/dist/common/grpc/decorators/index.d.ts +1 -0
- package/dist/common/grpc/decorators/index.js +18 -0
- package/dist/common/grpc/decorators/index.js.map +1 -0
- package/dist/common/grpc/decorators/is-grpc-struct.decorator.d.ts +3 -0
- package/dist/common/grpc/decorators/is-grpc-struct.decorator.js +13 -0
- package/dist/common/grpc/decorators/is-grpc-struct.decorator.js.map +1 -0
- package/dist/common/grpc/filters/index.d.ts +1 -0
- package/dist/common/grpc/filters/index.js +18 -0
- package/dist/common/grpc/filters/index.js.map +1 -0
- package/dist/common/grpc/filters/rpc-exception.filter.d.ts +6 -0
- package/dist/common/grpc/filters/rpc-exception.filter.js +63 -0
- package/dist/common/grpc/filters/rpc-exception.filter.js.map +1 -0
- package/dist/common/grpc/index.d.ts +4 -0
- package/dist/common/grpc/index.js +21 -0
- package/dist/common/grpc/index.js.map +1 -0
- package/dist/common/grpc/proxies/base-client-grpc.proxy.d.ts +7 -0
- package/dist/common/grpc/proxies/base-client-grpc.proxy.js +72 -0
- package/dist/common/grpc/proxies/base-client-grpc.proxy.js.map +1 -0
- package/dist/common/grpc/proxies/index.d.ts +1 -0
- package/dist/common/grpc/proxies/index.js +18 -0
- package/dist/common/grpc/proxies/index.js.map +1 -0
- package/dist/common/grpc/rpc-exception.filter.d.ts +6 -0
- package/dist/common/grpc/rpc-exception.filter.js +63 -0
- package/dist/common/grpc/rpc-exception.filter.js.map +1 -0
- package/dist/common/grpc/transformers/index.d.ts +1 -0
- package/dist/common/grpc/transformers/index.js +18 -0
- package/dist/common/grpc/transformers/index.js.map +1 -0
- package/dist/common/grpc/transformers/struct/index.d.ts +2 -0
- package/dist/common/grpc/transformers/struct/index.js +19 -0
- package/dist/common/grpc/transformers/struct/index.js.map +1 -0
- package/dist/common/grpc/transformers/struct/struct.interface.d.ts +17 -0
- package/dist/common/grpc/transformers/struct/struct.interface.js +3 -0
- package/dist/common/grpc/transformers/struct/struct.interface.js.map +1 -0
- package/dist/common/grpc/transformers/struct/struct.transformer.d.ts +8 -0
- package/dist/common/grpc/transformers/struct/struct.transformer.js +86 -0
- package/dist/common/grpc/transformers/struct/struct.transformer.js.map +1 -0
- package/dist/common/grpc/transformers/struct.transformer.d.ts +47 -0
- package/dist/common/grpc/transformers/struct.transformer.js +81 -0
- package/dist/common/grpc/transformers/struct.transformer.js.map +1 -0
- package/dist/common/validators/entity/entity.constant.d.ts +1 -0
- package/dist/common/validators/entity/entity.constant.js +5 -0
- package/dist/common/validators/entity/entity.constant.js.map +1 -0
- package/dist/common/validators/entity/entity.constraint.d.ts +9 -0
- package/dist/common/validators/entity/entity.constraint.js +74 -0
- package/dist/common/validators/entity/entity.constraint.js.map +1 -0
- package/dist/common/validators/entity/entity.decorator.d.ts +3 -0
- package/dist/common/validators/entity/entity.decorator.js +20 -0
- package/dist/common/validators/entity/entity.decorator.js.map +1 -0
- package/dist/common/validators/entity/entity.interface.d.ts +5 -0
- package/dist/common/validators/entity/entity.interface.js +3 -0
- package/dist/common/validators/entity/entity.interface.js.map +1 -0
- package/dist/common/validators/entity/index.d.ts +4 -0
- package/dist/common/validators/entity/index.js +21 -0
- package/dist/common/validators/entity/index.js.map +1 -0
- package/dist/common/validators/index.d.ts +1 -0
- package/dist/common/validators/index.js +18 -0
- package/dist/common/validators/index.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +43 -5
package/README.md
CHANGED
|
@@ -1,2 +1,89 @@
|
|
|
1
1
|
# nestjs-common-tools
|
|
2
2
|
NestJS Common Tools
|
|
3
|
+
|
|
4
|
+
## Entity Validator
|
|
5
|
+
|
|
6
|
+
A custom validator for NestJS that validates if an entity exists in the database using TypeORM.
|
|
7
|
+
|
|
8
|
+
## Setup
|
|
9
|
+
|
|
10
|
+
1. Register the validator and setup class-validator container in your application:
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
// main.ts
|
|
14
|
+
import { useContainer } from 'class-validator';
|
|
15
|
+
|
|
16
|
+
async function bootstrap() {
|
|
17
|
+
const app = await NestFactory.create(AppModule);
|
|
18
|
+
|
|
19
|
+
useContainer(app.select(AppModule), {
|
|
20
|
+
fallbackOnErrors: true
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
await app.listen(3000);
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
2. Register the validator constraint in your `AppModule`:
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
// app.module.ts
|
|
31
|
+
import { Module } from '@nestjs/common';
|
|
32
|
+
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
33
|
+
import { EntityConstraint } from './common/validators/entity/entity.constraint';
|
|
34
|
+
|
|
35
|
+
@Module({
|
|
36
|
+
providers: [
|
|
37
|
+
EntityConstraint,
|
|
38
|
+
// other providers
|
|
39
|
+
]
|
|
40
|
+
})
|
|
41
|
+
export class AppModule {}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { IsEntity } from '@russ-b/nestjs-common-tools/common/validators/entity';
|
|
48
|
+
|
|
49
|
+
export class UserDto {
|
|
50
|
+
// Validate single entity
|
|
51
|
+
@IsEntity(User)
|
|
52
|
+
userId: string;
|
|
53
|
+
|
|
54
|
+
// Validate array of entities
|
|
55
|
+
@IsEntity(Role, { each: true })
|
|
56
|
+
roleIds: string[];
|
|
57
|
+
|
|
58
|
+
// Custom property validation
|
|
59
|
+
@IsEntity(User, { property: 'customId' })
|
|
60
|
+
userCustomId: string;
|
|
61
|
+
|
|
62
|
+
// Disable UUID validation
|
|
63
|
+
@IsEntity(User, { isUuid: false })
|
|
64
|
+
numericId: number;
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Options
|
|
69
|
+
|
|
70
|
+
| Option | Type | Default | Description |
|
|
71
|
+
|-----------|-----------|---------|----------------------------------------------|
|
|
72
|
+
| isUuid | boolean | false | Validate if the value is a valid UUID |
|
|
73
|
+
| each | boolean | false | Apply validation to each item in array |
|
|
74
|
+
| property | string | 'id' | Database property to check against |
|
|
75
|
+
|
|
76
|
+
## Example
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
// user.dto.ts
|
|
80
|
+
export class AssignRolesDto {
|
|
81
|
+
@IsArray()
|
|
82
|
+
@IsEntity(Role, {
|
|
83
|
+
each: true,
|
|
84
|
+
isUuid: true,
|
|
85
|
+
property: 'id'
|
|
86
|
+
})
|
|
87
|
+
roleIds: string[];
|
|
88
|
+
}
|
|
89
|
+
```
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseClientGrpcProxy = void 0;
|
|
4
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
5
|
+
const grpc_js_1 = require("@grpc/grpc-js");
|
|
6
|
+
const is_grpc_struct_decorator_1 = require("@common/decorators/is-grpc-struct.decorator");
|
|
7
|
+
const grpc_struct_transformer_1 = require("@common/transformers/grpc-struct.transformer");
|
|
8
|
+
const rxjs_1 = require("rxjs");
|
|
9
|
+
class BaseClientGrpcProxy extends microservices_1.ClientGrpcProxy {
|
|
10
|
+
getService(name) {
|
|
11
|
+
const service = super.getService(name);
|
|
12
|
+
const packageDefinition = this.getClient(name);
|
|
13
|
+
return new Proxy(service, {
|
|
14
|
+
get: (target, prop) => {
|
|
15
|
+
if (typeof target[prop] !== 'function') {
|
|
16
|
+
return target[prop];
|
|
17
|
+
}
|
|
18
|
+
return (...args) => {
|
|
19
|
+
const methodName = Object.keys(target).find((key) => key.toLowerCase() === prop.toLowerCase() && key !== prop);
|
|
20
|
+
if (args.length > 0 && args[0] && typeof args[0] === 'object') {
|
|
21
|
+
const dto = args[0];
|
|
22
|
+
const structFields = Reflect.getMetadata(is_grpc_struct_decorator_1.IS_GRPC_STRUCT_KEY, dto.constructor?.prototype) || [];
|
|
23
|
+
if (structFields.length) {
|
|
24
|
+
const modified = { ...dto };
|
|
25
|
+
structFields.forEach((field) => {
|
|
26
|
+
if (modified[field]) {
|
|
27
|
+
modified[field] = (0, grpc_struct_transformer_1.fromObjectToStruct)(modified[field]);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
args[0] = modified;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return target[prop].apply(target, args).pipe((0, rxjs_1.map)((response) => {
|
|
34
|
+
const fields = packageDefinition[name].service?.[methodName].responseType.type
|
|
35
|
+
?.field || [];
|
|
36
|
+
const structFields = fields
|
|
37
|
+
.filter((field) => field.typeName === 'google.protobuf.Struct')
|
|
38
|
+
.map((f) => f.name) || [];
|
|
39
|
+
if (structFields.length) {
|
|
40
|
+
structFields.forEach((field) => {
|
|
41
|
+
response[field] = (0, grpc_struct_transformer_1.fromStructToObject)(response[field]);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return response;
|
|
45
|
+
}));
|
|
46
|
+
};
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
serializeError(err) {
|
|
51
|
+
if (err.code === grpc_js_1.status.UNAVAILABLE) {
|
|
52
|
+
setTimeout(() => process.exit(69), 2000);
|
|
53
|
+
}
|
|
54
|
+
return new microservices_1.RpcException(err);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.BaseClientGrpcProxy = BaseClientGrpcProxy;
|
|
58
|
+
//# sourceMappingURL=base-client-grpc.proxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-client-grpc.proxy.js","sourceRoot":"","sources":["../../../src/common/grpc/base-client-grpc.proxy.ts"],"names":[],"mappings":";;;AAAA,yDAAsE;AACtE,2CAAuC;AACvC,0FAAiF;AACjF,0FAGsD;AACtD,+BAA2B;AAS3B,MAAa,mBAAoB,SAAQ,+BAAe;IACtD,UAAU,CAAe,IAAY;QACnC,MAAM,OAAO,GAAG,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAEvC,MAAM,iBAAiB,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QAE/C,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE;YACxB,GAAG,EAAE,CAAC,MAAW,EAAE,IAAY,EAAE,EAAE;gBACjC,IAAI,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,UAAU,EAAE,CAAC;oBACvC,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC;gBACtB,CAAC;gBAED,OAAO,CAAC,GAAG,IAAW,EAAE,EAAE;oBAExB,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CACzC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,IAAI,CAAC,WAAW,EAAE,IAAI,GAAG,KAAK,IAAI,CAClE,CAAC;oBAEF,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ,EAAE,CAAC;wBAC9D,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;wBACpB,MAAM,YAAY,GAChB,OAAO,CAAC,WAAW,CACjB,6CAAkB,EAClB,GAAG,CAAC,WAAW,EAAE,SAAS,CAC3B,IAAI,EAAE,CAAC;wBAEV,IAAI,YAAY,CAAC,MAAM,EAAE,CAAC;4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C,GAAQ;QAC/B,IAAI,GAAG,CAAC,IAAI,KAAK,gBAAM,CAAC,WAAW,EAAE,CAAC;YACpC,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;QAC3C,CAAC;QAED,OAAO,IAAI,4BAAY,CAAC,GAAG,CAAC,CAAC;IAC/B,CAAC;CACF;AAxED,kDAwEC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { DescriptorProto, FieldDescriptorProto } from 'ts-proto/build/protos';
|
|
2
|
+
export interface IGrpcMethod {
|
|
3
|
+
requestType: {
|
|
4
|
+
type: DescriptorProto;
|
|
5
|
+
};
|
|
6
|
+
responseType: {
|
|
7
|
+
type: DescriptorProto;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export interface IMethodDescriptor {
|
|
11
|
+
request: FieldDescriptorProto[];
|
|
12
|
+
response: FieldDescriptorProto[];
|
|
13
|
+
}
|
|
14
|
+
interface IStruct {
|
|
15
|
+
fields?: {
|
|
16
|
+
[k: string]: IValue;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
interface IListValue {
|
|
20
|
+
values?: IValue[];
|
|
21
|
+
}
|
|
22
|
+
export interface IValue {
|
|
23
|
+
kind?: string;
|
|
24
|
+
nullValue?: 0;
|
|
25
|
+
numberValue?: number;
|
|
26
|
+
stringValue?: string;
|
|
27
|
+
boolValue?: boolean;
|
|
28
|
+
structValue?: IStruct;
|
|
29
|
+
listValue?: IListValue;
|
|
30
|
+
}
|
|
31
|
+
export interface StructObject {
|
|
32
|
+
[key: string]: any;
|
|
33
|
+
}
|
|
34
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-grpc.interface.js","sourceRoot":"","sources":["../../../src/common/grpc/client-grpc.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './is-grpc-struct.decorator';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./is-grpc-struct.decorator"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/common/grpc/decorators/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6DAA2C"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IS_GRPC_STRUCT_KEY = void 0;
|
|
4
|
+
exports.IsGrpcStruct = IsGrpcStruct;
|
|
5
|
+
require("reflect-metadata");
|
|
6
|
+
exports.IS_GRPC_STRUCT_KEY = Symbol('IsGrpcStruct');
|
|
7
|
+
function IsGrpcStruct() {
|
|
8
|
+
return (target, propertyKey) => {
|
|
9
|
+
const existingKeys = Reflect.getMetadata(exports.IS_GRPC_STRUCT_KEY, target) || [];
|
|
10
|
+
Reflect.defineMetadata(exports.IS_GRPC_STRUCT_KEY, [...existingKeys, propertyKey], target);
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=is-grpc-struct.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-grpc-struct.decorator.js","sourceRoot":"","sources":["../../../../src/common/grpc/decorators/is-grpc-struct.decorator.ts"],"names":[],"mappings":";;;AAIA,oCASC;AAbD,4BAA0B;AAEb,QAAA,kBAAkB,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;AAEzD,SAAgB,YAAY;IAC1B,OAAO,CAAC,MAAc,EAAE,WAA4B,EAAE,EAAE;QACtD,MAAM,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,0BAAkB,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;QAC3E,OAAO,CAAC,cAAc,CACpB,0BAAkB,EAClB,CAAC,GAAG,YAAY,EAAE,WAAW,CAAC,EAC9B,MAAM,CACP,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './rpc-exception.filter';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./rpc-exception.filter"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/common/grpc/filters/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAuC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
|
|
2
|
+
import { RpcException } from '@nestjs/microservices';
|
|
3
|
+
export declare class RpcExceptionFilter implements ExceptionFilter {
|
|
4
|
+
private logger;
|
|
5
|
+
catch(exception: RpcException, host: ArgumentsHost): void;
|
|
6
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.RpcExceptionFilter = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
12
|
+
let RpcExceptionFilter = class RpcExceptionFilter {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.logger = new common_1.Logger(this.constructor.name);
|
|
15
|
+
}
|
|
16
|
+
catch(exception, host) {
|
|
17
|
+
const ctx = host.switchToHttp();
|
|
18
|
+
const response = ctx.getResponse();
|
|
19
|
+
const error = exception.getError();
|
|
20
|
+
let status = common_1.HttpStatus.INTERNAL_SERVER_ERROR;
|
|
21
|
+
let message = 'Internal server error';
|
|
22
|
+
switch (error.code) {
|
|
23
|
+
case 1:
|
|
24
|
+
break;
|
|
25
|
+
case 3:
|
|
26
|
+
status = common_1.HttpStatus.BAD_REQUEST;
|
|
27
|
+
break;
|
|
28
|
+
case 5:
|
|
29
|
+
status = common_1.HttpStatus.NOT_FOUND;
|
|
30
|
+
break;
|
|
31
|
+
case 7:
|
|
32
|
+
status = common_1.HttpStatus.FORBIDDEN;
|
|
33
|
+
break;
|
|
34
|
+
case 12:
|
|
35
|
+
status = common_1.HttpStatus.NOT_IMPLEMENTED;
|
|
36
|
+
break;
|
|
37
|
+
case 16:
|
|
38
|
+
status = common_1.HttpStatus.UNAUTHORIZED;
|
|
39
|
+
break;
|
|
40
|
+
default:
|
|
41
|
+
this.logger.error(error);
|
|
42
|
+
}
|
|
43
|
+
message = error.message || message;
|
|
44
|
+
let details = '';
|
|
45
|
+
try {
|
|
46
|
+
details = JSON.parse(error.details)['error'];
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
details = error.details;
|
|
50
|
+
}
|
|
51
|
+
response.status(status).json({
|
|
52
|
+
statusCode: status,
|
|
53
|
+
message: message,
|
|
54
|
+
error: common_1.HttpStatus[status],
|
|
55
|
+
details: details,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
exports.RpcExceptionFilter = RpcExceptionFilter;
|
|
60
|
+
exports.RpcExceptionFilter = RpcExceptionFilter = __decorate([
|
|
61
|
+
(0, common_1.Catch)(microservices_1.RpcException)
|
|
62
|
+
], RpcExceptionFilter);
|
|
63
|
+
//# sourceMappingURL=rpc-exception.filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc-exception.filter.js","sourceRoot":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C,CAAC,OAAO,CAAC,CAAC;QAC/C,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;QAC1B,CAAC;QAED,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC;YAC3B,UAAU,EAAE,MAAM;YAClB,OAAO,EAAE,OAAO;YAChB,KAAK,EAAE,mBAAU,CAAC,MAAM,CAAC;YACzB,OAAO,EAAE,OAAO;SACjB,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AAlDY,gDAAkB;6BAAlB,kBAAkB;IAD9B,IAAA,cAAK,EAAC,4BAAY,CAAC;GACP,kBAAkB,CAkD9B"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./decorators"), exports);
|
|
18
|
+
__exportStar(require("./filters"), exports);
|
|
19
|
+
__exportStar(require("./proxies"), exports);
|
|
20
|
+
__exportStar(require("./transformers"), exports);
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/common/grpc/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAC7B,4CAA0B;AAC1B,4CAA0B;AAC1B,iDAA+B"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ClientGrpcProxy } from '@nestjs/microservices';
|
|
2
|
+
export declare class BaseClientGrpcProxy extends ClientGrpcProxy {
|
|
3
|
+
getService<T extends {}>(name: string): T;
|
|
4
|
+
private getMethodName;
|
|
5
|
+
private getMethodFields;
|
|
6
|
+
protected serializeError(err: any): any;
|
|
7
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseClientGrpcProxy = void 0;
|
|
4
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
5
|
+
const grpc_js_1 = require("@grpc/grpc-js");
|
|
6
|
+
const decorators_1 = require("../decorators");
|
|
7
|
+
const rxjs_1 = require("rxjs");
|
|
8
|
+
const transformers_1 = require("../transformers");
|
|
9
|
+
class BaseClientGrpcProxy extends microservices_1.ClientGrpcProxy {
|
|
10
|
+
getService(name) {
|
|
11
|
+
const service = super.getService(name);
|
|
12
|
+
const packageDefinition = this.getClient(name);
|
|
13
|
+
const methodFields = packageDefinition[name];
|
|
14
|
+
return new Proxy(service, {
|
|
15
|
+
get: (target, prop) => {
|
|
16
|
+
if (typeof target[prop] !== 'function') {
|
|
17
|
+
return target[prop];
|
|
18
|
+
}
|
|
19
|
+
return (...args) => {
|
|
20
|
+
const methodName = this.getMethodName(target, prop);
|
|
21
|
+
const fields = this.getMethodFields(methodFields, methodName);
|
|
22
|
+
if (args.length > 0 && args[0] && typeof args[0] === 'object') {
|
|
23
|
+
const dto = args[0];
|
|
24
|
+
const structFields = Reflect.getMetadata(decorators_1.IS_GRPC_STRUCT_KEY, dto.constructor?.prototype) || [];
|
|
25
|
+
if (structFields.length) {
|
|
26
|
+
const modified = { ...dto };
|
|
27
|
+
structFields.forEach((field) => {
|
|
28
|
+
if (modified[field]) {
|
|
29
|
+
modified[field] = transformers_1.StructTransformer.toObject(modified[field]);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
args[0] = modified;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return target[prop].apply(target, args).pipe((0, rxjs_1.map)((response) => {
|
|
36
|
+
const structFields = fields.response
|
|
37
|
+
.filter((field) => field.typeName === 'google.protobuf.Struct')
|
|
38
|
+
.map((f) => f.name) || [];
|
|
39
|
+
if (structFields.length) {
|
|
40
|
+
structFields.forEach((field) => {
|
|
41
|
+
response[field] = transformers_1.StructTransformer.toObject(response[field]);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return response;
|
|
45
|
+
}));
|
|
46
|
+
};
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
getMethodName(target, prop) {
|
|
51
|
+
const methodName = Object.keys(target).find((key) => key.toLowerCase() === prop.toLowerCase() && key !== prop);
|
|
52
|
+
if (!methodName) {
|
|
53
|
+
throw new Error(`Undefined methodName ${prop}`);
|
|
54
|
+
}
|
|
55
|
+
return methodName;
|
|
56
|
+
}
|
|
57
|
+
getMethodFields(service, methodName) {
|
|
58
|
+
const method = service?.[methodName];
|
|
59
|
+
return {
|
|
60
|
+
request: method.requestType.type?.field,
|
|
61
|
+
response: method.responseType.type?.field,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
serializeError(err) {
|
|
65
|
+
if (err.code === grpc_js_1.status.UNAVAILABLE) {
|
|
66
|
+
setTimeout(() => process.exit(69), 2000);
|
|
67
|
+
}
|
|
68
|
+
return new microservices_1.RpcException(err);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.BaseClientGrpcProxy = BaseClientGrpcProxy;
|
|
72
|
+
//# sourceMappingURL=base-client-grpc.proxy.js.map
|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G,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,IAAI,CAAC,WAAW,EAAE,IAAI,GAAG,KAAK,IAAI,CAClE,CAAC;QAEF,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,EAAE,CAAC,CAAC;QAClD,CAAC;QAED,OAAO,UAAU,CAAC;IACpB,CAAC;IAEO,eAAe,CACrB,OAAoC,EACpC,UAAkB;QAElB,MAAM,MAAM,GAAG,OAAO,EAAE,CAAC,UAAU,CAAC,CAAC;QAErC,OAAO;YACL,OAAO,EAAE,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,KAAK;YACvC,QAAQ,EAAE,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK;SAC1C,CAAC;IACJ,CAAC;IAES,cAAc,CAAC,GAAQ;QAC/B,IAAI,GAAG,CAAC,IAAI,KAAK,gBAAM,CAAC,WAAW,EAAE,CAAC;YACpC,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;QAC3C,CAAC;QAED,OAAO,IAAI,4BAAY,CAAC,GAAG,CAAC,CAAC;IAC/B,CAAC;CACF;AA/FD,kDA+FC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './base-client-grpc.proxy';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./base-client-grpc.proxy"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/common/grpc/proxies/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2DAAyC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
|
|
2
|
+
import { RpcException } from '@nestjs/microservices';
|
|
3
|
+
export declare class RpcExceptionFilter implements ExceptionFilter {
|
|
4
|
+
private logger;
|
|
5
|
+
catch(exception: RpcException, host: ArgumentsHost): void;
|
|
6
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.RpcExceptionFilter = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
12
|
+
let RpcExceptionFilter = class RpcExceptionFilter {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.logger = new common_1.Logger(this.constructor.name);
|
|
15
|
+
}
|
|
16
|
+
catch(exception, host) {
|
|
17
|
+
const ctx = host.switchToHttp();
|
|
18
|
+
const response = ctx.getResponse();
|
|
19
|
+
const error = exception.getError();
|
|
20
|
+
let status = common_1.HttpStatus.INTERNAL_SERVER_ERROR;
|
|
21
|
+
let message = 'Internal server error';
|
|
22
|
+
switch (error.code) {
|
|
23
|
+
case 1:
|
|
24
|
+
break;
|
|
25
|
+
case 3:
|
|
26
|
+
status = common_1.HttpStatus.BAD_REQUEST;
|
|
27
|
+
break;
|
|
28
|
+
case 5:
|
|
29
|
+
status = common_1.HttpStatus.NOT_FOUND;
|
|
30
|
+
break;
|
|
31
|
+
case 7:
|
|
32
|
+
status = common_1.HttpStatus.FORBIDDEN;
|
|
33
|
+
break;
|
|
34
|
+
case 12:
|
|
35
|
+
status = common_1.HttpStatus.NOT_IMPLEMENTED;
|
|
36
|
+
break;
|
|
37
|
+
case 16:
|
|
38
|
+
status = common_1.HttpStatus.UNAUTHORIZED;
|
|
39
|
+
break;
|
|
40
|
+
default:
|
|
41
|
+
this.logger.error(error);
|
|
42
|
+
}
|
|
43
|
+
message = error.message || message;
|
|
44
|
+
let details = '';
|
|
45
|
+
try {
|
|
46
|
+
details = JSON.parse(error.details)['error'];
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
details = error.details;
|
|
50
|
+
}
|
|
51
|
+
response.status(status).json({
|
|
52
|
+
statusCode: status,
|
|
53
|
+
message: message,
|
|
54
|
+
error: common_1.HttpStatus[status],
|
|
55
|
+
details: details,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
exports.RpcExceptionFilter = RpcExceptionFilter;
|
|
60
|
+
exports.RpcExceptionFilter = RpcExceptionFilter = __decorate([
|
|
61
|
+
(0, common_1.Catch)(microservices_1.RpcException)
|
|
62
|
+
], RpcExceptionFilter);
|
|
63
|
+
//# sourceMappingURL=rpc-exception.filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc-exception.filter.js","sourceRoot":"","sources":["../../../src/common/grpc/rpc-exception.filter.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAMwB;AACxB,yDAAqD;AAI9C,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IAAxB;QACG,WAAM,GAAG,IAAI,eAAM,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAiDrD,CAAC;IA/CC,KAAK,CAAC,SAAuB,EAAE,IAAmB;QAChD,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;QAChC,MAAM,QAAQ,GAAG,GAAG,CAAC,WAAW,EAAY,CAAC;QAE7C,MAAM,KAAK,GAAQ,SAAS,CAAC,QAAQ,EAAE,CAAC;QACxC,IAAI,MAAM,GAAG,mBAAU,CAAC,qBAAqB,CAAC;QAC9C,IAAI,OAAO,GAAG,uBAAuB,CAAC;QAGtC,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;YACnB,KAAK,CAAC;gBACJ,MAAM;YACR,KAAK,CAAC;gBACJ,MAAM,GAAG,mBAAU,CAAC,WAAW,CAAC;gBAChC,MAAM;YACR,KAAK,CAAC;gBACJ,MAAM,GAAG,mBAAU,CAAC,SAAS,CAAC;gBAC9B,MAAM;YACR,KAAK,CAAC;gBACJ,MAAM,GAAG,mBAAU,CAAC,SAAS,CAAC;gBAC9B,MAAM;YACR,KAAK,EAAE;gBACL,MAAM,GAAG,mBAAU,CAAC,eAAe,CAAC;gBACpC,MAAM;YACR,KAAK,EAAE;gBACL,MAAM,GAAG,mBAAU,CAAC,YAAY,CAAC;gBACjC,MAAM;YACR;gBACE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC7B,CAAC;QAED,OAAO,GAAG,KAAK,CAAC,OAAO,IAAI,OAAO,CAAC;QAEnC,IAAI,OAAO,GAAG,EAAE,CAAC;QACjB,IAAI,CAAC;YACH,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC;QAC/C,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;QAC1B,CAAC;QAED,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC;YAC3B,UAAU,EAAE,MAAM;YAClB,OAAO,EAAE,OAAO;YAChB,KAAK,EAAE,mBAAU,CAAC,MAAM,CAAC;YACzB,OAAO,EAAE,OAAO;SACjB,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AAlDY,gDAAkB;6BAAlB,kBAAkB;IAD9B,IAAA,cAAK,EAAC,4BAAY,CAAC;GACP,kBAAkB,CAkD9B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './struct';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./struct"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/common/grpc/transformers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./struct.interface"), exports);
|
|
18
|
+
__exportStar(require("./struct.transformer"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/common/grpc/transformers/struct/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,uDAAqC"}
|