@supabase/storage-js
Version:
Isomorphic storage client for Supabase.
94 lines • 4.34 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 });
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) => {
const status = error.status || 500;
const statusCode = (err === null || err === void 0 ? void 0 : err.statusCode) || status + '';
reject(new errors_1.StorageApiError(_getErrorMessage(err), status, statusCode));
})
.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' || !body) {
return params;
}
if ((0, helpers_1.isPlainObject)(body)) {
params.headers = Object.assign({ 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers);
params.body = JSON.stringify(body);
}
else {
params.body = 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
;