@platform/cell.client
Version:
A strongly typed HTTP client for operating with a CellOS service end-point.
143 lines (142 loc) • 6.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpClientCellFile = void 0;
var tslib_1 = require("tslib");
var common_1 = require("../common");
function HttpClientCellFile(args) {
var parent = args.parent, urls = args.urls, http = args.http, path = args.path;
var api = {
path: path,
info: function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var linkRes, link, url, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, getCellLinkByFilename(parent, path)];
case 1:
linkRes = _a.sent();
if (linkRes.error) {
return [2, linkRes.error];
}
if (!linkRes.link) {
throw new Error("Link should exist.");
}
link = linkRes.link;
url = urls.file(link.uri).info;
return [4, http.get(url.toString())];
case 2:
res = _a.sent();
return [2, common_1.util.fromHttpResponse(res).toClientResponse()];
}
});
});
},
exists: function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var info;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, api.info()];
case 1:
info = _a.sent();
return [2, info.ok && info.body.exists];
}
});
});
},
download: function (options) {
if (options === void 0) { options = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var expires, linkRes, link, hash, url, res, mime_1, bodyType, message, httpError, error;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
expires = options.expires;
return [4, getCellLinkByFilename(parent, path)];
case 1:
linkRes = _a.sent();
if (linkRes.error)
return [2, linkRes.error];
if (!linkRes.link) {
throw new Error("Link should exist.");
}
link = linkRes.link;
hash = link.query.hash || undefined;
url = parent.url.file
.byFileUri(link.uri.toString(), link.ext)
.query({ hash: hash, expires: expires })
.toString();
return [4, http.get(url)];
case 2:
res = _a.sent();
if (res.ok) {
mime_1 = (res.headers['content-type'] || '').toString().trim();
bodyType = toBodyType(mime_1);
return [2, common_1.util.fromHttpResponse(res).toClientResponse({ bodyType: bodyType })];
}
else {
message = "Failed while downloading file \"".concat(parent.uri.toString(), "\".");
httpError = res.contentType.is.json ? res.json : undefined;
if (httpError) {
error = "".concat(message, " ").concat(httpError.message);
return [2, common_1.util.toError(res.status, httpError.type, error)];
}
else {
return [2, common_1.util.toError(res.status, common_1.ERROR.HTTP.SERVER, message)];
}
}
return [2];
}
});
});
},
};
return api;
}
exports.HttpClientCellFile = HttpClientCellFile;
function toBodyType(mime) {
if (mime === 'application/json')
return 'JSON';
if (mime.startsWith('text/'))
return 'TEXT';
return 'BINARY';
}
var getCellInfo = function (cell) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var res, message, error, data;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, cell.info()];
case 1:
res = _a.sent();
if (!res.body) {
message = "Info about the cell \"".concat(cell.uri.toString(), "\" not found.");
error = common_1.util.toError(404, common_1.ERROR.HTTP.NOT_FOUND, message);
return [2, { res: res, error: error }];
}
else {
data = res.body.data;
return [2, { res: res, data: data }];
}
return [2];
}
});
}); };
var getCellLinkByFilename = function (cell, path) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var _a, error, data, link, message, error_1;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4, getCellInfo(cell)];
case 1:
_a = _b.sent(), error = _a.error, data = _a.data;
if (!data || error)
return [2, { error: error }];
link = common_1.Schema.File.Links.find(data.links).byName(path);
if (!link) {
message = "A link within \"".concat(cell.uri.toString(), "\" to the filename '").concat(path, "' does not exist.");
error_1 = common_1.util.toError(404, common_1.ERROR.HTTP.NOT_LINKED, message);
return [2, { error: error_1 }];
}
return [2, { link: link }];
}
});
}); };