UNPKG

tickethead-sdk

Version:

SDK for the Tickethead API

31 lines 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeMultipartForm = writeMultipartForm; exports.generateBoundary = generateBoundary; /** * Method creates multipart body with provided file. * * @param fileName * @param fileData * @param boundary Parameter used for the multipart boundary. * @returns Multipart body as Uint8Array. */ function writeMultipartForm(fileName, fileData, boundary, name, contentType = 'application/octet-stream') { // Header contains metadata for file and the initial boundary const header = new Uint8Array(Buffer.from(`--${boundary}\r\nContent-Disposition: form-data; name="${name}"; filename="${fileName}"\r\nContent-Type: "${contentType}"\r\n\r\n`)); const lastBoundary = new Uint8Array(Buffer.from(`\r\n--${boundary}--`)); const finalData = new Uint8Array(header.byteLength + lastBoundary.byteLength + fileData.byteLength); // Put together header, data and last boundary in one array. finalData.set(header, 0); finalData.set(fileData, header.byteLength); finalData.set(lastBoundary, header.byteLength + fileData.byteLength); return finalData; } /** * Boundary is used to separate fields in multipart, a value that should not appear in the data. * This value is added in the `Content-Type` header. */ function generateBoundary() { return `----CustomBoundary${Date.now()}`; } //# sourceMappingURL=upload.js.map