@fine-dev/fine-js
Version:
Javascript client for Fine BaaS
130 lines • 5.48 kB
JavaScript
;
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 });
class FineStorageClient {
constructor({ baseUrl, headers, fetch: customFetch }) {
this.baseUrl = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
if (!customFetch)
customFetch = fetch.bind(window);
this.fetch = (input, init) => customFetch(input, Object.assign(Object.assign({}, init), { headers, credentials: "include" }));
}
/**
* Builds the base URL path for an entity and file
* @param entity The entity reference
* @param filename The filename
* @returns The formatted URL path
*/
getEntityPath(entity, filename) {
const encode = encodeURIComponent;
return `${this.baseUrl}/${encode(entity.table)}/${encode(entity.id)}/${encode(entity.field)}/${encode(filename)}`;
}
/**
* Upload a file associated with an entity
* @param entity The entity reference
* @param file The file to upload
* @param metadata Optional metadata for the file
* @param isPublic Whether the file should be publicly accessible
* @returns Promise with file details
*/
upload(entity_1, file_1, metadata_1) {
return __awaiter(this, arguments, void 0, function* (entity, file, metadata, isPublic = false) {
const formData = new FormData();
formData.append("file", file);
formData.append("entity", JSON.stringify(entity));
if (metadata)
formData.append("metadata", JSON.stringify(metadata));
// Add the public flag to form data
formData.append("public", String(isPublic));
const response = yield this.fetch(`${this.baseUrl}/upload`, {
method: "POST",
body: formData
});
if (!response.ok) {
const errorText = yield response.text();
throw new Error(`Failed to upload file: ${errorText}`);
}
return response.json();
});
}
/**
* Generate a download URL for a file
* @param entity The entity reference
* @param filename The filename
* @returns The download URL
*/
getDownloadUrl(entity, filename) {
return this.getEntityPath(entity, filename);
}
/**
* Download a file associated with an entity
* @param entity The entity reference
* @param filename The filename
* @returns Promise that resolves when the download starts
*/
download(entity, filename) {
return __awaiter(this, void 0, void 0, function* () {
const url = this.getEntityPath(entity, filename);
const response = yield this.fetch(url);
if (!response.ok) {
const errorText = yield response.text();
throw new Error(`Failed to download file: ${errorText}`);
}
const blob = yield response.blob();
// Create a download link and trigger the download
const downloadUrl = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
// Clean up
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(downloadUrl);
}, 0);
});
}
/**
* Get the file content as a Blob without triggering a download
* @param entity The entity reference
* @param filename The filename
* @returns Promise with the file blob
*/
getFileBlob(entity, filename) {
return __awaiter(this, void 0, void 0, function* () {
const url = this.getEntityPath(entity, filename);
const response = yield this.fetch(url);
if (!response.ok) {
const errorText = yield response.text();
throw new Error(`Failed to get file blob: ${errorText}`);
}
return response.blob();
});
}
/**
* Delete a file associated with an entity
* @param entity The entity reference
* @param filename The filename
* @returns Promise that resolves when the deletion is complete
*/
delete(entity, filename) {
return __awaiter(this, void 0, void 0, function* () {
const url = this.getEntityPath(entity, filename);
const response = yield this.fetch(url, { method: "DELETE" });
if (!response.ok) {
const errorText = yield response.text();
throw new Error(`Failed to delete file: ${errorText}`);
}
});
}
}
exports.default = FineStorageClient;
//# sourceMappingURL=storage.js.map