nest-base-lib
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

Nest Base Lib 🚀

nest-base-lib is a library that is complemented by the implementation of a clean architecture. It offers easy-to-use helpers, decorators, middleware, interceptors, filters, swagger documentation, external services such as mongoose and postgres.

The main objective is to decentralize the entire base library of the clean Nestjs architecture in order to have it available for several projects without having to copy all the files each time.

Installation 🛠️

Make sure you have access to the clean architecture repository developed for NestJS before installing this package to enjoy the pre-built tools.

# pnpm
$ pnpm install nest-base-lib

# npm
$ npm install nest-base-lib

# yarn
$ yarn add nest-base-lib

Get started ✨

To start using nest-base-lib, follow these basic steps:

  1. Initial Setup: Import and configure the necessary modules in your NestJS application.
  2. Using Decorators and Middleware: Apply the decorators and middleware provided by the library in your controllers and services.
  3. Swagger Documentation: Generate your API documentation using the included Swagger tools.

ErrorCodes and CodesDefinitions example:

// errors.ts
import { ErrorFromEnumObject } from 'nest-base-lib';

export enum ErrorCodes {
  // Add custom codes
  USER_NOT_FOUND = 'USER_NOT_FOUND',
  GENERAL_ERROR = 'GENERAL_ERROR',
}

export const ErrorCodesDefinition: ErrorFromEnumObject<typeof ErrorCodes> = {
  // Add custom codes definition
  USER_NOT_FOUND: {
    message: 'User not found',
    statusCode: HttpStatus.NOT_FOUND,
  },
  GENERAL_ERROR: {
    message: 'General error',
    statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
    description: 'Server error. For unhandled errors',
  },
};

/** Error codes are grouped */
export const GroupedGeneralErrors = {
  authCodes: [ErrorCodes.USER_NOT_FOUND],
};

AppModule Example Usage:

import {
  LoggerMiddleware,
  RequestIdMiddleware,
  AppLogger,
  BaseModule
} from 'nest-base-lib';

import {
  ErrorCodes,
  ErrorCodesDefinition,
  GroupedGeneralErrors
} from './errors';

@Module({
  imports: [
    // Configuration
    ConfigModule.forRoot({
      envFilePath: `./src/config/env/.env.${process.env.NODE_ENV}`,
      load: [config],
      isGlobal: true,
      validationSchema: ConfigSchema,
    }),

    // Base Module
    BaseModule.forRootAsync({
      inject: [config.KEY],
      useFactory: (configService: ConfigType<typeof config>) => ({
        configs: {
          LOGS_DAYS_PERSIST: configService.app.LOGS_DAYS_PERSIST,
          LOGS_SIZE_BY_FILE: configService.app.LOGS_SIZE_BY_FILE,
        },
        errors: {
          errorCodes: ErrorCodes,
          errorCodesDefinition: ErrorCodesDefinition,
          groupedGeneralErrors: GroupedGeneralErrors,
        },
      }),
    }),

    // Local Providers
    ProvidersModule,
    ApplicationLayerModule,
  ],
  controllers: [
    // Add your controllers here
  ],
  providers: [
    // Add your gateways here3
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerMiddleware).forRoutes('*');
    consumer.apply(RequestIdMiddleware).forRoutes('*');
  }
}

Contributing 🤝

We welcome contributions! Please read our contributing guidelines to get started.

Dependents (0)

Package Sidebar

Install

npm i nest-base-lib

Weekly Downloads

30

Version

1.2.0

License

UNLICENSED

Unpacked Size

730 kB

Total Files

291

Last publish

Collaborators

  • aggbo