Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hodfords-solutions/nestjs-grpc-helper
A utility for simplifying gRPC integration and communication in NestJS applications
https://github.com/hodfords-solutions/nestjs-grpc-helper
grpc nestjs nodejs sdk
Last synced: 1 day ago
JSON representation
A utility for simplifying gRPC integration and communication in NestJS applications
- Host: GitHub
- URL: https://github.com/hodfords-solutions/nestjs-grpc-helper
- Owner: hodfords-solutions
- Created: 2024-03-01T05:15:21.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-12-05T05:04:00.000Z (about 1 month ago)
- Last Synced: 2025-01-01T03:08:54.404Z (8 days ago)
- Topics: grpc, nestjs, nodejs, sdk
- Language: TypeScript
- Homepage: https://opensource.hodfords.uk/nestjs-grpc-helper
- Size: 1020 KB
- Stars: 44
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
nestjs-grpc-helper 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.## Installation π€
Install the `nestjs-grpc-helper` package with:
```bash
npm install @hodfords/nestjs-grpc-helper --save
```Next, automatically generate the proto file and include it in main.ts before starting the application:
```typescript
import { generateProtoService } from '@hodfords/nestjs-grpc-helper';generateProtoService(camelCase(env.APP_NAME), env.ROOT_PATH + '/../');
```## Usage π
### Creating microservices
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:
```typescript
@GrpcMicroservice()
export class UserMicroservice {
constructor(private userService: UserService) {}@GrpcAction('Get user by id')
@ResponseModel(UserResponse)
findUserById(@GrpcValue() dto: GetUserByIdDto): Promise {
return this.userService.findUserById(dto.userId);
}
}
```### Any Type
You can use any type if fixed types are not an option. However, since itβs passed as JSON, the performance may not be as optimal as with binary. Consider using binary if performance is a concern.
```typescript
@Property({ type: 'any', required: false })
@AnyType()
data: any;
```### Create SDK
To generate a TypeScript SDK for your gRPC services, use the following command:
```shell
npm run wz-command make-sdk
```#### What this command does
This command will:
1. **Collect all request and response types**: It gathers all `@GrpcValue` request and response types from your project.
2. **Generate proto file**: Automatically generates the necessary proto files based on the collected types.
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.### SDK usage
After publishing the SDK, other services can easily integrate it. Hereβs an example of how to use the generated SDK
1. **Import the sdk package**
2. **Register the microservice module**: Configure the microservice in `AppModule` with the appropriate gRPC URL and timeout settings.
```typescript
UserModule.register({
url: env.GRPC_URL,
timeout: 5000
});
```3. **Use the SDK in another service**: Import the SDK and use it to interact with your gRPC services.
```typescript
export class OtherService {
constructor(private userMicroservice: UserMicroservice) {}async doTask(userId: string): Promise {
const user = await this.userMicroservice.findUserById({ id: userId });
// Process user information as needed
}
}
```In this example, `OtherService` uses the `UserMicroservice` class from the SDK to call the `findUserById` method.
### Mock response
To effectively generate and handle mock data in your application, you can use the `@MockMethod`, `@MockSample`, and `@MockNested` decorators.
##### Generate dynamic data with `@MockMethod`
Use `@MockMethod` to apply Faker methods for generating random values.
For example, to create a random string of 10 characters
```typescript
@Property({ type: String, required: false })
@MockMethod('faker.datatype.string', [10])
@IsString()
name: string;
```##### Set fixed values with `@MockSample`
If you need to set a fixed value for a property, use the `@MockSample` decorator. This is useful for enumerations or other predefined values.
For example, to set a fixed user type
```typescript
@Property({
type: String,
enum: UserTypeEnum,
enumName: 'UserTypeEnum'
})
@MockSample(UserTypeEnum.STANDARD)
@IsEnum(UserTypeEnum)
type: UserTypeEnum;
```##### Generate nested data
Use `@MockNested` to generate mock data for nested objects or arrays of nested objects.
For example, to create an array of 5 nested objects
```typescript
@Property({ type: UserResponse, isArray: true })
@IsArray()
@ValidateNested()
@Type(() => UserResponse)
@MockNested(5)
users: UserResponse[];
```### Document for GRPC
You can go to `http://xyz/microservice-documents` to check and try to call the gRPC method
```typescript
MicroserviceDocumentModule.register({
isEnable: true,
prefix: ,
packageName: camelCase(),
clientOptions: { ...microserviceGrpcConfig, customClass: CustomGrpcClient, transport: undefined }
})
```## License π
This project is licensed under the MIT License