11<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 >
33</p >
44
55# nest-http-interface
@@ -39,7 +39,7 @@ import {Module} from '@nestjs/common';
3939import {HttpInterfaceModule } from ' @r2don/nest-http-interface' ;
4040
4141@Module ({
42- imports: [HttpInterfaceModule .register ()],
42+ imports: [HttpInterfaceModule .forRoot ()],
4343})
4444export class AppModule {
4545}
@@ -48,13 +48,13 @@ export class AppModule {
4848Then, you need to create the desired HTTP service and specify several decorators:
4949
5050``` ts
51- import {HttpInterface , GetExchange , ResponseBody , imitation } from ' @r2don/nest-http-interface' ;
51+ import {HttpInterface , GetExchange , ResponseBody , imitation , PathVariable } from ' @r2don/nest-http-interface' ;
5252
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 > {
5858 return imitation (); // this is a mock function to prevent the type error
5959 }
6060}
@@ -64,15 +64,14 @@ After adding the service to the providers in the module, you can use it and make
6464the ` request ` method:
6565
6666``` 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+ }
7071
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+ }
7675}
7776```
7877
0 commit comments