GithubHelp home page GithubHelp logo

ctfdavis / nestjs-standard-http-response-shape Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 197 KB

NestJS HTTP response interceptor & exception filter for uniforming the response shape

License: MIT License

Shell 0.77% JavaScript 0.96% TypeScript 98.27%

nestjs-standard-http-response-shape's Introduction

NestJS Standard HTTP Response Shape

A package for nest.js applications that provides an HTTP response interceptor and exception filter for uniforming the response shape.

This package is heavily inspired by a blog post by Andrey Petrov titled "How I Design JSON API Responses", which discusses the benefits of having a standard response shape for JSON APIs. By using nestjs-standard-http-response-shape, developers can utilize a standardized response shape in nest.js applications, leading to more consistent and predictable API behavior.

Installation

# npm
npm install nestjs-standard-http-response-shape

# yarn
yarn add nestjs-standard-http-response-shape

# pnpm
pnpm add nestjs-standard-http-response-shape

Usage

To use this package in your NestJS application, simply register the FormattedResponseInterceptor and FormattedExceptionFilter providers in your application:

// main.ts
import { Reflector } from '@nestjs/core';
import { FormattedResponseInterceptor, FormattedExceptionFilter } from 'nestjs-standard-http-response-shape';
// ...

const reflector = new Reflector();
const adapterHost = app.get(HttpAdapterHost);
app.useGlobalInterceptors(new FormattedResponseInterceptor(reflector));
app.useGlobalFilters(new FormattedExceptionFilter(adapterHost, reflector));

The providers will intercept all successful responses and catch exceptions, and then format them into a standard shape.

You can use the FormattedMessages function to conveniently set the messages property for a particular route handler or to add formatted messages to an exception:

import { Controller, Get, HttpException } from '@nestjs/common';
import { FormattedMessages } from 'nestjs-standard-http-response-shape';

@Controller()
export class AppController {
  @Get()
  @FormattedMessages(['Hello, world!'])
  getHello() {
    return { message: 'Hello, world!' };
  }

  @Get('error')
  getError() {
    throw FormattedMessages(['An error occurred.'], new HttpException('An error occurred.', 500));
  }
}

In this example, the FormattedMessages function is used in two ways:

  1. As a decorator for the getHello route handler, it sets the messages property with the given array of messages. In this case, it sets the messages property to ['Hello, world!'].
  2. For the getError route handler, it is used differently. Instead of being a decorator, the FormattedMessages function is called directly with two arguments: an array of messages and an instance of HttpException. The function then adds the formatted messages to the exception by setting the metadata on a new instance of the exception. This new instance is then thrown in the getError route handler. In this case, the formatted messages are ['An error occurred.'].

Response Shape

The formatted response shape is defined by the following types:

enum Status {
    OK = 'ok',
    ERROR = 'error'
}

type NotUndefined = {} | null;

interface Formatted<T extends NotUndefined = NotUndefined> {
    status: Status;
    messages: string[];
    payload: T;
    code: number;
}

interface FormattedResponse<T extends NotUndefined> extends Formatted<T> {
    status: Status.OK;
}

interface FormattedException extends Formatted {
    status: Status.ERROR;
}

Examples

Successful responses will have a status of Status.OK and the payload will be included in the payload property. Error responses will have a status of Status.ERROR and any error messages will be included in the messages property.

Successful Response

// app.controller.ts
@Get('/hello')
@FormattedMessages(['This is a message.'])
// A string is also accepted:
// @FormattedMessages('This is a message.')
getHello() {
    return { message: 'Hello, world!' };
}
# GET /hello
{
    "status": "ok",
    "messages": [
        "This is a message."
    ],
    "payload": {
        "message": "Hello, world!"
    },
    "code": 200
}

Error Response

// app.controller.ts
@Get('/error')
getError() {
    throw FormattedMessages(
        ['An error occurred.'],
        new BadRequestException({ error: 'Error goes here.' }),
    );
    // alternatively, use HttpException:
    // throw FormattedMessages(['An error occurred.'], new HttpException({ error: 'Error goes here.' }, 500));
}
# GET /error
{
    "status": "error",
    "messages": [
        "An error occurred."
    ],
    "payload": {
        "error": "Error goes here."
    },
    "code": 500
}
Differentiation between payload and messages

As for error responses, the purpose of the payload field in error responses is to provide additional, structured information about the error, whereas messages is for human-readable error messages

Therefore, this library will not automatically add any error message to the messages property. Instead, the developer must explicitly add the error message to the messages property using the FormattedMessages function.

For example, the following code will not add the error message to the messages property:

// app.controller.ts
@Get('/error')
getError() {
    throw new BadRequestException('error string');
}
# GET /error
{
    "status": "error",
    "messages": [],
    "payload": {
      "statusCode": 400,
      "message": "error string",
      "error": "Bad Request"
    },
    "code": 400
}

As seen above, the library will add the string to the message property of the payload object. To add the error message to the messages property, the developer must explicitly add the error message to the messages property.

Platforms

nestjs-standard-http-response-shape supports both the Express and Fastify platforms.

Testing

The package is tested with both unit tests (/test) and e2e tests (/e2e).

nestjs-standard-http-response-shape's People

Contributors

ctfdavis avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.