1.0.79 • Published 11 months ago

res-saas-gl-library v1.0.79

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

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:


In this version only the following microservices are available:

  • Auth
  • Documentation
  • Notification
  • Storage

Installation

$ pnpm install

If you dont want install from npm, you can install locally

$ pnpm install /route/to/local/folder/res-saas-gl-library

generate interfaces from proto files

$ pnpm run generate:proto

create build

$ pnpm run build

upgrading version

$ pnpm version patch

publish package

$ pnpm publish

Usage

Install Library

$ pnpm install saas-grpc-library

Example 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

License

Nest is MIT licensed.

1.0.79

11 months ago

1.0.78

11 months ago

1.0.77

11 months ago

1.0.76

11 months ago

1.0.75

11 months ago

1.0.74

11 months ago

1.0.73

11 months ago

1.0.72

11 months ago

1.0.71

11 months ago

1.0.69

11 months ago

1.0.68

11 months ago

1.0.67

11 months ago

1.0.66

11 months ago

1.0.65

11 months ago

1.0.64

11 months ago

1.0.63

11 months ago

1.0.62

11 months ago

1.0.61

11 months ago

1.0.60

11 months ago

1.0.59

11 months ago

1.0.58

11 months ago

1.0.57

11 months ago

1.0.56

11 months ago

1.0.55

11 months ago

1.0.54

11 months ago

1.0.52

11 months ago

1.0.51

11 months ago

1.0.50

11 months ago

1.0.49

11 months ago

1.0.48

12 months ago

1.0.47

12 months ago

1.0.46

12 months ago

1.0.45

12 months ago

1.0.44

12 months ago

1.0.43

12 months ago

1.0.42

12 months ago

1.0.41

12 months ago

1.0.40

12 months ago

1.0.39

12 months ago

1.0.38

12 months ago

1.0.37

12 months ago

1.0.36

12 months ago

1.0.35

12 months ago

1.0.34

12 months ago

1.0.33

12 months ago

1.0.32

12 months ago

1.0.31

12 months ago

1.0.30

12 months ago

1.0.29

12 months ago