1.0.79 • Published 3 months ago

res-saas-gl-library v1.0.79

Weekly downloads
-
License
MIT
Repository
github
Last release
3 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

3 months ago

1.0.78

3 months ago

1.0.77

3 months ago

1.0.76

3 months ago

1.0.75

3 months ago

1.0.74

3 months ago

1.0.73

3 months ago

1.0.72

3 months ago

1.0.71

3 months ago

1.0.69

3 months ago

1.0.68

3 months ago

1.0.67

3 months ago

1.0.66

3 months ago

1.0.65

4 months ago

1.0.64

4 months ago

1.0.63

4 months ago

1.0.62

4 months ago

1.0.61

4 months ago

1.0.60

4 months ago

1.0.59

4 months ago

1.0.58

4 months ago

1.0.57

4 months ago

1.0.56

4 months ago

1.0.55

4 months ago

1.0.54

4 months ago

1.0.52

4 months ago

1.0.51

4 months ago

1.0.50

4 months ago

1.0.49

4 months ago

1.0.48

4 months ago

1.0.47

4 months ago

1.0.46

4 months ago

1.0.45

4 months ago

1.0.44

4 months ago

1.0.43

4 months ago

1.0.42

4 months ago

1.0.41

4 months ago

1.0.40

4 months ago

1.0.39

4 months ago

1.0.38

4 months ago

1.0.37

4 months ago

1.0.36

4 months ago

1.0.35

4 months ago

1.0.34

4 months ago

1.0.33

4 months ago

1.0.32

4 months ago

1.0.31

4 months ago

1.0.30

4 months ago

1.0.29

4 months ago