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
188 lines (153 loc) · 4.87 KB
/
Copy pathintegrations.ts
File metadata and controls
188 lines (153 loc) · 4.87 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/** @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);
}
function isJsonSerializableBody(body: any): boolean {
if (body === null || body === undefined || typeof body !== 'object') {
return false;
}
if (
body instanceof FormData ||
body instanceof Blob ||
body instanceof URLSearchParams ||
body instanceof ArrayBuffer ||
ArrayBuffer.isView(body)
) {
return false;
}
return true;
}
function buildBodyInit(body: unknown, init?: RequestInit): RequestInit {
if (!isJsonSerializableBody(body)) {
return { ...init, body: body as BodyInit | null | undefined };
}
return {
...init,
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
...init?.headers,
},
};
}
function getRequestUrl(input: RequestInfo | URL): string {
if (typeof input === 'string') {
return input;
}
if (input instanceof URL) {
return input.toString();
}
return input.url;
}
function normalizeHeaders(headers?: HeadersInit): Record<string, string> | undefined {
if (!headers) {
return undefined;
}
const result: Record<string, string> = {};
new Headers(headers).forEach((value, key) => {
result[key] = value;
});
return result;
}
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 requestConfig = {
url: getRequestUrl(input),
method: (init?.method || 'GET').toUpperCase(),
headers: normalizeHeaders(init?.headers),
data: init?.body,
};
const result = await this.retryManager.execute<Response>(async () => {
try {
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;
} catch (error: any) {
error.config = requestConfig;
throw error;
}
});
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, { ...buildBodyInit(body, init), method: 'POST' });
}
async put(url: string, body?: any, init?: RequestInit): Promise<Response> {
return this.fetch(url, { ...buildBodyInit(body, init), method: 'PUT' });
}
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, { ...buildBodyInit(body, init), method: 'PATCH' });
}
getRetryManager(): RetryManager {
return this.retryManager;
}
}
export function createFetchRetry(config?: RetryConfig, storePath?: string): FetchRetry {
return new FetchRetry(config, storePath);
}