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

@nodeflip/nest-axios-http

v1.0.2

Published

A NestJS module for simplified HTTP requests using Axios with dynamic configuration, logging, and interceptor support.

Downloads

10

Readme

test workflow npm version downloads

@nodeflip/nest-axios-http

Description

@nodeflip/nest-axios-http is a NestJS module that simplifies HTTP requests using Axios within NestJS applications. It provides an easy-to-use interface for making HTTP calls while integrating seamlessly with NestJS dependency injection and module system.

Features

  • Simplified HTTP Requests: Easily make HTTP requests with Axios using a service-oriented approach.
  • Dependency Injection: Utilize NestJS's powerful dependency injection system to manage HTTP service instances.
  • Customization Options: Customize Axios instance configuration, logging, request/response interceptors, and error handling.
  • Dynamic Module Configuration: Dynamically configure HTTP services based on module options using forRoot, forFeature, and forFeatureWithProvider methods.

Benefits

  • Integration with NestJS: Seamlessly integrates with NestJS architecture, making it easy to manage HTTP services as injectable dependencies.
  • Modular Design: Allows for dynamic module configuration, enabling multiple HTTP service instances with different configurations within a single NestJS application.
  • Logging and Interceptors: Built-in support for custom logging and request/response interceptors, providing flexibility in handling HTTP interactions.
  • Named Services: Supports injecting named HTTP services using serviceName for different configurations or service instances within the same application.

Installation

npm install @nodeflip/nest-axios-http

Usage

Importing the Module

Import HttpModule into your NestJS application's module (AppModule, for example).

import { Module } from '@nestjs/common';
import { HttpModule } from '@nodeflip/nest-axios-http';

@Module({
  imports: [
    HttpModule.forRoot({
      config: {
        baseURL: 'https://api.example.com',
        enableLogging: true,
        onRequest: (config) => {
          // Customize request logging or modifications
          return config;
        },
        onResponse: (response) => {
          // Customize response logging or modifications
          return response;
        },
        onError: (error) => {
          // Customize error handling or logging
          return Promise.reject(error);
        },
      },
    }),
  ],
})
export class AppModule {}

Using HTTP Service

Inject HttpService into your NestJS components or services to make HTTP requests.

import { Injectable } from '@nestjs/common';
import { HttpService } from '@nodeflip/nest-axios-http';
import { AxiosResponse } from 'axios';

@Injectable()
export class MyService {
  constructor(private readonly httpService: HttpService) {}

  async fetchData(): Promise<AxiosResponse<any>> {
    return this.httpService.get('/data');
  }

  async postData(data: any): Promise<AxiosResponse<any>> {
    return this.httpService.post('/data', data);
  }
}

Injecting Named HTTP Services

You can inject named HTTP services using the serviceName option and the @Inject decorator.

import { Module } from '@nestjs/common';
import { HttpModule } from '@nodeflip/nest-axios-http';

@Module({
  imports: [
    HttpModule.forRoot({
      serviceName: 'CustomHttpService',
      config: {
        baseURL: 'https://api.example.com',
        enableLogging: true,
        onRequest: (config) => {
          // Customize request logging or modifications
          return config;
        },
        onResponse: (response) => {
          // Customize response logging or modifications
          return response;
        },
        onError: (error) => {
          // Customize error handling or logging
          return Promise.reject(error);
        },
      },
    }),
  ],
})
export class AppModule {}
import { Injectable, Inject } from '@nestjs/common';
import { HttpService } from '@nodeflip/nest-axios-http';
import { AxiosResponse } from 'axios';

@Injectable()
export class AnotherService {
  constructor(
    @Inject('CustomHttpService') private readonly customHttpService: HttpService,
    @Inject(HttpService) private readonly defaultHttpService: HttpService,
  ) {}

  async fetchDataFromCustomService(): Promise<AxiosResponse<any>> {
    return this.customHttpService.get('/custom-data');
  }

  async fetchDataFromDefaultService(): Promise<AxiosResponse<any>> {
    return this.defaultHttpService.get('/default-data');
  }
}

Custom Logging and Interceptors

You can customize logging and request/response interceptors by providing logger and config options during module initialization.

import { Logger } from "@nestjs/common";
import { Module } from "@nestjs/common";
import { HttpModule } from "@nodeflip/nest-axios-http";

const customLogger = new Logger("CustomLogger");

@Module({
  imports: [
    HttpModule.forRoot({
      logger: customLogger, // Custom logger instance
      config: {
        baseURL: "https://api.example.com",
        enableLogging: true,
        onRequest: (config) => {
          // Customize request logging or modifications
          return config;
        },
        onResponse: (response) => {
          // Customize response logging or modifications
          return response;
        },
        onError: (error) => {
          // Customize error handling or logging
          return Promise.reject(error);
        },
      },
    }),
  ],
})
export class AppModule {}

Configuring Multiple HTTP Services

You can configure multiple HTTP services using an array of options with forFeature.

import { Module } from "@nestjs/common";
import { HttpModule } from "@nodeflip/nest-axios-http";

@Module({
  imports: [
    HttpModule.forFeature([
      {
        serviceName: "HTTP_SERVICE_2",
        config: {
          baseURL: "https://api.service1.com",
          enableLogging: true,
          onRequest: (config) => {
            // Optional: Customize request logging or modifications
            return config;
          },
          onResponse: (response) => {
            // Optional: Customize response logging or modifications
            return response;
          },
          onError: (error) => {
            // Optional: Customize error handling or logging
            return Promise.reject(error);
          },
        },
      },
      {
        serviceName: "HTTP_SERVICE_2",
        config: {
          baseURL: "https://api.service2.com",
          enableLogging: true,
          onRequest: (config) => {
            // Optional: Customize request logging or modifications
            return config;
          },
          onResponse: (response) => {
            // Optional: Customize response logging or modifications
            return response;
          },
          onError: (error) => {
            // Optional: Customize error handling or logging
            return Promise.reject(error);
          },
        },
      },
    ]),
  ],
})
export class AppModule {}

License

This package is MIT licensed.

Issues

For any issues, bugs, or feature requests, please file an issue on GitHub.

Repository

Find this package on npm.