@@ -35,20 +35,25 @@ $ npm install @r2don/nest-http-interface
3535First, the module we created must be imported into ` AppModule.ts ` :
3636
3737``` 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' ;
4040
4141@Module ({
4242 imports: [HttpInterfaceModule .forRoot ()],
4343})
44- export class AppModule {
45- }
44+ export class AppModule {}
4645```
4746
4847Then, you need to create the desired HTTP service and specify several decorators:
4948
5049``` 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' ;
5257
5358@HttpInterface (' https://example.com/api' ) // base url
5459class UserHttpService {
@@ -66,8 +71,7 @@ the `request` method:
6671``` ts
6772@Injectable ()
6873class UserService {
69- constructor (private readonly client : UserHttpService ) {
70- }
74+ constructor (private readonly client : UserHttpService ) {}
7175
7276 async getUser(id : number ): Promise <UserResponse > {
7377 return this .client .request (id );
@@ -92,8 +96,9 @@ class UserService {
9296
9397- ` @RequestParam(key?: string, defaultValue?: string) ` : Specifies the query string parameter, requiring the key of the
9498 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 `
97102
98103- ` @RequestHeader(key?: string, option?: { defaultValue?: string; transform?: (value: string) => string }) ` : Specifies
99104 the request header, requiring the key of the header optionally.
@@ -108,6 +113,49 @@ class UserService {
108113- ` @RequestForm(key?: string, defaultValue?: string) ` : Specifies the request form
109114 using ` application/x-www-form-urlencoded ` as the content type, requiring the key of the body optionally.
110115
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+
111159## License
112160
113161This library is licensed under the MIT license.
0 commit comments