0e061f8e61
Bumps the dependencies group with 1 update: [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node). Updates `@types/node` from 20.12.10 to 20.12.11 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node) --- updated-dependencies: - dependency-name: "@types/node" dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dependencies ... Signed-off-by: dependabot[bot] <support@github.com> |
||
---|---|---|
.github | ||
lib | ||
.editorconfig | ||
.eslintignore | ||
.eslintrc.js | ||
.gitignore | ||
.prettierrc | ||
.releaserc | ||
.spr.yml | ||
LICENSE | ||
package-lock.json | ||
package.json | ||
README.md | ||
tsconfig.json |
nestjs-http-promise
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.
- 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: 10,
...
}
)]
})
default configuration
- default config of axios-retry : https://github.com/softonic/axios-retry#options
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: 10,
...
}),
});
- 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: 10,
...
};
}
}
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,
});