UNPKG

axios

Version:

Promise based HTTP client for the browser and node.js

38 lines (34 loc) • 1.41 kB
import utils from '../utils.js'; import platform from '../platform/index.js'; import AxiosError from './AxiosError.js'; import formDataToBlob, { FormDataError } from '../helpers/formDataToBlob.js'; export default function createMultipartBody(form, Blob, limit, limitError, boundary) { // Report missing Blob support before trying to select a random source. if (!utils.isFunction(Blob)) { throw new AxiosError('Blob is not supported', AxiosError.ERR_NOT_SUPPORT); } if (boundary === undefined) { if (utils.isFunction(platform.generateString)) { boundary = 'axios-' + platform.generateString(36, '0123456789abcdef'); } else { const crypto = utils.global.crypto; if (!crypto || !utils.isFunction(crypto.getRandomValues)) { throw new AxiosError('Random byte generation is not supported', AxiosError.ERR_NOT_SUPPORT); } const random = new Uint8Array(18); crypto.getRandomValues(random); boundary = 'axios-' + Array.from(random, (value) => ('0' + value.toString(16)).slice(-2)).join(''); } } try { return formDataToBlob(form, Blob, limit, limitError, boundary); } catch (error) { if (error instanceof FormDataError) { const code = error.reason === 'boundary' ? AxiosError.ERR_BAD_OPTION_VALUE : AxiosError.ERR_BAD_REQUEST; throw new AxiosError(error.message, code); } throw error; } }