@hodfords/nestjs-grpc-helper 10.1.0 β†’ 10.1.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.
Files changed (2) hide show
  1. package/README.md +97 -23
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,33 +1,46 @@
1
- # nestjs-grpc-helper
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
- ## Preinstall
4
- https://docs.nestjs.com/microservices/grpc
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>
5
8
 
6
- ```
7
- npm i --save @grpc/grpc-js @grpc/proto-loader
9
+ ## Pre-Installation πŸ”§
10
+
11
+ Before using nestjs-grpc-helper, you'll need to install the necessary gRPC dependencies. Follow
12
+ [the official NestJS documentation](https://docs.nestjs.com/microservices/grpc) for gRPC setup by installing the required packages:
13
+
14
+ ```bash
15
+ npm install @grpc/grpc-js @grpc/proto-loader --save
8
16
  ```
9
17
 
10
18
  ## Installation πŸ€–
11
19
 
12
- ```
20
+ Install the `nestjs-grpc-helper` package with:
21
+
22
+ ```bash
13
23
  npm install @hodfords/nestjs-grpc-helper --save
14
24
  ```
15
25
 
16
- Automatically generate proto file, add it to main.ts, before application starts.
26
+ Next, automatically generate the proto file and include it in main.ts before starting the application:
27
+
17
28
  ```typescript
18
29
  import { generateProtoService } from '@hodfords/nestjs-grpc-helper';
19
30
 
20
31
  generateProtoService(camelCase(env.APP_NAME), env.ROOT_PATH + '/../');
21
32
  ```
22
33
 
23
- Create microservices, it works similar to Controller.
24
- Response must be defined according to the rules of nestjs-response.
34
+ ## Usage πŸš€
35
+
36
+ ### Creating microservices
37
+
38
+ 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
39
 
26
40
  ```typescript
27
41
  @GrpcMicroservice()
28
42
  export class UserMicroservice {
29
- constructor(private userService: UserService) {
30
- }
43
+ constructor(private userService: UserService) {}
31
44
 
32
45
  @GrpcAction('Get user by id')
33
46
  @ResponseModel(UserResponse)
@@ -37,9 +50,62 @@ export class UserMicroservice {
37
50
  }
38
51
  ```
39
52
 
53
+ ### Create SDK
54
+
55
+ To generate a TypeScript SDK for your gRPC services, use the following command:
56
+
57
+ ```shell
58
+ npm run wz-command make-sdk <package-name> <folder>
59
+ ```
60
+
61
+ #### What this command does
62
+
63
+ This command will:
64
+
65
+ 1. **Collect all request and response types**: It gathers all `@GrpcValue` request and response types from your project.
66
+ 2. **Generate proto file**: Automatically generates the necessary proto files based on the collected types.
67
+ 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.
68
+
69
+ ### SDK usage
70
+
71
+ After publishing the SDK, other services can easily integrate it. Here’s an example of how to use the generated SDK
72
+
73
+ 1. **Import the sdk package**
74
+
75
+ 2. **Register the microservice module**: Configure the microservice in `AppModule` with the appropriate gRPC URL and timeout settings.
76
+
77
+ ```typescript
78
+ UserModule.register({
79
+ url: env.GRPC_URL,
80
+ timeout: 5000
81
+ });
82
+ ```
83
+
84
+ 3. **Use the SDK in another service**: Import the SDK and use it to interact with your gRPC services.
85
+
86
+ ```typescript
87
+ export class OtherService {
88
+ constructor(private userMicroservice: UserMicroservice) {}
89
+
90
+ async doTask(userId: string): Promise<void> {
91
+ const user = await this.userMicroservice.findUserById({ id: userId });
92
+ // Process user information as needed
93
+ }
94
+ }
95
+ ```
96
+
97
+ In this example, `OtherService` uses the `UserMicroservice` class from the SDK to call the `findUserById` method.
98
+
40
99
  ### Mock response
41
100
 
42
- Use faker as a method: `MockMethod`
101
+ To effectively generate and handle mock data in your application, you can use the `@MockMethod`, `@MockSample`, and `@MockNested` decorators.
102
+
103
+ ##### Generate dynamic data with `@MockMethod`
104
+
105
+ Use `@MockMethod` to apply Faker methods for generating random values.
106
+
107
+ For example, to create a random string of 10 characters
108
+
43
109
  ```typescript
44
110
  @Property({ type: String, required: false })
45
111
  @MockMethod('faker.datatype.string', [10])
@@ -47,7 +113,12 @@ Use faker as a method: `MockMethod`
47
113
  name: string;
48
114
  ```
49
115
 
50
- Use fixed values: `MockSample`
116
+ ##### Set fixed values with `@MockSample`
117
+
118
+ If you need to set a fixed value for a property, use the `@MockSample` decorator. This is useful for enumerations or other predefined values.
119
+
120
+ For example, to set a fixed user type
121
+
51
122
  ```typescript
52
123
  @Property({
53
124
  type: String,
@@ -59,7 +130,12 @@ Use fixed values: `MockSample`
59
130
  type: UserTypeEnum;
60
131
  ```
61
132
 
62
- Specifies the number of elements to return: `MockNested`
133
+ ##### Generate nested data
134
+
135
+ Use `@MockNested` to generate mock data for nested objects or arrays of nested objects.
136
+
137
+ For example, to create an array of 5 nested objects
138
+
63
139
  ```typescript
64
140
  @Property({ type: UserResponse, isArray: true })
65
141
  @IsArray()
@@ -69,21 +145,19 @@ Specifies the number of elements to return: `MockNested`
69
145
  users: UserResponse[];
70
146
  ```
71
147
 
72
- ## Create SDK
148
+ ### Document for GRPC
73
149
 
74
- Run this command to create the typescript sdk
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
150
+ You can go to `http://xyz/microservice-documents` to check and try to call the gRPC method
81
151
 
82
152
  ```typescript
83
153
  MicroserviceDocumentModule.register({
84
154
  isEnable: true,
85
155
  prefix: <app-prefix>,
86
- packageName: camelCase(<package-name>>),
156
+ packageName: camelCase(<package-name>),
87
157
  clientOptions: { ...microserviceGrpcConfig, customClass: CustomGrpcClient, transport: undefined }
88
158
  })
89
159
  ```
160
+
161
+ ## License πŸ“
162
+
163
+ This project is licensed under the MIT License
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hodfords/nestjs-grpc-helper",
3
- "version": "10.1.0",
3
+ "version": "10.1.1",
4
4
  "description": "A utility for simplifying gRPC integration and communication in NestJS applications",
5
5
  "author": "",
6
6
  "license": "UNLICENSED",