forked from AubaidFarrukh/smart-retry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrations.ts
More file actions
146 lines (119 loc) · 3.77 KB
/
Copy pathintegrations.ts
File metadata and controls
146 lines (119 loc) · 3.77 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/** @format */
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { RetryManager } from './retryManager';
import { RetryConfig } from './types';
export class AxiosRetry {
private retryManager: RetryManager;
constructor(config?: RetryConfig, storePath?: string) {
this.retryManager = new RetryManager(config, storePath);
}
async request<T = any>(config: AxiosRequestConfig): Promise<AxiosResponse<T>> {
const result = await this.retryManager.execute<AxiosResponse<T>>(() => axios.request(config));
if (!result.success) {
throw result.error;
}
return result.data!;
}
async get<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
return this.request<T>({ ...config, method: 'GET', url });
}
async post<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<AxiosResponse<T>> {
return this.request<T>({ ...config, method: 'POST', url, data });
}
async put<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<AxiosResponse<T>> {
return this.request<T>({ ...config, method: 'PUT', url, data });
}
async delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
return this.request<T>({ ...config, method: 'DELETE', url });
}
async patch<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<AxiosResponse<T>> {
return this.request<T>({ ...config, method: 'PATCH', url, data });
}
getRetryManager(): RetryManager {
return this.retryManager;
}
}
export function createAxiosRetry(config?: RetryConfig, storePath?: string): AxiosRetry {
return new AxiosRetry(config, storePath);
}
export class FetchRetry {
private retryManager: RetryManager;
constructor(config?: RetryConfig, storePath?: string) {
this.retryManager = new RetryManager(config, storePath);
}
async fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
const result = await this.retryManager.execute<Response>(async () => {
const response = await fetch(input, init);
if (!response.ok) {
const error: any = new Error(`HTTP ${response.status}: ${response.statusText}`);
error.response = {
status: response.status,
statusText: response.statusText,
};
throw error;
}
return response;
});
if (!result.success) {
throw result.error;
}
return result.data!;
}
async get(url: string, init?: RequestInit): Promise<Response> {
return this.fetch(url, { ...init, method: 'GET' });
}
async post(url: string, body?: any, init?: RequestInit): Promise<Response> {
return this.fetch(url, {
...init,
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
...init?.headers,
},
});
}
async put(url: string, body?: any, init?: RequestInit): Promise<Response> {
return this.fetch(url, {
...init,
method: 'PUT',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
...init?.headers,
},
});
}
async delete(url: string, init?: RequestInit): Promise<Response> {
return this.fetch(url, { ...init, method: 'DELETE' });
}
async patch(url: string, body?: any, init?: RequestInit): Promise<Response> {
return this.fetch(url, {
...init,
method: 'PATCH',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
...init?.headers,
},
});
}
getRetryManager(): RetryManager {
return this.retryManager;
}
}
export function createFetchRetry(config?: RetryConfig, storePath?: string): FetchRetry {
return new FetchRetry(config, storePath);
}