res-saas-gl-library v1.0.79
Description
Library for centralizing and abstracting work with .proto files and gRPC requests.
Prerequisites
Before starting, make sure you have the following installed in your local environment:
- Node.js (version 20 or higher).
In this version only the following microservices are available:
- Auth
- Documentation
- Notification
- Storage
Installation
$ pnpm installIf you dont want install from npm, you can install locally
$ pnpm install /route/to/local/folder/res-saas-gl-librarygenerate interfaces from proto files
$ pnpm run generate:protocreate build
$ pnpm run buildupgrading version
$ pnpm version patchpublish package
$ pnpm publishUsage
Install Library
$ pnpm install saas-grpc-libraryExample for using the library
Usage the Infisical Module:
- Create a global module as shown below:
//infisical.common.module.ts
import { Global, Module } from '@nestjs/common';
import { InfisicalService } from '../infisical.service';
@Global()
@Module({
providers: [InfisicalService],
exports: [InfisicalService],
})
export class InfisicalModule {}- In this module file create the Infisical Service:
//infisical.service.ts
import { Injectable, Logger } from '@nestjs/common';
import {
InfisicalClientService,
InfisicalClientInterface,
} from 'res-saas-gl-library';
import { env } from '../env/env';
@Injectable()
export class InfisicalService implements InfisicalClientInterface {
private infisicalClient: InfisicalClientService;
private logger = new Logger(InfisicalService.name);
constructor() {
this.initializeInfisicalClient();
}
private initializeInfisicalClient() {
const infisicalEnv = env.INFISICAL_ENV;
const infisicalProjectId = env.INFISICAL_PROJECT_ID;
const infisicalToken = env.INFISICAL_TOKEN;
const infisicalUrl = env.INFISICAL_URL;
const infisicalSecretPath = env.INFISICAL_SECRET_PATH;
this.infisicalClient = InfisicalClientService.getInstance(
infisicalEnv,
infisicalProjectId,
infisicalToken,
infisicalUrl,
infisicalSecretPath,
);
}
public async getSecrets(): Promise<Record<string, string>> {
return this.infisicalClient.getSecrets();
}
public async getSecret(key: string): Promise<string> {
const secrets = await this.infisicalClient.getSecrets();
const value = secrets[key];
if (!value) {
this.logger.warn(`Secret ${key} not found in Infisical.`);
}
return value;
}
}- Import the global module in the app module as shown below:
//app.module.ts
import { Module } from '@nestjs/common';
import { InfisicalModule, InfisicalService } from './common/infisical/infisical.common.module';
@Module({
imports: [InfisicalModule],
controllers: [],
providers: [InfisicalService],
})
export class AppModule {}Usage the Grpc Modules
- In the module that you want to use the library, import the library as shown below:
//auth-grpc.module.ts
import { AuthServiceClient, GRPCAuthModule } from 'saas-grpc-library';
@Module({
imports: [
GRPCAuthModule.register(process.env.GRPC_AUTH_DOMAIN),
],
providers: [ AuthServiceClient],
exports: [ AuthServiceClient],
})
export class AuthGrpcModule {}- Import the client in the service
//auth-grpc.service.ts
import {
AuthServiceClient,
GetPermissionsRequest,
GetUserProfileRequest,
SetServicesRequest,
} from 'saas-grpc-library';
@Injectable()
export class AuthGrpcService {
constructor(private readonly authServiceClient: AuthServiceClient) {}
getPermissions(data: GetPermissionsRequest) {
return this.authServiceClient.getPermissions(data);
}
setServices(request: SetServicesRequest) {
return this.authServiceClient.setServices(request);
}
getUserProfile(request: GetUserProfileRequest) {
return this.authServiceClient.getUserProfile(request);
}
}Usage in the guard
- Create a global module as shown below:
//auth.common.module.ts
import { Global, Module } from '@nestjs/common';
import { InfisicalService } from '../../config/infisical/infisical.service';
import { JwtModule, JwtService } from '@nestjs/jwt';
import {
PublicKeyService,
GRPCAuthModule,
AuthServiceClient,
} from 'res-saas-gl-library';
@Global()
@Module({
imports: [
JwtModule.register({}),
GRPCAuthModule.registerAsync({
inject: [InfisicalService],
useFactory: async (infisicalService: InfisicalService) => {
const url = await infisicalService.getSecret('GRPC_AUTH_DOMAIN');
return { url: url };
},
}),
],
controllers: [],
providers: [
{ provide: 'InfisicalService', useClass: InfisicalService },
{
provide: 'JwtService',
useClass: JwtService,
},
PublicKeyService,
AuthServiceClient,
],
exports: [
'InfisicalService',
'JwtService',
PublicKeyService,
AuthServiceClient,
],
})
export class AuthCommonModule {}- Import the global module in the app module as shown below:
//app.module.ts
import { Module } from '@nestjs/common';
import { AuthCommonModule } from './common/auth/auth.common.module';
@Module({
imports: [AuthCommonModule],
controllers: [],
providers: [],
})
export class AppModule {}Usage of the filters, loggingMiddleware, interceptors, formatResponses and formatErrors
- Import CommonModule in AppModule
//app.module.ts
import { Module } from '@nestjs/common';
import { CommonModule } from 'res-saas-gl-library';
@Module({
imports: [CommonModule],
controllers: [],
providers: [],
})
export class AppModule {}Stay in touch
- Author - Cute Digital Media
License
Nest is MIT licensed.
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago