@betsys-nestjs/http-client
v4.0.0
Published
HTTP client for NestJS applications
Downloads
23
Maintainers
Keywords
Readme
@betsys-nestjs/http-client
This is a simple wrapper module built around @nestjs/axios.
Dependencies
| Package | Version | | ---------------- | ------- | | @nestjs/common | ^10.0.0 | | @nestjs/core | ^10.0.0 | | reflect-metadata | <1.0.0 | | rxjs | ^7.0.0 | | @nestjs/axios | ^3.0.0 | | axios | ^1.0.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 {}
Monitoring and Logger support
The library is ready to work with monitoring and logger. To enable it you need to implement your own monitoring and logger service based on abstraction provided by this library.
Monitoring
Time monitoring - used for monitoring of request time, your provided service must implement HttpClientTimeMonitoringInterface
. Implementation of startTimerHttpRequestTime
starts your custom timer returning a function which stops the timer.
Example of time monitoring using @betsys-nestjs/monitoring:
import { Injectable } from '@nestjs/common';
import {
Histogram,
InjectMonitoringConfig,
InjectMonitoringRegistry,
MonitoringConfig,
Registry,
AbstractMonitoringService, exponentialBuckets,
} from '@betsys-nestjs/monitoring';
@Injectable()
export class MonitoringService extends AbstractMonitoringService {
private readonly requestTime: Histogram<string>;
constructor(
@InjectMonitoringConfig() private readonly config: MonitoringConfig,
@InjectMonitoringRegistry() protected readonly registry: Registry,
) {
super(registry);
this.requestTime = this.createMetric(Histogram, {
name: config.getMetricsName('http_client'),
help: 'Histogram of http request times',
buckets: exponentialBuckets(0.001, 2, 20),
labelNames: ['url', 'statusCode', 'api'],
registers: [registry],
});
}
startTimerHttpRequestTime(url: string): ({ statusCode }: { statusCode: number }) => number {
return this.requestTime.startTimer({ url, api: 'api/v1/test' });
}
}
Logger
Similar to monitoring you can simply implement custom service following HttpClientLoggerInterface
.
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.)
To start using Logger or Monitoring service, you simply insert class references to register
or registerAsync
method of HttpClientModule like this:
import { HttpClientModule } from '@betsys-nestjs/http-client';
import { Logger } from '@betsys-nestjs/logger';
import { MonitoringService } from '...'; // the path to the monitoring service implementation
@Module({
imports: [
HttpClientModule.register({
log: true, // default true
logLevel: HttpClientLogLevel.Info, // default 'HttpClientLogLevel.Info'
logger: Logger,
monitoring: MonitoringService,
}),
],
providers: [CatsService],
})
export class CatsModule {}