/nestjs-general-interceptor

Nest.js interceptor to make general API response

Primary LanguageJavaScript

nestjs-general-interceptor

This is a simple copy from NestJS document and StackOverflow answer.

but it works out of the box.

This package is a Nest.js interceptor, it makes your controllers response to this general format:

{
  "statusCode": 200,
  "message": "ok",
  "data": "<Your orgin return data>"
}

If your controller thrown an Exception, it does nothing like the documented base example.

Installation

You can install the package from npm registry. Installation is done using command npm install :

$ npm install nestjs-general-interceptor

Usage

Within your Nest.js main.ts, import and use this module like the example below:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { TransformInterceptor } from 'nestjs-general-interceptor';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  
  // This is for global, which wrappers all your controllers
  app.useGlobalInterceptors(new TransforInterceptor());
  
  await app.listen(3000);
}

bootstrap();