New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@betsys-nestjs/http-client

Package Overview
Dependencies
Maintainers
9
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@betsys-nestjs/http-client

HTTP client for NestJS applications

  • 4.0.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
9
Created
Source

@betsys-nestjs/http-client

This is a simple wrapper module built around @nestjs/axios.

Dependencies

PackageVersion
@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 information
  • HttpClientLogLevel.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 {}

FAQs

Package last updated on 09 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc