UNPKG

@codex-storage/sdk-js

Version:

Codex SDK to interact with the Codex decentralized storage network.

50 lines (48 loc) 1.54 kB
import { FetchAuthBuilder } from './chunk-2VOCE3TZ.mjs'; import { CodexError } from './chunk-MVZZ6JVF.mjs'; import Undici from 'undici'; var NodeUploadStategy = class { body; metadata; abortController; constructor(body, metadata) { this.body = body; this.metadata = metadata; } async upload(url, { auth }) { var _a, _b, _c, _d; const headers = FetchAuthBuilder.build(auth); if ((_a = this.metadata) == null ? void 0 : _a.filename) { headers["Content-Disposition"] = 'attachment; filename="' + ((_b = this.metadata) == null ? void 0 : _b.filename) + '"'; } if ((_c = this.metadata) == null ? void 0 : _c.mimetype) { headers["Content-Type"] = (_d = this.metadata) == null ? void 0 : _d.mimetype; } const controller = new AbortController(); this.abortController = controller; const res = await Undici.request(url, { method: "POST", headers, body: this.body, signal: controller.signal }); if (res.statusCode < 200 || res.statusCode >= 300) { const msg = `The status code is invalid got ${res.statusCode} - ${await res.body.text()} `; return { error: true, data: new CodexError(msg, { code: res.statusCode }) }; } return { error: false, data: await res.body.text() }; } abort() { var _a; try { (_a = this.abortController) == null ? void 0 : _a.abort(); } catch (_) { } } }; export { NodeUploadStategy }; //# sourceMappingURL=node.mjs.map //# sourceMappingURL=node.mjs.map