@opra/common 0.16.1 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/document/data-type/complex-type.js +69 -56
- package/cjs/document/data-type/simple-type.js +0 -1
- package/cjs/document/resource/collection.js +15 -16
- package/cjs/document/resource/singleton.js +2 -2
- package/cjs/http/http-params.js +10 -11
- package/esm/document/api-document.js +33 -37
- package/esm/document/constants.js +3 -6
- package/esm/document/data-type/api-field.js +12 -15
- package/esm/document/data-type/builtin/any.type.js +5 -8
- package/esm/document/data-type/builtin/base64.type.js +5 -8
- package/esm/document/data-type/builtin/bigint.type.js +5 -8
- package/esm/document/data-type/builtin/boolean.type.js +8 -11
- package/esm/document/data-type/builtin/date.type.js +7 -10
- package/esm/document/data-type/builtin/guid.type.js +9 -12
- package/esm/document/data-type/builtin/index.js +10 -13
- package/esm/document/data-type/builtin/integer.type.js +10 -13
- package/esm/document/data-type/builtin/number.type.js +7 -10
- package/esm/document/data-type/builtin/object.type.js +5 -8
- package/esm/document/data-type/builtin/string.type.js +8 -11
- package/esm/document/data-type/builtin/time.type.js +7 -10
- package/esm/document/data-type/builtin/timestamp.type.js +9 -12
- package/esm/document/data-type/complex-type.js +94 -85
- package/esm/document/data-type/data-type.js +9 -12
- package/esm/document/data-type/enum-type.js +15 -19
- package/esm/document/data-type/mapped-type.js +24 -29
- package/esm/document/data-type/simple-type.js +17 -22
- package/esm/document/data-type/union-type.js +26 -29
- package/esm/document/factory/add-references.js +3 -7
- package/esm/document/factory/create-document.js +10 -16
- package/esm/document/factory/factory.js +33 -37
- package/esm/document/factory/import-resource-class.js +16 -22
- package/esm/document/factory/import-type-class.js +24 -34
- package/esm/document/factory/index.js +1 -4
- package/esm/document/factory/process-resources.js +10 -16
- package/esm/document/factory/process-types.js +31 -37
- package/esm/document/index.js +14 -17
- package/esm/document/resource/collection.js +58 -63
- package/esm/document/resource/resource.js +9 -12
- package/esm/document/resource/singleton.js +27 -31
- package/esm/document/utils/inspect.util.js +4 -7
- package/esm/document/utils/string-compare.util.js +1 -5
- package/esm/exception/enums/issue-severity.enum.js +2 -5
- package/esm/exception/error-issue.js +1 -2
- package/esm/exception/http-errors/bad-request.error.js +4 -8
- package/esm/exception/http-errors/failed-dependency.error.js +4 -8
- package/esm/exception/http-errors/forbidden.error.js +4 -8
- package/esm/exception/http-errors/internal-server.error.js +4 -8
- package/esm/exception/http-errors/method-not-allowed.error.js +4 -8
- package/esm/exception/http-errors/not-acceptable.error.js +4 -8
- package/esm/exception/http-errors/not-found.error.js +4 -8
- package/esm/exception/http-errors/unauthorized.error.js +4 -8
- package/esm/exception/http-errors/unprocessable-entity.error.js +4 -8
- package/esm/exception/index.js +15 -18
- package/esm/exception/opra-exception.js +3 -7
- package/esm/exception/resource-errors/resource-conflict.error.js +4 -8
- package/esm/exception/resource-errors/resource-not-found.error.js +4 -8
- package/esm/exception/wrap-exception.js +20 -24
- package/esm/filter/antlr/OpraFilterLexer.js +6 -9
- package/esm/filter/antlr/OpraFilterListener.js +2 -5
- package/esm/filter/antlr/OpraFilterParser.js +79 -126
- package/esm/filter/antlr/OpraFilterVisitor.js +2 -5
- package/esm/filter/ast/abstract/ast.js +1 -5
- package/esm/filter/ast/abstract/expression.js +2 -6
- package/esm/filter/ast/abstract/literal.js +2 -6
- package/esm/filter/ast/abstract/term.js +2 -6
- package/esm/filter/ast/expressions/arithmetic-expression.js +3 -8
- package/esm/filter/ast/expressions/array-expression.js +2 -6
- package/esm/filter/ast/expressions/comparison-expression.js +2 -6
- package/esm/filter/ast/expressions/logical-expression.js +2 -6
- package/esm/filter/ast/expressions/parentheses-expression.js +2 -6
- package/esm/filter/ast/index.js +16 -19
- package/esm/filter/ast/terms/boolean-literal.js +2 -6
- package/esm/filter/ast/terms/date-literal.js +8 -12
- package/esm/filter/ast/terms/external-constant.js +2 -6
- package/esm/filter/ast/terms/null-literal.js +2 -6
- package/esm/filter/ast/terms/number-literal.js +4 -8
- package/esm/filter/ast/terms/qualified-identifier.js +2 -6
- package/esm/filter/ast/terms/string-literal.js +4 -8
- package/esm/filter/ast/terms/time-literal.js +6 -10
- package/esm/filter/build.js +45 -69
- package/esm/filter/errors.js +3 -9
- package/esm/filter/filter-tree-visitor.js +26 -31
- package/esm/filter/index.js +5 -8
- package/esm/filter/opra-error-listener.js +4 -8
- package/esm/filter/parse.js +14 -19
- package/esm/filter/utils.js +2 -7
- package/esm/helpers/function-utils.js +8 -14
- package/esm/helpers/get-stack-filename.js +1 -5
- package/esm/helpers/index.js +8 -11
- package/esm/helpers/is-url.js +1 -5
- package/esm/helpers/mixin-utils.js +2 -7
- package/esm/helpers/object-utils.js +6 -12
- package/esm/helpers/path-to-object-tree.js +1 -5
- package/esm/helpers/responsive-map.js +1 -5
- package/esm/helpers/type-guards.js +7 -17
- package/esm/http/codecs/boolean-codec.js +1 -5
- package/esm/http/codecs/date-codec.js +1 -5
- package/esm/http/codecs/filter-codec.js +5 -9
- package/esm/http/codecs/integer-codec.js +2 -6
- package/esm/http/codecs/number-codec.js +1 -5
- package/esm/http/codecs/string-codec.js +1 -5
- package/esm/http/enums/http-headers-codes.enum.js +2 -5
- package/esm/http/enums/http-status-codes.enum.js +2 -5
- package/esm/http/enums/http-status-messages.js +1 -4
- package/esm/http/http-headers.js +5 -9
- package/esm/http/http-message.host.js +13 -18
- package/esm/http/http-params.js +30 -36
- package/esm/http/http-request-message.js +18 -22
- package/esm/http/http-response-message.js +23 -28
- package/esm/http/index.js +14 -17
- package/esm/http/interfaces/client-http-headers.interface.js +1 -2
- package/esm/http/interfaces/cookie-options.interface.js +1 -2
- package/esm/http/interfaces/server-http-headers.interface.js +1 -2
- package/esm/i18n/i18n.js +23 -28
- package/esm/i18n/index.js +5 -9
- package/esm/i18n/string-utils.js +2 -7
- package/esm/i18n/translate.js +3 -7
- package/esm/index.js +11 -16
- package/esm/schema/constants.js +1 -4
- package/esm/schema/data-type/complex-type.interface.js +2 -5
- package/esm/schema/data-type/data-type.interface.js +1 -2
- package/esm/schema/data-type/enum-type.interface.js +2 -5
- package/esm/schema/data-type/field.interface.js +1 -2
- package/esm/schema/data-type/mapped-type.interface.js +2 -5
- package/esm/schema/data-type/simple-type.interface.js +2 -5
- package/esm/schema/data-type/union-type.interface.js +2 -5
- package/esm/schema/document.interface.js +1 -2
- package/esm/schema/index.js +31 -34
- package/esm/schema/resource/collection.interface.js +2 -5
- package/esm/schema/resource/container.interface.js +2 -5
- package/esm/schema/resource/endpoint.interface.js +1 -2
- package/esm/schema/resource/resource.interface.js +1 -2
- package/esm/schema/resource/singleton.interface.js +2 -5
- package/esm/schema/type-guards.js +40 -53
- package/esm/types.js +1 -2
- package/esm/url/index.js +5 -8
- package/esm/url/opra-url-path-component.js +3 -7
- package/esm/url/opra-url-path.js +14 -18
- package/esm/url/opra-url.js +15 -19
- package/esm/url/utils/decode-path-component.js +4 -8
- package/esm/url/utils/encode-path-component.js +3 -8
- package/esm/utils/path-utils.js +2 -7
- package/package.json +1 -1
- package/types/document/data-type/complex-type.d.ts +8 -2
- package/types/document/resource/collection.d.ts +5 -3
- package/types/document/resource/singleton.d.ts +3 -1
- package/types/filter/ast/terms/qualified-identifier.d.ts +4 -0
- package/types/http/http-params.d.ts +20 -19
|
@@ -90,65 +90,78 @@ const proto = {
|
|
|
90
90
|
this.fields.set(field.name, field);
|
|
91
91
|
return field;
|
|
92
92
|
},
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
93
|
+
findField(nameOrPath) {
|
|
94
|
+
let field;
|
|
95
|
+
for (const [, f] of this.iteratePath(nameOrPath, true)) {
|
|
96
|
+
if (!f)
|
|
97
|
+
return;
|
|
98
|
+
field = f;
|
|
99
|
+
}
|
|
100
|
+
return field;
|
|
101
|
+
},
|
|
102
|
+
getField(nameOrPath) {
|
|
103
|
+
let field;
|
|
104
|
+
for (const [, f, path] of this.iteratePath(nameOrPath)) {
|
|
105
|
+
if (!f)
|
|
106
|
+
throw new Error(`Invalid field definition "${path}"`);
|
|
107
|
+
field = f;
|
|
107
108
|
}
|
|
108
|
-
|
|
109
|
-
if (!
|
|
110
|
-
throw new Error(`
|
|
111
|
-
return
|
|
109
|
+
/* istanbul ignore next */
|
|
110
|
+
if (!field)
|
|
111
|
+
throw new Error(`Invalid field definition "${nameOrPath}"`);
|
|
112
|
+
return field;
|
|
113
|
+
},
|
|
114
|
+
iteratePath(path, silent) {
|
|
115
|
+
const arr = path.split('.');
|
|
116
|
+
const len = arr.length;
|
|
117
|
+
let dataType = this;
|
|
118
|
+
let field;
|
|
119
|
+
let curPath = '';
|
|
120
|
+
let s;
|
|
121
|
+
let i = -1;
|
|
122
|
+
return {
|
|
123
|
+
[Symbol.iterator]() {
|
|
124
|
+
return this;
|
|
125
|
+
},
|
|
126
|
+
next() {
|
|
127
|
+
i++;
|
|
128
|
+
if (i < len) {
|
|
129
|
+
s = arr[i];
|
|
130
|
+
if (dataType && !(dataType instanceof exports.ComplexType)) {
|
|
131
|
+
if (silent)
|
|
132
|
+
return { done: true, value: [] };
|
|
133
|
+
throw new TypeError(`"${curPath}" field is not a complex type and has no child fields`);
|
|
134
|
+
}
|
|
135
|
+
field = dataType?.fields.get(s);
|
|
136
|
+
if (field) {
|
|
137
|
+
curPath += (curPath ? '.' : '') + field.name;
|
|
138
|
+
dataType = field.type;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
curPath += (curPath ? '.' : '') + s;
|
|
142
|
+
if (dataType && !dataType.additionalFields) {
|
|
143
|
+
if (silent)
|
|
144
|
+
return { done: true, value: [] };
|
|
145
|
+
throw new Error(`Invalid field definition "${curPath}"`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
done: i >= len,
|
|
151
|
+
value: [field?.name || s, field, curPath]
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
};
|
|
112
155
|
},
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
156
|
+
normalizeFieldPath(path) {
|
|
157
|
+
if (Array.isArray(path))
|
|
158
|
+
return path.map((s) => this.normalizeFieldPath(s));
|
|
159
|
+
let curPath = '';
|
|
160
|
+
for (const [, , p] of this.iteratePath(path)) {
|
|
161
|
+
curPath = p;
|
|
162
|
+
}
|
|
163
|
+
return curPath;
|
|
117
164
|
}
|
|
118
165
|
};
|
|
119
166
|
Object.assign(exports.ComplexType.prototype, proto);
|
|
120
167
|
Object.setPrototypeOf(exports.ComplexType.prototype, data_type_js_1.DataType.prototype);
|
|
121
|
-
/**
|
|
122
|
-
* Normalizes field names
|
|
123
|
-
*/
|
|
124
|
-
function _normalizeFieldsNames(target, document, dataType, node, parentPath = '', actualPath = '') {
|
|
125
|
-
let curPath = '';
|
|
126
|
-
for (const k of Object.keys(node)) {
|
|
127
|
-
const nodeVal = node[k];
|
|
128
|
-
const field = dataType?.fields.get(k);
|
|
129
|
-
if (!field) {
|
|
130
|
-
curPath = parentPath ? parentPath + '.' + k : k;
|
|
131
|
-
if ((dataType && !dataType.additionalFields))
|
|
132
|
-
throw new TypeError(`Unknown field "${curPath}"`);
|
|
133
|
-
if (typeof nodeVal === 'object')
|
|
134
|
-
_normalizeFieldsNames(target, document, undefined, nodeVal, curPath, actualPath);
|
|
135
|
-
else
|
|
136
|
-
target.push(curPath);
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
curPath = parentPath ? parentPath + '.' + field.name : field.name;
|
|
140
|
-
if (typeof nodeVal === 'object') {
|
|
141
|
-
if (!(field.type instanceof exports.ComplexType))
|
|
142
|
-
throw new TypeError(`"${(actualPath ? actualPath + '.' + curPath : curPath)}" is not a complex type and has no sub fields`);
|
|
143
|
-
if (target.findIndex(x => x === parentPath) >= 0)
|
|
144
|
-
continue;
|
|
145
|
-
target = _normalizeFieldsNames(target, document, field.type, nodeVal, curPath, actualPath);
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
if (target.findIndex(x => x.startsWith(curPath + '.')) >= 0) {
|
|
149
|
-
target = target.filter(x => !x.startsWith(curPath + '.'));
|
|
150
|
-
}
|
|
151
|
-
target.push(curPath);
|
|
152
|
-
}
|
|
153
|
-
return target;
|
|
154
|
-
}
|
|
@@ -64,9 +64,9 @@ const proto = {
|
|
|
64
64
|
if (!_this.primaryKey.length)
|
|
65
65
|
throw new TypeError(`You must provide primaryKey for Collection resource ("${_this.name}")`);
|
|
66
66
|
_this.primaryKey.forEach(f => {
|
|
67
|
-
const
|
|
68
|
-
if (!(
|
|
69
|
-
throw new TypeError(`Only Simple type allowed for primary keys but "${f}" is a ${
|
|
67
|
+
const field = dataType.getField(f);
|
|
68
|
+
if (!(field?.type instanceof simple_type_js_1.SimpleType))
|
|
69
|
+
throw new TypeError(`Only Simple type allowed for primary keys but "${f}" is a ${field.type.kind}`);
|
|
70
70
|
});
|
|
71
71
|
if (_this.controller) {
|
|
72
72
|
const instance = typeof _this.controller == 'function'
|
|
@@ -122,13 +122,11 @@ const proto = {
|
|
|
122
122
|
return result;
|
|
123
123
|
}
|
|
124
124
|
},
|
|
125
|
-
|
|
126
|
-
return this.type.
|
|
125
|
+
normalizeFieldPath(path) {
|
|
126
|
+
return this.type.normalizeFieldPath(path);
|
|
127
127
|
},
|
|
128
128
|
normalizeSortFields(fields) {
|
|
129
|
-
const normalized = this.
|
|
130
|
-
if (!normalized)
|
|
131
|
-
return;
|
|
129
|
+
const normalized = this.type.normalizeFieldPath(fields);
|
|
132
130
|
const findManyEndpoint = this.operations.findMany;
|
|
133
131
|
const sortFields = findManyEndpoint && findManyEndpoint.sortFields;
|
|
134
132
|
normalized.forEach(field => {
|
|
@@ -139,25 +137,26 @@ const proto = {
|
|
|
139
137
|
});
|
|
140
138
|
return normalized;
|
|
141
139
|
},
|
|
142
|
-
|
|
140
|
+
normalizeFilter(ast) {
|
|
143
141
|
if (ast instanceof index_js_2.ComparisonExpression) {
|
|
144
|
-
this.
|
|
142
|
+
this.normalizeFilter(ast.left);
|
|
145
143
|
}
|
|
146
144
|
else if (ast instanceof index_js_2.LogicalExpression) {
|
|
147
|
-
ast.items.forEach(item => this.
|
|
145
|
+
ast.items.forEach(item => this.normalizeFilter(item));
|
|
148
146
|
}
|
|
149
147
|
else if (ast instanceof index_js_2.ArithmeticExpression) {
|
|
150
|
-
ast.items.forEach(item => this.
|
|
148
|
+
ast.items.forEach(item => this.normalizeFilter(item.expression));
|
|
151
149
|
}
|
|
152
150
|
else if (ast instanceof index_js_2.ArrayExpression) {
|
|
153
|
-
ast.items.forEach(item => this.
|
|
151
|
+
ast.items.forEach(item => this.normalizeFilter(item));
|
|
154
152
|
}
|
|
155
153
|
else if (ast instanceof index_js_2.ParenthesesExpression) {
|
|
156
|
-
this.
|
|
154
|
+
this.normalizeFilter(ast.expression);
|
|
157
155
|
}
|
|
158
156
|
else if (ast instanceof index_js_2.QualifiedIdentifier) {
|
|
159
|
-
|
|
160
|
-
ast.
|
|
157
|
+
ast.field = this.type.findField(ast.value);
|
|
158
|
+
ast.dataType = ast.field?.type || this.document.getDataType('any');
|
|
159
|
+
ast.value = this.type.normalizeFieldPath(ast.value);
|
|
161
160
|
}
|
|
162
161
|
return ast;
|
|
163
162
|
}
|
|
@@ -67,8 +67,8 @@ const proto = {
|
|
|
67
67
|
}));
|
|
68
68
|
return out;
|
|
69
69
|
},
|
|
70
|
-
|
|
71
|
-
return this.type.
|
|
70
|
+
normalizeFieldPath(path) {
|
|
71
|
+
return this.type.normalizeFieldPath(path);
|
|
72
72
|
},
|
|
73
73
|
};
|
|
74
74
|
Object.assign(exports.Singleton.prototype, proto);
|
package/cjs/http/http-params.js
CHANGED
|
@@ -191,7 +191,7 @@ class HttpParams {
|
|
|
191
191
|
get(target, p, receiver) {
|
|
192
192
|
if (typeof p === 'string') {
|
|
193
193
|
const v = _this[kEntries].get(p);
|
|
194
|
-
return v ? (v.length > 1 ? v : v[0]) :
|
|
194
|
+
return v ? (v.length > 1 ? v : v[0]) : null;
|
|
195
195
|
}
|
|
196
196
|
return Reflect.get(target, p, receiver);
|
|
197
197
|
},
|
|
@@ -236,7 +236,7 @@ class HttpParams {
|
|
|
236
236
|
? value.map((v) => fn(v)).join(prmDef.arrayDelimiter || ',')
|
|
237
237
|
: fn(value);
|
|
238
238
|
}
|
|
239
|
-
return encodeURIParam(value);
|
|
239
|
+
return encodeURIParam(String(value));
|
|
240
240
|
}
|
|
241
241
|
decodeValue(value, key) {
|
|
242
242
|
const prmDef = this[kParamDefs].get(key);
|
|
@@ -268,6 +268,12 @@ class HttpParams {
|
|
|
268
268
|
}
|
|
269
269
|
return decodeURIComponent(value);
|
|
270
270
|
}
|
|
271
|
+
[(_a = kEntries, _b = kSize, _c = kParamDefs, Symbol.iterator)]() {
|
|
272
|
+
return this.entries();
|
|
273
|
+
}
|
|
274
|
+
get [Symbol.toStringTag]() {
|
|
275
|
+
return 'HttpParams';
|
|
276
|
+
}
|
|
271
277
|
_append(name, value) {
|
|
272
278
|
let values = this[kEntries].get(name);
|
|
273
279
|
if (!values) {
|
|
@@ -283,8 +289,7 @@ class HttpParams {
|
|
|
283
289
|
return false;
|
|
284
290
|
const oldSize = this[kSize];
|
|
285
291
|
if (value) {
|
|
286
|
-
const
|
|
287
|
-
const newValues = oldValues.filter(x => !valueToDelete.includes(x));
|
|
292
|
+
const newValues = oldValues.filter(x => x === value);
|
|
288
293
|
this[kEntries].set(name, newValues);
|
|
289
294
|
this[kSize] += -oldValues.length + newValues.length;
|
|
290
295
|
}
|
|
@@ -295,7 +300,7 @@ class HttpParams {
|
|
|
295
300
|
return oldSize !== this[kSize];
|
|
296
301
|
}
|
|
297
302
|
_set(name, value, index) {
|
|
298
|
-
if (value
|
|
303
|
+
if (value === undefined) {
|
|
299
304
|
this._delete(name);
|
|
300
305
|
return;
|
|
301
306
|
}
|
|
@@ -314,12 +319,6 @@ class HttpParams {
|
|
|
314
319
|
values[Math.min(index, values.length)] = value;
|
|
315
320
|
this[kSize] += -oldLen + values.length;
|
|
316
321
|
}
|
|
317
|
-
[(_a = kEntries, _b = kSize, _c = kParamDefs, Symbol.iterator)]() {
|
|
318
|
-
return this.entries();
|
|
319
|
-
}
|
|
320
|
-
get [Symbol.toStringTag]() {
|
|
321
|
-
return 'HttpParams';
|
|
322
|
-
}
|
|
323
322
|
}
|
|
324
323
|
exports.HttpParams = HttpParams;
|
|
325
324
|
HttpParams.kEntries = kEntries;
|
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const complex_type_js_1 = require("./data-type/complex-type.js");
|
|
9
|
-
const simple_type_js_1 = require("./data-type/simple-type.js");
|
|
10
|
-
class ApiDocument {
|
|
1
|
+
import { NotAcceptableError, NotFoundError } from '../exception/index.js';
|
|
2
|
+
import { cloneObject, omitUndefined, ResponsiveMap } from '../helpers/index.js';
|
|
3
|
+
import { OpraSchema } from '../schema/index.js';
|
|
4
|
+
import { NAMESPACE_PATTERN } from './constants.js';
|
|
5
|
+
import { ComplexType } from './data-type/complex-type.js';
|
|
6
|
+
import { SimpleType } from './data-type/simple-type.js';
|
|
7
|
+
export class ApiDocument {
|
|
11
8
|
constructor() {
|
|
12
|
-
this._typeCache = new
|
|
9
|
+
this._typeCache = new ResponsiveMap();
|
|
13
10
|
this._typesCacheByCtor = new Map();
|
|
14
|
-
this.references = new
|
|
15
|
-
this.types = new
|
|
16
|
-
this.resources = new
|
|
11
|
+
this.references = new ResponsiveMap();
|
|
12
|
+
this.types = new ResponsiveMap();
|
|
13
|
+
this.resources = new ResponsiveMap();
|
|
17
14
|
this.info = {
|
|
18
15
|
version: '',
|
|
19
16
|
title: ''
|
|
@@ -33,19 +30,19 @@ class ApiDocument {
|
|
|
33
30
|
if (t === null) {
|
|
34
31
|
if (silent)
|
|
35
32
|
return;
|
|
36
|
-
throw new
|
|
33
|
+
throw new NotFoundError(`Data type "${nameOrCtorName}" does not exists`);
|
|
37
34
|
}
|
|
38
35
|
if (typeof nameOrCtor === 'string') {
|
|
39
|
-
const m =
|
|
36
|
+
const m = NAMESPACE_PATTERN.exec(nameOrCtor);
|
|
40
37
|
if (!m)
|
|
41
|
-
throw new
|
|
38
|
+
throw new NotFoundError(`Invalid data type name "${nameOrCtorName}"`);
|
|
42
39
|
// If given string has namespace pattern (ns:type_name)
|
|
43
40
|
if (m[2]) {
|
|
44
41
|
const ref = this.references.get(m[1]);
|
|
45
42
|
if (!ref) {
|
|
46
43
|
if (silent)
|
|
47
44
|
return;
|
|
48
|
-
throw new
|
|
45
|
+
throw new NotFoundError(`Reference "${m[1]}" not found`);
|
|
49
46
|
}
|
|
50
47
|
dataType = ref.getDataType(m[2]);
|
|
51
48
|
this._typeCache.set(nameOrCtor, dataType);
|
|
@@ -70,7 +67,7 @@ class ApiDocument {
|
|
|
70
67
|
else if (typeof nameOrCtor === 'function') {
|
|
71
68
|
const types = Array.from(this.types.values()).reverse();
|
|
72
69
|
for (const dt of types) {
|
|
73
|
-
if ((dt instanceof
|
|
70
|
+
if ((dt instanceof ComplexType || dt instanceof SimpleType) && dt.own.ctor === nameOrCtor) {
|
|
74
71
|
dataType = dt;
|
|
75
72
|
break;
|
|
76
73
|
}
|
|
@@ -86,7 +83,7 @@ class ApiDocument {
|
|
|
86
83
|
}
|
|
87
84
|
}
|
|
88
85
|
if (dataType) {
|
|
89
|
-
if ((dataType instanceof
|
|
86
|
+
if ((dataType instanceof ComplexType || dataType instanceof SimpleType) &&
|
|
90
87
|
dataType.own?.ctor && dataType.own.ctor !== Object)
|
|
91
88
|
this._typesCacheByCtor.set(dataType.own.ctor, dataType);
|
|
92
89
|
return dataType;
|
|
@@ -99,7 +96,7 @@ class ApiDocument {
|
|
|
99
96
|
}
|
|
100
97
|
if (silent)
|
|
101
98
|
return;
|
|
102
|
-
throw new
|
|
99
|
+
throw new NotFoundError(`Data type "${nameOrCtorName}" does not exists`);
|
|
103
100
|
}
|
|
104
101
|
getComplexType(nameOrCtor, silent) {
|
|
105
102
|
if (nameOrCtor === Object)
|
|
@@ -107,30 +104,30 @@ class ApiDocument {
|
|
|
107
104
|
const t = this.getDataType(nameOrCtor);
|
|
108
105
|
if (!t && silent)
|
|
109
106
|
return;
|
|
110
|
-
if (t && t.kind ===
|
|
107
|
+
if (t && t.kind === OpraSchema.ComplexType.Kind)
|
|
111
108
|
return t;
|
|
112
|
-
throw new
|
|
109
|
+
throw new NotAcceptableError(`Data type "${t.name}" is not a ComplexType`);
|
|
113
110
|
}
|
|
114
111
|
getSimpleType(nameOrCtor, silent) {
|
|
115
112
|
const t = this.getDataType(nameOrCtor);
|
|
116
113
|
if (!t && silent)
|
|
117
114
|
return;
|
|
118
|
-
if (t && t.kind ===
|
|
115
|
+
if (t && t.kind === OpraSchema.SimpleType.Kind)
|
|
119
116
|
return t;
|
|
120
|
-
throw new
|
|
117
|
+
throw new NotAcceptableError(`Data type "${t.name || t}" is not a SimpleType`);
|
|
121
118
|
}
|
|
122
119
|
getResource(path, silent) {
|
|
123
120
|
let resource;
|
|
124
|
-
const m =
|
|
121
|
+
const m = NAMESPACE_PATTERN.exec(path);
|
|
125
122
|
if (!m)
|
|
126
|
-
throw new
|
|
123
|
+
throw new NotFoundError(`Invalid resource path "${path}"`);
|
|
127
124
|
// If given string has namespace pattern (ns:type_name)
|
|
128
125
|
if (m[2]) {
|
|
129
126
|
const ref = this.references.get(m[1]);
|
|
130
127
|
if (!ref) {
|
|
131
128
|
if (silent)
|
|
132
129
|
return;
|
|
133
|
-
throw new
|
|
130
|
+
throw new NotFoundError(`Reference "${m[1]}" not found`);
|
|
134
131
|
}
|
|
135
132
|
return ref.getResource(m[2]);
|
|
136
133
|
}
|
|
@@ -150,32 +147,32 @@ class ApiDocument {
|
|
|
150
147
|
}
|
|
151
148
|
if (silent)
|
|
152
149
|
return;
|
|
153
|
-
throw new
|
|
150
|
+
throw new NotFoundError(`Resource not found (${path})`);
|
|
154
151
|
}
|
|
155
152
|
getCollection(path, silent) {
|
|
156
153
|
const t = this.getResource(path);
|
|
157
154
|
if (!t && silent)
|
|
158
155
|
return;
|
|
159
|
-
if (t && t.kind ===
|
|
156
|
+
if (t && t.kind === OpraSchema.Collection.Kind)
|
|
160
157
|
return t;
|
|
161
|
-
throw new
|
|
158
|
+
throw new NotAcceptableError(`Resource type "${t.name}" is not a Collection`);
|
|
162
159
|
}
|
|
163
160
|
getSingleton(path, silent) {
|
|
164
161
|
const t = this.getResource(path);
|
|
165
162
|
if (!t && silent)
|
|
166
163
|
return;
|
|
167
|
-
if (t && t.kind ===
|
|
164
|
+
if (t && t.kind === OpraSchema.Singleton.Kind)
|
|
168
165
|
return t;
|
|
169
|
-
throw new
|
|
166
|
+
throw new NotAcceptableError(`Resource type "${t.name}" is not a Singleton`);
|
|
170
167
|
}
|
|
171
168
|
/**
|
|
172
169
|
* Export as Opra schema definition object
|
|
173
170
|
*/
|
|
174
171
|
exportSchema() {
|
|
175
172
|
const schema = {
|
|
176
|
-
version:
|
|
173
|
+
version: OpraSchema.SpecVersion,
|
|
177
174
|
url: this.url,
|
|
178
|
-
info:
|
|
175
|
+
info: cloneObject(this.info)
|
|
179
176
|
};
|
|
180
177
|
if (this.references.size) {
|
|
181
178
|
const references = {};
|
|
@@ -201,11 +198,10 @@ class ApiDocument {
|
|
|
201
198
|
types[name] = r.exportSchema();
|
|
202
199
|
}
|
|
203
200
|
}
|
|
204
|
-
return
|
|
201
|
+
return omitUndefined(schema);
|
|
205
202
|
}
|
|
206
203
|
invalidate() {
|
|
207
204
|
this._typeCache.clear();
|
|
208
205
|
this._typesCacheByCtor.clear();
|
|
209
206
|
}
|
|
210
207
|
}
|
|
211
|
-
exports.ApiDocument = ApiDocument;
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.METADATA_KEY = Symbol.for('opra.metadata');
|
|
5
|
-
exports.NAMESPACE_PATTERN = /([A-Z$_]\w+)(?::.+)?/i;
|
|
6
|
-
exports.TYPENAME_PATTERN = /^(.*)Type$/;
|
|
1
|
+
export const METADATA_KEY = Symbol.for('opra.metadata');
|
|
2
|
+
export const NAMESPACE_PATTERN = /([A-Z$_]\w+)(?::.+)?/i;
|
|
3
|
+
export const TYPENAME_PATTERN = /^(.*)Type$/;
|
|
@@ -1,22 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const index_js_2 = require("../../schema/index.js");
|
|
6
|
-
const constants_js_1 = require("../constants.js");
|
|
7
|
-
const enum_type_js_1 = require("./enum-type.js");
|
|
1
|
+
import { omitUndefined } from '../../helpers/index.js';
|
|
2
|
+
import { OpraSchema } from '../../schema/index.js';
|
|
3
|
+
import { METADATA_KEY } from '../constants.js';
|
|
4
|
+
import { EnumType } from './enum-type.js';
|
|
8
5
|
/**
|
|
9
6
|
* @class ComplexType
|
|
10
7
|
*/
|
|
11
|
-
|
|
8
|
+
export const ApiField = function (...args) {
|
|
12
9
|
// ClassDecorator
|
|
13
10
|
if (!this) {
|
|
14
11
|
const [options] = args;
|
|
15
12
|
return function (target, propertyKey) {
|
|
16
13
|
if (typeof propertyKey !== 'string')
|
|
17
14
|
throw new TypeError(`Symbol properties can't be used as a field`);
|
|
18
|
-
const metadata = Reflect.getOwnMetadata(
|
|
19
|
-
metadata.kind =
|
|
15
|
+
const metadata = Reflect.getOwnMetadata(METADATA_KEY, target.constructor) || {};
|
|
16
|
+
metadata.kind = OpraSchema.ComplexType.Kind;
|
|
20
17
|
metadata.fields = metadata.fields || {};
|
|
21
18
|
const designType = Reflect.getMetadata('design:type', target, propertyKey);
|
|
22
19
|
const isArray = designType === Array;
|
|
@@ -34,17 +31,17 @@ exports.ApiField = function (...args) {
|
|
|
34
31
|
o[v] = v;
|
|
35
32
|
return o;
|
|
36
33
|
}, {});
|
|
37
|
-
|
|
34
|
+
EnumType(enumObj);
|
|
38
35
|
elemMeta.enum = enumObj;
|
|
39
36
|
}
|
|
40
37
|
else {
|
|
41
|
-
const m = Reflect.getOwnMetadata(
|
|
42
|
-
if (!
|
|
38
|
+
const m = Reflect.getOwnMetadata(METADATA_KEY, options?.enum);
|
|
39
|
+
if (!OpraSchema.isEnumType(m))
|
|
43
40
|
throw new TypeError(`Invalid "enum" value. Did you forget to set metadata using EnumType() method?`);
|
|
44
41
|
elemMeta.enum = options.enum;
|
|
45
42
|
}
|
|
46
43
|
}
|
|
47
|
-
Reflect.defineMetadata(
|
|
44
|
+
Reflect.defineMetadata(METADATA_KEY, omitUndefined(metadata), target.constructor);
|
|
48
45
|
};
|
|
49
46
|
}
|
|
50
47
|
// Constructor
|
|
@@ -79,4 +76,4 @@ const proto = {
|
|
|
79
76
|
return out;
|
|
80
77
|
}
|
|
81
78
|
};
|
|
82
|
-
Object.assign(
|
|
79
|
+
Object.assign(ApiField.prototype, proto);
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.AnyType = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const simple_type_js_1 = require("../simple-type.js");
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { SimpleType } from '../simple-type.js';
|
|
6
3
|
let AnyType = class AnyType {
|
|
7
4
|
};
|
|
8
|
-
AnyType =
|
|
9
|
-
|
|
5
|
+
AnyType = __decorate([
|
|
6
|
+
SimpleType({
|
|
10
7
|
description: 'Any value',
|
|
11
8
|
ctor: Object
|
|
12
9
|
})
|
|
13
10
|
], AnyType);
|
|
14
|
-
|
|
11
|
+
export { AnyType };
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.Base64Type = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const simple_type_js_1 = require("../simple-type.js");
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { SimpleType } from '../simple-type.js';
|
|
6
3
|
const BASE64_PATTERN = /^(?:[A-Za-z0-9+/]{4})*(?:(?:[A-Za-z0-9+/][AQgw](:?==)?)|(?:[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=?))?$/;
|
|
7
4
|
let Base64Type = class Base64Type {
|
|
8
5
|
decode(v) {
|
|
@@ -29,10 +26,10 @@ let Base64Type = class Base64Type {
|
|
|
29
26
|
throw new TypeError(`Invalid base64 value "${String(v).substring(0, 10)}..."`);
|
|
30
27
|
}
|
|
31
28
|
};
|
|
32
|
-
Base64Type =
|
|
33
|
-
|
|
29
|
+
Base64Type = __decorate([
|
|
30
|
+
SimpleType({
|
|
34
31
|
description: 'A stream of bytes, base64 encoded',
|
|
35
32
|
ctor: ArrayBuffer
|
|
36
33
|
})
|
|
37
34
|
], Base64Type);
|
|
38
|
-
|
|
35
|
+
export { Base64Type };
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.BigIntType = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const simple_type_js_1 = require("../simple-type.js");
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { SimpleType } from '../simple-type.js';
|
|
6
3
|
const BigIntConstructor = Object.getPrototypeOf(BigInt(0)).constructor;
|
|
7
4
|
let BigIntType = class BigIntType {
|
|
8
5
|
decode(v) {
|
|
@@ -23,11 +20,11 @@ let BigIntType = class BigIntType {
|
|
|
23
20
|
throw new TypeError(`Invalid number value "${v}"`);
|
|
24
21
|
}
|
|
25
22
|
};
|
|
26
|
-
BigIntType =
|
|
27
|
-
|
|
23
|
+
BigIntType = __decorate([
|
|
24
|
+
SimpleType({
|
|
28
25
|
name: 'bigint',
|
|
29
26
|
description: 'BigInt number',
|
|
30
27
|
ctor: BigIntConstructor
|
|
31
28
|
})
|
|
32
29
|
], BigIntType);
|
|
33
|
-
|
|
30
|
+
export { BigIntType };
|
|
@@ -1,28 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const putil_varhelpers_1 = require("putil-varhelpers");
|
|
6
|
-
const simple_type_js_1 = require("../simple-type.js");
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { toBoolean } from 'putil-varhelpers';
|
|
3
|
+
import { SimpleType } from '../simple-type.js';
|
|
7
4
|
let BooleanType = class BooleanType {
|
|
8
5
|
decode(v) {
|
|
9
6
|
if (v == null)
|
|
10
7
|
return v;
|
|
11
|
-
return
|
|
8
|
+
return toBoolean(v);
|
|
12
9
|
}
|
|
13
10
|
encode(v) {
|
|
14
11
|
if (v == null)
|
|
15
12
|
return v;
|
|
16
|
-
return
|
|
13
|
+
return toBoolean(v);
|
|
17
14
|
}
|
|
18
15
|
coerce(v) {
|
|
19
16
|
return this.decode(v);
|
|
20
17
|
}
|
|
21
18
|
};
|
|
22
|
-
BooleanType =
|
|
23
|
-
|
|
19
|
+
BooleanType = __decorate([
|
|
20
|
+
SimpleType({
|
|
24
21
|
description: 'Simple true/false value',
|
|
25
22
|
ctor: Boolean
|
|
26
23
|
})
|
|
27
24
|
], BooleanType);
|
|
28
|
-
|
|
25
|
+
export { BooleanType };
|