Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 47 additions & 14 deletions src/main/actions/makeApiClientRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,28 @@ const makeApiClientRequest = async ({ apiRequest }) => {
const formData = new AdvancedFormData();
apiRequest.body.forEach(({ key, value }) => {
if (Array.isArray(value)) {
const files = value.map((entry) => {
const stream = entry?.path ? fs.createReadStream(entry.path) : null;
if(stream) {
return {
stream,
fileName: entry.name || entry.path.split("/").pop(),
};
}
return null;
}).filter(Boolean)
files.forEach(({stream, fileName}) => {
const files = value
.map((entry) => {
const stream = entry?.path
? fs.createReadStream(entry.path)
: null;
if (stream) {
return {
stream,
fileName: entry.name || entry.path.split("/").pop(),
};
Comment thread
nafees87n marked this conversation as resolved.
}
return null;
})
.filter(Boolean);
files.forEach(({ stream, fileName }) => {
try {
formData.append(key, stream, fileName);
} catch (error) {
console.error(`Error appending file to formData for key ${key}:`, error);
console.error(
`Error appending file to formData for key ${key}:`,
error
);
}
});
} else {
Expand All @@ -61,18 +68,44 @@ const makeApiClientRequest = async ({ apiRequest }) => {
});
body = formData;
headers = {
'content-type': `${formData.getHeaders()}`,
"content-type": `${formData.getHeaders()}`,
...headers,
}
};
Comment thread
nafees87n marked this conversation as resolved.
}

const requestStartTime = performance.now();
const axios = getProxiedAxios(apiRequest.includeCredentials);

const hasJsonContentType = (() => {
const contentTypeHeader = Object.keys(headers).find(
(key) => key.toLowerCase() === "content-type"
);

return headers[contentTypeHeader]
?.toLowerCase()
?.includes("application/json");
})();

let transformRequest;

// Body would always be a string here but to double check
if (hasJsonContentType && typeof body === "string") {
try {
JSON.parse(body);
// Valid JSON → let axios handle it (default behavior of transformRequest)
} catch {
// Invalid JSON → bypass axios to send raw
// otherwise axios will double stringify the body to make it a valid JSON string
transformRequest = [(data) => data];
}
}

const response = await axios({
url,
method,
headers,
data: body,
transformRequest,
Comment thread
nafees87n marked this conversation as resolved.
responseType: "arraybuffer",
withCredentials: false,
validateStatus: () => {
Expand Down