@platform/cell.client
Version:
A strongly typed HTTP client for operating with a CellOS service end-point.
159 lines (158 loc) • 8.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpClientCellFs = void 0;
var tslib_1 = require("tslib");
var common_1 = require("../common");
var HttpClientCellFile_1 = require("./HttpClientCellFile");
var HttpClientCellFs_copy_1 = require("./HttpClientCellFs.copy");
var HttpClientCellFs_delete_1 = require("./HttpClientCellFs.delete");
var HttpClientCellFs_upload_1 = require("./HttpClientCellFs.upload");
function HttpClientCellFs(args) {
var _this = this;
var parent = args.parent, urls = args.urls, http = args.http;
var uri = parent.uri;
var getBase = function (args) {
if (args === void 0) { args = {}; }
return tslib_1.__awaiter(_this, void 0, void 0, function () {
var filter, getError, url, files, status_1, type, message, body;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
filter = args.filter, getError = args.getError;
url = parent.url.files.list.query({ filter: filter }).toString();
return [4, http.get(url)];
case 1:
files = _a.sent();
if (!files.ok) {
status_1 = files.status;
type = status_1 === 404 ? common_1.ERROR.HTTP.NOT_FOUND : common_1.ERROR.HTTP.SERVER;
message = getError
? getError({ status: status_1 })
: "Failed to get files data for '".concat(uri.toString(), "'.");
return [2, common_1.util.toError(status_1, type, message)];
}
body = files.json;
return [2, common_1.util.toClientResponse(200, body)];
}
});
});
};
var api = {
uri: uri,
toString: function () { return uri.toString(); },
file: function (path) {
return (0, HttpClientCellFile_1.HttpClientCellFile)({ parent: parent, urls: urls, http: http, path: path });
},
urls: function () {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function () {
var getError, base, ok, status, error, body;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
getError = function () { return "Failed to get file URLs for [".concat(uri.toString(), "]"); };
return [4, getBase({ getError: getError })];
case 1:
base = _b.sent();
ok = base.ok, status = base.status, error = base.error;
if (!ok)
return [2, common_1.util.toClientResponse(status, {}, { error: error })];
body = (_a = base.body.urls) === null || _a === void 0 ? void 0 : _a.files.map(function (item) { return toFileUrl(item); });
return [2, common_1.util.toClientResponse(status, body || [])];
}
});
});
},
map: function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var getError, base, ok, status, body, error;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
getError = function () { return "Failed to get file map for [".concat(uri.toString(), "]"); };
return [4, getBase({ getError: getError })];
case 1:
base = _a.sent();
ok = base.ok, status = base.status;
body = ok ? base.body.files : {};
error = base.error;
return [2, common_1.util.toClientResponse(status, body, { error: error })];
}
});
});
},
list: function (options) {
var _a;
if (options === void 0) { options = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var filter, getError, base, status_2, body_1, error, urls, map, ns, body;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
filter = options.filter;
getError = function () { return "Failed to get file list for [".concat(uri.toString(), "]"); };
return [4, getBase({ getError: getError, filter: filter })];
case 1:
base = _b.sent();
if (!base.ok) {
status_2 = base.status;
body_1 = {};
error = base.error;
return [2, common_1.util.toClientResponse(status_2, body_1, { error: error })];
}
urls = ((_a = base.body.urls) === null || _a === void 0 ? void 0 : _a.files) || [];
map = base.body.files || {};
ns = uri.ns;
body = Object.keys(map).reduce(function (acc, fileid) {
var value = map[fileid];
if (value) {
var uri_1 = common_1.Schema.Uri.create.file(ns, fileid);
var url = urls.find(function (item) { return item.uri === uri_1; });
if (url) {
var _a = parsePath(url.path), path = _a.path, filename = _a.filename, dir = _a.dir;
acc.push(tslib_1.__assign({ uri: uri_1, path: path, filename: filename, dir: dir }, value));
}
}
return acc;
}, []);
return [2, common_1.util.toClientResponse(200, body)];
}
});
});
},
upload: function (input, options) {
if (options === void 0) { options = {}; }
var changes = options.changes;
var cellUri = uri.toString();
return (0, HttpClientCellFs_upload_1.uploadFiles)({ input: input, http: http, urls: urls, cellUri: cellUri, changes: changes });
},
delete: function (filename) {
var urls = parent.url;
return (0, HttpClientCellFs_delete_1.deleteFiles)({ http: http, urls: urls, filename: filename, action: 'DELETE' });
},
unlink: function (filename) {
var urls = parent.url;
return (0, HttpClientCellFs_delete_1.deleteFiles)({ http: http, urls: urls, filename: filename, action: 'UNLINK' });
},
copy: function (files, options) {
if (options === void 0) { options = {}; }
var changes = options.changes, permission = options.permission;
var urls = parent.url;
return (0, HttpClientCellFs_copy_1.copyFiles)({ http: http, urls: urls, files: files, changes: changes, permission: permission });
},
};
return api;
}
exports.HttpClientCellFs = HttpClientCellFs;
function parsePath(path) {
var index = path.lastIndexOf('/');
var filename = index < 0 ? path : path.substring(index + 1);
var dir = index < 0 ? '' : path.substring(0, index);
return { path: path, filename: filename, dir: dir };
}
var toFileUrl = function (args) {
var path = args.path, uri = args.uri, url = args.url;
var _a = parsePath(path), filename = _a.filename, dir = _a.dir;
var res = { uri: uri, filename: filename, dir: dir, path: path, url: url };
return res;
};