@platform/cell.client
Version:
A strongly typed HTTP client for operating with a CellOS service end-point.
141 lines (140 loc) • 6.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("../common");
var HttpClientCellFile = (function () {
function HttpClientCellFile(args) {
this.args = args;
}
HttpClientCellFile.create = function (args) {
return new HttpClientCellFile(args);
};
HttpClientCellFile.prototype.name = function (path) {
var http = this.args.http;
var self = this;
var parent = this.args.parent;
return {
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, self.getCellLinkByFilename(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 = self.args.urls.file(link.uri).info;
return [4, http.get(url.toString())];
case 2:
res = _a.sent();
return [2, common_1.util.fromHttpResponse(res).toClientResponse()];
}
});
});
},
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, bodyType, message, httpError, error;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
expires = options.expires;
return [4, self.getCellLinkByFilename(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 = (res.headers['content-type'] || '').toString().trim();
bodyType = mime.startsWith('text/') ? 'TEXT' : 'BINARY';
return [2, common_1.util.fromHttpResponse(res).toClientResponse({ bodyType: bodyType })];
}
else {
message = "Failed while downloading file \"" + parent.uri.toString() + "\".";
httpError = res.contentType.is.json ? res.json : undefined;
if (httpError) {
error = message + " " + 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];
}
});
});
},
};
};
HttpClientCellFile.prototype.getCellInfo = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var parent, res, message, error, data;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
parent = this.args.parent;
return [4, parent.info()];
case 1:
res = _a.sent();
if (!res.body) {
message = "Info about the cell \"" + parent.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];
}
});
});
};
HttpClientCellFile.prototype.getCellLinkByFilename = function (path) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var parent, _a, error, data, link, message, error_1;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
parent = this.args.parent;
return [4, this.getCellInfo()];
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 \"" + parent.uri.toString() + "\" to the filename '" + 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 }];
}
});
});
};
return HttpClientCellFile;
}());
exports.HttpClientCellFile = HttpClientCellFile;