1
1
<p align =" center " >
2
- <a href =" http ://nestjs.com" ><img src =" https://nestjs.com/img/logo_text.svg " alt =" Nest Logo " width =" 320 " /></a >
2
+ <a href =" https ://nestjs.com" ><img src =" https://nestjs.com/img/logo_text.svg " alt =" Nest Logo " width =" 320 " /></a >
3
3
</p >
4
4
5
5
# nest-http-interface
@@ -39,7 +39,7 @@ import {Module} from '@nestjs/common';
39
39
import {HttpInterfaceModule } from ' @r2don/nest-http-interface' ;
40
40
41
41
@Module ({
42
- imports: [HttpInterfaceModule .register ()],
42
+ imports: [HttpInterfaceModule .forRoot ()],
43
43
})
44
44
export class AppModule {
45
45
}
@@ -48,13 +48,13 @@ export class AppModule {
48
48
Then, you need to create the desired HTTP service and specify several decorators:
49
49
50
50
``` ts
51
- import {HttpInterface , GetExchange , ResponseBody , imitation } from ' @r2don/nest-http-interface' ;
51
+ import {HttpInterface , GetExchange , ResponseBody , imitation , PathVariable } from ' @r2don/nest-http-interface' ;
52
52
53
- @HttpInterface ()
54
- class SampleClient {
55
- @GetExchange (' https://example.com/api ' ) // path or full url
56
- @ResponseBody (ResponseTest ) // response dto
57
- async request() : Promise <ResponseTest > {
53
+ @HttpInterface (' https://example.com/api ' ) // base url
54
+ class UserHttpService {
55
+ @GetExchange (' /users/{id} ' ) // path
56
+ @ResponseBody (UserResponse ) // response dto
57
+ async request(@ PathVariable () id : number ) : Promise <UserResponse > {
58
58
return imitation (); // this is a mock function to prevent the type error
59
59
}
60
60
}
@@ -64,15 +64,14 @@ After adding the service to the providers in the module, you can use it and make
64
64
the ` request ` method:
65
65
66
66
``` ts
67
- import {Module } from ' @nestjs/common' ;
68
- import {HttpInterfaceModule } from ' @r2don/http-interface' ;
69
- import {SampleClient } from ' ./sample.client' ;
67
+ @Injectable ()
68
+ class UserService {
69
+ constructor (private readonly client : UserHttpService ) {
70
+ }
70
71
71
- @Module ({
72
- imports: [HttpInterfaceModule .register ()],
73
- providers: [SampleClient ]
74
- })
75
- export class AppModule {
72
+ async getUser(id : number ): Promise <UserResponse > {
73
+ return this .client .request (id );
74
+ }
76
75
}
77
76
```
78
77
0 commit comments