@betsys/nestjs-http-client
This is a simple wrapper module built around @nestjs/axios.
It adds prometheus monitoring and logging to all outgoing HTTP calls.
Dependencies
For this module to work, you need to install the following peer dependencies:
Package | Version |
---|
@nestjs/common | ^7.0.0 || ^8.0.0 |
@betsys/nestjs-logger | ^0.0.5 |
@betsys/nestjs-monitoring | ^0.0.5 |
reflect-metadata | ^0.1.12 |
rxjs | ^7.1.0 |
Usage
The basic usage is similar to the @nestjs/axios module.
You use the HttpClientService
to make HTTP requests.
@Injectable()
export class CatsService {
constructor(private httpService: HttpClientService) {}
findAll(): Observable<AxiosResponse<Cat[]>> {
return this.httpService.get('http://localhost:3000/cats');
}
}
You need to import HttpClient
module to use the HttpClientService
. Both register
and registerAsync
are available:
@Module({
imports: [
HttpModule.register({
timeout: 5000,
maxRedirects: 5,
}),
],
providers: [CatsService],
})
export class CatsModule {}
In additional to params accepted by @nestjs/axios
module, you can define if logging should be enabled, and you can choose from two logging levels:
HttpClientLogLevel.Info
, logs basic request and response informationHttpClientLogLevel.Full
, logs detailed request and response information (including headers, body, etc.)
@Module({
imports: [
HttpModule.register({
log: true,
logLevel: HttpClientLogLevel.Info,
}),
],
providers: [CatsService],
})
export class CatsModule {}