@platform/cell.schema
Version:
URI and database schemas for the `cell.os`.
205 lines (204 loc) • 6.85 kB
JavaScript
import { R, value } from '../common';
import { Uri } from '../Uri';
import { Url } from './Url';
import * as util from './util';
import { ROUTES } from '../Url.routes';
export class Urls {
constructor(input) {
this.toUrl = (path, options = {}) => {
const { query } = options;
return new Url({ origin: this.origin, path, query });
};
const { protocol, host, port, origin } = Urls.parse(input);
this.host = host;
this.protocol = protocol;
this.port = port;
this.origin = origin;
}
static create(input) {
return new Urls(input);
}
static parse(input) {
var _a;
input = value.isNumeric(input) ? `localhost:${input}` : (_a = input) === null || _a === void 0 ? void 0 : _a.toString();
let text = (input || '').trim();
text = text || 'localhost';
const host = R.pipe(util.stripHttp, util.stripSlash, util.stripPort)(text);
const protocol = util.toProtocol(host);
const port = util.toPort(text) || 80;
const origin = port === 80 ? `${protocol}://${host}` : `${protocol}://${host}:${port}`;
return { protocol, host, port, origin };
}
get sys() {
const toPath = this.toUrl;
return {
get info() {
return toPath('.sys');
},
get uid() {
return toPath('.uid');
},
};
}
get local() {
const toPath = this.toUrl;
return {
get fs() {
return toPath(`/local/fs`);
},
};
}
ns(input) {
const toPath = this.toUrl;
let id = (typeof input === 'string' ? input : input.ns) || '';
if (!id.includes(':')) {
id = `ns:${id}`;
}
const uri = Uri.parse(id);
const 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 {
const 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}`);
},
};
}
cell(input) {
const toPath = this.toUrl;
const uri = typeof input === 'string' ? input : Uri.create.cell(input.ns, input.key);
const cell = Uri.parse(uri);
if (cell.error) {
throw new Error(cell.error.message);
}
const { ns, key, type } = cell.parts;
if (type !== 'CELL') {
const err = `The given URI is a ${type} not a CELL ("${uri}")`;
throw new Error(err);
}
const api = {
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(filename) {
filename = (filename || '').trim();
if (!filename) {
throw new Error(`Filename not provided.`);
}
return toPath(`/cell:${ns}:${key}/file/${filename}`);
},
byFileUri(fileUri, fileExtension) {
fileExtension = (fileExtension || '').trim();
const uri = Uri.parse(fileUri).parts;
if (uri.type !== 'FILE') {
throw new Error(`The given URI [${fileUri}] is not of type [file:]`);
}
const ext = (fileExtension || '').trim().replace(/^\.*/, '');
const filename = `${uri.file}${ext ? `.${ext}` : ''}`.trim();
if (!filename) {
throw new Error(`File uri/name could not be derived..`);
}
const file = `file:${filename}`;
return toPath(`/cell:${ns}:${key}/${file}`);
},
},
};
return api;
}
row(input) {
const toPath = this.toUrl;
const uri = typeof input === 'string' ? input : Uri.create.row(input.ns, input.key);
const row = Uri.parse(uri);
if (row.error) {
throw new Error(row.error.message);
}
const { ns, key, type } = row.parts;
if (type !== 'ROW') {
const err = `The given URI is a ${type} not a ROW ("${uri}")`;
throw new Error(err);
}
return {
uri,
get info() {
return toPath(`/cell:${ns}:${key}`);
},
};
}
column(input) {
const toPath = this.toUrl;
const uri = typeof input === 'string' ? input : Uri.create.column(input.ns, input.key);
const column = Uri.parse(uri);
if (column.error) {
throw new Error(column.error.message);
}
const { ns, key, type } = column.parts;
if (type !== 'COLUMN') {
const err = `The given URI is a ${type} not a COLUMN ("${uri}")`;
throw new Error(err);
}
return {
uri,
get info() {
return toPath(`/cell:${ns}:${key}`);
},
};
}
file(input) {
const toPath = this.toUrl;
const uri = typeof input === 'string' ? input : Uri.create.file(input.ns, input.file);
const file = Uri.parse(uri);
if (file.error) {
throw new Error(file.error.message);
}
if (file.parts.type !== 'FILE') {
const err = `The given URI is not of type "file:" ("${uri}")`;
throw new Error(err);
}
const { id } = file.parts;
return {
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;
Urls.routes = ROUTES;