GithubHelp home page GithubHelp logo

anthony917 / nestjs-http-promise Goto Github PK

View Code? Open in Web Editor NEW

This project forked from benhason1/nestjs-http-promise

0.0 0.0 0.0 114 KB

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

License: MIT License

JavaScript 12.33% TypeScript 87.67%

nestjs-http-promise's Introduction

nestjs-http-promise

npm version

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

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,
});

nestjs-http-promise's People

Contributors

benhason1 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.