No description
Find a file
dependabot[bot] 9b0d44b246
build(deps-dev): bump the dependencies group with 4 updates
Bumps the dependencies group with 4 updates: [@nestjs/common](https://github.com/nestjs/nest/tree/HEAD/packages/common), [@nestjs/core](https://github.com/nestjs/nest/tree/HEAD/packages/core), [@nestjs/platform-express](https://github.com/nestjs/nest/tree/HEAD/packages/platform-express) and [eslint-plugin-sonarjs](https://github.com/SonarSource/eslint-plugin-sonarjs).


Updates `@nestjs/common` from 10.3.6 to 10.3.7
- [Release notes](https://github.com/nestjs/nest/releases)
- [Commits](https://github.com/nestjs/nest/commits/v10.3.7/packages/common)

Updates `@nestjs/core` from 10.3.6 to 10.3.7
- [Release notes](https://github.com/nestjs/nest/releases)
- [Commits](https://github.com/nestjs/nest/commits/v10.3.7/packages/core)

Updates `@nestjs/platform-express` from 10.3.6 to 10.3.7
- [Release notes](https://github.com/nestjs/nest/releases)
- [Commits](https://github.com/nestjs/nest/commits/v10.3.7/packages/platform-express)

Updates `eslint-plugin-sonarjs` from 0.24.0 to 0.25.0
- [Release notes](https://github.com/SonarSource/eslint-plugin-sonarjs/releases)
- [Commits](https://github.com/SonarSource/eslint-plugin-sonarjs/compare/0.24.0...0.25.0)

---
updated-dependencies:
- dependency-name: "@nestjs/common"
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dependencies
- dependency-name: "@nestjs/core"
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dependencies
- dependency-name: "@nestjs/platform-express"
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: dependencies
- dependency-name: eslint-plugin-sonarjs
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: dependencies
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-03-28 14:25:27 +00:00
.github ci(action): bump the dependencies group with 1 update 2024-03-22 14:05:49 +00:00
lib refactor: adjust default retries to 10 2024-03-24 00:12:44 -05:00
.editorconfig style: add editorconfig 2023-07-05 17:31:06 -05:00
.eslintignore change readme and removed unused file 2021-09-07 23:01:22 +03:00
.eslintrc.js refactor: make console an error 2024-02-10 17:32:57 -06:00
.gitignore build: stricter typescript 2023-07-05 18:33:32 -05:00
.prettierrc style: update prettierrc 2023-07-05 17:32:25 -05:00
.releaserc ci(release): add branch config 2023-07-05 17:51:11 -05:00
.spr.yml chore: add spr config 2023-12-13 15:54:52 -06:00
LICENSE working -> renamed from nestjs-http-promise-retry 2021-09-07 21:25:31 +03:00
package-lock.json build(deps-dev): bump the dependencies group with 4 updates 2024-03-28 14:25:27 +00:00
package.json build(deps-dev): bump the dependencies group with 4 updates 2024-03-28 14:25:27 +00:00
README.md added monthly downloads stats 2022-01-28 18:53:12 +02:00
tsconfig.json refactor(ts): make consistent with other packages 2023-12-29 20:29:53 -06:00

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

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