/
Fetcher.ts
184 lines (164 loc) · 5.29 KB
/
Fetcher.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
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
import { default as FormData } from "form-data";
import qs from "qs";
import { APIResponse } from "./APIResponse";
if (typeof window === "undefined") {
global.fetch = require("node-fetch");
}
export type FetchFunction = <R = unknown>(args: Fetcher.Args) => Promise<APIResponse<R, Fetcher.Error>>;
export declare namespace Fetcher {
export interface Args {
url: string;
method: string;
contentType?: string;
headers?: Record<string, string | undefined>;
queryParameters?: Record<string, string | string[]>;
body?: unknown;
timeoutMs?: number;
maxRetries?: number;
withCredentials?: boolean;
responseType?: "json" | "blob" | "streaming";
}
export type Error = FailedStatusCodeError | NonJsonError | TimeoutError | UnknownError;
export interface FailedStatusCodeError {
reason: "status-code";
statusCode: number;
body: unknown;
}
export interface NonJsonError {
reason: "non-json";
statusCode: number;
rawBody: string;
}
export interface TimeoutError {
reason: "timeout";
}
export interface UnknownError {
reason: "unknown";
errorMessage: string;
}
}
const INITIAL_RETRY_DELAY = 1;
const MAX_RETRY_DELAY = 60;
const DEFAULT_MAX_RETRIES = 2;
async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIResponse<R, Fetcher.Error>> {
const headers: Record<string, string> = {};
if (args.body !== undefined && args.contentType != null) {
headers["Content-Type"] = args.contentType;
}
if (args.headers != null) {
for (const [key, value] of Object.entries(args.headers)) {
if (value != null) {
headers[key] = value;
}
}
}
const url =
Object.keys(args.queryParameters ?? {}).length > 0
? `${args.url}?${qs.stringify(args.queryParameters, { arrayFormat: "repeat" })}`
: args.url;
let body: BodyInit | undefined = undefined;
if (args.body instanceof FormData) {
// @ts-expect-error
body = args.body;
} else {
body = JSON.stringify(args.body);
}
const makeRequest = async (): Promise<Response> => {
const controller = new AbortController();
let abortId = undefined;
if (args.timeoutMs != null) {
abortId = setTimeout(() => controller.abort(), args.timeoutMs);
}
const response = await fetch(url, {
method: args.method,
headers,
body,
signal: controller.signal,
credentials: args.withCredentials ? "same-origin" : undefined,
});
if (abortId != null) {
clearTimeout(abortId);
}
return response;
};
try {
let response = await makeRequest();
for (let i = 0; i < (args.maxRetries ?? DEFAULT_MAX_RETRIES); ++i) {
if (
response.status === 408 ||
response.status === 409 ||
response.status === 429 ||
response.status >= 500
) {
const delay = Math.min(INITIAL_RETRY_DELAY * Math.pow(i, 2), MAX_RETRY_DELAY);
await new Promise((resolve) => setTimeout(resolve, delay));
response = await makeRequest();
} else {
break;
}
}
let body: unknown;
if (response.body != null && args.responseType === "blob") {
body = await response.blob();
} else if (response.body != null && args.responseType === "streaming") {
body = response.body;
} else {
const text = await response.text();
if (text.length > 0) {
try {
body = JSON.parse(text);
} catch (err) {
return {
ok: false,
error: {
reason: "non-json",
statusCode: response.status,
rawBody: text,
},
};
}
}
}
if (response.status >= 200 && response.status < 400) {
return {
ok: true,
body: body as R,
headers: response.headers,
};
} else {
return {
ok: false,
error: {
reason: "status-code",
statusCode: response.status,
body,
},
};
}
} catch (error) {
if (error instanceof Error && error.name === "AbortError") {
return {
ok: false,
error: {
reason: "timeout",
},
};
} else if (error instanceof Error) {
return {
ok: false,
error: {
reason: "unknown",
errorMessage: error.message,
},
};
}
return {
ok: false,
error: {
reason: "unknown",
errorMessage: JSON.stringify(error),
},
};
}
}
export const fetcher: FetchFunction = fetcherImpl;