promise implementation of nestjs http module with retries feature using axios-retry and axios
npm install nestjs-http-promise.toPromise() every http call.{ retries: NUMBER_OF_RETRIES } in the config of the http module.
$ npm install nestjs-http-promise
`Using yarn:
`
$ yarn add nestjs-http-promise
`$3
import the module:
`ts
import { HttpModule } from 'nestjs-http-promise'@Module({
imports: [HttpModule]
})
`inject the service in the class:
`ts
import { HttpService } from 'nestjs-http-promise'class Demo {
constructor(private readonly httpService: HttpService) {}
}
`use the service:
`ts
public callSomeServer(): Promise`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
`ts
import { HttpModule } from 'nestjs-http-promise'@Module({
imports: [HttpModule.register(
{
timeout: 1000,
retries: 5,
...
}
)]
})
`$3
* 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
`ts
HttpModule.registerAsync({
useFactory: () => ({
timeout: 1000,
retries: 5,
...
}),
});
`* using class
`ts
HttpModule.registerAsync({
useClass: HttpConfigService,
});
`
Note that in this example, the HttpConfigService has to implement HttpModuleOptionsFactory interface as shown below.
`ts
@Injectable()
class HttpConfigService implements HttpModuleOptionsFactory {
async createHttpOptions(): Promise {
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.
`ts
HttpModule.registerAsync({
imports: [ConfigModule],
useExisting: ConfigService,
});
``