@platform/cell.client
Version:
A strongly typed HTTP client for operating with a CellOS service end-point.
80 lines (79 loc) • 3.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpClientCellLinks = void 0;
var tslib_1 = require("tslib");
var common_1 = require("../common");
var HttpClientCellLinks_1 = require("./HttpClientCellLinks");
var HttpClientNs_1 = require("./HttpClientNs");
tslib_1.__exportStar(require("./HttpClientCellLinks.info"), exports);
function HttpClientCellLinks(args) {
var uri = args.uri, urls = args.urls, http = args.http;
var RefLinks = common_1.Schema.Ref.Links;
var api = {
get uri() {
return uri.toString();
},
read: function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var info, message, cell, links, body;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
return [4, args.getInfo()];
case 1:
info = _a.sent();
if (info.error) {
message = "Failed to get links for '".concat(uri.toString(), "'. ").concat(info.error.message);
return [2, common_1.util.toError(info.status, info.error.type, message)];
}
cell = info.body.data;
links = cell.links || {};
body = (0, HttpClientCellLinks_1.HttpClientCellLinksInfo)({ links: links, urls: urls, http: http });
return [2, common_1.util.toClientResponse(200, body)];
}
});
});
},
write: function (link, options) {
var _a;
var list = Array.isArray(link) ? link : [link];
var links = list
.map(function (link) { return (tslib_1.__assign(tslib_1.__assign({}, link), { key: RefLinks.toKey(link.key) })); })
.reduce(function (acc, next) {
acc[next.key] = (next.value || '').trim();
return acc;
}, {});
var ns = (0, HttpClientNs_1.HttpClientNs)({ uri: common_1.Uri.ns(uri.ns), urls: urls, http: http });
return ns.write({ cells: (_a = {}, _a[uri.key] = { links: links }, _a) }, options);
},
delete: function (key, options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var keys, links;
return tslib_1.__generator(this, function (_a) {
keys = Array.isArray(key) ? key : [key];
links = keys.map(function (key) { return ({ key: key, value: '' }); });
return [2, api.write(links, options)];
});
});
},
exists: function (key) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var wanted, links, linked, exists;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
wanted = Array.isArray(key) ? key : [key];
return [4, api.read()];
case 1:
links = (_a.sent()).body;
linked = links.list.map(function (link) { return common_1.Schema.Ref.Links.parseKey(link.key).path; });
exists = linked.some(function (linkedKey) { return wanted.includes(linkedKey); });
return [2, exists];
}
});
});
},
};
return api;
}
exports.HttpClientCellLinks = HttpClientCellLinks;