@platform/cell.schema
Version:
URI and database schemas for the `cell.os`.
63 lines (62 loc) • 2.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ManifestFetch = void 0;
var tslib_1 = require("tslib");
var common_1 = require("../common");
var ManifestUrl_1 = require("../ManifestUrl");
exports.ManifestFetch = {
get: function (args) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function () {
var path, urls, url, res, ok, status, json, _b, exists, files;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
path = toPath(args.path);
urls = common_1.Urls.create(args.host);
url = urls.cell(args.cell).file.byName(path).toString();
return [4, fetch(url)];
case 1:
res = _c.sent();
ok = res.ok, status = res.status;
if (!ok) return [3, 3];
return [4, res.json()];
case 2:
_b = (_c.sent());
return [3, 4];
case 3:
_b = undefined;
_c.label = 4;
case 4:
json = _b;
exists = Boolean(json);
files = (_a = json === null || json === void 0 ? void 0 : json.files) !== null && _a !== void 0 ? _a : [];
return [2, { ok: ok, url: url, path: path, status: status, exists: exists, json: json, files: files }];
}
});
});
},
url: function (input) {
var url = ManifestUrl_1.ManifestUrl.parse(input);
return {
url: url,
get: function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var domain, cell, path;
return tslib_1.__generator(this, function (_a) {
if (url.error)
throw new Error("Cannot GET manifest. ".concat(url.error));
domain = url.domain, cell = url.cell, path = url.path;
return [2, exports.ManifestFetch.get({ host: domain, cell: cell, path: path })];
});
});
},
};
},
};
function toPath(input) {
var path = (input || '').trim();
if (!path.endsWith('.json'))
path = "".concat(path.replace(/\/*$/, ''), "/index.json");
return path;
}