@hodfords/nestjs-grpc-helper 10.1.0 β 10.2.1
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
CHANGED
|
@@ -1,33 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="http://opensource.hodfords.uk" target="blank"><img src="https://opensource.hodfords.uk/img/logo.svg" width="320" alt="Hodfords Logo" /></a>
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
```
|
|
7
|
-
npm i --save @grpc/grpc-js @grpc/proto-loader
|
|
8
|
-
```
|
|
5
|
+
<p align="center">
|
|
6
|
+
<b>nestjs-grpc-helper</b> simplifies gRPC integration in NestJS, allowing seamless communication between services. It enables easy setup of gRPC clients and servers, and supports building SDK packages that any service can import and use, ensuring consistent API interaction across your microservices architecture.
|
|
7
|
+
</p>
|
|
9
8
|
|
|
10
9
|
## Installation π€
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
Install the `nestjs-grpc-helper` package with:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
13
14
|
npm install @hodfords/nestjs-grpc-helper --save
|
|
14
15
|
```
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
Next, automatically generate the proto file and include it in main.ts before starting the application:
|
|
18
|
+
|
|
17
19
|
```typescript
|
|
18
20
|
import { generateProtoService } from '@hodfords/nestjs-grpc-helper';
|
|
19
21
|
|
|
20
22
|
generateProtoService(camelCase(env.APP_NAME), env.ROOT_PATH + '/../');
|
|
21
23
|
```
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
## Usage π
|
|
26
|
+
|
|
27
|
+
### Creating microservices
|
|
28
|
+
|
|
29
|
+
Create microservices using the `@GrpcMicroservice` decorator, similar to how you would use a Controller. Ensure that the response adheres to the [nestjs-response](https://www.npmjs.com/package/@hodfords/nestjs-response) rules:
|
|
25
30
|
|
|
26
31
|
```typescript
|
|
27
32
|
@GrpcMicroservice()
|
|
28
33
|
export class UserMicroservice {
|
|
29
|
-
constructor(private userService: UserService) {
|
|
30
|
-
}
|
|
34
|
+
constructor(private userService: UserService) {}
|
|
31
35
|
|
|
32
36
|
@GrpcAction('Get user by id')
|
|
33
37
|
@ResponseModel(UserResponse)
|
|
@@ -37,9 +41,62 @@ export class UserMicroservice {
|
|
|
37
41
|
}
|
|
38
42
|
```
|
|
39
43
|
|
|
44
|
+
### Create SDK
|
|
45
|
+
|
|
46
|
+
To generate a TypeScript SDK for your gRPC services, use the following command:
|
|
47
|
+
|
|
48
|
+
```shell
|
|
49
|
+
npm run wz-command make-sdk <package-name> <folder>
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
#### What this command does
|
|
53
|
+
|
|
54
|
+
This command will:
|
|
55
|
+
|
|
56
|
+
1. **Collect all request and response types**: It gathers all `@GrpcValue` request and response types from your project.
|
|
57
|
+
2. **Generate proto file**: Automatically generates the necessary proto files based on the collected types.
|
|
58
|
+
3. **Create JavaScript Package**: Packages the generated code into a JavaScript SDK. The SDK will be published using the name and version specified in your package.json, making it available for other services to import and use. The arguments, response structure, and method names remain consistent with the definitions in your gRPC service, ensuring seamless integration and functionality across services.
|
|
59
|
+
|
|
60
|
+
### SDK usage
|
|
61
|
+
|
|
62
|
+
After publishing the SDK, other services can easily integrate it. Hereβs an example of how to use the generated SDK
|
|
63
|
+
|
|
64
|
+
1. **Import the sdk package**
|
|
65
|
+
|
|
66
|
+
2. **Register the microservice module**: Configure the microservice in `AppModule` with the appropriate gRPC URL and timeout settings.
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
UserModule.register({
|
|
70
|
+
url: env.GRPC_URL,
|
|
71
|
+
timeout: 5000
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
3. **Use the SDK in another service**: Import the SDK and use it to interact with your gRPC services.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
export class OtherService {
|
|
79
|
+
constructor(private userMicroservice: UserMicroservice) {}
|
|
80
|
+
|
|
81
|
+
async doTask(userId: string): Promise<void> {
|
|
82
|
+
const user = await this.userMicroservice.findUserById({ id: userId });
|
|
83
|
+
// Process user information as needed
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
In this example, `OtherService` uses the `UserMicroservice` class from the SDK to call the `findUserById` method.
|
|
89
|
+
|
|
40
90
|
### Mock response
|
|
41
91
|
|
|
42
|
-
|
|
92
|
+
To effectively generate and handle mock data in your application, you can use the `@MockMethod`, `@MockSample`, and `@MockNested` decorators.
|
|
93
|
+
|
|
94
|
+
##### Generate dynamic data with `@MockMethod`
|
|
95
|
+
|
|
96
|
+
Use `@MockMethod` to apply Faker methods for generating random values.
|
|
97
|
+
|
|
98
|
+
For example, to create a random string of 10 characters
|
|
99
|
+
|
|
43
100
|
```typescript
|
|
44
101
|
@Property({ type: String, required: false })
|
|
45
102
|
@MockMethod('faker.datatype.string', [10])
|
|
@@ -47,7 +104,12 @@ Use faker as a method: `MockMethod`
|
|
|
47
104
|
name: string;
|
|
48
105
|
```
|
|
49
106
|
|
|
50
|
-
|
|
107
|
+
##### Set fixed values with `@MockSample`
|
|
108
|
+
|
|
109
|
+
If you need to set a fixed value for a property, use the `@MockSample` decorator. This is useful for enumerations or other predefined values.
|
|
110
|
+
|
|
111
|
+
For example, to set a fixed user type
|
|
112
|
+
|
|
51
113
|
```typescript
|
|
52
114
|
@Property({
|
|
53
115
|
type: String,
|
|
@@ -59,7 +121,12 @@ Use fixed values: `MockSample`
|
|
|
59
121
|
type: UserTypeEnum;
|
|
60
122
|
```
|
|
61
123
|
|
|
62
|
-
|
|
124
|
+
##### Generate nested data
|
|
125
|
+
|
|
126
|
+
Use `@MockNested` to generate mock data for nested objects or arrays of nested objects.
|
|
127
|
+
|
|
128
|
+
For example, to create an array of 5 nested objects
|
|
129
|
+
|
|
63
130
|
```typescript
|
|
64
131
|
@Property({ type: UserResponse, isArray: true })
|
|
65
132
|
@IsArray()
|
|
@@ -69,21 +136,19 @@ Specifies the number of elements to return: `MockNested`
|
|
|
69
136
|
users: UserResponse[];
|
|
70
137
|
```
|
|
71
138
|
|
|
72
|
-
|
|
139
|
+
### Document for GRPC
|
|
73
140
|
|
|
74
|
-
|
|
75
|
-
```shell
|
|
76
|
-
npm run wz-command make-sdk <package-name> <folder>
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
## Document for GRPC
|
|
80
|
-
You can go to `http://xyz/microservice-documents` to check and try to call the grpc method
|
|
141
|
+
You can go to `http://xyz/microservice-documents` to check and try to call the gRPC method
|
|
81
142
|
|
|
82
143
|
```typescript
|
|
83
144
|
MicroserviceDocumentModule.register({
|
|
84
145
|
isEnable: true,
|
|
85
146
|
prefix: <app-prefix>,
|
|
86
|
-
packageName: camelCase(<package-name
|
|
147
|
+
packageName: camelCase(<package-name>),
|
|
87
148
|
clientOptions: { ...microserviceGrpcConfig, customClass: CustomGrpcClient, transport: undefined }
|
|
88
149
|
})
|
|
89
150
|
```
|
|
151
|
+
|
|
152
|
+
## License π
|
|
153
|
+
|
|
154
|
+
This project is licensed under the MIT License
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"microservice-document.module.js","sourceRoot":"","sources":["../../../libs/documents/microservice-document.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAAuD;AACvD,yFAAoF;AAEpF,yDAAsD;AACtD,uDAAyD;AACzD,gDAAwB;AACxB,2EAAsE;
|
|
1
|
+
{"version":3,"file":"microservice-document.module.js","sourceRoot":"","sources":["../../../libs/documents/microservice-document.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAAuD;AACvD,yFAAoF;AAEpF,yDAAsD;AACtD,uDAAyD;AACzD,gDAAwB;AACxB,2EAAsE;AAG/D,IAAM,0BAA0B,kCAAhC,MAAM,0BAA0B;IACnC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAiC;QACnD,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;YACpB,OAAO;gBACH,MAAM,EAAE,4BAA0B;aACrC,CAAC;QACN,CAAC;QAED,IAAI,SAAS,GAAG,yBAAyB,CAAC;QAC1C,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;YACjB,SAAS,GAAG,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,EAAE,CAAC;QACjD,CAAC;QAGD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC3B,UAAU,CAAC,GAAG,EAAE;gBACZ,OAAO,CAAC;oBACJ,MAAM,EAAE,4BAA0B;oBAClC,SAAS,EAAE;wBACP;4BACI,OAAO,EAAE,kBAAkB;4BAC3B,QAAQ,EAAE,OAAO;yBACpB;wBACD,yCAAkB;qBACrB;oBACD,WAAW,EAAE,CAAC,iEAA8B,CAAC;oBAC7C,OAAO,EAAE;wBACL,6BAAa,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC,CAAC;wBAC5E,gCAAiB,CAAC,OAAO,CAAC;4BACtB,QAAQ,EAAE,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,aAAa,CAAC;4BAChD,UAAU,EAAE,IAAI;4BAChB,SAAS,EAAE,SAAS;yBACvB,CAAC;qBACL;iBACJ,CAAC,CAAC;YACP,CAAC,EAAE,OAAO,CAAC,WAAW,IAAI,GAAG,CAAC,CAAC;QACnC,CAAC,CAAC,CAAC;IACP,CAAC;CACJ,CAAA;AAtCY,gEAA0B;qCAA1B,0BAA0B;IADtC,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,0BAA0B,CAsCtC"}
|
package/helpers/mock.helper.js
CHANGED
|
@@ -1,32 +1,9 @@
|
|
|
1
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
3
|
exports.sample = sample;
|
|
27
4
|
const property_helper_1 = require("./property.helper");
|
|
28
5
|
const class_transformer_1 = require("class-transformer");
|
|
29
|
-
const
|
|
6
|
+
const faker_1 = require("@faker-js/faker");
|
|
30
7
|
const lodash_1 = require("lodash");
|
|
31
8
|
function getData(option) {
|
|
32
9
|
const { mock } = option;
|
|
@@ -40,7 +17,7 @@ function getData(option) {
|
|
|
40
17
|
return sample(option.type);
|
|
41
18
|
}
|
|
42
19
|
if (mock.method && mock.method.startsWith('faker.')) {
|
|
43
|
-
const method = (0, lodash_1.get)({ faker }, mock.method);
|
|
20
|
+
const method = (0, lodash_1.get)({ faker: faker_1.faker }, mock.method);
|
|
44
21
|
return method(...mock.args);
|
|
45
22
|
}
|
|
46
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock.helper.js","sourceRoot":"","sources":["../../../libs/helpers/mock.helper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mock.helper.js","sourceRoot":"","sources":["../../../libs/helpers/mock.helper.ts"],"names":[],"mappings":";;AAwBA,wBAWC;AAnCD,uDAAyD;AACzD,yDAAoD;AACpD,2CAAwC;AACxC,mCAA6B;AAG7B,SAAS,OAAO,CAAC,MAA0B;IACvC,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC;IACxB,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;QACd,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IACD,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;QACrB,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YACjB,OAAO,IAAI,KAAK,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;QAChF,CAAC;QACD,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IACD,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QAClD,MAAM,MAAM,GAAQ,IAAA,YAAG,EAAC,EAAE,KAAK,EAAL,aAAK,EAAE,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QAEhD,OAAO,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC;AACL,CAAC;AAED,SAAgB,MAAM,CAAI,GAAgB;IACtC,MAAM,IAAI,GAAG,EAAE,CAAC;IAChB,MAAM,UAAU,GAAG,IAAA,sCAAoB,EAAC,GAAG,CAAC,CAAC;IAC7C,KAAK,MAAM,QAAQ,IAAI,UAAU,EAAE,CAAC;QAChC,IAAI,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACvB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;QACnD,CAAC;aAAM,CAAC;YACJ,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC;QAClD,CAAC;IACL,CAAC;IACD,OAAO,IAAA,mCAAe,EAAC,GAAG,EAAE,IAAI,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PropertyOptionType, PropertyType } from '@hodfords/nestjs-grpc-helper';
|
|
2
|
-
export declare function getClassHasProperties():
|
|
2
|
+
export declare function getClassHasProperties(): MapIterator<Function>;
|
|
3
3
|
export declare function addPropertyToStorage(constructor: Function, propertyName: string, option: PropertyOptionType): void;
|
|
4
4
|
export declare function extractProperties(): Record<string, {
|
|
5
5
|
option: PropertyOptionType;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hodfords/nestjs-grpc-helper",
|
|
3
|
-
"version": "10.1
|
|
3
|
+
"version": "10.2.1",
|
|
4
4
|
"description": "A utility for simplifying gRPC integration and communication in NestJS applications",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -31,42 +31,46 @@
|
|
|
31
31
|
"cspell": "cspell",
|
|
32
32
|
"lint-staged": "lint-staged"
|
|
33
33
|
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@grpc/grpc-js": "^1.11.3",
|
|
36
|
+
"@grpc/proto-loader": "^0.7.13",
|
|
37
|
+
"@faker-js/faker": "^9.0.1"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@grpc/grpc-js": "^1.11.3",
|
|
41
|
+
"@grpc/proto-loader": "^0.7.13",
|
|
42
|
+
"@faker-js/faker": "^9.0.1"
|
|
43
|
+
},
|
|
34
44
|
"devDependencies": {
|
|
35
|
-
"@hodfords/nestjs-cls-translation": "10.1.
|
|
36
|
-
"@hodfords/nestjs-command": "10.1.
|
|
37
|
-
"@hodfords/nestjs-
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
40
|
-
"@nestjs/cli": "10.4.
|
|
41
|
-
"@nestjs/common": "10.4.
|
|
42
|
-
"@nestjs/core": "10.4.
|
|
43
|
-
"@nestjs/microservices": "10.4.
|
|
44
|
-
"@nestjs/platform-express": "10.4.
|
|
45
|
+
"@hodfords/nestjs-cls-translation": "10.1.1",
|
|
46
|
+
"@hodfords/nestjs-command": "10.1.1",
|
|
47
|
+
"@hodfords/nestjs-eslint-config": "10.0.4",
|
|
48
|
+
"@hodfords/nestjs-prettier-config": "10.0.1",
|
|
49
|
+
"@hodfords/nestjs-response": "10.2.2",
|
|
50
|
+
"@nestjs/cli": "10.4.5",
|
|
51
|
+
"@nestjs/common": "10.4.3",
|
|
52
|
+
"@nestjs/core": "10.4.3",
|
|
53
|
+
"@nestjs/microservices": "10.4.3",
|
|
54
|
+
"@nestjs/platform-express": "10.4.3",
|
|
45
55
|
"@nestjs/schematics": "10.1.4",
|
|
46
56
|
"@nestjs/serve-static": "4.0.2",
|
|
47
|
-
"@nestjs/swagger": "7.4.
|
|
48
|
-
"@nestjs/testing": "10.4.
|
|
57
|
+
"@nestjs/swagger": "7.4.2",
|
|
58
|
+
"@nestjs/testing": "10.4.3",
|
|
49
59
|
"@nestjs/typeorm": "10.0.2",
|
|
50
60
|
"@types/express": "4.17.21",
|
|
51
|
-
"@types/
|
|
52
|
-
"@types/jest": "29.5.12",
|
|
61
|
+
"@types/jest": "29.5.13",
|
|
53
62
|
"@types/lodash": "4.17.7",
|
|
54
|
-
"@types/node": "22.5.
|
|
63
|
+
"@types/node": "22.5.5",
|
|
55
64
|
"@types/supertest": "6.0.2",
|
|
56
|
-
"
|
|
57
|
-
"@typescript-eslint/parser": "8.3.0",
|
|
58
|
-
"auto-changelog": "2.4.0",
|
|
65
|
+
"auto-changelog": "2.5.0",
|
|
59
66
|
"class-transformer": "0.5.1",
|
|
60
67
|
"class-validator": "0.14.1",
|
|
61
|
-
"cspell": "8.14.
|
|
62
|
-
"eslint": "
|
|
63
|
-
"
|
|
64
|
-
"eslint-plugin-prettier": "5.2.1",
|
|
65
|
-
"faker": "6.6.6",
|
|
66
|
-
"husky": "9.1.5",
|
|
68
|
+
"cspell": "8.14.4",
|
|
69
|
+
"eslint": "9.11.0",
|
|
70
|
+
"husky": "9.1.6",
|
|
67
71
|
"is-ci": "3.0.1",
|
|
68
72
|
"jest": "29.7.0",
|
|
69
|
-
"lint-staged": "15.2.
|
|
73
|
+
"lint-staged": "15.2.10",
|
|
70
74
|
"lodash": "4.17.21",
|
|
71
75
|
"reflect-metadata": "0.2.2",
|
|
72
76
|
"rimraf": "6.0.1",
|
|
@@ -78,7 +82,7 @@
|
|
|
78
82
|
"ts-node": "10.9.2",
|
|
79
83
|
"tsconfig-paths": "4.2.0",
|
|
80
84
|
"typeorm": "0.3.20",
|
|
81
|
-
"typescript": "5.
|
|
85
|
+
"typescript": "5.6.2"
|
|
82
86
|
},
|
|
83
87
|
"jest": {
|
|
84
88
|
"moduleFileExtensions": [
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { Faker } from '@faker-js/faker';
|
|
1
2
|
type NestedKeyOf<ObjectType extends object> = {
|
|
2
3
|
[Key in keyof ObjectType & (string | number)]: ObjectType[Key] extends object ? `${Key}` | `${Key}.${NestedKeyOf<ObjectType[Key]>}` : `${Key}`;
|
|
3
4
|
}[keyof ObjectType & (string | number)];
|
|
4
|
-
export type MockMethodType = `faker.${NestedKeyOf<Faker
|
|
5
|
+
export type MockMethodType = `faker.${NestedKeyOf<Faker>}`;
|
|
5
6
|
export type MockOptionType = {
|
|
6
7
|
method?: MockMethodType;
|
|
7
8
|
args?: any[];
|