Skip to content

Commit cfae4d6

Browse files
committed
docs: add docs
1 parent ad21314 commit cfae4d6

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

README.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
# NestJS Response Structure
22

3-
GraphQL Types and interfaces for great response
3+
GraphQL Types and interfaces for great response. The library provides types for the structured display of responses to the client.
4+
5+
## Features
6+
7+
- Structured types covered with TypeScript
8+
- `data` and `error` fields only without any extensions
9+
- Detailed error information including uuid, description, code, message and stacktrace
10+
- Stacktrace can be disabled in any time. Example: production mode
11+
12+
**Success response:**
13+
14+
![image](https://i.ibb.co/BgXh0sC/photo-2021-11-24-16-55-43.jpg)
15+
16+
**Error response:**
17+
18+
![image](https://i.ibb.co/vzVJstR/photo-2021-11-24-16-57-17.jpg)
419

520
## Installation
621

@@ -10,6 +25,70 @@ Requires node version >=12.х
1025
npm i --save nestjs-response-structure
1126
```
1227

28+
## Documentation
29+
30+
### Setting up
31+
32+
1. Add filter to your NestJS project. It will catch all errors and return formatted error.
33+
34+
```ts
35+
// main.ts
36+
37+
import { ResponseFilter } from 'nestjs-response-structure'
38+
39+
async function bootstrap(): Promise<void> {
40+
const app = await NestFactory.create(AppModule)
41+
42+
app.useGlobalFilters(new ResponseFilter({}))
43+
44+
// your code here
45+
}
46+
```
47+
48+
2. Extend your GraphQL type. You can also make it abstract.
49+
50+
```ts
51+
// user.type.ts
52+
53+
@ObjectType({ isAbstract: true })
54+
class UserPayloadType {
55+
@Field(() => ID)
56+
id: string
57+
58+
@Field()
59+
name: string
60+
}
61+
62+
@ObjectType('User')
63+
export class UserType extends ResponsePayloadType(UserPayloadType) {}
64+
```
65+
66+
3. Throw your custom errors. Filter uses NestJS exceptions and take `message` and `description` from NestJS error response.
67+
68+
69+
```ts
70+
// user.service.ts
71+
72+
const user = await userRepository.findOne({ id })
73+
74+
if (!user) {
75+
throw new NotFoundException({
76+
message: 'USER_NOT_FOUND',
77+
description: `User not found with id ${id}`,
78+
})
79+
}
80+
81+
return user
82+
```
83+
84+
### Error type fields
85+
86+
- `id` auto generated UUID to quickly find an entry in logs.
87+
- `message` is used for code message responses. For example, if frontend have a localization by the error code message.
88+
- `code` HTTP status codes.
89+
- `description` is used for development. The field helps to find out a more detailed reason for the error.
90+
- `stack` error stacktrace. You can disable this function in the filter constructor.
91+
1392
## License
1493

1594

0 commit comments

Comments
 (0)