@platform/cell.typesystem
Version:
The 'strongly typed sheets' system of the CellOS.
55 lines (54 loc) • 1.86 kB
JavaScript
import { ERROR } from '../common';
function fromClient(http) {
const getNs = async (args) => {
const res = await http.ns(args.ns).read();
const exists = res.body.exists;
let error;
if (!exists) {
const message = `The namespace (${args.ns}) does not exist`;
error = { status: 404, message, type: ERROR.HTTP.NOT_FOUND };
}
const ns = (res.body.data.ns.props || {});
const payload = { ns, error };
return payload;
};
const getColumns = async (args) => {
const res = await http.ns(args.ns).read({ columns: true });
const error = formatError(res.error, msg => `Failed to retrieve type information from namespace (${args.ns}). ${msg}`);
const columns = res.body.data.columns;
const payload = { columns, error };
return payload;
};
const getCells = async (args) => {
const { ns, query } = args;
const res = await http.ns(ns).read({ cells: query, total: 'rows' });
const error = formatError(res.error, msg => `Failed to retrieve cells "${query}" within namespace (${ns}). ${msg}`);
const cells = res.body.data.cells;
const total = res.body.data.total || {};
const rows = total.rows || 0;
const payload = { total: { rows }, cells, error };
return payload;
};
return fromFuncs({
getNs,
getColumns,
getCells,
});
}
function fromFuncs(args) {
const { getNs, getCells, getColumns } = args;
return { getNs, getCells, getColumns };
}
export const fetcher = {
fromClient,
fromFuncs,
};
function formatError(error, getMessage) {
if (!error) {
return undefined;
}
else {
const message = getMessage(error.message);
return Object.assign(Object.assign({}, error), { message });
}
}