@leordev-web5/api
Version:
SDK for accessing the features and capabilities of Web5
34 lines • 1.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.dataToBlob = void 0;
var common_1 = require("@web5/common");
/**
* Set/detect the media type and return the data as bytes.
*
* @beta
*/
var dataToBlob = function (data, dataFormat) {
var dataBlob;
// Check for Object or String, and if neither, assume bytes.
var detectedType = (0, common_1.universalTypeOf)(data);
if (dataFormat === 'text/plain' || detectedType === 'String') {
dataBlob = new Blob([data], { type: 'text/plain' });
}
else if (dataFormat === 'application/json' || detectedType === 'Object') {
var dataBytes = common_1.Convert.object(data).toUint8Array();
dataBlob = new Blob([dataBytes], { type: 'application/json' });
}
else if (detectedType === 'Uint8Array' || detectedType === 'ArrayBuffer') {
dataBlob = new Blob([data], { type: 'application/octet-stream' });
}
else if (detectedType === 'Blob') {
dataBlob = data;
}
else {
throw new Error('data type not supported.');
}
dataFormat = dataFormat || dataBlob.type || 'application/octet-stream';
return { dataBlob: dataBlob, dataFormat: dataFormat };
};
exports.dataToBlob = dataToBlob;
//# sourceMappingURL=utils.js.map