-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmakeRequest.js
191 lines (167 loc) · 5.17 KB
/
makeRequest.js
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
189
190
191
/* ============================================================================
* Copyright (c) Cloud Annotations
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* ========================================================================== */
async function loadImage(content) {
return new Promise((accept, reject) => {
const reader = new FileReader();
reader.onabort = () => {
console.log("file reading was aborted");
reject();
};
reader.onerror = () => {
console.log("file reading has failed");
reject();
};
reader.onload = () => {
// Do whatever you want with the file contents
const binaryStr = reader.result;
accept(binaryStr);
};
reader.readAsArrayBuffer(content);
});
}
async function makeRequest(request, proxy, _body) {
console.log(request)
const headers = request.toJSON().header;
let myHeaders = new Headers();
if (headers) {
headers.shift()
headers.forEach((header) => {
if (header.key && header.value) {
myHeaders.append(header.key, header.value);
}
});
} // The following code handles multiple files in the same formdata param.
// It removes the form data params where the src property is an array of filepath strings
// Splits that array into different form data params with src set as a single filepath string
// TODO:
// if (request.body && request.body.mode === 'formdata') {
// let formdata = request.body.formdata,
// formdataArray = [];
// formdata.members.forEach((param) => {
// let key = param.key,
// type = param.type,
// disabled = param.disabled,
// contentType = param.contentType;
// // check if type is file or text
// if (type === 'file') {
// // if src is not of type string we check for array(multiple files)
// if (typeof param.src !== 'string') {
// // if src is an array(not empty), iterate over it and add files as separate form fields
// if (Array.isArray(param.src) && param.src.length) {
// param.src.forEach((filePath) => {
// addFormParam(
// formdataArray,
// key,
// param.type,
// filePath,
// disabled,
// contentType
// );
// });
// }
// // if src is not an array or string, or is an empty array, add a placeholder for file path(no files case)
// else {
// addFormParam(
// formdataArray,
// key,
// param.type,
// '/path/to/file',
// disabled,
// contentType
// );
// }
// }
// // if src is string, directly add the param with src as filepath
// else {
// addFormParam(
// formdataArray,
// key,
// param.type,
// param.src,
// disabled,
// contentType
// );
// }
// }
// // if type is text, directly add it to formdata array
// else {
// addFormParam(
// formdataArray,
// key,
// param.type,
// param.value,
// disabled,
// contentType
// );
// }
// });
// request.body.update({
// mode: 'formdata',
// formdata: formdataArray,
// });
// }
const body = request.body?.toJSON();
let myBody = undefined;
if (body !== undefined && Object.keys(body).length > 0) {
switch (body.mode) {
case "urlencoded": {
myBody = new URLSearchParams();
if (Array.isArray(body.urlencoded)) {
for (const data of body.urlencoded) {
if (data.key && data.value) {
myBody.append(data.key, data.value);
}
}
}
break;
}
case "raw": {
myBody = (body.raw ?? "").toString();
break;
}
case "formdata": {
myBody = new FormData();
if (Array.isArray(request.body.formdata.members)) {
for (const data of request.body.formdata.members) {
if (data.type == "file"){
myBody.append(data.key, data.value.content, "file");
}else{
if (data.key && data.value) {
myBody.append(data.key, data.value);
}
}
}
}
break;
}
case "file": {
if (_body.type === "raw" && _body.content?.type === "file") {
myBody = await loadImage(_body.content.value.content);
}
break;
}
default:
break;
}
}
const requestOptions = {
method: request.method,
headers: myHeaders,
body: myBody,
};
let finalUrl = request.url.toString();
if (proxy) {
// Ensure the proxy ends with a slash.
let normalizedProxy = proxy.replace(/\/$/, "") + "/";
finalUrl = normalizedProxy + request.url.toString();
}
console.log("Fetch", finalUrl, requestOptions)
return await fetch(finalUrl, requestOptions).then((response) => {
return response.text();
});
}
export default makeRequest;