@hodfords/nestjs-response 10.2.0 → 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.
Files changed (2) hide show
  1. package/README.md +151 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,151 @@
1
+ <p align="center">
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
+ </p>
4
+
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.
7
+ </p>
8
+
9
+ ## Installation 🤖
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 {}
36
+ ```
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 {}
46
+ ```
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
+
62
+ ```typescript
63
+ import { ResponseModel } from '@hodfords/nestjs-response';
64
+
65
+ class UserResponse {
66
+ @ApiProperty()
67
+ @IsNotEmpty()
68
+ @IsString()
69
+ name: string
70
+ }
71
+
72
+ export class UserController {
73
+ @Get()
74
+ @ResponseModel(UserResponse, true)
75
+ getAllUser() {
76
+ return [{ name: "John" }]
77
+ }
78
+ }
79
+
80
+ ```
81
+
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;
101
+ }
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
+ }
122
+ }
123
+
124
+ ```
125
+
126
+ ### Exception Handling
127
+
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
+ }
145
+ }
146
+
147
+ ```
148
+
149
+ ## License
150
+
151
+ This project is licensed under the MIT License
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hodfords/nestjs-response",
3
- "version": "10.2.0",
3
+ "version": "10.2.2",
4
4
  "description": "Standardizes and validates API responses in NestJS for consistent and reliable communication",
5
5
  "author": "",
6
6
  "license": "UNLICENSED",