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

@sgfe/axios

v0.0.10

Published

promise implementation of nestjs http module with retries feature using axios-retry and axios

Downloads

8

Readme

nestjs-http-promise

npm version npm downloads

description

nestjs module that just doing little modification to the original and good nestjs http module.

features

  • axios - the most used package for http requests in npm and the one used by nestjs official http library.
    • better axios stack trace - axios has an open issue about improvement of their stack trace. in this library there is a default interceptor that will intercept the stack trace and will add data to it.
  • promise based - most of us using the current http module that uses observable which we don't use most of the time and in order to avoid it were just calling .toPromise() every http call.
  • retries - in many cases we will want to retry a failing http call. with observable we could just add the retry operator (rxjs) but with promises we need to implement this logic ourselves. this package will make it easy for you, just pass { retries: NUMBER_OF_RETRIES } in the config of the http module. more details in the configuration section

quick start

installing

Using npm:

$ npm install nestjs-http-promise

Using yarn:

$ yarn add nestjs-http-promise

usage - just like every nest.js module

import the module:

import { HttpModule } from 'nestjs-http-promise'

@Module({ 
  imports: [HttpModule]
})

inject the service in the class:

import { HttpService } from 'nestjs-http-promise'

class Demo {
    constructor(private readonly httpService: HttpService) {}
}

use the service:

public callSomeServer(): Promise<object> {
  return this.httpService.get('http://fakeService') 
}

configuration

the service uses axios and axios-retry, so you can pass any AxiosRequestConfig And/Or AxiosRetryConfig

just pass it in the .register() method as you would do in the original nestjs httpModule

import { HttpModule } from 'nestjs-http-promise'

@Module({
  imports: [HttpModule.register(
    {
      timeout: 1000,
      retries: 5,
        ...
    }
  )]
})

default configuration

  • default config of axios-retry : https://github.com/softonic/axios-retry#options
  • better axios stack trace is added by default, you can turn it off by passing the isBetterStackTraceEnabled to false.

async configuration

When you need to pass module options asynchronously instead of statically, use the registerAsync() method just like in nest httpModule.

you have a couple of techniques to do it:

  • with the useFactory
HttpModule.registerAsync({
    useFactory: () => ({
    timeout: 1000,
    retries: 5,
      ...
    }),
});
  • using class
HttpModule.registerAsync({
  useClass: HttpConfigService,
});

Note that in this example, the HttpConfigService has to implement HttpModuleOptionsFactory interface as shown below.

@Injectable()
class HttpConfigService implements HttpModuleOptionsFactory {
  async createHttpOptions(): Promise<HttpModuleOptions> {
    const configurationData = await someAsyncMethod();
    return {
      timeout: configurationData.timeout,
      retries: 5,
        ...
    };
  }
}

If you want to reuse an existing options provider instead of creating a copy inside the HttpModule, use the useExisting syntax.

HttpModule.registerAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
});