Coder Social home page Coder Social logo

temarusanov / nestjs-response-structure Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 2.0 913 KB

NestJS response payload structure

License: MIT License

JavaScript 24.39% TypeScript 75.61%
nestjs nestjs-graphql graphql typeorm exception-filter graphql-js typescript

nestjs-response-structure's Introduction

NestJS Response Structure

GraphQL Types and interfaces for great response. The library provides types for the structured display of responses to the client.

Features

  • Structured types covered with TypeScript
  • data and error fields only without any extensions
  • Detailed error information including uuid, description, code, message and stacktrace
  • Stacktrace can be disabled in any time. Example: production mode

Success response:

image

Error response:

image

Installation

Requires node version >=12.ั…

npm i --save nestjs-response-structure

Documentation

Setting up

  1. Add filter to your NestJS project. It will catch all errors and return formatted error.
// main.ts

import { ResponseFilter } from 'nestjs-response-structure'

async function bootstrap(): Promise<void> {
    const app = await NestFactory.create(AppModule)

    app.useGlobalFilters(new ResponseFilter({}))

    // your code here
}
  1. Extend your GraphQL type. You can also make it abstract.
// user.type.ts

import { ResponsePayloadType } from 'nestjs-response-structure'

@ObjectType({ isAbstract: true })
class UserPayloadType {
    @Field(() => ID)
    id: string

    @Field()
    name: string
}

@ObjectType('User')
export class UserType extends ResponsePayloadType(UserPayloadType) {}
  1. Throw your custom errors. Filter uses NestJS exceptions and take message and description from NestJS error response.
// user.service.ts

const user = await userRepository.findOne({ id })

if (!user) {
    throw new NotFoundException({
        message: 'USER_NOT_FOUND',
        description: `User not found with id ${id}`,
    })        
}

return user

Error type fields

  • id auto generated UUID to quickly find an entry in logs.
  • message is used for code message responses. For example, if frontend have a localization by the error code message.
  • code HTTP status codes.
  • description is used for development. The field helps to find out a more detailed reason for the error.
  • stack error stacktrace. You can disable this function in the filter constructor.

License

MIT

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.