https://github.com/temarusanov/nestjs-response-structure
NestJS response payload structure
https://github.com/temarusanov/nestjs-response-structure
exception-filter graphql graphql-js nestjs nestjs-graphql typeorm typescript
Last synced: 10 months ago
JSON representation
NestJS response payload structure
- Host: GitHub
- URL: https://github.com/temarusanov/nestjs-response-structure
- Owner: temarusanov
- License: mit
- Created: 2021-11-24T06:39:47.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-05T03:12:18.000Z (over 3 years ago)
- Last Synced: 2024-11-15T03:51:39.510Z (over 1 year ago)
- Topics: exception-filter, graphql, graphql-js, nestjs, nestjs-graphql, typeorm, typescript
- Language: TypeScript
- Homepage:
- Size: 892 KB
- Stars: 10
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:**

**Error response:**

## Installation
Requires node version >=12.х
```bash
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.
```ts
// main.ts
import { ResponseFilter } from 'nestjs-response-structure'
async function bootstrap(): Promise {
const app = await NestFactory.create(AppModule)
app.useGlobalFilters(new ResponseFilter({}))
// your code here
}
```
2. Extend your GraphQL type. You can also make it abstract.
```ts
// 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) {}
```
3. Throw your custom errors. Filter uses NestJS exceptions and take `message` and `description` from NestJS error response.
```ts
// 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](https://github.com/i-link-pro-team/logardian/blob/main/LICENSE)