-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectToFormData.ts
32 lines (30 loc) · 1.03 KB
/
objectToFormData.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
/** Converts pointed object with nested properties to plain FormData
* @returns Pointed formData or new FormData */
export default function objectToFormData(
fromObj: Record<string, any> | Array<any>,
toForm: FormData | null | undefined = null
): FormData {
toForm ||= new FormData();
function map(v: any, prop: string): void {
if (Array.isArray(v)) {
v.forEach((val, index) => {
map(val, `${prop}[${index}]`);
});
} else if (v != null && typeof v === "object") {
if (v instanceof Date) {
toForm!.append(prop, v.toJSON());
} else if (v instanceof File) {
toForm!.append(prop, v, v.name);
} else {
Object.keys(v).forEach((key) => {
const s = prop ? `${prop}[${key as string}]` : (key as string); // NiceToHave: add ability to use . instead
map((v as Record<string, any>)[key as string], s);
});
}
} else {
toForm!.append(prop, v); // Add primitive values (strings, numbers, etc.)
}
}
map(fromObj, "");
return toForm;
}