npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@betsys-nestjs/http-client

v4.0.0

Published

HTTP client for NestJS applications

Downloads

6

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 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 {}