New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@redhare/interceptors

Package Overview
Dependencies
Maintainers
3
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@redhare/interceptors

NestJS provide a Interceptor concept. Interceptors have a set of useful capabilities which are inspired by the Aspect Oriented Programming (AOP) technique. This package function is to collect commonly used interceptor.

latest
npmnpm
Version
0.0.2
Version published
Weekly downloads
0
-100%
Maintainers
3
Weekly downloads
 
Created
Source

@infra-node-kit/interceptors

NestJS provide a Interceptor concept. Interceptors have a set of useful capabilities which are inspired by the Aspect Oriented Programming (AOP) technique. This package function is to collect commonly used interceptor.

Installation

yarn add '@infra-node-kit/interceptors'

StandardResponseInterceptor

Introduce

This interceptor wrap normal return value to data field of standard response. The standard response as follow:

{
  data,
  code: 0,
  message: 'OK',
  status: 200
}
  • Hint: if you has the demand of custom the special response format, you can use @Res() response to skip the StandardResponseInterceptor.

Usage

Import and call app.use

import { StandardResponseInterceptor } from '@infra-node-kit/interceptors';

const app = await NestFactory(AppModule);
...
app.use(new StandardResponseInterceptor());

await app.listen(3000);
  • Response example
@Controller('')
export class TestController {
  @Get()
  async test() {
    return {
      name: 'infra-node-kit'
    }
  }
}

the response will be:

{
  data: {
    name: 'infra-node-kit'
  },
  code: 0,
  message: 'OK',
  status: 200
}
  • Not work example
@Controller('')
export class TestController {
  @Get()
  async ResPassthrough(@Res() res: Response) {
    res.send({
      name: 'infra-node-kit'
    })
  }
}

the response will be:

{
  name: 'infra-node-kit'
}

Custom Option

GetStandardResponseInterceptor( options: IStandardResponseOptions )

interface IStandardResponseOptions {
  skipPaths?: string[] // the path in the skipPaths will ignore wrapper
  code?: number // the uniform code
  message?: string // the uniform message
}
import { GetStandardResponseInterceptor } from '@infra-node-kit/interceptors';

const app = await NestFactory(AppModule);
...
const StandardResponseInterceptor = GetStandardResponseInterceptor({
  skipPaths: ['/metrics'],
  code: 1,
  message: 'is success'
})
app.use(new StandardResponseInterceptor());

await app.listen(3000);

FAQs

Package last updated on 15 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts