(form: FormData, key: string, value: unknown)
| 325 | } |
| 326 | |
| 327 | const addFormValue = async (form: FormData, key: string, value: unknown): Promise<void> => { |
| 328 | if (value === undefined) return; |
| 329 | if (value == null) { |
| 330 | throw new TypeError( |
| 331 | `Received null for "${key}"; to pass null in FormData, you must use the string 'null'` |
| 332 | ); |
| 333 | } |
| 334 | |
| 335 | // TODO: make nested formats configurable |
| 336 | if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") { |
| 337 | form.append(key, String(value)); |
| 338 | } else if ( |
| 339 | isUploadable(value) || |
| 340 | isBlobLike(value) || |
| 341 | value instanceof Buffer || |
| 342 | value instanceof ArrayBuffer |
| 343 | ) { |
| 344 | const file = await toFile(value); |
| 345 | form.append(key, file as File); |
| 346 | } else if (Array.isArray(value)) { |
| 347 | await Promise.all(value.map((entry) => addFormValue(form, key + "[]", entry))); |
| 348 | } else if (typeof value === "object") { |
| 349 | await Promise.all( |
| 350 | Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)) |
| 351 | ); |
| 352 | } else { |
| 353 | throw new TypeError( |
| 354 | `Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead` |
| 355 | ); |
| 356 | } |
| 357 | }; |
| 358 | |
| 359 | export type ToFileInput = Uploadable | Exclude<BlobLikePart, string> | AsyncIterable<BlobLikePart>; |
| 360 |
no test coverage detected
searching dependent graphs…