UNPKG

bunnycdn-storage-ts

Version:

![GitHub](https://img.shields.io/github/license/bynarig/bunnycdn-storage-ts?style=flat-square) [![npm](https://img.shields.io/npm/v/bunnycdn-storage-ts?style=flat-square)](https://www.npmjs.com/package/bunnycdn-storage-ts)

68 lines 2.46 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 }); const axios_1 = require("axios"); const fs_1 = require("fs"); const path_1 = require("path"); const defaultBaseUrl = 'https://storage.bunnycdn.com'; class BunnyCDNStorage { constructor(apiKey, storageZoneName, region) { const baseURL = region ? `https://${region}.storage.bunnycdn.com` : defaultBaseUrl; this.client = axios_1.default.create({ baseURL: `${baseURL}/${storageZoneName}/`, headers: { AccessKey: apiKey }, maxContentLength: Infinity, maxBodyLength: Infinity, }); } list(path) { return this.client({ method: 'GET', url: path }); } delete(path) { return this.client({ method: 'DELETE', url: path }); } upload(fileOrPath, remotePath) { return __awaiter(this, void 0, void 0, function* () { let file; if (!Buffer.isBuffer(fileOrPath)) { if (typeof remotePath === 'undefined') { remotePath = (0, path_1.parse)(fileOrPath).base; } file = (0, fs_1.createReadStream)(fileOrPath); } else { file = fileOrPath; } return yield this.client({ method: 'PUT', url: remotePath, data: file }); }); } download(filePath, responseType) { return this.client({ method: 'GET', url: filePath, responseType: responseType || 'arraybuffer' }); } } exports.default = BunnyCDNStorage; //# sourceMappingURL=main.js.map