Skip to content

Commit 575917b

Browse files
committed
docs: update README.md
1 parent 6678929 commit 575917b

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

README.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,136 @@
11
# nest-http-interface
2+
3+
[![npm version](https://badge.fury.io/js/@r2don%2Fnest-http-interface.svg)](https://badge.fury.io/js/@r2don%2Fnest-http-interface)
4+
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=r2don_nest-http-interface&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=r2don_nest-http-interface)
5+
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=r2don_nest-http-interface&metric=coverage)](https://sonarcloud.io/summary/new_code?id=r2don_nest-http-interface)
6+
7+
This library is inspired by the HTTP interface in Spring 6 and provides a similar API for Nest.js.
8+
9+
## Requirements
10+
11+
- Node.js 18 or later (because this library uses `fetch` internally)
12+
- Nest.js
13+
14+
## Installation
15+
16+
```bash
17+
$ npm install @r2don/nest-http-interface
18+
```
19+
20+
## Usage
21+
22+
First, the module we created must be imported into `AppModule.ts`:
23+
24+
```ts
25+
import {Module} from '@nestjs/common';
26+
import {HttpInterfaceModule} from '@r2don/nest-http-interface';
27+
28+
@Module({
29+
imports: [HttpInterfaceModule.register()],
30+
})
31+
export class AppModule {
32+
}
33+
```
34+
35+
Then, you need to create the desired HTTP service and specify several decorators:
36+
37+
```ts
38+
import {HttpInterface, GetExchange, ResponseBody, imitation} from '@r2don/nest-http-interface';
39+
40+
@HttpInterface()
41+
class SampleClient {
42+
@GetExchange('https://example.com/api') // path or full url
43+
@ResponseBody(ResponseTest) // response dto
44+
async request(): Promise<ResponseTest> {
45+
return imitation(); // this is a mock function to prevent the type error
46+
}
47+
}
48+
```
49+
50+
After adding the service to the providers in the module, you can use it and make HTTP requests when calling
51+
the `request` method:
52+
53+
```ts
54+
import {Module} from '@nestjs/common';
55+
import {HttpInterfaceModule} from '@your-namespace/http-interface';
56+
import {SampleClient} from './sample.client';
57+
58+
@Module({
59+
imports: [HttpInterfaceModule.register()],
60+
providers: [SampleClient]
61+
})
62+
export class AppModule {
63+
}
64+
```
65+
66+
## Decorators
67+
68+
- `@HttpInterface()`
69+
70+
This decorator is used to mark the class as an HTTP service.
71+
72+
- `@{HTTP Method}Exchange(path: string)`
73+
74+
These decorators are used to mark the method as an HTTP request method.
75+
`path` is the path or full URL of the request.
76+
77+
- `@ResponseBody(dto: ClassConstructor, options?: ClassTransformOptions)`
78+
79+
This decorator is used to specify the response DTO.
80+
It requires a class constructor and options from `class-transformer` library.
81+
82+
- `@PathVariable(name?: string)`
83+
84+
This decorator is used to specify the path variable.
85+
It requires the name of the path variable.
86+
87+
- `@RequestParam(key?: string)`
88+
89+
This decorator is used to specify the query string parameter.
90+
It requires the key of query string parameter.
91+
If `key` is not specified, it requires the parameter to be an object.
92+
93+
```ts
94+
// with key
95+
class TestService {
96+
request(@RequestParam('foo') query: string): string {
97+
return imitation();
98+
}
99+
}
100+
101+
// without key
102+
class TestService {
103+
request(@RequestParam() query: { foo: string }): string {
104+
return imitation();
105+
}
106+
}
107+
```
108+
109+
- `@RequestHeader(key?: string)`
110+
111+
This decorator is used to specify the request header.
112+
It requires the key of request header optionally.
113+
114+
- `@RequestBody(key?: string)`
115+
116+
This decorator is used to specify the request body.
117+
`application/json` is used as the content type.
118+
It requires the key of request body optionally.
119+
120+
- `@RequestForm(key?: string)`
121+
122+
This decorator is used to specify the request form.
123+
`application/x-www-form-urlencoded` is used as the content type.
124+
It requires the key of request body optionally.
125+
126+
## License
127+
128+
This library is licensed under the MIT license.
129+
130+
## Testing
131+
132+
To run tests, execute:
133+
134+
```bash
135+
$ pnpm test
136+
```

0 commit comments

Comments
 (0)