UNPKG

@codex-storage/sdk-js

Version:

Codex SDK to interact with the Codex decentralized storage network.

80 lines (76 loc) 2.14 kB
'use strict'; require('valibot'); // src/errors/errors.ts var CodexError = class extends Error { code; errors; sourceStack; constructor(message, { code, errors, sourceStack } = {}) { super(message); this.code = code || null; this.errors = errors || null; this.sourceStack = sourceStack || null; } }; // src/data/browser-upload.ts var BrowserUploadStategy = class { file; onProgress; metadata; xhr; constructor(file, onProgress, metadata) { this.file = file; this.onProgress = onProgress; this.metadata = metadata; } upload(url, { auth }) { const xhr = new XMLHttpRequest(); this.xhr = xhr; return new Promise((resolve) => { var _a, _b; xhr.upload.onprogress = (evt) => { var _a2; if (evt.lengthComputable) { (_a2 = this.onProgress) == null ? void 0 : _a2.call(this, evt.loaded, evt.total); } }; xhr.open("POST", url, true); if ((_a = this.metadata) == null ? void 0 : _a.filename) { xhr.setRequestHeader( "Content-Disposition", 'attachment; filename="' + this.metadata.filename + '"' ); } if (auth == null ? void 0 : auth.basic) { xhr.setRequestHeader("Authorization", "Basic " + auth.basic); } if ((_b = this.metadata) == null ? void 0 : _b.mimetype) { xhr.setRequestHeader("Content-Type", this.metadata.mimetype); } xhr.send(this.file); xhr.onload = function() { if (xhr.status != 200) { resolve({ error: true, data: new CodexError(xhr.responseText, { code: xhr.status }) }); } else { resolve({ error: false, data: xhr.response }); } }; xhr.onerror = function() { resolve({ error: true, data: new CodexError("Something went wrong during the file upload.") }); }; }); } abort() { var _a; (_a = this.xhr) == null ? void 0 : _a.abort(); } }; exports.BrowserUploadStategy = BrowserUploadStategy; //# sourceMappingURL=browser.js.map //# sourceMappingURL=browser.js.map