Created
August 5, 2022 00:13
-
-
Save apacheli/013797e914623cf874260951c798e936 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export interface Field { | |
| contentType?: string; | |
| filename?: string; | |
| name: string; | |
| value: string | Uint8Array; | |
| } | |
| export const boundary = Math.random().toString(16).substring(2); | |
| export async function* fileIterator(fields: Field[]) { | |
| for (let i = 0; i < fields.length; i++) { | |
| const field = fields[i]; | |
| let str = `Content-Disposition:form-data;name=${field.name}`; | |
| if (field.filename !== undefined) { | |
| str += `;filename=${field.filename}`; | |
| } | |
| if (field.contentType !== undefined) { | |
| str += `;Content-Type:${field.contentType}`; | |
| } | |
| yield `\n--${boundary}\n${str}\n\n`; | |
| yield field.value; | |
| } | |
| yield `\n--${boundary}--`; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment