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

@pedro2s/nestjs-http-async

v1.0.1

Published

HttpService Async for NestJS

Downloads

151

Readme

nestjs-http-async

nestjs-http-async is a package that provides an asynchronous HTTP client for NestJS applications. It offers the same functionality as NestJS's built-in HttpService, but with a key difference: all methods return Promises instead of Observables.

Features

  • Drop-in replacement for NestJS's HttpService
  • All methods return Promises for easier async/await usage
  • Supports all HTTP methods (GET, POST, PUT, DELETE, etc.)
  • Configurable via NestJS's dependency injection system

Installation

To install the package, run the following command in your project directory:

npm install @pedro2s/nestjs-http-async axios

Usage

  1. Import the HttpAsyncModule in your app.module.ts or any other module where you want to use it:
import { Module } from '@nestjs/common';
import { HttpAsyncModule } from 'nestjs-http-async';

@Module({
  imports: [HttpAsyncModule.forRoot()],
  // ...
})
export class AppModule {}
  1. Inject the HttpAsyncService in your service or controller:
import { Injectable } from '@nestjs/common';
import { HttpAsyncService } from 'nestjs-http-async';

@Injectable()
export class MyService {
  constructor(private readonly httpAsyncService: HttpAsyncService) {}

  async fetchData() {
    try {
      const response = await this.httpAsyncService.get('https://api.example.com/data');
      return response.data;
    } catch (error) {
      // Handle error
    }
  }
}

API

The HttpAsyncService provides the following methods:

  • get<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>
  • post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>
  • put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>
  • delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>
  • patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>
  • head<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>
  • options<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>

All methods return a Promise that resolves with an AxiosResponse object.

Configuration

  1. You can configure the HttpAsyncModule by using the forRoot method:
import { Module } from '@nestjs/common';
import { HttpAsyncModule } from 'nestjs-http-async';

@Module({
  imports: [
    HttpAsyncModule.forRoot({
      enableLogging: true
      timeout: 5000,
      maxRedirects: 5,
      // Other Axios configuration options
    }),
  ],
  // ...
})
export class AppModule {}
  1. You can configure the HttpAsyncModule by using the forFeature method:
import { Module } from '@nestjs/common';
import { HttpAsyncModule } from 'nestjs-http-async';

@Module({
  imports: [
    HttpAsyncModule.forFeature({
      serviceName: 'CustomHttpService',
      config: {
        enableLogging: true,
        baseURL: 'https://api.example.com',
        // Other Axios configuration options
      },
    }),
  ],
  // ...
})
export class AppModule {}

or

import { Module } from '@nestjs/common';
import { HttpAsyncModule } from 'nestjs-http-async';

@Module({
  imports: [
    HttpAsyncModule.forFeature([
      {
        serviceName: 'JsonPlaceholder',
        config: {
          enableLogging: true,
          baseURL: 'https://jsonplaceholder.typicode.com',
          // Other Axios configuration options
        },
      },
      {
        serviceName: 'AdviceSlip',
        config: {
          baseURL: 'https://api.adviceslip.com/advice'
          // Other Axios configuration options
        }
      }
    ]),
  ],
  // ...
})
export class AppModule {}

If your HttpAsyncModule configuration is via the forFeature method inject HttpAsyncService into your service or controller like this::

import { Injectable } from '@nestjs/common';
import { HttpAsyncService } from 'nestjs-http-async';

@Injectable()
export class MyService {
  constructor(
    @Inject('CustomHttpService')
    private readonly httpAsyncService: HttpAsyncService) {}

  async fetchData() {
    try {
      const response = await this.httpAsyncService.get('/data');
      return response.data;
    } catch (error) {
      // Handle error
    }
  }
}

Differences from NestJS HttpService

The main difference between nestjs-http-async and NestJS's HttpService is the return type of the methods:

  • HttpService methods return Observable<AxiosResponse<T>>
  • HttpAsyncService methods return Promise<AxiosResponse<T>>

This change allows for easier use with async/await syntax and eliminates the need for RxJS operators in most cases.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.