UNPKG

@nurbxfit/cloudflare-r2

Version:

Simple nodejs client SDK for cloudflare r2

75 lines (74 loc) 3.49 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 }); // seems like unnecessary to implement, but why not class Bucket { constructor(bucketResult, client) { this.name = bucketResult.name; this.creationDate = bucketResult.creation_date; this.client = client; } getObjects() { return __awaiter(this, void 0, void 0, function* () { const bucketObjectsResponse = yield this.client.getBucketObjects(this.name); return bucketObjectsResponse.result; }); } getObject(objectKey) { return __awaiter(this, void 0, void 0, function* () { const bucketObjectsResponse = yield this.client.searchBucketObjects(this.name, objectKey); // this is unnecessary and weird, const bucketObject = bucketObjectsResponse.result.find((object) => object.key == objectKey); // not sure want to do something like this or just return nothing if not found. // if (!bucketObject) throw new Error("Object not found!"); return bucketObject; }); } uploadObject(objectKey, objectBin, contentType) { return __awaiter(this, void 0, void 0, function* () { const uploadBucketResponse = yield this.client.putBucketObject(this.name, objectKey, objectBin, contentType); return uploadBucketResponse.result; }); } deleteObject(objectKey) { return __awaiter(this, void 0, void 0, function* () { const deletedBucketResponse = yield this.client.deleteBucketObject(this.name, objectKey); return deletedBucketResponse.result[0]; }); } deleteObjects(objectKeys) { return __awaiter(this, void 0, void 0, function* () { const deletedBucketResponse = yield this.client.deleteBucketObjects(this.name, objectKeys); return deletedBucketResponse.result; }); } getCustomDomains() { return __awaiter(this, void 0, void 0, function* () { const getCustomDomainResponse = yield this.client.getBucketCustomDomains(this.name); return getCustomDomainResponse.result.domains.map((d) => { if (d.status.ssl !== "active") { return `http://${d.domain}`; } return `https://${d.domain}`; }); }); } getObjectPublicURLs(objectKey) { return __awaiter(this, void 0, void 0, function* () { const objectCustomDomains = yield this.getCustomDomains(); const foundObject = yield this.getObject(objectKey); return !!foundObject ? objectCustomDomains.map((domain) => `${domain}/${objectKey}`) : []; }); } } exports.default = Bucket;