UNPKG

multi-data

Version:
40 lines (39 loc) 1.44 kB
let MultiData = class MultiData { /** * Append a part to the multipart form data. * * @param name The part name. * @param data The part data. * @param options Pass headers in the options for custom part headers. */ append(name, data, options) { if (name === undefined) throw new TypeError('name expected'); if (data === undefined) throw new TypeError('data expected'); this.lines.push(`--${this.boundary}`); this.lines.push(`Content-Disposition: form-data; name="${name}"`); if (options && options.headers) { const headers = options.headers; for(const key in headers)this.lines.push(`${key}: ${headers[key]}`); } this.lines.push(''); this.lines.push(data.toString()); return this; } /** * After appending data, use toString() to concatenate the form data for your request. */ toString() { this.lines.push(`--${this.boundary}--`); const string = this.lines.join('\r\n'); this.lines.pop(); return string; } /** * @param boundary The string used to define multipart boundaries and the end of body. */ constructor(boundary){ this.lines = []; if (boundary === undefined) throw new TypeError('boundary expected'); this.boundary = boundary; } }; /** * Class to build and concatenate multipart form data */ export { MultiData as default };