UNPKG

@platform/cell.typesystem

Version:

The 'strongly typed sheets' system of the CellOS.

179 lines (178 loc) 8.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeBuilder = void 0; var tslib_1 = require("tslib"); var common_1 = require("../common"); var TypeBuilderNs_1 = require("./TypeBuilderNs"); var TypeSystem_core_1 = require("../TypeSystem.core"); var TypeBuilder = (function () { function TypeBuilder() { this.builders = []; } TypeBuilder.prototype.toObject = function () { var _this = this; return this.builders.reduce(function (acc, builder) { var ns = builder.uri.toString(); acc[ns] = _this.toDef(builder); return acc; }, {}); }; TypeBuilder.prototype.toTypeDefs = function () { var _this = this; return this.builders.reduce(function (acc, builder) { return (0, tslib_1.__spreadArray)((0, tslib_1.__spreadArray)([], acc, true), _this.toTypeDef(builder), true); }, []); }; TypeBuilder.prototype.ns = function (uri) { uri = typeof uri === 'string' ? uri.trim() : uri; var ns = uri ? common_1.Uri.ns(uri) : common_1.Uri.create.ns(common_1.Uri.cuid()); var exists = this.builders.some(function (builder) { return builder.toString() === ns.toString(); }); if (exists) { var err = "The namespace '" + ns.toString() + "' already exists"; throw new Error(err); } var res = TypeBuilderNs_1.TypeBuilderNs.create({ uri: ns }); this.builders.push(res); return res; }; TypeBuilder.prototype.type = function (typename, options) { return this.ns().type(typename, options); }; TypeBuilder.prototype.formatType = function (value) { value = (value || '').trim(); if (value.startsWith('/')) { var typename = value.replace(/^\/*/, ''); var singular_1 = TypeSystem_core_1.TypeValue.trimArray(typename); var match = this.builders.find(function (ns) { return ns.types.find(function (type) { return type.typename === singular_1; }); }); if (!match) { var err = "Failed to prefix type '" + value + "' with namespace. The typename '" + typename + "' was not found in any of the available namespaces."; throw new Error(err); } value = match.uri.toString() + "/" + typename; } return value; }; TypeBuilder.prototype.write = function (http) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { var defs, typeDefs, saved, exists, errors, save; var _this = this; return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: defs = this.toObject(); typeDefs = this.toTypeDefs(); saved = []; exists = []; errors = []; save = function (typeDef) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { var ns, typename, def, client, error; return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: ns = typeDef.uri; typename = typeDef.typename; def = defs[ns]; client = http.ns(ns); return [4, client.exists()]; case 1: if (!!(_a.sent())) return [3, 3]; return [4, client.write(def)]; case 2: error = (_a.sent()).error; if (error) { errors.push({ typename: typename, ns: ns, error: error }); } else { saved.push({ typename: typename, ns: ns }); } return [3, 4]; case 3: exists.push({ typename: typename, ns: ns }); _a.label = 4; case 4: return [2]; } }); }); }; return [4, Promise.all(typeDefs.map(function (typeDef) { return save(typeDef); }))]; case 1: _a.sent(); return [2, { ok: errors.length === 0, saved: saved, exists: exists, errors: errors }]; } }); }); }; TypeBuilder.prototype.typescript = function (options) { var typeDefs = this.toTypeDefs(); return TypeSystem_core_1.TypeClient.typescript(typeDefs, options); }; TypeBuilder.prototype.toDef = function (ns) { var _this = this; var columns = {}; var getOrCreateObject = function (target, key) { return target[key] ? target[key] : (target[key] = {}); }; var attachDef = function (def, column) { var props = getOrCreateObject(column, 'props'); if (!props.def) { props.def = def; } else { if (!Array.isArray(props.def)) { props.def = [props.def]; } props.def.push(def); } }; ns.types.forEach(function (type) { var typename = type.typename; type.props .map(function (prop) { return prop.toObject(); }) .forEach(function (prop) { var def = common_1.value.deleteUndefined({ prop: typename + "." + prop.name, type: _this.formatType(prop.type), default: prop.default, target: prop.target, }); var column = getOrCreateObject(columns, prop.column); attachDef(def, column); }); }); return { columns: columns }; }; TypeBuilder.prototype.toTypeDef = function (ns) { var uri = ns.uri.toString(); var columns = this.toDef(ns).columns; var res = []; var add = function (column, item) { var _a = TypeSystem_core_1.TypeProp.parse(item.prop), prop = _a.prop, typename = _a.type, optional = _a.optional; var exists = res.some(function (def) { return def.typename === typename; }); if (!exists) { res.push({ uri: uri, typename: typename, columns: [], errors: [] }); } var typeDef = res.find(function (def) { return def.typename === typename; }); if (typeDef) { typeDef.columns.push({ column: column, prop: prop, type: TypeSystem_core_1.TypeValue.toType(item.type), target: item.target, default: TypeSystem_core_1.TypeDefault.toTypeDefault(item.default), optional: optional, }); } }; Object.keys(columns).forEach(function (column) { var _a; var props = ((_a = columns[column]) === null || _a === void 0 ? void 0 : _a.props) || {}; if (props.def) { var defs = Array.isArray(props.def) ? props.def : [props.def]; defs.forEach(function (item) { return add(column, item); }); } }); return res; }; TypeBuilder.create = function () { return new TypeBuilder(); }; return TypeBuilder; }()); exports.TypeBuilder = TypeBuilder;