|
1 | 1 | # nestjs-http |
2 | 2 |
|
3 | | -### Rich featured HttpClient for `nestjs` applications |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
4 | 7 |
|
5 | | -It implements wrapper around [Got](https://www.npmjs.com/package/got) lib instead of native nest HttpModule |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
6 | 12 |
|
7 | | -It provides: |
| 13 | + |
| 14 | + |
| 15 | + |
8 | 16 |
|
9 | | -1. [Got](https://www.npmjs.com/package/got) integration for [nestjs](https://nestjs.com/) |
10 | | -2. Accept external Tracing Service for attaching specific http-headers across your microservice architecture |
| 17 | +## Description |
| 18 | + |
| 19 | +### Rich featured HttpClient for [nestjs](https://nestjs.com/) applications |
| 20 | + |
| 21 | +- [Got](https://www.npmjs.com/package/got) integration for `nestjs`; |
| 22 | +- Retries and all `Got` functions out of the box |
| 23 | +- Transparent `Got` usage (you will work with Got interface) |
| 24 | +- Accept external `Tracing Service` via DI for attaching specific http-headers across your microservice architecture; |
| 25 | +- Modularity - create instances for your modules with different configurations; |
| 26 | +- Default keep-alive http/https agent. |
| 27 | + |
| 28 | +## Requirements |
| 29 | + |
| 30 | +1. @nestjs/common ^7.2.0 |
| 31 | +2. @nestjs/core ^7.2.0 |
| 32 | + |
| 33 | +## Installation |
| 34 | + |
| 35 | +```bash |
| 36 | +npm install --save @ukitgroup/nestjs-http |
| 37 | +``` |
| 38 | + |
| 39 | +or |
| 40 | + |
| 41 | +```bash |
| 42 | +yarn add @ukitgroup/nestjs-http |
| 43 | +``` |
| 44 | + |
| 45 | +## Short example |
| 46 | + |
| 47 | +**cat.service.ts** |
| 48 | + |
| 49 | +```typescript |
| 50 | +@Injectable() |
| 51 | +export class CatService { |
| 52 | + constructor(@Inject(HttpClientService) private readonly httpClient: Got) {} |
| 53 | + |
| 54 | + meow(): string { |
| 55 | + // httpClient is an instance of Got |
| 56 | + // configuration from AppModule and CatModule |
| 57 | + // set built-in headers |
| 58 | + this.httpClient.post('/meow'); |
| 59 | + return `meows..`; |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +**cat.module.ts** |
| 65 | + |
| 66 | +```typescript |
| 67 | +@Module({ |
| 68 | + imports: [ |
| 69 | + HttpClient.forInstance({ |
| 70 | + imports: [YourConfigModule], |
| 71 | + providers: [ |
| 72 | + { |
| 73 | + // override got configuration |
| 74 | + provide: HTTP_CLIENT_INSTANCE_GOT_OPTS, |
| 75 | + inject: [YourConfig], |
| 76 | + useFactory: config => { |
| 77 | + return { |
| 78 | + retry: config.retry, |
| 79 | + }; |
| 80 | + }, |
| 81 | + }, |
| 82 | + ], |
| 83 | + }), |
| 84 | + ], |
| 85 | + providers: [CatService], |
| 86 | +}) |
| 87 | +export class CatModule {} |
| 88 | +``` |
| 89 | + |
| 90 | +**app.module.ts** |
| 91 | + |
| 92 | +```typescript |
| 93 | +@Module({ |
| 94 | + imports: [HttpClient.forRoot({}), CatModule], |
| 95 | +}) |
| 96 | +export class AppModule {} |
| 97 | +``` |
| 98 | + |
| 99 | +## API |
| 100 | + |
| 101 | +Define root configuration for Got in AppModule with: |
| 102 | + |
| 103 | +``` |
| 104 | +HttpClient.forRoot(options: HttpClientForRootType) |
| 105 | +``` |
| 106 | + |
| 107 | +``` |
| 108 | +HttpClientForRootType: { |
| 109 | + imports?: [], |
| 110 | + providers?: [], |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +Provide configuration with tokens: |
| 115 | + |
| 116 | +- `HTTP_SERVICE_CONFIG` - ServiceConfigType for HttpClientService |
| 117 | +- `TRACE_DATA_SERVICE` - should implements TraceDataServiceInterface |
| 118 | +- `GOT_CONFIG` - got configuration |
| 119 | + |
| 120 | +Provided configuration will be merged: |
| 121 | +defaultConfig -> forRoot() -> forInstance() -> execution |
| 122 | + |
| 123 | +default config for httpService: |
| 124 | + |
| 125 | +```typescript |
| 126 | +const defaultConfig = { |
| 127 | + enableTraceService: false, |
| 128 | + headersMap: { |
| 129 | + traceId: 'x-trace-id', |
| 130 | + ip: 'x-real-ip', |
| 131 | + userAgent: 'user-agent', |
| 132 | + referrer: 'referrer', |
| 133 | + }, |
| 134 | + excludeHeaders: [], |
| 135 | +}; |
| 136 | +``` |
| 137 | + |
| 138 | +default config for got extends default config from [got documentation](https://github.com/sindresorhus/got) |
| 139 | + |
| 140 | +```typescript |
| 141 | +const defaultConfig = { |
| 142 | + agent: { |
| 143 | + http: new http.Agent({ keepAlive: true }), |
| 144 | + https: new https.Agent({ keepAlive: true }), |
| 145 | + }, |
| 146 | +}; |
| 147 | +``` |
| 148 | + |
| 149 | +Define instance configuration, trace service and http client service config in your module with: |
| 150 | + |
| 151 | +``` |
| 152 | +HttpClient.forInstance(options: HttpClientForRootType) |
| 153 | +``` |
| 154 | + |
| 155 | +# Trace service injection |
| 156 | + |
| 157 | +Usually you pass some headers across your microservices such as: `trace-id`, `ip`, `user-agent` etc. |
| 158 | +To make it convenient you can inject traceService via `dependency injection` and `HTTPClient` will pass this data with headers |
| 159 | + |
| 160 | +You just have to implement TraceServiceInterface: |
| 161 | + |
| 162 | +```typescript |
| 163 | +export interface TraceDataServiceInterface { |
| 164 | + getRequestData(): TraceDataType; |
| 165 | +} |
| 166 | + |
| 167 | +// and TraceDataType |
| 168 | +export type TraceDataType = { |
| 169 | + [key: string]: string; |
| 170 | +}; |
| 171 | +``` |
| 172 | + |
| 173 | +Headers mapping to values: |
| 174 | + |
| 175 | +```typescript |
| 176 | +const headersMap = { |
| 177 | + traceId: 'x-trace-id', |
| 178 | + ip: 'x-real-ip', |
| 179 | + userAgent: 'user-agent', |
| 180 | + referrer: 'referrer', |
| 181 | +}; |
| 182 | +``` |
| 183 | + |
| 184 | +Sou you can just define your service which should return these fields. For example, you can use `cls-hooked` for retrieving request data |
| 185 | + |
| 186 | +```typescript |
| 187 | +export class TraceService implements TraceDataServiceInterface { |
| 188 | + getRequestData() { |
| 189 | + return { |
| 190 | + traceId: 'unique-id', |
| 191 | + ip: '127.0.0.1', |
| 192 | + }; |
| 193 | + } |
| 194 | +} |
| 195 | +``` |
| 196 | + |
| 197 | +And then just define configuration: |
| 198 | + |
| 199 | +```typescript |
| 200 | +@Module({ |
| 201 | + imports: [ |
| 202 | + HttpClient.forInstance({ |
| 203 | + providers: [ |
| 204 | + { |
| 205 | + provide: HTTP_SERVICE_CONFIG, |
| 206 | + useValue: { |
| 207 | + enableTraceService: true, |
| 208 | + }, |
| 209 | + }, |
| 210 | + { provide: TRACE_DATA_SERVICE, useClass: TraceService }, |
| 211 | + ], |
| 212 | + }), |
| 213 | + ], |
| 214 | +}) |
| 215 | +class AwesomeModule {} |
| 216 | +``` |
| 217 | + |
| 218 | +### TODO |
| 219 | + |
| 220 | +- Injection of `MetricService` for exports requests metrics such as: statuses, errors, retries, timing |
| 221 | +- Client balancing for servers with several `ip`s or endpoints in different AZ which don't support server's LB. |
| 222 | +- ... Feature/Pull requests are welcome!😅 |
| 223 | + |
| 224 | +## License |
| 225 | + |
| 226 | +@ukitgroup/nestjs-http is [MIT licensed](LICENSE). |
0 commit comments