@@ -35,20 +35,25 @@ $ npm install @r2don/nest-http-interface
35
35
First, the module we created must be imported into ` AppModule.ts ` :
36
36
37
37
``` ts
38
- import {Module } from ' @nestjs/common' ;
39
- import {HttpInterfaceModule } from ' @r2don/nest-http-interface' ;
38
+ import { Module } from ' @nestjs/common' ;
39
+ import { HttpInterfaceModule } from ' @r2don/nest-http-interface' ;
40
40
41
41
@Module ({
42
42
imports: [HttpInterfaceModule .forRoot ()],
43
43
})
44
- export class AppModule {
45
- }
44
+ export class AppModule {}
46
45
```
47
46
48
47
Then, you need to create the desired HTTP service and specify several decorators:
49
48
50
49
``` ts
51
- import {HttpInterface , GetExchange , ResponseBody , imitation , PathVariable } from ' @r2don/nest-http-interface' ;
50
+ import {
51
+ HttpInterface ,
52
+ GetExchange ,
53
+ ResponseBody ,
54
+ imitation ,
55
+ PathVariable ,
56
+ } from ' @r2don/nest-http-interface' ;
52
57
53
58
@HttpInterface (' https://example.com/api' ) // base url
54
59
class UserHttpService {
@@ -66,8 +71,7 @@ the `request` method:
66
71
``` ts
67
72
@Injectable ()
68
73
class UserService {
69
- constructor (private readonly client : UserHttpService ) {
70
- }
74
+ constructor (private readonly client : UserHttpService ) {}
71
75
72
76
async getUser(id : number ): Promise <UserResponse > {
73
77
return this .client .request (id );
@@ -92,8 +96,9 @@ class UserService {
92
96
93
97
- ` @RequestParam(key?: string, defaultValue?: string) ` : Specifies the query string parameter, requiring the key of the
94
98
parameter. If ` key ` is not specified, the parameter must be an object. See examples below:
95
- - Example with key: ` request(@RequestParam('foo') query: string): string `
96
- - Example without key: ` request(@RequestParam() query: { foo: string }): string `
99
+
100
+ - Example with key: ` request(@RequestParam('foo') query: string): string `
101
+ - Example without key: ` request(@RequestParam() query: { foo: string }): string `
97
102
98
103
- ` @RequestHeader(key?: string, option?: { defaultValue?: string; transform?: (value: string) => string }) ` : Specifies
99
104
the request header, requiring the key of the header optionally.
@@ -108,6 +113,49 @@ class UserService {
108
113
- ` @RequestForm(key?: string, defaultValue?: string) ` : Specifies the request form
109
114
using ` application/x-www-form-urlencoded ` as the content type, requiring the key of the body optionally.
110
115
116
+ ## Auto generate ` @ResponseBody() ` from return type of exchange method
117
+
118
+ Because of limitation of ` reflect-metadata ` , ` @ResponseBody() ` is required to specify the response DTO.
119
+
120
+ But this library provides a way to auto generate ` @ResponseBody() ` and infers response DTO from return type of exchange method.
121
+
122
+ It uses ` CLI Plugin ` like ` @nestjs/swagger ` and ` @nestjs/graphql ` .
123
+
124
+ To enable the plugin, open nest-cli.json
125
+
126
+ ``` json
127
+ {
128
+ "compilerOptions" : {
129
+ "plugins" : [" @nestjs/swagger" ]
130
+ }
131
+ }
132
+ ```
133
+
134
+ You can use the options property to customize the behavior of the plugin.
135
+
136
+ ``` json
137
+ {
138
+ "compilerOptions" : {
139
+ "plugins" : [
140
+ {
141
+ "name" : " @r2don/nest-http-interface" ,
142
+ "options" : {
143
+ "interfaceFilenameSuffix" : [" .http.service.ts" ]
144
+ }
145
+ }
146
+ ]
147
+ }
148
+ }
149
+ ```
150
+
151
+ Here is the list of options:
152
+
153
+ | option | default | description |
154
+ | ----------------------- | --------------- | ------------------------- |
155
+ | interfaceFilenameSuffix | [ '.service.ts'] | HTTP service files suffix |
156
+
157
+ ` @ResponseBody() ` will be added whenever ` nest start ` or ` nest build ` is executed.
158
+
111
159
## License
112
160
113
161
This library is licensed under the MIT license.
0 commit comments