UNPKG

@instantdb/core

Version:
72 lines 2.45 kB
"use strict"; 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"); async function uploadFile({ 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 = await (0, fetch_js_1.jsonFetch)(`${apiURI}/storage/upload`, { method: 'PUT', headers, body: file, }); return data; } async function deleteFile({ apiURI, appId, path, refreshToken, }) { const { data } = await (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) // --------------------------------- async function getSignedUploadUrl({ apiURI, appId, fileName, refreshToken, metadata = {}, }) { const { data } = await (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; } async function upload(presignedUrl, file) { const response = await fetch(presignedUrl, { method: 'PUT', body: file, headers: { 'Content-Type': file.type, }, }); return response.ok; } async function getDownloadUrl({ apiURI, appId, path, refreshToken, }) { const { data } = await (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