1/5/1404
This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.StorageClient = void 0;
|
||||
const StorageFileApi_1 = __importDefault(require("./packages/StorageFileApi"));
|
||||
const StorageBucketApi_1 = __importDefault(require("./packages/StorageBucketApi"));
|
||||
class StorageClient extends StorageBucketApi_1.default {
|
||||
constructor(url, headers = {}, fetch) {
|
||||
super(url, headers, fetch);
|
||||
}
|
||||
/**
|
||||
* Perform file operation in a bucket.
|
||||
*
|
||||
* @param id The bucket id to operate on.
|
||||
*/
|
||||
from(id) {
|
||||
return new StorageFileApi_1.default(this.url, this.headers, id, this.fetch);
|
||||
}
|
||||
}
|
||||
exports.StorageClient = StorageClient;
|
||||
//# sourceMappingURL=StorageClient.js.map
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.StorageClient = void 0;
|
||||
var StorageClient_1 = require("./StorageClient");
|
||||
Object.defineProperty(exports, "StorageClient", { enumerable: true, get: function () { return StorageClient_1.StorageClient; } });
|
||||
__exportStar(require("./lib/types"), exports);
|
||||
__exportStar(require("./lib/errors"), exports);
|
||||
//# sourceMappingURL=index.js.map
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.DEFAULT_HEADERS = void 0;
|
||||
const version_1 = require("./version");
|
||||
exports.DEFAULT_HEADERS = { 'X-Client-Info': `storage-js/${version_1.version}` };
|
||||
//# sourceMappingURL=constants.js.map
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.StorageUnknownError = exports.StorageApiError = exports.isStorageError = exports.StorageError = void 0;
|
||||
class StorageError extends Error {
|
||||
constructor(message) {
|
||||
super(message);
|
||||
this.__isStorageError = true;
|
||||
this.name = 'StorageError';
|
||||
}
|
||||
}
|
||||
exports.StorageError = StorageError;
|
||||
function isStorageError(error) {
|
||||
return typeof error === 'object' && error !== null && '__isStorageError' in error;
|
||||
}
|
||||
exports.isStorageError = isStorageError;
|
||||
class StorageApiError extends StorageError {
|
||||
constructor(message, status) {
|
||||
super(message);
|
||||
this.name = 'StorageApiError';
|
||||
this.status = status;
|
||||
}
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.name,
|
||||
message: this.message,
|
||||
status: this.status,
|
||||
};
|
||||
}
|
||||
}
|
||||
exports.StorageApiError = StorageApiError;
|
||||
class StorageUnknownError extends StorageError {
|
||||
constructor(message, originalError) {
|
||||
super(message);
|
||||
this.name = 'StorageUnknownError';
|
||||
this.originalError = originalError;
|
||||
}
|
||||
}
|
||||
exports.StorageUnknownError = StorageUnknownError;
|
||||
//# sourceMappingURL=errors.js.map
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
"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.remove = exports.head = exports.put = exports.post = exports.get = void 0;
|
||||
const errors_1 = require("./errors");
|
||||
const helpers_1 = require("./helpers");
|
||||
const _getErrorMessage = (err) => err.msg || err.message || err.error_description || err.error || JSON.stringify(err);
|
||||
const handleError = (error, reject, options) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
const Res = yield (0, helpers_1.resolveResponse)();
|
||||
if (error instanceof Res && !(options === null || options === void 0 ? void 0 : options.noResolveJson)) {
|
||||
error
|
||||
.json()
|
||||
.then((err) => {
|
||||
reject(new errors_1.StorageApiError(_getErrorMessage(err), error.status || 500));
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(new errors_1.StorageUnknownError(_getErrorMessage(err), err));
|
||||
});
|
||||
}
|
||||
else {
|
||||
reject(new errors_1.StorageUnknownError(_getErrorMessage(error), error));
|
||||
}
|
||||
});
|
||||
const _getRequestParams = (method, options, parameters, body) => {
|
||||
const params = { method, headers: (options === null || options === void 0 ? void 0 : options.headers) || {} };
|
||||
if (method === 'GET') {
|
||||
return params;
|
||||
}
|
||||
params.headers = Object.assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers);
|
||||
if (body) {
|
||||
params.body = JSON.stringify(body);
|
||||
}
|
||||
return Object.assign(Object.assign({}, params), parameters);
|
||||
};
|
||||
function _handleRequest(fetcher, method, url, options, parameters, body) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
return new Promise((resolve, reject) => {
|
||||
fetcher(url, _getRequestParams(method, options, parameters, body))
|
||||
.then((result) => {
|
||||
if (!result.ok)
|
||||
throw result;
|
||||
if (options === null || options === void 0 ? void 0 : options.noResolveJson)
|
||||
return result;
|
||||
return result.json();
|
||||
})
|
||||
.then((data) => resolve(data))
|
||||
.catch((error) => handleError(error, reject, options));
|
||||
});
|
||||
});
|
||||
}
|
||||
function get(fetcher, url, options, parameters) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
return _handleRequest(fetcher, 'GET', url, options, parameters);
|
||||
});
|
||||
}
|
||||
exports.get = get;
|
||||
function post(fetcher, url, body, options, parameters) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
return _handleRequest(fetcher, 'POST', url, options, parameters, body);
|
||||
});
|
||||
}
|
||||
exports.post = post;
|
||||
function put(fetcher, url, body, options, parameters) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
return _handleRequest(fetcher, 'PUT', url, options, parameters, body);
|
||||
});
|
||||
}
|
||||
exports.put = put;
|
||||
function head(fetcher, url, options, parameters) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
return _handleRequest(fetcher, 'HEAD', url, Object.assign(Object.assign({}, options), { noResolveJson: true }), parameters);
|
||||
});
|
||||
}
|
||||
exports.head = head;
|
||||
function remove(fetcher, url, body, options, parameters) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
return _handleRequest(fetcher, 'DELETE', url, options, parameters, body);
|
||||
});
|
||||
}
|
||||
exports.remove = remove;
|
||||
//# sourceMappingURL=fetch.js.map
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
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.recursiveToCamel = exports.resolveResponse = exports.resolveFetch = void 0;
|
||||
const resolveFetch = (customFetch) => {
|
||||
let _fetch;
|
||||
if (customFetch) {
|
||||
_fetch = customFetch;
|
||||
}
|
||||
else if (typeof fetch === 'undefined') {
|
||||
_fetch = (...args) => Promise.resolve().then(() => __importStar(require('@supabase/node-fetch'))).then(({ default: fetch }) => fetch(...args));
|
||||
}
|
||||
else {
|
||||
_fetch = fetch;
|
||||
}
|
||||
return (...args) => _fetch(...args);
|
||||
};
|
||||
exports.resolveFetch = resolveFetch;
|
||||
const resolveResponse = () => __awaiter(void 0, void 0, void 0, function* () {
|
||||
if (typeof Response === 'undefined') {
|
||||
// @ts-ignore
|
||||
return (yield Promise.resolve().then(() => __importStar(require('@supabase/node-fetch')))).Response;
|
||||
}
|
||||
return Response;
|
||||
});
|
||||
exports.resolveResponse = resolveResponse;
|
||||
const recursiveToCamel = (item) => {
|
||||
if (Array.isArray(item)) {
|
||||
return item.map((el) => (0, exports.recursiveToCamel)(el));
|
||||
}
|
||||
else if (typeof item === 'function' || item !== Object(item)) {
|
||||
return item;
|
||||
}
|
||||
const result = {};
|
||||
Object.entries(item).forEach(([key, value]) => {
|
||||
const newKey = key.replace(/([-_][a-z])/gi, (c) => c.toUpperCase().replace(/[-_]/g, ''));
|
||||
result[newKey] = (0, exports.recursiveToCamel)(value);
|
||||
});
|
||||
return result;
|
||||
};
|
||||
exports.recursiveToCamel = recursiveToCamel;
|
||||
//# sourceMappingURL=helpers.js.map
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
//# sourceMappingURL=types.js.map
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.version = void 0;
|
||||
// generated by genversion
|
||||
exports.version = '2.7.1';
|
||||
//# sourceMappingURL=version.js.map
|
||||
Generated
Vendored
+166
@@ -0,0 +1,166 @@
|
||||
"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 constants_1 = require("../lib/constants");
|
||||
const errors_1 = require("../lib/errors");
|
||||
const fetch_1 = require("../lib/fetch");
|
||||
const helpers_1 = require("../lib/helpers");
|
||||
class StorageBucketApi {
|
||||
constructor(url, headers = {}, fetch) {
|
||||
this.url = url;
|
||||
this.headers = Object.assign(Object.assign({}, constants_1.DEFAULT_HEADERS), headers);
|
||||
this.fetch = (0, helpers_1.resolveFetch)(fetch);
|
||||
}
|
||||
/**
|
||||
* Retrieves the details of all Storage buckets within an existing project.
|
||||
*/
|
||||
listBuckets() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
const data = yield (0, fetch_1.get)(this.fetch, `${this.url}/bucket`, { headers: this.headers });
|
||||
return { data, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Retrieves the details of an existing Storage bucket.
|
||||
*
|
||||
* @param id The unique identifier of the bucket you would like to retrieve.
|
||||
*/
|
||||
getBucket(id) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
const data = yield (0, fetch_1.get)(this.fetch, `${this.url}/bucket/${id}`, { headers: this.headers });
|
||||
return { data, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Creates a new Storage bucket
|
||||
*
|
||||
* @param id A unique identifier for the bucket you are creating.
|
||||
* @param options.public The visibility of the bucket. Public buckets don't require an authorization token to download objects, but still require a valid token for all other operations. By default, buckets are private.
|
||||
* @param options.fileSizeLimit specifies the max file size in bytes that can be uploaded to this bucket.
|
||||
* The global file size limit takes precedence over this value.
|
||||
* The default value is null, which doesn't set a per bucket file size limit.
|
||||
* @param options.allowedMimeTypes specifies the allowed mime types that this bucket can accept during upload.
|
||||
* The default value is null, which allows files with all mime types to be uploaded.
|
||||
* Each mime type specified can be a wildcard, e.g. image/*, or a specific mime type, e.g. image/png.
|
||||
* @returns newly created bucket id
|
||||
*/
|
||||
createBucket(id, options = {
|
||||
public: false,
|
||||
}) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
const data = yield (0, fetch_1.post)(this.fetch, `${this.url}/bucket`, {
|
||||
id,
|
||||
name: id,
|
||||
public: options.public,
|
||||
file_size_limit: options.fileSizeLimit,
|
||||
allowed_mime_types: options.allowedMimeTypes,
|
||||
}, { headers: this.headers });
|
||||
return { data, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Updates a Storage bucket
|
||||
*
|
||||
* @param id A unique identifier for the bucket you are updating.
|
||||
* @param options.public The visibility of the bucket. Public buckets don't require an authorization token to download objects, but still require a valid token for all other operations.
|
||||
* @param options.fileSizeLimit specifies the max file size in bytes that can be uploaded to this bucket.
|
||||
* The global file size limit takes precedence over this value.
|
||||
* The default value is null, which doesn't set a per bucket file size limit.
|
||||
* @param options.allowedMimeTypes specifies the allowed mime types that this bucket can accept during upload.
|
||||
* The default value is null, which allows files with all mime types to be uploaded.
|
||||
* Each mime type specified can be a wildcard, e.g. image/*, or a specific mime type, e.g. image/png.
|
||||
*/
|
||||
updateBucket(id, options) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
const data = yield (0, fetch_1.put)(this.fetch, `${this.url}/bucket/${id}`, {
|
||||
id,
|
||||
name: id,
|
||||
public: options.public,
|
||||
file_size_limit: options.fileSizeLimit,
|
||||
allowed_mime_types: options.allowedMimeTypes,
|
||||
}, { headers: this.headers });
|
||||
return { data, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Removes all objects inside a single bucket.
|
||||
*
|
||||
* @param id The unique identifier of the bucket you would like to empty.
|
||||
*/
|
||||
emptyBucket(id) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
const data = yield (0, fetch_1.post)(this.fetch, `${this.url}/bucket/${id}/empty`, {}, { headers: this.headers });
|
||||
return { data, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Deletes an existing bucket. A bucket can't be deleted with existing objects inside it.
|
||||
* You must first `empty()` the bucket.
|
||||
*
|
||||
* @param id The unique identifier of the bucket you would like to delete.
|
||||
*/
|
||||
deleteBucket(id) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
const data = yield (0, fetch_1.remove)(this.fetch, `${this.url}/bucket/${id}`, {}, { headers: this.headers });
|
||||
return { data, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.default = StorageBucketApi;
|
||||
//# sourceMappingURL=StorageBucketApi.js.map
|
||||
Generated
Vendored
+553
@@ -0,0 +1,553 @@
|
||||
"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 errors_1 = require("../lib/errors");
|
||||
const fetch_1 = require("../lib/fetch");
|
||||
const helpers_1 = require("../lib/helpers");
|
||||
const DEFAULT_SEARCH_OPTIONS = {
|
||||
limit: 100,
|
||||
offset: 0,
|
||||
sortBy: {
|
||||
column: 'name',
|
||||
order: 'asc',
|
||||
},
|
||||
};
|
||||
const DEFAULT_FILE_OPTIONS = {
|
||||
cacheControl: '3600',
|
||||
contentType: 'text/plain;charset=UTF-8',
|
||||
upsert: false,
|
||||
};
|
||||
class StorageFileApi {
|
||||
constructor(url, headers = {}, bucketId, fetch) {
|
||||
this.url = url;
|
||||
this.headers = headers;
|
||||
this.bucketId = bucketId;
|
||||
this.fetch = (0, helpers_1.resolveFetch)(fetch);
|
||||
}
|
||||
/**
|
||||
* Uploads a file to an existing bucket or replaces an existing file at the specified path with a new one.
|
||||
*
|
||||
* @param method HTTP method.
|
||||
* @param path The relative file path. Should be of the format `folder/subfolder/filename.png`. The bucket must already exist before attempting to upload.
|
||||
* @param fileBody The body of the file to be stored in the bucket.
|
||||
*/
|
||||
uploadOrUpdate(method, path, fileBody, fileOptions) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
let body;
|
||||
const options = Object.assign(Object.assign({}, DEFAULT_FILE_OPTIONS), fileOptions);
|
||||
let headers = Object.assign(Object.assign({}, this.headers), (method === 'POST' && { 'x-upsert': String(options.upsert) }));
|
||||
const metadata = options.metadata;
|
||||
if (typeof Blob !== 'undefined' && fileBody instanceof Blob) {
|
||||
body = new FormData();
|
||||
body.append('cacheControl', options.cacheControl);
|
||||
if (metadata) {
|
||||
body.append('metadata', this.encodeMetadata(metadata));
|
||||
}
|
||||
body.append('', fileBody);
|
||||
}
|
||||
else if (typeof FormData !== 'undefined' && fileBody instanceof FormData) {
|
||||
body = fileBody;
|
||||
body.append('cacheControl', options.cacheControl);
|
||||
if (metadata) {
|
||||
body.append('metadata', this.encodeMetadata(metadata));
|
||||
}
|
||||
}
|
||||
else {
|
||||
body = fileBody;
|
||||
headers['cache-control'] = `max-age=${options.cacheControl}`;
|
||||
headers['content-type'] = options.contentType;
|
||||
if (metadata) {
|
||||
headers['x-metadata'] = this.toBase64(this.encodeMetadata(metadata));
|
||||
}
|
||||
}
|
||||
if (fileOptions === null || fileOptions === void 0 ? void 0 : fileOptions.headers) {
|
||||
headers = Object.assign(Object.assign({}, headers), fileOptions.headers);
|
||||
}
|
||||
const cleanPath = this._removeEmptyFolders(path);
|
||||
const _path = this._getFinalPath(cleanPath);
|
||||
const res = yield this.fetch(`${this.url}/object/${_path}`, Object.assign({ method, body: body, headers }, ((options === null || options === void 0 ? void 0 : options.duplex) ? { duplex: options.duplex } : {})));
|
||||
const data = yield res.json();
|
||||
if (res.ok) {
|
||||
return {
|
||||
data: { path: cleanPath, id: data.Id, fullPath: data.Key },
|
||||
error: null,
|
||||
};
|
||||
}
|
||||
else {
|
||||
const error = data;
|
||||
return { data: null, error };
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Uploads a file to an existing bucket.
|
||||
*
|
||||
* @param path The file path, including the file name. Should be of the format `folder/subfolder/filename.png`. The bucket must already exist before attempting to upload.
|
||||
* @param fileBody The body of the file to be stored in the bucket.
|
||||
*/
|
||||
upload(path, fileBody, fileOptions) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
return this.uploadOrUpdate('POST', path, fileBody, fileOptions);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Upload a file with a token generated from `createSignedUploadUrl`.
|
||||
* @param path The file path, including the file name. Should be of the format `folder/subfolder/filename.png`. The bucket must already exist before attempting to upload.
|
||||
* @param token The token generated from `createSignedUploadUrl`
|
||||
* @param fileBody The body of the file to be stored in the bucket.
|
||||
*/
|
||||
uploadToSignedUrl(path, token, fileBody, fileOptions) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const cleanPath = this._removeEmptyFolders(path);
|
||||
const _path = this._getFinalPath(cleanPath);
|
||||
const url = new URL(this.url + `/object/upload/sign/${_path}`);
|
||||
url.searchParams.set('token', token);
|
||||
try {
|
||||
let body;
|
||||
const options = Object.assign({ upsert: DEFAULT_FILE_OPTIONS.upsert }, fileOptions);
|
||||
const headers = Object.assign(Object.assign({}, this.headers), { 'x-upsert': String(options.upsert) });
|
||||
if (typeof Blob !== 'undefined' && fileBody instanceof Blob) {
|
||||
body = new FormData();
|
||||
body.append('cacheControl', options.cacheControl);
|
||||
body.append('', fileBody);
|
||||
}
|
||||
else if (typeof FormData !== 'undefined' && fileBody instanceof FormData) {
|
||||
body = fileBody;
|
||||
body.append('cacheControl', options.cacheControl);
|
||||
}
|
||||
else {
|
||||
body = fileBody;
|
||||
headers['cache-control'] = `max-age=${options.cacheControl}`;
|
||||
headers['content-type'] = options.contentType;
|
||||
}
|
||||
const res = yield this.fetch(url.toString(), {
|
||||
method: 'PUT',
|
||||
body: body,
|
||||
headers,
|
||||
});
|
||||
const data = yield res.json();
|
||||
if (res.ok) {
|
||||
return {
|
||||
data: { path: cleanPath, fullPath: data.Key },
|
||||
error: null,
|
||||
};
|
||||
}
|
||||
else {
|
||||
const error = data;
|
||||
return { data: null, error };
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Creates a signed upload URL.
|
||||
* Signed upload URLs can be used to upload files to the bucket without further authentication.
|
||||
* They are valid for 2 hours.
|
||||
* @param path The file path, including the current file name. For example `folder/image.png`.
|
||||
* @param options.upsert If set to true, allows the file to be overwritten if it already exists.
|
||||
*/
|
||||
createSignedUploadUrl(path, options) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
let _path = this._getFinalPath(path);
|
||||
const headers = Object.assign({}, this.headers);
|
||||
if (options === null || options === void 0 ? void 0 : options.upsert) {
|
||||
headers['x-upsert'] = 'true';
|
||||
}
|
||||
const data = yield (0, fetch_1.post)(this.fetch, `${this.url}/object/upload/sign/${_path}`, {}, { headers });
|
||||
const url = new URL(this.url + data.url);
|
||||
const token = url.searchParams.get('token');
|
||||
if (!token) {
|
||||
throw new errors_1.StorageError('No token returned by API');
|
||||
}
|
||||
return { data: { signedUrl: url.toString(), path, token }, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Replaces an existing file at the specified path with a new one.
|
||||
*
|
||||
* @param path The relative file path. Should be of the format `folder/subfolder/filename.png`. The bucket must already exist before attempting to update.
|
||||
* @param fileBody The body of the file to be stored in the bucket.
|
||||
*/
|
||||
update(path, fileBody, fileOptions) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
return this.uploadOrUpdate('PUT', path, fileBody, fileOptions);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Moves an existing file to a new path in the same bucket.
|
||||
*
|
||||
* @param fromPath The original file path, including the current file name. For example `folder/image.png`.
|
||||
* @param toPath The new file path, including the new file name. For example `folder/image-new.png`.
|
||||
* @param options The destination options.
|
||||
*/
|
||||
move(fromPath, toPath, options) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
const data = yield (0, fetch_1.post)(this.fetch, `${this.url}/object/move`, {
|
||||
bucketId: this.bucketId,
|
||||
sourceKey: fromPath,
|
||||
destinationKey: toPath,
|
||||
destinationBucket: options === null || options === void 0 ? void 0 : options.destinationBucket,
|
||||
}, { headers: this.headers });
|
||||
return { data, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Copies an existing file to a new path in the same bucket.
|
||||
*
|
||||
* @param fromPath The original file path, including the current file name. For example `folder/image.png`.
|
||||
* @param toPath The new file path, including the new file name. For example `folder/image-copy.png`.
|
||||
* @param options The destination options.
|
||||
*/
|
||||
copy(fromPath, toPath, options) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
const data = yield (0, fetch_1.post)(this.fetch, `${this.url}/object/copy`, {
|
||||
bucketId: this.bucketId,
|
||||
sourceKey: fromPath,
|
||||
destinationKey: toPath,
|
||||
destinationBucket: options === null || options === void 0 ? void 0 : options.destinationBucket,
|
||||
}, { headers: this.headers });
|
||||
return { data: { path: data.Key }, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Creates a signed URL. Use a signed URL to share a file for a fixed amount of time.
|
||||
*
|
||||
* @param path The file path, including the current file name. For example `folder/image.png`.
|
||||
* @param expiresIn The number of seconds until the signed URL expires. For example, `60` for a URL which is valid for one minute.
|
||||
* @param options.download triggers the file as a download if set to true. Set this parameter as the name of the file if you want to trigger the download with a different filename.
|
||||
* @param options.transform Transform the asset before serving it to the client.
|
||||
*/
|
||||
createSignedUrl(path, expiresIn, options) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
let _path = this._getFinalPath(path);
|
||||
let data = yield (0, fetch_1.post)(this.fetch, `${this.url}/object/sign/${_path}`, Object.assign({ expiresIn }, ((options === null || options === void 0 ? void 0 : options.transform) ? { transform: options.transform } : {})), { headers: this.headers });
|
||||
const downloadQueryParam = (options === null || options === void 0 ? void 0 : options.download)
|
||||
? `&download=${options.download === true ? '' : options.download}`
|
||||
: '';
|
||||
const signedUrl = encodeURI(`${this.url}${data.signedURL}${downloadQueryParam}`);
|
||||
data = { signedUrl };
|
||||
return { data, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Creates multiple signed URLs. Use a signed URL to share a file for a fixed amount of time.
|
||||
*
|
||||
* @param paths The file paths to be downloaded, including the current file names. For example `['folder/image.png', 'folder2/image2.png']`.
|
||||
* @param expiresIn The number of seconds until the signed URLs expire. For example, `60` for URLs which are valid for one minute.
|
||||
* @param options.download triggers the file as a download if set to true. Set this parameter as the name of the file if you want to trigger the download with a different filename.
|
||||
*/
|
||||
createSignedUrls(paths, expiresIn, options) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
const data = yield (0, fetch_1.post)(this.fetch, `${this.url}/object/sign/${this.bucketId}`, { expiresIn, paths }, { headers: this.headers });
|
||||
const downloadQueryParam = (options === null || options === void 0 ? void 0 : options.download)
|
||||
? `&download=${options.download === true ? '' : options.download}`
|
||||
: '';
|
||||
return {
|
||||
data: data.map((datum) => (Object.assign(Object.assign({}, datum), { signedUrl: datum.signedURL
|
||||
? encodeURI(`${this.url}${datum.signedURL}${downloadQueryParam}`)
|
||||
: null }))),
|
||||
error: null,
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Downloads a file from a private bucket. For public buckets, make a request to the URL returned from `getPublicUrl` instead.
|
||||
*
|
||||
* @param path The full path and file name of the file to be downloaded. For example `folder/image.png`.
|
||||
* @param options.transform Transform the asset before serving it to the client.
|
||||
*/
|
||||
download(path, options) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const wantsTransformation = typeof (options === null || options === void 0 ? void 0 : options.transform) !== 'undefined';
|
||||
const renderPath = wantsTransformation ? 'render/image/authenticated' : 'object';
|
||||
const transformationQuery = this.transformOptsToQueryString((options === null || options === void 0 ? void 0 : options.transform) || {});
|
||||
const queryString = transformationQuery ? `?${transformationQuery}` : '';
|
||||
try {
|
||||
const _path = this._getFinalPath(path);
|
||||
const res = yield (0, fetch_1.get)(this.fetch, `${this.url}/${renderPath}/${_path}${queryString}`, {
|
||||
headers: this.headers,
|
||||
noResolveJson: true,
|
||||
});
|
||||
const data = yield res.blob();
|
||||
return { data, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Retrieves the details of an existing file.
|
||||
* @param path
|
||||
*/
|
||||
info(path) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const _path = this._getFinalPath(path);
|
||||
try {
|
||||
const data = yield (0, fetch_1.get)(this.fetch, `${this.url}/object/info/${_path}`, {
|
||||
headers: this.headers,
|
||||
});
|
||||
return { data: (0, helpers_1.recursiveToCamel)(data), error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Checks the existence of a file.
|
||||
* @param path
|
||||
*/
|
||||
exists(path) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const _path = this._getFinalPath(path);
|
||||
try {
|
||||
yield (0, fetch_1.head)(this.fetch, `${this.url}/object/${_path}`, {
|
||||
headers: this.headers,
|
||||
});
|
||||
return { data: true, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error) && error instanceof errors_1.StorageUnknownError) {
|
||||
const originalError = error.originalError;
|
||||
if ([400, 404].includes(originalError === null || originalError === void 0 ? void 0 : originalError.status)) {
|
||||
return { data: false, error };
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* A simple convenience function to get the URL for an asset in a public bucket. If you do not want to use this function, you can construct the public URL by concatenating the bucket URL with the path to the asset.
|
||||
* This function does not verify if the bucket is public. If a public URL is created for a bucket which is not public, you will not be able to download the asset.
|
||||
*
|
||||
* @param path The path and name of the file to generate the public URL for. For example `folder/image.png`.
|
||||
* @param options.download Triggers the file as a download if set to true. Set this parameter as the name of the file if you want to trigger the download with a different filename.
|
||||
* @param options.transform Transform the asset before serving it to the client.
|
||||
*/
|
||||
getPublicUrl(path, options) {
|
||||
const _path = this._getFinalPath(path);
|
||||
const _queryString = [];
|
||||
const downloadQueryParam = (options === null || options === void 0 ? void 0 : options.download)
|
||||
? `download=${options.download === true ? '' : options.download}`
|
||||
: '';
|
||||
if (downloadQueryParam !== '') {
|
||||
_queryString.push(downloadQueryParam);
|
||||
}
|
||||
const wantsTransformation = typeof (options === null || options === void 0 ? void 0 : options.transform) !== 'undefined';
|
||||
const renderPath = wantsTransformation ? 'render/image' : 'object';
|
||||
const transformationQuery = this.transformOptsToQueryString((options === null || options === void 0 ? void 0 : options.transform) || {});
|
||||
if (transformationQuery !== '') {
|
||||
_queryString.push(transformationQuery);
|
||||
}
|
||||
let queryString = _queryString.join('&');
|
||||
if (queryString !== '') {
|
||||
queryString = `?${queryString}`;
|
||||
}
|
||||
return {
|
||||
data: { publicUrl: encodeURI(`${this.url}/${renderPath}/public/${_path}${queryString}`) },
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Deletes files within the same bucket
|
||||
*
|
||||
* @param paths An array of files to delete, including the path and file name. For example [`'folder/image.png'`].
|
||||
*/
|
||||
remove(paths) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
const data = yield (0, fetch_1.remove)(this.fetch, `${this.url}/object/${this.bucketId}`, { prefixes: paths }, { headers: this.headers });
|
||||
return { data, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get file metadata
|
||||
* @param id the file id to retrieve metadata
|
||||
*/
|
||||
// async getMetadata(
|
||||
// id: string
|
||||
// ): Promise<
|
||||
// | {
|
||||
// data: Metadata
|
||||
// error: null
|
||||
// }
|
||||
// | {
|
||||
// data: null
|
||||
// error: StorageError
|
||||
// }
|
||||
// > {
|
||||
// try {
|
||||
// const data = await get(this.fetch, `${this.url}/metadata/${id}`, { headers: this.headers })
|
||||
// return { data, error: null }
|
||||
// } catch (error) {
|
||||
// if (isStorageError(error)) {
|
||||
// return { data: null, error }
|
||||
// }
|
||||
// throw error
|
||||
// }
|
||||
// }
|
||||
/**
|
||||
* Update file metadata
|
||||
* @param id the file id to update metadata
|
||||
* @param meta the new file metadata
|
||||
*/
|
||||
// async updateMetadata(
|
||||
// id: string,
|
||||
// meta: Metadata
|
||||
// ): Promise<
|
||||
// | {
|
||||
// data: Metadata
|
||||
// error: null
|
||||
// }
|
||||
// | {
|
||||
// data: null
|
||||
// error: StorageError
|
||||
// }
|
||||
// > {
|
||||
// try {
|
||||
// const data = await post(
|
||||
// this.fetch,
|
||||
// `${this.url}/metadata/${id}`,
|
||||
// { ...meta },
|
||||
// { headers: this.headers }
|
||||
// )
|
||||
// return { data, error: null }
|
||||
// } catch (error) {
|
||||
// if (isStorageError(error)) {
|
||||
// return { data: null, error }
|
||||
// }
|
||||
// throw error
|
||||
// }
|
||||
// }
|
||||
/**
|
||||
* Lists all the files within a bucket.
|
||||
* @param path The folder path.
|
||||
*/
|
||||
list(path, options, parameters) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
const body = Object.assign(Object.assign(Object.assign({}, DEFAULT_SEARCH_OPTIONS), options), { prefix: path || '' });
|
||||
const data = yield (0, fetch_1.post)(this.fetch, `${this.url}/object/list/${this.bucketId}`, body, { headers: this.headers }, parameters);
|
||||
return { data, error: null };
|
||||
}
|
||||
catch (error) {
|
||||
if ((0, errors_1.isStorageError)(error)) {
|
||||
return { data: null, error };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
encodeMetadata(metadata) {
|
||||
return JSON.stringify(metadata);
|
||||
}
|
||||
toBase64(data) {
|
||||
if (typeof Buffer !== 'undefined') {
|
||||
return Buffer.from(data).toString('base64');
|
||||
}
|
||||
return btoa(data);
|
||||
}
|
||||
_getFinalPath(path) {
|
||||
return `${this.bucketId}/${path}`;
|
||||
}
|
||||
_removeEmptyFolders(path) {
|
||||
return path.replace(/^\/|\/$/g, '').replace(/\/+/g, '/');
|
||||
}
|
||||
transformOptsToQueryString(transform) {
|
||||
const params = [];
|
||||
if (transform.width) {
|
||||
params.push(`width=${transform.width}`);
|
||||
}
|
||||
if (transform.height) {
|
||||
params.push(`height=${transform.height}`);
|
||||
}
|
||||
if (transform.resize) {
|
||||
params.push(`resize=${transform.resize}`);
|
||||
}
|
||||
if (transform.format) {
|
||||
params.push(`format=${transform.format}`);
|
||||
}
|
||||
if (transform.quality) {
|
||||
params.push(`quality=${transform.quality}`);
|
||||
}
|
||||
return params.join('&');
|
||||
}
|
||||
}
|
||||
exports.default = StorageFileApi;
|
||||
//# sourceMappingURL=StorageFileApi.js.map
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
{
|
||||
"name": "@supabase/storage-js",
|
||||
"version": "2.7.1",
|
||||
"description": "Isomorphic storage client for Supabase.",
|
||||
"keywords": [
|
||||
"javascript",
|
||||
"typescript",
|
||||
"supabase"
|
||||
],
|
||||
"homepage": "https://github.com/supabase/storage-js",
|
||||
"bugs": "https://github.com/supabase/storage-js/issues",
|
||||
"license": "MIT",
|
||||
"author": "Supabase",
|
||||
"files": [
|
||||
"dist",
|
||||
"src"
|
||||
],
|
||||
"main": "dist/main/index.js",
|
||||
"module": "dist/module/index.js",
|
||||
"types": "dist/module/index.d.ts",
|
||||
"sideEffects": false,
|
||||
"repository": "supabase/storage-js",
|
||||
"scripts": {
|
||||
"clean": "rimraf dist docs/v2",
|
||||
"format": "prettier --write \"{src,test}/**/*.ts\"",
|
||||
"build": "genversion src/lib/version.ts --es6 && run-s clean format build:*",
|
||||
"build:main": "tsc -p tsconfig.json",
|
||||
"build:module": "tsc -p tsconfig.module.json",
|
||||
"build:umd": "webpack",
|
||||
"types-generate": "dts-gen -m '@supabase/storage-js' -s",
|
||||
"test": "run-s test:clean test:infra test:suite test:clean",
|
||||
"test:suite": "jest --runInBand",
|
||||
"test:infra": "cd infra && docker compose down && docker compose up -d --build && sleep 10",
|
||||
"test:clean": "cd infra && docker compose down --remove-orphans",
|
||||
"docs": "typedoc --entryPoints src/index.ts --out docs/v2 --entryPoints src/packages/* --excludePrivate --excludeProtected",
|
||||
"docs:json": "typedoc --json docs/v2/spec.json --entryPoints src/index.ts --entryPoints src/packages/* --excludePrivate --excludeExternals --excludeProtected"
|
||||
},
|
||||
"dependencies": {
|
||||
"@supabase/node-fetch": "^2.6.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^26.0.13",
|
||||
"form-data": "^4.0.0",
|
||||
"genversion": "^3.0.1",
|
||||
"husky": "^4.3.0",
|
||||
"jest": "^29.0.0",
|
||||
"jest-environment-jsdom": "^29.5.0",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"prettier": "^2.1.2",
|
||||
"pretty-quick": "^3.1.0",
|
||||
"rimraf": "^3.0.2",
|
||||
"semantic-release-plugin-update-version-in-files": "^1.1.0",
|
||||
"ts-jest": "^29.0.0",
|
||||
"ts-loader": "^9.4.2",
|
||||
"typedoc": "^0.22.16",
|
||||
"typescript": "^4.6.3",
|
||||
"webpack": "^5.75.0",
|
||||
"webpack-cli": "^5.0.1"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "pretty-quick --staged"
|
||||
}
|
||||
},
|
||||
"jsdelivr": "dist/umd/supabase.js",
|
||||
"unpkg": "dist/umd/supabase.js",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user