-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaxios-retry.module.ts
41 lines (37 loc) · 1.28 KB
/
axios-retry.module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { HttpModule, HttpService } from "@nestjs/axios";
import { Module, DynamicModule, Global } from "@nestjs/common";
import axios, { AxiosRequestConfig } from "axios";
import axiosRetry, { IAxiosRetryConfig, DEFAULT_OPTIONS } from "axios-retry";
interface AxiosRetryOptions {
axiosConfig?: AxiosRequestConfig;
axiosRetryConfig?: IAxiosRetryConfig;
}
/**
* A module that provides retry functionality for Axios HTTP requests.
* This module can be imported in a NestJS application to enable automatic retry of failed requests.
*/
@Global()
@Module({})
export class AxiosRetryModule {
/**
* Creates a dynamic module for the AxiosRetryModule.
* @param options - Optional configuration options for the retry behavior.
* @returns A dynamic module that can be imported in a NestJS application.
*/
static forRoot(
options: AxiosRetryOptions = { axiosRetryConfig: DEFAULT_OPTIONS }
): DynamicModule {
const axiosInstance = axios.create(options.axiosConfig);
axiosRetry(axiosInstance, options.axiosRetryConfig);
const axiosProvider = {
provide: HttpService,
useValue: new HttpService(axiosInstance),
};
return {
module: AxiosRetryModule,
imports: [HttpModule],
providers: [axiosProvider],
exports: [axiosProvider],
};
}
}