Skip to content
Merged
Show file tree
Hide file tree
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
25 changes: 3 additions & 22 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,13 @@
* @param fileFieldName - The field name for the file (defaults to "file")
* @returns FormData object ready for upload
*/
export function createFileUploadFormData(file: Blob | Buffer, options?: Record<string, any>, fileFieldName: string = "file"): FormData {
export function createFileUploadFormData(file: Blob | Buffer, body?: Record<string, any>): FormData {
const formData = new FormData();

// Convert Buffer to Blob if needed
const fileToAppend = file instanceof Buffer ? new Blob([file]) : file;
formData.append(fileFieldName, fileToAppend);

// Add options as form fields, handling arrays and objects properly
if (options) {
for (const [key, value] of Object.entries(options)) {
if (value === undefined || value === null) {
continue; // Skip undefined/null values
}

if (Array.isArray(value)) {
// Convert arrays to JSON strings
formData.append(key, JSON.stringify(value));
} else if (typeof value === "object") {
// Convert objects to JSON strings
formData.append(key, JSON.stringify(value));
} else {
// Convert primitives to strings
formData.append(key, String(value));
}
}
}
formData.append("file", fileToAppend);
formData.append("body", JSON.stringify(body));

return formData;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const TEST_URLS = {
image: "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png",
pdf: "https://www.w3.org/WAI/WCAG21/working-examples/pdf-table/table.pdf",
audio: "https://jigsawstack.com/preview/stt-example.wav",
webpage: "https://example.com",
webpage: "https://www.google.com",
newsWebpage: "https://news.ycombinator.com",
};

Expand Down