@hodfords/nestjs-response 10.2.1 → 10.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -47
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,38 +1,67 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<a href="http://
|
|
2
|
+
<a href="http://opensource.hodfords.uk" target="blank"><img src="https://opensource.hodfords.uk/img/logo.svg" width="320" alt="Nest Logo" /></a>
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
|
|
9
|
-
<p align="center">
|
|
10
|
-
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
|
|
11
|
-
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
|
|
12
|
-
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
|
|
13
|
-
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
|
|
14
|
-
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
|
|
15
|
-
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
|
|
16
|
-
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
|
|
17
|
-
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
|
|
18
|
-
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg"/></a>
|
|
19
|
-
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
|
|
20
|
-
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
|
|
5
|
+
<p align="center">
|
|
6
|
+
Nestjs-Response is a simple yet powerful library for managing API responses in a NestJS application. It provides decorators to handle response models, allowing easy integration with Swagger for API documentation and validation.
|
|
21
7
|
</p>
|
|
22
|
-
<!--[](https://opencollective.com/nest#backer)
|
|
23
|
-
[](https://opencollective.com/nest#sponsor)-->
|
|
24
|
-
|
|
25
|
-
# nestjs-response
|
|
26
|
-
For response, use sync, not async.
|
|
27
8
|
|
|
28
9
|
## Installation 🤖
|
|
29
10
|
|
|
11
|
+
To begin using it, we first install the required dependencies.
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
npm install @hodfords/nestjs-response
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Interceptor Setup 🚀
|
|
18
|
+
|
|
19
|
+
- `Global Interceptor (Recommended):`
|
|
20
|
+
|
|
21
|
+
Global interceptors are applied across the entire application. To set up a global interceptor, you can register it in the providers array in your module.
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { APP_INTERCEPTOR } from '@nestjs/core';
|
|
25
|
+
import { ResponseInterceptor } from '@hodfords/nestjs-response';
|
|
26
|
+
|
|
27
|
+
@Module({
|
|
28
|
+
providers: [
|
|
29
|
+
{
|
|
30
|
+
provide: APP_INTERCEPTOR,
|
|
31
|
+
useClass: ResponseInterceptor
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
})
|
|
35
|
+
export class AppModule {}
|
|
30
36
|
```
|
|
31
|
-
npm install @hodfords/nestjs-response --save
|
|
32
37
|
|
|
38
|
+
- `Interceptor with Decorator:`
|
|
39
|
+
|
|
40
|
+
For microservices or specific scenarios, use the @UseInterceptors decorator to apply interceptors at the controller or method level. However, it's generally recommended to use global interceptors.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
@Controller()
|
|
44
|
+
@UseResponseInterceptor()
|
|
45
|
+
export class AppController {}
|
|
33
46
|
```
|
|
34
47
|
|
|
48
|
+
## Usage 🚀
|
|
49
|
+
|
|
50
|
+
`@ResponseModel()`
|
|
51
|
+
|
|
52
|
+
Use the @ResponseModel decorator when an API return single response type.
|
|
53
|
+
|
|
54
|
+
Parameter:
|
|
55
|
+
|
|
56
|
+
- `responseClass`: The class that defines the response model.
|
|
57
|
+
- `isArray` (optional): Set to `true` if the response is an array of `responseClass`. Defaults to `false`.
|
|
58
|
+
- `isAllowEmpty` (optional): Set to true if the response can be empty. Defaults to `false`.
|
|
59
|
+
|
|
60
|
+
Example of usage:
|
|
61
|
+
|
|
35
62
|
```typescript
|
|
63
|
+
import { ResponseModel } from '@hodfords/nestjs-response';
|
|
64
|
+
|
|
36
65
|
class UserResponse {
|
|
37
66
|
@ApiProperty()
|
|
38
67
|
@IsNotEmpty()
|
|
@@ -40,41 +69,83 @@ class UserResponse {
|
|
|
40
69
|
name: string
|
|
41
70
|
}
|
|
42
71
|
|
|
43
|
-
class UserController{
|
|
72
|
+
export class UserController {
|
|
44
73
|
@Get()
|
|
45
74
|
@ResponseModel(UserResponse, true)
|
|
46
75
|
getAllUser() {
|
|
47
|
-
return [{name: "
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
@Get()
|
|
51
|
-
@ResponseModel(UserResponse, true, true)
|
|
52
|
-
getUser() {
|
|
53
|
-
return undefined
|
|
76
|
+
return [{ name: "John" }]
|
|
54
77
|
}
|
|
55
78
|
}
|
|
56
79
|
|
|
57
80
|
```
|
|
58
81
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
82
|
+
`@ResponseModels()`
|
|
83
|
+
|
|
84
|
+
Use the @ResponseModels decorator when an API might return multiple response types.
|
|
85
|
+
|
|
86
|
+
Parameter:
|
|
87
|
+
|
|
88
|
+
- `...responseClasses`: A list of response classes or arrays of response classes.
|
|
89
|
+
|
|
90
|
+
Example of usage:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { ResponseModel } from '@hodfords/nestjs-response';
|
|
94
|
+
|
|
95
|
+
class AppController {
|
|
96
|
+
@Get('list-models/:type')
|
|
97
|
+
@ResponseModels(Number, [Number], UserPaginationResponse, [UserResponse], undefined, null)
|
|
98
|
+
getModels(@Param('type') type: string) {
|
|
99
|
+
if (type == 'undefined') {
|
|
100
|
+
return undefined;
|
|
66
101
|
}
|
|
67
|
-
|
|
102
|
+
if (type == 'pagination') {
|
|
103
|
+
return {
|
|
104
|
+
items: [{ name: 'John' }, { name: 'Daniel' }],
|
|
105
|
+
total: 2,
|
|
106
|
+
lastPage: 1,
|
|
107
|
+
perPage: 10,
|
|
108
|
+
currentPage: 1
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (type == 'multiple') {
|
|
112
|
+
return [{ name: 'John' }, { name: 'Daniel' }];
|
|
113
|
+
}
|
|
114
|
+
if (type == 'list-number') {
|
|
115
|
+
return [123, 456];
|
|
116
|
+
}
|
|
117
|
+
if (type == 'number') {
|
|
118
|
+
return 456;
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
68
122
|
}
|
|
123
|
+
|
|
69
124
|
```
|
|
70
125
|
|
|
71
|
-
|
|
126
|
+
### Exception Handling
|
|
72
127
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
128
|
+
When the response data does not match the expected model, a validation exception will be raised. This ensures that the API returns data conforming to the defined structure.
|
|
129
|
+
|
|
130
|
+
Example Case: If a property is expected to be a string, but a number is returned, a validation error will occur.
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
class UserResponse {
|
|
134
|
+
@ApiProperty()
|
|
135
|
+
@IsString()
|
|
136
|
+
name: string;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export class UserController {
|
|
140
|
+
@Get()
|
|
141
|
+
@ResponseModel(UserResponse)
|
|
142
|
+
getUser() {
|
|
143
|
+
return { name: 123 } // Error: name must be a number ...
|
|
144
|
+
}
|
|
79
145
|
}
|
|
80
|
-
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
This project is licensed under the MIT License
|
package/package.json
CHANGED