1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.CoordSchema = exports.NsSchema = exports.Schema = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var common_1 = require("../common");
|
6 | var Fs_1 = require("../Fs");
|
7 | var Ref_1 = require("../Ref");
|
8 | var Uri_1 = require("../Uri");
|
9 | var Url_1 = require("../Url");
|
10 | var Encoding_1 = require("../Encoding");
|
11 | exports.Schema = {
|
12 | Mime: common_1.Mime,
|
13 | File: Fs_1.FileSchema,
|
14 | Ref: Ref_1.RefSchema,
|
15 | cuid: common_1.cuid,
|
16 | slug: common_1.slug,
|
17 | hash: common_1.hash,
|
18 | coord: common_1.coord,
|
19 | encoding: Encoding_1.Encoding,
|
20 | Uri: Uri_1.Uri,
|
21 | Url: Url_1.Url,
|
22 | Urls: Url_1.Urls,
|
23 | urls: function (host) { return Url_1.Urls.create(host); },
|
24 | ns: function (id) { return new NsSchema({ id: id }); },
|
25 | query: {
|
26 | cells: '/CELL/*',
|
27 | rows: '/ROW/*',
|
28 | columns: '/COL/*',
|
29 | files: '/FILE/*',
|
30 | },
|
31 | from: {
|
32 | ns: function (input) {
|
33 | return from({
|
34 | input: input,
|
35 | toUri: function (path) { return NsSchema.uri({ path: path }); },
|
36 | toPath: function (uri) { return exports.Schema.ns(uri).path; },
|
37 | });
|
38 | },
|
39 | cell: function (input) {
|
40 | return from({
|
41 | input: input,
|
42 | toUri: function (path) { return CoordSchema.uri({ path: path }); },
|
43 | toPath: function (uri) {
|
44 | var _a = Uri_1.Uri.cell(uri), ns = _a.ns, key = _a.key;
|
45 | return exports.Schema.ns(ns).cell(key).path;
|
46 | },
|
47 | });
|
48 | },
|
49 | row: function (input) {
|
50 | return from({
|
51 | input: input,
|
52 | toUri: function (path) { return CoordSchema.uri({ path: path }); },
|
53 | toPath: function (uri) {
|
54 | var _a = Uri_1.Uri.row(uri), ns = _a.ns, key = _a.key;
|
55 | return exports.Schema.ns(ns).row(key).path;
|
56 | },
|
57 | });
|
58 | },
|
59 | column: function (input) {
|
60 | return from({
|
61 | input: input,
|
62 | toUri: function (path) { return CoordSchema.uri({ path: path }); },
|
63 | toPath: function (uri) {
|
64 | var _a = Uri_1.Uri.column(uri), ns = _a.ns, key = _a.key;
|
65 | return exports.Schema.ns(ns).column(key).path;
|
66 | },
|
67 | });
|
68 | },
|
69 | file: function (input) {
|
70 | return from({
|
71 | input: input,
|
72 | toUri: function (path) { return Fs_1.FileSchema.uri({ path: path }); },
|
73 | toPath: function (uri) {
|
74 | var _a = Uri_1.Uri.file(uri), ns = _a.ns, file = _a.file;
|
75 | return exports.Schema.ns(ns).file(file).path;
|
76 | },
|
77 | });
|
78 | },
|
79 | },
|
80 | };
|
81 | var NsSchema = (function () {
|
82 | function NsSchema(args) {
|
83 | var id = args.id || common_1.cuid();
|
84 | if (Uri_1.Uri.is.uri(id)) {
|
85 | var uri = Uri_1.Uri.parse(id);
|
86 | if (uri.error) {
|
87 | throw new Error(uri.error.message);
|
88 | }
|
89 | if (uri.parts.type !== 'NS') {
|
90 | throw new Error("The given URI does not represent a namespace (\"" + uri.toString() + "\").");
|
91 | }
|
92 | id = uri.parts.id;
|
93 | }
|
94 | this.id = id;
|
95 | this.path = "NS/" + id;
|
96 | this.uri = Uri_1.Uri.create.ns(id);
|
97 | }
|
98 | NsSchema.prototype.cell = function (key) {
|
99 | var uri = Uri_1.Uri.create.cell(this.id, key);
|
100 | return new CoordSchema({ type: 'CELL', nsPath: this.path, id: key, uri: uri });
|
101 | };
|
102 | NsSchema.prototype.column = function (key) {
|
103 | var uri = Uri_1.Uri.create.column(this.id, key);
|
104 | return new CoordSchema({ type: 'COL', nsPath: this.path, id: key, uri: uri });
|
105 | };
|
106 | NsSchema.prototype.row = function (key) {
|
107 | var uri = Uri_1.Uri.create.row(this.id, key);
|
108 | return new CoordSchema({ type: 'ROW', nsPath: this.path, id: key, uri: uri });
|
109 | };
|
110 | NsSchema.prototype.file = function (fileid) {
|
111 | var uri = Uri_1.Uri.create.file(this.id, fileid);
|
112 | return Fs_1.FileSchema.toObject({ nsPath: this.path, fileid: fileid, uri: uri });
|
113 | };
|
114 | NsSchema.uri = function (args) {
|
115 | var parts = args.path.split('/');
|
116 | var type = parts[0];
|
117 | var id = parts[1];
|
118 | if (type === 'NS') {
|
119 | return Uri_1.Uri.create.ns(id);
|
120 | }
|
121 | throw new Error("Model path could not be converted to URI (\"" + args.path + "\")");
|
122 | };
|
123 | return NsSchema;
|
124 | }());
|
125 | exports.NsSchema = NsSchema;
|
126 | var CoordSchema = (function () {
|
127 | function CoordSchema(args) {
|
128 | this.id = args.id;
|
129 | this.type = args.type;
|
130 | this.path = args.nsPath + "/" + args.type + "/" + this.id;
|
131 | this._uri = args.uri;
|
132 | }
|
133 | CoordSchema.uri = function (args) {
|
134 | var parts = args.path.split('/');
|
135 | var ns = parts[1];
|
136 | var type = parts[2];
|
137 | var key = parts[3];
|
138 | if (type === 'CELL') {
|
139 | return Uri_1.Uri.create.cell(ns, key);
|
140 | }
|
141 | if (type === 'ROW') {
|
142 | return Uri_1.Uri.create.row(ns, key);
|
143 | }
|
144 | if (type === 'COL') {
|
145 | return Uri_1.Uri.create.column(ns, key);
|
146 | }
|
147 | throw new Error("Model path could not be converted to URI (\"" + args.path + "\")");
|
148 | };
|
149 | Object.defineProperty(CoordSchema.prototype, "uri", {
|
150 | get: function () {
|
151 | return Uri_1.Uri.parse(this._uri).parts;
|
152 | },
|
153 | enumerable: false,
|
154 | configurable: true
|
155 | });
|
156 | return CoordSchema;
|
157 | }());
|
158 | exports.CoordSchema = CoordSchema;
|
159 | var from = function (args) {
|
160 | var path = '';
|
161 | var uri = '';
|
162 | if (typeof args.input === 'object') {
|
163 | path = args.input.path;
|
164 | uri = args.toUri(path);
|
165 | }
|
166 | else {
|
167 | if (Uri_1.Uri.is.uri(args.input)) {
|
168 | uri = args.input;
|
169 | path = args.toPath(uri);
|
170 | }
|
171 | else {
|
172 | path = args.input;
|
173 | uri = args.toUri(path);
|
174 | }
|
175 | }
|
176 | if (!path) {
|
177 | throw new Error("Model schema [path] could not be derived.");
|
178 | }
|
179 | if (!uri) {
|
180 | throw new Error("Model URI could not be derived.");
|
181 | }
|
182 | var parts = Uri_1.Uri.parse(uri);
|
183 | return tslib_1.__assign(tslib_1.__assign({}, parts), { path: path });
|
184 | };
|