Skip to content

Commit 1a900b9

Browse files
authored
increase api timeout (#1053)
1 parent 0cd7c2c commit 1a900b9

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
function fetchWithtimeout(url, options, timeout = 10000) {
4+
return Promise.race([
5+
fetch(url, options),
6+
new Promise((_, reject) =>
7+
setTimeout(() => reject(new Error("Request timed out")), timeout)
8+
),
9+
]);
10+
}
11+
async function loadImage(content) {
12+
return new Promise((accept, reject) => {
13+
const reader = new FileReader();
14+
reader.onabort = () => {
15+
console.log("file reading was aborted");
16+
reject();
17+
};
18+
reader.onerror = () => {
19+
console.log("file reading has failed");
20+
reject();
21+
};
22+
reader.onload = () => {
23+
// Do whatever you want with the file contents
24+
const binaryStr = reader.result;
25+
accept(binaryStr);
26+
};
27+
reader.readAsArrayBuffer(content);
28+
});
29+
}
30+
async function makeRequest(request, proxy, _body) {
31+
const headers = request.toJSON().header;
32+
let myHeaders = new Headers();
33+
if (headers) {
34+
headers.forEach((header) => {
35+
if (header.key && header.value) {
36+
myHeaders.append(header.key, header.value);
37+
}
38+
});
39+
}
40+
// The following code handles multiple files in the same formdata param.
41+
// It removes the form data params where the src property is an array of filepath strings
42+
// Splits that array into different form data params with src set as a single filepath string
43+
// TODO:
44+
// if (request.body && request.body.mode === 'formdata') {
45+
// let formdata = request.body.formdata,
46+
// formdataArray = [];
47+
// formdata.members.forEach((param) => {
48+
// let key = param.key,
49+
// type = param.type,
50+
// disabled = param.disabled,
51+
// contentType = param.contentType;
52+
// // check if type is file or text
53+
// if (type === 'file') {
54+
// // if src is not of type string we check for array(multiple files)
55+
// if (typeof param.src !== 'string') {
56+
// // if src is an array(not empty), iterate over it and add files as separate form fields
57+
// if (Array.isArray(param.src) && param.src.length) {
58+
// param.src.forEach((filePath) => {
59+
// addFormParam(
60+
// formdataArray,
61+
// key,
62+
// param.type,
63+
// filePath,
64+
// disabled,
65+
// contentType
66+
// );
67+
// });
68+
// }
69+
// // if src is not an array or string, or is an empty array, add a placeholder for file path(no files case)
70+
// else {
71+
// addFormParam(
72+
// formdataArray,
73+
// key,
74+
// param.type,
75+
// '/path/to/file',
76+
// disabled,
77+
// contentType
78+
// );
79+
// }
80+
// }
81+
// // if src is string, directly add the param with src as filepath
82+
// else {
83+
// addFormParam(
84+
// formdataArray,
85+
// key,
86+
// param.type,
87+
// param.src,
88+
// disabled,
89+
// contentType
90+
// );
91+
// }
92+
// }
93+
// // if type is text, directly add it to formdata array
94+
// else {
95+
// addFormParam(
96+
// formdataArray,
97+
// key,
98+
// param.type,
99+
// param.value,
100+
// disabled,
101+
// contentType
102+
// );
103+
// }
104+
// });
105+
// request.body.update({
106+
// mode: 'formdata',
107+
// formdata: formdataArray,
108+
// });
109+
// }
110+
const body = request.body?.toJSON();
111+
let myBody = undefined;
112+
if (body !== undefined && Object.keys(body).length > 0) {
113+
switch (body.mode) {
114+
case "urlencoded": {
115+
myBody = new URLSearchParams();
116+
if (Array.isArray(body.urlencoded)) {
117+
for (const data of body.urlencoded) {
118+
if (data.key && data.value) {
119+
myBody.append(data.key, data.value);
120+
}
121+
}
122+
}
123+
break;
124+
}
125+
case "raw": {
126+
myBody = (body.raw ?? "").toString();
127+
break;
128+
}
129+
case "formdata": {
130+
// The Content-Type header will be set automatically based on the type of body.
131+
myHeaders.delete("Content-Type");
132+
myBody = new FormData();
133+
const members = request.body?.formdata?.members;
134+
if (Array.isArray(members)) {
135+
for (const data of members) {
136+
if (data.key && data.value.content) {
137+
myBody.append(data.key, data.value.content);
138+
}
139+
// handle generic key-value payload
140+
if (data.key && typeof data.value === "string") {
141+
myBody.append(data.key, data.value);
142+
}
143+
}
144+
}
145+
break;
146+
}
147+
case "file": {
148+
if (_body.type === "raw" && _body.content?.type === "file") {
149+
myBody = await loadImage(_body.content.value.content);
150+
}
151+
break;
152+
}
153+
default:
154+
break;
155+
}
156+
}
157+
const requestOptions = {
158+
method: request.method,
159+
headers: myHeaders,
160+
body: myBody,
161+
};
162+
let finalUrl = request.url.toString();
163+
if (proxy) {
164+
// Ensure the proxy ends with a slash.
165+
let normalizedProxy = proxy.replace(/\/$/, "") + "/";
166+
finalUrl = normalizedProxy + request.url.toString();
167+
}
168+
return fetchWithtimeout(finalUrl, requestOptions).then((response) => {
169+
const contentType = response.headers.get("content-type");
170+
let fileExtension = "";
171+
if (contentType) {
172+
if (contentType.includes("application/pdf")) {
173+
fileExtension = ".pdf";
174+
} else if (contentType.includes("image/jpeg")) {
175+
fileExtension = ".jpg";
176+
} else if (contentType.includes("image/png")) {
177+
fileExtension = ".png";
178+
} else if (contentType.includes("image/gif")) {
179+
fileExtension = ".gif";
180+
} else if (contentType.includes("image/webp")) {
181+
fileExtension = ".webp";
182+
} else if (contentType.includes("video/mpeg")) {
183+
fileExtension = ".mpeg";
184+
} else if (contentType.includes("video/mp4")) {
185+
fileExtension = ".mp4";
186+
} else if (contentType.includes("audio/mpeg")) {
187+
fileExtension = ".mp3";
188+
} else if (contentType.includes("audio/ogg")) {
189+
fileExtension = ".ogg";
190+
} else if (contentType.includes("application/octet-stream")) {
191+
fileExtension = ".bin";
192+
} else if (contentType.includes("application/zip")) {
193+
fileExtension = ".zip";
194+
}
195+
if (fileExtension) {
196+
return response.blob().then((blob) => {
197+
const url = window.URL.createObjectURL(blob);
198+
const link = document.createElement("a");
199+
link.href = url;
200+
// Now the file name includes the extension
201+
link.setAttribute("download", `file${fileExtension}`);
202+
// These two lines are necessary to make the link click in Firefox
203+
link.style.display = "none";
204+
document.body.appendChild(link);
205+
link.click();
206+
// After link is clicked, it's safe to remove it.
207+
setTimeout(() => document.body.removeChild(link), 0);
208+
return response;
209+
});
210+
} else {
211+
return response;
212+
}
213+
}
214+
return response;
215+
});
216+
}
217+
exports.default = makeRequest;

0 commit comments

Comments
 (0)