UNPKG

@instantdb/core

Version:

Instant's core local abstraction

91 lines 3.66 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.uploadFile = uploadFile; exports.deleteFile = deleteFile; exports.getSignedUploadUrl = getSignedUploadUrl; exports.upload = upload; exports.getDownloadUrl = getDownloadUrl; const fetch_js_1 = require("./utils/fetch.js"); function uploadFile(_a) { return __awaiter(this, arguments, void 0, function* ({ apiURI, appId, path, file, refreshToken, contentType, contentDisposition, }) { const headers = { app_id: appId, path, authorization: `Bearer ${refreshToken}`, 'content-type': contentType || file.type, }; if (contentDisposition) { headers['content-disposition'] = contentDisposition; } const data = yield (0, fetch_js_1.jsonFetch)(`${apiURI}/storage/upload`, { method: 'PUT', headers, body: file, }); return data; }); } function deleteFile(_a) { return __awaiter(this, arguments, void 0, function* ({ apiURI, appId, path, refreshToken, }) { const { data } = yield (0, fetch_js_1.jsonFetch)(`${apiURI}/storage/files?app_id=${appId}&filename=${encodeURIComponent(path)}`, { method: 'DELETE', headers: { 'content-type': 'application/json', authorization: `Bearer ${refreshToken}`, }, }); return data; }); } // Deprecated Storage API (Jan 2025) // --------------------------------- function getSignedUploadUrl(_a) { return __awaiter(this, arguments, void 0, function* ({ apiURI, appId, fileName, refreshToken, metadata = {}, }) { const { data } = yield (0, fetch_js_1.jsonFetch)(`${apiURI}/storage/signed-upload-url`, { method: 'POST', headers: { 'content-type': 'application/json', authorization: `Bearer ${refreshToken}`, }, body: JSON.stringify({ app_id: appId, filename: fileName, }), }); return data; }); } function upload(presignedUrl, file) { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(presignedUrl, { method: 'PUT', body: file, headers: { 'Content-Type': file.type, }, }); return response.ok; }); } function getDownloadUrl(_a) { return __awaiter(this, arguments, void 0, function* ({ apiURI, appId, path, refreshToken, }) { const { data } = yield (0, fetch_js_1.jsonFetch)(`${apiURI}/storage/signed-download-url?app_id=${appId}&filename=${encodeURIComponent(path)}`, { method: 'GET', headers: { 'content-type': 'application/json', authorization: `Bearer ${refreshToken}`, }, }); return data; }); } //# sourceMappingURL=StorageAPI.js.map