@platform/cell.schema
Version:
URI and database schemas for the `cell.os`.
219 lines (218 loc) • 7.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var common_1 = require("../common");
var Uri_1 = require("../Uri");
var Url_1 = require("./Url");
var util = require("./util");
var Url_routes_1 = require("../Url.routes");
var Urls = (function () {
function Urls(input) {
var _this = this;
this.toUrl = function (path, options) {
if (options === void 0) { options = {}; }
var query = options.query;
return new Url_1.Url({ origin: _this.origin, path: path, query: query });
};
var _a = Urls.parse(input), protocol = _a.protocol, host = _a.host, port = _a.port, origin = _a.origin;
this.host = host;
this.protocol = protocol;
this.port = port;
this.origin = origin;
}
Urls.create = function (input) {
return new Urls(input);
};
Urls.parse = function (input) {
var _a;
input = common_1.value.isNumeric(input) ? "localhost:" + input : (_a = input) === null || _a === void 0 ? void 0 : _a.toString();
var text = (input || '').trim();
text = text || 'localhost';
var host = common_1.R.pipe(util.stripHttp, util.stripSlash, util.stripPort)(text);
var protocol = util.toProtocol(host);
var port = util.toPort(text) || 80;
var origin = port === 80 ? protocol + "://" + host : protocol + "://" + host + ":" + port;
return { protocol: protocol, host: host, port: port, origin: origin };
};
Object.defineProperty(Urls.prototype, "sys", {
get: function () {
var toPath = this.toUrl;
return {
get info() {
return toPath('.sys');
},
get uid() {
return toPath('.uid');
},
};
},
enumerable: true,
configurable: true
});
Object.defineProperty(Urls.prototype, "local", {
get: function () {
var toPath = this.toUrl;
return {
get fs() {
return toPath("/local/fs");
},
};
},
enumerable: true,
configurable: true
});
Urls.prototype.ns = function (input) {
var toPath = this.toUrl;
var id = (typeof input === 'string' ? input : input.ns) || '';
if (!id.includes(':')) {
id = "ns:" + id;
}
var uri = Uri_1.Uri.parse(id);
var type = uri.parts.type;
if (uri.error) {
throw new Error(uri.error.message);
}
if (type === 'NS') {
id = uri.parts.id;
}
else if (type === 'CELL') {
id = uri.parts.ns;
}
else {
var err = "The id for the namespace is a URI, but not of type \"ns:\" or \"cell:\" (\"" + id + "\")";
throw new Error(err);
}
return {
uri: uri.toString(),
get info() {
return toPath("/ns:" + id);
},
};
};
Urls.prototype.cell = function (input) {
var toPath = this.toUrl;
var uri = typeof input === 'string' ? input : Uri_1.Uri.create.cell(input.ns, input.key);
var cell = Uri_1.Uri.parse(uri);
if (cell.error) {
throw new Error(cell.error.message);
}
var _a = cell.parts, ns = _a.ns, key = _a.key, type = _a.type;
if (type !== 'CELL') {
var err = "The given URI is a " + type + " not a CELL (\"" + uri + "\")";
throw new Error(err);
}
var api = {
uri: uri,
get info() {
return toPath("/cell:" + ns + ":" + key);
},
files: {
get list() {
return toPath("/cell:" + ns + ":" + key + "/files");
},
get delete() {
return toPath("/cell:" + ns + ":" + key + "/files");
},
get upload() {
return toPath("/cell:" + ns + ":" + key + "/files/upload");
},
get uploaded() {
return toPath("/cell:" + ns + ":" + key + "/files/uploaded");
},
},
file: {
byName: function (filename) {
filename = (filename || '').trim();
if (!filename) {
throw new Error("Filename not provided.");
}
return toPath("/cell:" + ns + ":" + key + "/file/" + filename);
},
byFileUri: function (fileUri, fileExtension) {
fileExtension = (fileExtension || '').trim();
var uri = Uri_1.Uri.parse(fileUri).parts;
if (uri.type !== 'FILE') {
throw new Error("The given URI [" + fileUri + "] is not of type [file:]");
}
var ext = (fileExtension || '').trim().replace(/^\.*/, '');
var filename = ("" + uri.file + (ext ? "." + ext : '')).trim();
if (!filename) {
throw new Error("File uri/name could not be derived..");
}
var file = "file:" + filename;
return toPath("/cell:" + ns + ":" + key + "/" + file);
},
},
};
return api;
};
Urls.prototype.row = function (input) {
var toPath = this.toUrl;
var uri = typeof input === 'string' ? input : Uri_1.Uri.create.row(input.ns, input.key);
var row = Uri_1.Uri.parse(uri);
if (row.error) {
throw new Error(row.error.message);
}
var _a = row.parts, ns = _a.ns, key = _a.key, type = _a.type;
if (type !== 'ROW') {
var err = "The given URI is a " + type + " not a ROW (\"" + uri + "\")";
throw new Error(err);
}
return {
uri: uri,
get info() {
return toPath("/cell:" + ns + ":" + key);
},
};
};
Urls.prototype.column = function (input) {
var toPath = this.toUrl;
var uri = typeof input === 'string' ? input : Uri_1.Uri.create.column(input.ns, input.key);
var column = Uri_1.Uri.parse(uri);
if (column.error) {
throw new Error(column.error.message);
}
var _a = column.parts, ns = _a.ns, key = _a.key, type = _a.type;
if (type !== 'COLUMN') {
var err = "The given URI is a " + type + " not a COLUMN (\"" + uri + "\")";
throw new Error(err);
}
return {
uri: uri,
get info() {
return toPath("/cell:" + ns + ":" + key);
},
};
};
Urls.prototype.file = function (input) {
var toPath = this.toUrl;
var uri = typeof input === 'string' ? input : Uri_1.Uri.create.file(input.ns, input.file);
var file = Uri_1.Uri.parse(uri);
if (file.error) {
throw new Error(file.error.message);
}
if (file.parts.type !== 'FILE') {
var err = "The given URI is not of type \"file:\" (\"" + uri + "\")";
throw new Error(err);
}
var id = file.parts.id;
return {
uri: uri,
get info() {
return toPath("/file:" + id + "/info");
},
get download() {
return toPath("/file:" + id);
},
get delete() {
return toPath("/file:" + id);
},
get uploaded() {
return toPath("/file:" + id + "/uploaded");
},
};
};
Urls.uri = Uri_1.Uri;
Urls.routes = Url_routes_1.ROUTES;
return Urls;
}());
exports.Urls = Urls;