@opra/common 0.25.4 → 0.26.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/browser.js +1843 -1355
- package/cjs/document/api-document.js +16 -220
- package/cjs/document/data-type/complex-type-class.js +41 -19
- package/cjs/document/data-type/complex-type.js +1 -1
- package/cjs/document/data-type/data-type.js +2 -1
- package/cjs/document/data-type/enum-type-class.js +15 -12
- package/cjs/document/data-type/enum-type.js +28 -14
- package/cjs/document/data-type/field-class.js +21 -3
- package/cjs/document/data-type/field-decorator.js +0 -19
- package/cjs/document/data-type/mapped-type-class.js +8 -10
- package/cjs/document/data-type/mapped-type.js +1 -1
- package/cjs/document/data-type/simple-type-class.js +17 -8
- package/cjs/document/data-type/simple-type.js +1 -1
- package/cjs/document/data-type/union-type-class.js +15 -13
- package/cjs/document/document-base.js +24 -0
- package/cjs/document/factory/api-document-factory.js +231 -0
- package/cjs/document/factory/type-document-factory.js +324 -0
- package/cjs/document/index.js +7 -1
- package/cjs/document/resource/action-decorator.js +27 -0
- package/cjs/document/resource/collection-class.js +95 -44
- package/cjs/document/resource/collection-decorator.js +182 -0
- package/cjs/document/resource/collection.js +4 -4
- package/cjs/document/resource/container-class.js +82 -0
- package/cjs/document/resource/container-decorator.js +24 -0
- package/cjs/document/resource/container.js +29 -0
- package/cjs/document/resource/crud-resource.js +31 -0
- package/cjs/document/resource/endpoint.js +28 -21
- package/cjs/document/resource/operation-decorator.js +27 -0
- package/cjs/document/resource/parameter.js +51 -0
- package/cjs/document/{decorators/resource.decorator.js → resource/resource-decorator.js} +9 -14
- package/cjs/document/resource/resource.js +20 -5
- package/cjs/document/resource/singleton-class.js +60 -40
- package/cjs/document/resource/singleton-decorator.js +57 -0
- package/cjs/document/resource/singleton.js +4 -4
- package/cjs/document/resource/storage-class.js +11 -17
- package/cjs/document/resource/storage-decorator.js +66 -0
- package/cjs/document/resource/storage.js +3 -3
- package/cjs/document/type-document.js +195 -0
- package/cjs/exception/http-errors/internal-server.error.js +1 -0
- package/cjs/exception/opra-exception.js +18 -12
- package/cjs/exception/wrap-exception.js +2 -1
- package/cjs/helpers/responsive-map.js +3 -3
- package/cjs/http/opra-url-path.js +5 -1
- package/cjs/schema/document/document-base.interface.js +2 -0
- package/cjs/schema/document/type-document.interface.js +2 -0
- package/cjs/schema/opra-schema.ns.js +3 -1
- package/cjs/schema/type-guards.js +3 -3
- package/esm/document/api-document.js +16 -220
- package/esm/document/data-type/complex-type-class.js +40 -19
- package/esm/document/data-type/complex-type.js +1 -1
- package/esm/document/data-type/data-type.js +2 -1
- package/esm/document/data-type/enum-type-class.js +15 -12
- package/esm/document/data-type/enum-type.js +26 -12
- package/esm/document/data-type/field-class.js +20 -3
- package/esm/document/data-type/field-decorator.js +0 -19
- package/esm/document/data-type/mapped-type-class.js +9 -11
- package/esm/document/data-type/mapped-type.js +1 -1
- package/esm/document/data-type/simple-type-class.js +15 -6
- package/esm/document/data-type/simple-type.js +1 -1
- package/esm/document/data-type/union-type-class.js +11 -9
- package/esm/document/document-base.js +20 -0
- package/esm/document/factory/api-document-factory.js +227 -0
- package/esm/document/factory/type-document-factory.js +320 -0
- package/esm/document/index.js +7 -1
- package/esm/document/resource/action-decorator.js +23 -0
- package/esm/document/resource/collection-class.js +95 -44
- package/esm/document/resource/collection-decorator.js +178 -0
- package/esm/document/resource/collection.js +4 -4
- package/esm/document/resource/container-class.js +78 -0
- package/esm/document/resource/container-decorator.js +20 -0
- package/esm/document/resource/container.js +25 -0
- package/esm/document/resource/crud-resource.js +27 -0
- package/esm/document/resource/endpoint.js +27 -21
- package/esm/document/resource/operation-decorator.js +23 -0
- package/esm/document/resource/parameter.js +46 -0
- package/esm/document/resource/resource-decorator.js +28 -0
- package/esm/document/resource/resource.js +20 -5
- package/esm/document/resource/singleton-class.js +58 -38
- package/esm/document/resource/singleton-decorator.js +53 -0
- package/esm/document/resource/singleton.js +4 -4
- package/esm/document/resource/storage-class.js +9 -15
- package/esm/document/resource/storage-decorator.js +62 -0
- package/esm/document/resource/storage.js +3 -3
- package/esm/document/type-document.js +191 -0
- package/esm/exception/http-errors/internal-server.error.js +1 -0
- package/esm/exception/opra-exception.js +18 -12
- package/esm/exception/wrap-exception.js +2 -1
- package/esm/helpers/responsive-map.js +3 -3
- package/esm/http/opra-url-path.js +4 -1
- package/esm/schema/document/document-base.interface.js +1 -0
- package/esm/schema/document/type-document.interface.js +1 -0
- package/esm/schema/opra-schema.ns.js +3 -1
- package/esm/schema/type-guards.js +1 -1
- package/package.json +2 -2
- package/types/document/api-document.d.ts +32 -73
- package/types/document/data-type/complex-type-class.d.ts +5 -5
- package/types/document/data-type/complex-type.d.ts +8 -3
- package/types/document/{decorators → data-type}/complex-type.decorator.d.ts +1 -1
- package/types/document/data-type/data-type.d.ts +11 -1
- package/types/document/data-type/enum-type-class.d.ts +6 -6
- package/types/document/data-type/enum-type.d.ts +12 -6
- package/types/document/data-type/field-class.d.ts +9 -1
- package/types/document/data-type/field-decorator.d.ts +1 -1
- package/types/document/data-type/field.d.ts +4 -3
- package/types/document/data-type/mapped-type-class.d.ts +5 -9
- package/types/document/data-type/mapped-type.d.ts +6 -7
- package/types/document/data-type/simple-type-class.d.ts +6 -3
- package/types/document/{decorators → data-type}/simple-type.decorator.d.ts +1 -1
- package/types/document/data-type/union-type-class.d.ts +2 -8
- package/types/document/data-type/union-type.d.ts +7 -6
- package/types/document/document-base.d.ts +10 -0
- package/types/document/factory/api-document-factory.d.ts +50 -0
- package/types/document/factory/type-document-factory.d.ts +56 -0
- package/types/document/index.d.ts +7 -1
- package/types/document/resource/action-decorator.d.ts +6 -0
- package/types/document/resource/collection-class.d.ts +17 -12
- package/types/document/resource/collection-decorator.d.ts +192 -0
- package/types/document/resource/collection.d.ts +11 -19
- package/types/document/resource/container-class.d.ts +66 -0
- package/types/document/resource/container-decorator.d.ts +37 -0
- package/types/document/resource/container.d.ts +25 -0
- package/types/document/resource/crud-resource.d.ts +16 -0
- package/types/document/resource/endpoint.d.ts +26 -19
- package/types/document/resource/operation-decorator.d.ts +6 -0
- package/types/document/resource/parameter.d.ts +35 -0
- package/types/document/resource/resource-decorator.d.ts +32 -0
- package/types/document/resource/resource.d.ts +15 -13
- package/types/document/resource/singleton-class.d.ts +15 -14
- package/types/document/resource/singleton-decorator.d.ts +125 -0
- package/types/document/resource/singleton.d.ts +11 -16
- package/types/document/resource/storage-class.d.ts +13 -6
- package/types/document/resource/storage-decorator.d.ts +98 -0
- package/types/document/resource/storage.d.ts +9 -12
- package/types/document/type-document.d.ts +68 -0
- package/types/exception/opra-exception.d.ts +1 -1
- package/types/http/opra-url-path.d.ts +2 -1
- package/types/schema/data-type/complex-type.interface.d.ts +1 -1
- package/types/schema/data-type/data-type.interface.d.ts +3 -3
- package/types/schema/data-type/enum-type.interface.d.ts +6 -7
- package/types/schema/data-type/mapped-type.interface.d.ts +4 -3
- package/types/schema/data-type/simple-type.interface.d.ts +3 -3
- package/types/schema/data-type/union-type.interface.d.ts +6 -4
- package/types/schema/document/api-document.interface.d.ts +11 -0
- package/types/schema/document/document-base.interface.d.ts +24 -0
- package/types/schema/document/type-document.interface.d.ts +6 -0
- package/types/schema/opra-schema.ns.d.ts +3 -1
- package/types/schema/resource/collection.interface.d.ts +31 -31
- package/types/schema/resource/container.interface.d.ts +4 -3
- package/types/schema/resource/resource.interface.d.ts +1 -1
- package/types/schema/resource/singleton.interface.d.ts +15 -10
- package/types/schema/resource/storage.interface.d.ts +49 -45
- package/types/schema/type-guards.d.ts +3 -2
- package/cjs/document/decorators/collection-decorator.js +0 -38
- package/cjs/document/decorators/singleton.decorator.js +0 -36
- package/cjs/document/decorators/storage.decorator.js +0 -35
- package/cjs/document/factory/add-references.js +0 -20
- package/cjs/document/factory/create-document.js +0 -83
- package/cjs/document/factory/factory.js +0 -66
- package/cjs/document/factory/import-resource-class.js +0 -54
- package/cjs/document/factory/import-type-class.js +0 -146
- package/cjs/document/factory/index.js +0 -4
- package/cjs/document/factory/process-resources.js +0 -70
- package/cjs/document/factory/process-types.js +0 -191
- package/cjs/document/utils/generate-codec.js +0 -39
- package/esm/document/decorators/collection-decorator.js +0 -34
- package/esm/document/decorators/resource.decorator.js +0 -33
- package/esm/document/decorators/singleton.decorator.js +0 -32
- package/esm/document/decorators/storage.decorator.js +0 -31
- package/esm/document/factory/add-references.js +0 -16
- package/esm/document/factory/create-document.js +0 -77
- package/esm/document/factory/factory.js +0 -62
- package/esm/document/factory/import-resource-class.js +0 -48
- package/esm/document/factory/import-type-class.js +0 -136
- package/esm/document/factory/index.js +0 -1
- package/esm/document/factory/process-resources.js +0 -63
- package/esm/document/factory/process-types.js +0 -185
- package/esm/document/utils/generate-codec.js +0 -33
- package/types/document/decorators/collection-decorator.d.ts +0 -30
- package/types/document/decorators/resource.decorator.d.ts +0 -9
- package/types/document/decorators/singleton.decorator.d.ts +0 -25
- package/types/document/decorators/storage.decorator.d.ts +0 -22
- package/types/document/factory/add-references.d.ts +0 -4
- package/types/document/factory/create-document.d.ts +0 -12
- package/types/document/factory/factory.d.ts +0 -63
- package/types/document/factory/import-resource-class.d.ts +0 -10
- package/types/document/factory/import-type-class.d.ts +0 -17
- package/types/document/factory/index.d.ts +0 -1
- package/types/document/factory/process-resources.d.ts +0 -9
- package/types/document/factory/process-types.d.ts +0 -6
- package/types/document/utils/generate-codec.d.ts +0 -10
- package/types/schema/document.interface.d.ts +0 -34
- /package/cjs/document/{decorators → data-type}/complex-type.decorator.js +0 -0
- /package/cjs/document/{decorators → data-type}/simple-type.decorator.js +0 -0
- /package/cjs/schema/{document.interface.js → document/api-document.interface.js} +0 -0
- /package/esm/document/{decorators → data-type}/complex-type.decorator.js +0 -0
- /package/esm/document/{decorators → data-type}/simple-type.decorator.js +0 -0
- /package/esm/schema/{document.interface.js → document/api-document.interface.js} +0 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypeDocument = void 0;
|
|
4
|
+
const index_js_1 = require("../exception/index.js");
|
|
5
|
+
const index_js_2 = require("../helpers/index.js");
|
|
6
|
+
const index_js_3 = require("../schema/index.js");
|
|
7
|
+
const constants_js_1 = require("./constants.js");
|
|
8
|
+
const complex_type_js_1 = require("./data-type/complex-type.js");
|
|
9
|
+
const document_base_js_1 = require("./document-base.js");
|
|
10
|
+
class TypeDocument extends document_base_js_1.DocumentBase {
|
|
11
|
+
constructor() {
|
|
12
|
+
super();
|
|
13
|
+
this._designCtorMap = new Map();
|
|
14
|
+
this._typeCache = new index_js_2.ResponsiveMap();
|
|
15
|
+
this._typesCacheByCtor = new Map();
|
|
16
|
+
this.references = new index_js_2.ResponsiveMap();
|
|
17
|
+
this.types = new index_js_2.ResponsiveMap();
|
|
18
|
+
const BigIntConstructor = Object.getPrototypeOf(BigInt(0)).constructor;
|
|
19
|
+
const BufferConstructor = Object.getPrototypeOf(Buffer.from([]));
|
|
20
|
+
this._designCtorMap.set(String, 'string');
|
|
21
|
+
this._designCtorMap.set(Number, 'number');
|
|
22
|
+
this._designCtorMap.set(Boolean, 'boolean');
|
|
23
|
+
this._designCtorMap.set(Object, 'any');
|
|
24
|
+
this._designCtorMap.set(Date, 'timestamp');
|
|
25
|
+
this._designCtorMap.set(BigIntConstructor, 'bigint');
|
|
26
|
+
this._designCtorMap.set(ArrayBuffer, 'base64');
|
|
27
|
+
this._designCtorMap.set(SharedArrayBuffer, 'base64');
|
|
28
|
+
this._designCtorMap.set(BufferConstructor, 'base64');
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
getDataType(arg0, silent) {
|
|
34
|
+
// Convert design ctor to type name (String -> 'string')
|
|
35
|
+
if (typeof arg0 === 'function') {
|
|
36
|
+
const x = this._designCtorMap.get(arg0);
|
|
37
|
+
if (x)
|
|
38
|
+
arg0 = x;
|
|
39
|
+
}
|
|
40
|
+
let dataType;
|
|
41
|
+
let name;
|
|
42
|
+
// Determine name
|
|
43
|
+
if (typeof arg0 === 'function') {
|
|
44
|
+
const metadata = Reflect.getMetadata(constants_js_1.DATATYPE_METADATA, arg0);
|
|
45
|
+
name = metadata?.name || arg0.name;
|
|
46
|
+
}
|
|
47
|
+
else if (typeof arg0 === 'function') {
|
|
48
|
+
const metadata = Reflect.getMetadata(constants_js_1.DATATYPE_METADATA, arg0);
|
|
49
|
+
name = metadata?.name;
|
|
50
|
+
}
|
|
51
|
+
else
|
|
52
|
+
name = String(arg0);
|
|
53
|
+
// Try to get instance from cache
|
|
54
|
+
const t = typeof arg0 === 'string'
|
|
55
|
+
? this._typeCache.get(arg0)
|
|
56
|
+
: this._typesCacheByCtor.get(arg0);
|
|
57
|
+
if (t)
|
|
58
|
+
return t;
|
|
59
|
+
// If cached as null, it means "not found"
|
|
60
|
+
if (t === null) {
|
|
61
|
+
if (silent)
|
|
62
|
+
return;
|
|
63
|
+
throw new Error(`Data type "${name}" does not exists`);
|
|
64
|
+
}
|
|
65
|
+
if (typeof arg0 === 'string' && arg0) {
|
|
66
|
+
const m = constants_js_1.NAMESPACE_PATTERN.exec(arg0);
|
|
67
|
+
if (!m)
|
|
68
|
+
throw new TypeError(`Invalid data type name "${name}"`);
|
|
69
|
+
// If given string has namespace pattern (ns:type_name)
|
|
70
|
+
if (m[2]) {
|
|
71
|
+
const ref = this.references.get(m[1]);
|
|
72
|
+
if (!ref) {
|
|
73
|
+
if (silent)
|
|
74
|
+
return;
|
|
75
|
+
throw new Error(`No referenced document found for given namespace "${m[1]}"`);
|
|
76
|
+
}
|
|
77
|
+
dataType = ref.getDataType(m[2], silent);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
// Get instance from own types
|
|
81
|
+
dataType = this.types.get(arg0);
|
|
82
|
+
// if not found, search in references (from last to first)
|
|
83
|
+
if (!dataType) {
|
|
84
|
+
const references = Array.from(this.references.values()).reverse();
|
|
85
|
+
for (const ref of references) {
|
|
86
|
+
dataType = ref.getDataType(name, true);
|
|
87
|
+
if (dataType)
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (dataType) {
|
|
93
|
+
this._typeCache.set(arg0, dataType);
|
|
94
|
+
return dataType;
|
|
95
|
+
}
|
|
96
|
+
if (!silent)
|
|
97
|
+
throw new index_js_1.NotFoundError(`Data type "${arg0}" does not exists`);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (typeof arg0 === 'function') {
|
|
101
|
+
const types = Array.from(this.types.values()).reverse();
|
|
102
|
+
for (const dt of types) {
|
|
103
|
+
if (dt instanceof complex_type_js_1.ComplexType && dt.isTypeOf(arg0)) {
|
|
104
|
+
dataType = dt;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// if not found, search in references (from last to first)
|
|
109
|
+
if (!dataType) {
|
|
110
|
+
const references = Array.from(this.references.values()).reverse();
|
|
111
|
+
for (const ref of references) {
|
|
112
|
+
dataType = ref.getDataType(arg0, true);
|
|
113
|
+
if (dataType)
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (dataType)
|
|
118
|
+
this._typesCacheByCtor.set(arg0, dataType);
|
|
119
|
+
if (dataType)
|
|
120
|
+
return dataType;
|
|
121
|
+
if (!silent)
|
|
122
|
+
throw new Error(`No data type mapping found for class "${name}"`);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (typeof arg0 === 'object') {
|
|
126
|
+
const metadata = arg0[constants_js_1.DATATYPE_METADATA];
|
|
127
|
+
if (metadata && metadata.name) {
|
|
128
|
+
dataType = this.getDataType(metadata.name, true);
|
|
129
|
+
if (dataType)
|
|
130
|
+
return dataType;
|
|
131
|
+
if (!silent)
|
|
132
|
+
throw new Error(`No data type mapping found for class "${name}"`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/* istanbul ignore next */
|
|
136
|
+
if (!silent)
|
|
137
|
+
throw new TypeError('Invalid argument');
|
|
138
|
+
}
|
|
139
|
+
getComplexType(nameOrCtor, silent) {
|
|
140
|
+
if (nameOrCtor === Object)
|
|
141
|
+
nameOrCtor = 'object';
|
|
142
|
+
const t = this.getDataType(nameOrCtor);
|
|
143
|
+
if (!t && silent)
|
|
144
|
+
return;
|
|
145
|
+
if (t && t.kind === index_js_3.OpraSchema.ComplexType.Kind)
|
|
146
|
+
return t;
|
|
147
|
+
throw new index_js_1.NotAcceptableError(`Data type "${t.name}" is not a ComplexType`);
|
|
148
|
+
}
|
|
149
|
+
getSimpleType(nameOrCtor, silent) {
|
|
150
|
+
const t = this.getDataType(nameOrCtor);
|
|
151
|
+
if (!t && silent)
|
|
152
|
+
return;
|
|
153
|
+
if (t && t.kind === index_js_3.OpraSchema.SimpleType.Kind)
|
|
154
|
+
return t;
|
|
155
|
+
throw new index_js_1.NotAcceptableError(`Data type "${t.name || t}" is not a SimpleType`);
|
|
156
|
+
}
|
|
157
|
+
getEnumType(nameOrCtor, silent) {
|
|
158
|
+
const t = this.getDataType(nameOrCtor);
|
|
159
|
+
if (!t && silent)
|
|
160
|
+
return;
|
|
161
|
+
if (t && t.kind === index_js_3.OpraSchema.EnumType.Kind)
|
|
162
|
+
return t;
|
|
163
|
+
throw new index_js_1.NotAcceptableError(`Data type "${t.name || t}" is not a EnumType`);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Export as Opra schema definition object
|
|
167
|
+
*/
|
|
168
|
+
exportSchema(options) {
|
|
169
|
+
const schema = super.exportSchema();
|
|
170
|
+
if (this.references.size) {
|
|
171
|
+
const references = {};
|
|
172
|
+
let i = 0;
|
|
173
|
+
for (const [ns, r] of this.references.entries()) {
|
|
174
|
+
if (ns.toLowerCase() === 'opra')
|
|
175
|
+
continue;
|
|
176
|
+
references[ns] = r.url ? r.url : r.exportSchema(options);
|
|
177
|
+
i++;
|
|
178
|
+
}
|
|
179
|
+
if (i)
|
|
180
|
+
schema.references = references;
|
|
181
|
+
}
|
|
182
|
+
if (this.types.size) {
|
|
183
|
+
const types = schema.types = {};
|
|
184
|
+
for (const [name, r] of this.types.entries()) {
|
|
185
|
+
types[name] = r.exportSchema(options);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return schema;
|
|
189
|
+
}
|
|
190
|
+
invalidate() {
|
|
191
|
+
this._typeCache.clear();
|
|
192
|
+
this._typesCacheByCtor.clear();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
exports.TypeDocument = TypeDocument;
|
|
@@ -25,7 +25,7 @@ class OpraException extends Error {
|
|
|
25
25
|
this.initString(issue);
|
|
26
26
|
else if (issue instanceof Error)
|
|
27
27
|
this.initError(issue);
|
|
28
|
-
else
|
|
28
|
+
else
|
|
29
29
|
this.init(issue);
|
|
30
30
|
this.message = this.message || this.constructor.name;
|
|
31
31
|
}
|
|
@@ -47,25 +47,31 @@ class OpraException extends Error {
|
|
|
47
47
|
}, true);
|
|
48
48
|
}
|
|
49
49
|
init(issue) {
|
|
50
|
-
this.message = issue
|
|
51
|
-
this.severity = issue
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
this.message = issue?.message || this.constructor.name;
|
|
51
|
+
this.severity = issue?.severity || 'error';
|
|
52
|
+
if (issue) {
|
|
53
|
+
this.system = issue.system;
|
|
54
|
+
this.code = issue.code;
|
|
55
|
+
this.details = issue.details;
|
|
56
|
+
}
|
|
55
57
|
}
|
|
56
58
|
initString(issue) {
|
|
57
|
-
this.
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
this.init({
|
|
60
|
+
message: String(issue || '') || this.constructor.name,
|
|
61
|
+
severity: 'error',
|
|
62
|
+
code: this.constructor.name
|
|
63
|
+
});
|
|
60
64
|
}
|
|
61
65
|
initError(issue) {
|
|
62
66
|
if (typeof issue.status === 'number')
|
|
63
67
|
this.status = issue.status;
|
|
64
68
|
else if (typeof issue.getStatus === 'function')
|
|
65
69
|
this.status = issue.getStatus();
|
|
66
|
-
this.
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
this.init({
|
|
71
|
+
message: issue.message,
|
|
72
|
+
severity: issue.severity || 'error',
|
|
73
|
+
code: issue.code || (issue.constructor.name)
|
|
74
|
+
});
|
|
69
75
|
}
|
|
70
76
|
}
|
|
71
77
|
exports.OpraException = OpraException;
|
|
@@ -4,6 +4,7 @@ exports.wrapException = void 0;
|
|
|
4
4
|
const bad_request_error_js_1 = require("./http-errors/bad-request.error.js");
|
|
5
5
|
const failed_dependency_error_js_1 = require("./http-errors/failed-dependency.error.js");
|
|
6
6
|
const forbidden_error_js_1 = require("./http-errors/forbidden.error.js");
|
|
7
|
+
const internal_server_error_js_1 = require("./http-errors/internal-server.error.js");
|
|
7
8
|
const method_not_allowed_error_js_1 = require("./http-errors/method-not-allowed.error.js");
|
|
8
9
|
const not_acceptable_error_js_1 = require("./http-errors/not-acceptable.error.js");
|
|
9
10
|
const not_found_error_js_1 = require("./http-errors/not-found.error.js");
|
|
@@ -36,7 +37,7 @@ function wrapException(error) {
|
|
|
36
37
|
case 424:
|
|
37
38
|
return new failed_dependency_error_js_1.FailedDependencyError(error);
|
|
38
39
|
default:
|
|
39
|
-
return new
|
|
40
|
+
return new internal_server_error_js_1.InternalServerError(error);
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
43
|
exports.wrapException = wrapException;
|
|
@@ -52,11 +52,11 @@ class ResponsiveMap extends Map {
|
|
|
52
52
|
return this;
|
|
53
53
|
}
|
|
54
54
|
keys() {
|
|
55
|
-
return this[kKeyOrder][Symbol.iterator]();
|
|
55
|
+
return [...this[kKeyOrder]][Symbol.iterator]();
|
|
56
56
|
}
|
|
57
57
|
values() {
|
|
58
58
|
let i = -1;
|
|
59
|
-
const arr = this[kKeyOrder];
|
|
59
|
+
const arr = [...this[kKeyOrder]];
|
|
60
60
|
const map = this;
|
|
61
61
|
return {
|
|
62
62
|
[Symbol.iterator]() {
|
|
@@ -73,7 +73,7 @@ class ResponsiveMap extends Map {
|
|
|
73
73
|
}
|
|
74
74
|
entries() {
|
|
75
75
|
let i = -1;
|
|
76
|
-
const arr = this[kKeyOrder];
|
|
76
|
+
const arr = [...this[kKeyOrder]];
|
|
77
77
|
const map = this;
|
|
78
78
|
return {
|
|
79
79
|
[Symbol.iterator]() {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var _a;
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.OpraURLPath = void 0;
|
|
4
|
+
exports.OpraURLPathComponent = exports.OpraURLPath = void 0;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const fast_tokenizer_1 = require("fast-tokenizer");
|
|
7
7
|
const putil_isplainobject_1 = tslib_1.__importDefault(require("putil-isplainobject"));
|
|
@@ -21,6 +21,9 @@ class OpraURLPath {
|
|
|
21
21
|
get length() {
|
|
22
22
|
return this[kLength];
|
|
23
23
|
}
|
|
24
|
+
slice(start, end) {
|
|
25
|
+
return new OpraURLPath(...[...this].slice(start, end));
|
|
26
|
+
}
|
|
24
27
|
resolve(...items) {
|
|
25
28
|
this._resolve(items);
|
|
26
29
|
return this;
|
|
@@ -245,6 +248,7 @@ class OpraURLPathComponent {
|
|
|
245
248
|
});
|
|
246
249
|
}
|
|
247
250
|
}
|
|
251
|
+
exports.OpraURLPathComponent = OpraURLPathComponent;
|
|
248
252
|
function removeLeadingSeparator(s) {
|
|
249
253
|
const m = /^\/*(.*)/.exec(s);
|
|
250
254
|
return m?.[1] || s;
|
|
@@ -15,5 +15,7 @@ tslib_1.__exportStar(require("./resource/resource.interface.js"), exports);
|
|
|
15
15
|
tslib_1.__exportStar(require("./resource/singleton.interface.js"), exports);
|
|
16
16
|
tslib_1.__exportStar(require("./resource/storage.interface.js"), exports);
|
|
17
17
|
tslib_1.__exportStar(require("./constants.js"), exports);
|
|
18
|
-
tslib_1.__exportStar(require("./document.interface.js"), exports);
|
|
18
|
+
tslib_1.__exportStar(require("./document/document-base.interface.js"), exports);
|
|
19
|
+
tslib_1.__exportStar(require("./document/type-document.interface.js"), exports);
|
|
20
|
+
tslib_1.__exportStar(require("./document/api-document.interface.js"), exports);
|
|
19
21
|
tslib_1.__exportStar(require("./type-guards.js"), exports);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isContainer = exports.isStorage = exports.isSingleton = exports.isCollection = exports.
|
|
3
|
+
exports.isContainer = exports.isStorage = exports.isSingleton = exports.isCollection = exports.isResource = exports.isEnumType = exports.isMappedType = exports.isUnionType = exports.isSimpleType = exports.isComplexType = exports.isDataType = void 0;
|
|
4
4
|
const complex_type_interface_js_1 = require("./data-type/complex-type.interface.js");
|
|
5
5
|
const enum_type_interface_js_1 = require("./data-type/enum-type.interface.js");
|
|
6
6
|
const mapped_type_interface_js_1 = require("./data-type/mapped-type.interface.js");
|
|
@@ -39,14 +39,14 @@ function isEnumType(obj) {
|
|
|
39
39
|
return obj && typeof obj === 'object' && obj.kind === enum_type_interface_js_1.EnumType.Kind;
|
|
40
40
|
}
|
|
41
41
|
exports.isEnumType = isEnumType;
|
|
42
|
-
function
|
|
42
|
+
function isResource(obj) {
|
|
43
43
|
return obj && typeof obj === 'object' &&
|
|
44
44
|
(obj.kind === container_interface_js_1.Container.Kind ||
|
|
45
45
|
obj.kind === collection_interface_js_1.Collection.Kind ||
|
|
46
46
|
obj.kind === singleton_interface_js_1.Singleton.Kind ||
|
|
47
47
|
obj.kind === storage_interface_js_1.Storage.Kind);
|
|
48
48
|
}
|
|
49
|
-
exports.
|
|
49
|
+
exports.isResource = isResource;
|
|
50
50
|
function isCollection(obj) {
|
|
51
51
|
return obj && typeof obj === 'object' && obj.kind === collection_interface_js_1.Collection.Kind;
|
|
52
52
|
}
|
|
@@ -1,236 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { NAMESPACE_PATTERN } from './constants.js';
|
|
5
|
-
import { ComplexType } from './data-type/complex-type.js';
|
|
6
|
-
export class ApiDocument {
|
|
1
|
+
import { Container } from './resource/container.js';
|
|
2
|
+
import { TypeDocument } from './type-document.js';
|
|
3
|
+
export class ApiDocument extends TypeDocument {
|
|
7
4
|
constructor() {
|
|
8
|
-
|
|
9
|
-
this.
|
|
10
|
-
this._typesCacheByCtor = new Map();
|
|
11
|
-
this.references = new ResponsiveMap();
|
|
12
|
-
this.types = new ResponsiveMap();
|
|
13
|
-
this.resources = new ResponsiveMap();
|
|
14
|
-
this.info = {
|
|
15
|
-
version: '',
|
|
16
|
-
title: ''
|
|
17
|
-
};
|
|
18
|
-
const BigIntConstructor = Object.getPrototypeOf(BigInt(0)).constructor;
|
|
19
|
-
const BufferConstructor = Object.getPrototypeOf(Buffer.from([]));
|
|
20
|
-
this._designCtorMap.set(String, 'string');
|
|
21
|
-
this._designCtorMap.set(Number, 'number');
|
|
22
|
-
this._designCtorMap.set(Boolean, 'boolean');
|
|
23
|
-
this._designCtorMap.set(Object, 'any');
|
|
24
|
-
this._designCtorMap.set(Date, 'timestamp');
|
|
25
|
-
this._designCtorMap.set(BigIntConstructor, 'bigint');
|
|
26
|
-
this._designCtorMap.set(ArrayBuffer, 'base64');
|
|
27
|
-
this._designCtorMap.set(SharedArrayBuffer, 'base64');
|
|
28
|
-
this._designCtorMap.set(BufferConstructor, 'base64');
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
*
|
|
32
|
-
*/
|
|
33
|
-
getDataType(arg0, silent) {
|
|
34
|
-
let dataType;
|
|
35
|
-
const name = typeof arg0 === 'function' ? arg0.name : String(arg0);
|
|
36
|
-
// Try to get instance from cache
|
|
37
|
-
const t = typeof arg0 === 'string'
|
|
38
|
-
? this._typeCache.get(arg0)
|
|
39
|
-
: this._typesCacheByCtor.get(arg0);
|
|
40
|
-
if (t)
|
|
41
|
-
return t;
|
|
42
|
-
// If cached as null, it means "not found"
|
|
43
|
-
if (t === null) {
|
|
44
|
-
if (silent)
|
|
45
|
-
return;
|
|
46
|
-
throw new Error(`Data type "${name}" does not exists`);
|
|
47
|
-
}
|
|
48
|
-
// Convert design ctor to type name (String -> 'string')
|
|
49
|
-
if (typeof arg0 === 'function') {
|
|
50
|
-
const x = this._designCtorMap.get(arg0);
|
|
51
|
-
if (x)
|
|
52
|
-
arg0 = x;
|
|
53
|
-
}
|
|
54
|
-
if (typeof arg0 === 'string') {
|
|
55
|
-
const m = NAMESPACE_PATTERN.exec(arg0);
|
|
56
|
-
if (!m)
|
|
57
|
-
throw new TypeError(`Invalid data type name "${name}"`);
|
|
58
|
-
// If given string has namespace pattern (ns:type_name)
|
|
59
|
-
if (m[2]) {
|
|
60
|
-
const ref = this.references.get(m[1]);
|
|
61
|
-
if (!ref) {
|
|
62
|
-
if (silent)
|
|
63
|
-
return;
|
|
64
|
-
throw new Error(`No referenced document found for given namespace "${m[1]}"`);
|
|
65
|
-
}
|
|
66
|
-
dataType = ref.getDataType(m[2], silent);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
// Get instance from own types
|
|
70
|
-
dataType = this.types.get(arg0);
|
|
71
|
-
// if not found, search in references (from last to first)
|
|
72
|
-
if (!dataType) {
|
|
73
|
-
const references = Array.from(this.references.values()).reverse();
|
|
74
|
-
for (const ref of references) {
|
|
75
|
-
dataType = ref.getDataType(name, true);
|
|
76
|
-
if (dataType)
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
if (dataType) {
|
|
82
|
-
this._typeCache.set(arg0, dataType);
|
|
83
|
-
return dataType;
|
|
84
|
-
}
|
|
85
|
-
if (!silent)
|
|
86
|
-
throw new NotFoundError(`Data type "${arg0}" does not exists`);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
if (typeof arg0 === 'function') {
|
|
90
|
-
const types = Array.from(this.types.values()).reverse();
|
|
91
|
-
for (const dt of types) {
|
|
92
|
-
if (dt instanceof ComplexType && dt.isTypeOf(arg0)) {
|
|
93
|
-
dataType = dt;
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
// if not found, search in references (from last to first)
|
|
98
|
-
if (!dataType) {
|
|
99
|
-
const references = Array.from(this.references.values()).reverse();
|
|
100
|
-
for (const ref of references) {
|
|
101
|
-
dataType = ref.getDataType(arg0, true);
|
|
102
|
-
if (dataType)
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
if (dataType)
|
|
107
|
-
this._typesCacheByCtor.set(arg0, dataType);
|
|
108
|
-
if (dataType)
|
|
109
|
-
return dataType;
|
|
110
|
-
if (!silent)
|
|
111
|
-
throw new NotFoundError(`No data type mapping found for class "${name}"`);
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
/* istanbul ignore next */
|
|
115
|
-
if (!silent)
|
|
116
|
-
throw new TypeError('Invalid argument');
|
|
117
|
-
}
|
|
118
|
-
getComplexType(nameOrCtor, silent) {
|
|
119
|
-
if (nameOrCtor === Object)
|
|
120
|
-
nameOrCtor = 'object';
|
|
121
|
-
const t = this.getDataType(nameOrCtor);
|
|
122
|
-
if (!t && silent)
|
|
123
|
-
return;
|
|
124
|
-
if (t && t.kind === OpraSchema.ComplexType.Kind)
|
|
125
|
-
return t;
|
|
126
|
-
throw new NotAcceptableError(`Data type "${t.name}" is not a ComplexType`);
|
|
127
|
-
}
|
|
128
|
-
getSimpleType(nameOrCtor, silent) {
|
|
129
|
-
const t = this.getDataType(nameOrCtor);
|
|
130
|
-
if (!t && silent)
|
|
131
|
-
return;
|
|
132
|
-
if (t && t.kind === OpraSchema.SimpleType.Kind)
|
|
133
|
-
return t;
|
|
134
|
-
throw new NotAcceptableError(`Data type "${t.name || t}" is not a SimpleType`);
|
|
5
|
+
super(...arguments);
|
|
6
|
+
this.root = new Container(this, { name: '' });
|
|
135
7
|
}
|
|
136
8
|
getResource(path, silent) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
// If given string has namespace pattern (ns:type_name)
|
|
142
|
-
if (m[2]) {
|
|
143
|
-
const ref = this.references.get(m[1]);
|
|
144
|
-
if (!ref) {
|
|
145
|
-
if (silent)
|
|
146
|
-
return;
|
|
147
|
-
throw new NotFoundError(`Reference "${m[1]}" not found`);
|
|
148
|
-
}
|
|
149
|
-
return ref.getResource(m[2]);
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
const name = m[1];
|
|
153
|
-
// Get instance from own types
|
|
154
|
-
resource = this.resources.get(name);
|
|
155
|
-
if (resource)
|
|
156
|
-
return resource;
|
|
157
|
-
// if not found, search in references (from last to first)
|
|
158
|
-
const references = Array.from(this.references.values()).reverse();
|
|
159
|
-
for (const ref of references) {
|
|
160
|
-
resource = silent ? ref.getResource(name, silent) : ref.getResource(name);
|
|
161
|
-
if (resource)
|
|
162
|
-
return resource;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
if (silent)
|
|
166
|
-
return;
|
|
167
|
-
throw new ResourceNotFoundError(path);
|
|
9
|
+
return this.root.getResource(path, silent);
|
|
10
|
+
}
|
|
11
|
+
getContainer(path, silent) {
|
|
12
|
+
return this.root.getContainer(path, silent);
|
|
168
13
|
}
|
|
169
14
|
getCollection(path, silent) {
|
|
170
|
-
|
|
171
|
-
if (!t && silent)
|
|
172
|
-
return;
|
|
173
|
-
if (t && t.kind === OpraSchema.Collection.Kind)
|
|
174
|
-
return t;
|
|
175
|
-
throw new NotAcceptableError(`Resource type "${t.name}" is not a Collection`);
|
|
15
|
+
return this.root.getCollection(path, silent);
|
|
176
16
|
}
|
|
177
17
|
getSingleton(path, silent) {
|
|
178
|
-
|
|
179
|
-
if (!t && silent)
|
|
180
|
-
return;
|
|
181
|
-
if (t && t.kind === OpraSchema.Singleton.Kind)
|
|
182
|
-
return t;
|
|
183
|
-
throw new NotAcceptableError(`Resource type "${t.name}" is not a Singleton`);
|
|
18
|
+
return this.root.getSingleton(path, silent);
|
|
184
19
|
}
|
|
185
20
|
getStorage(path, silent) {
|
|
186
|
-
|
|
187
|
-
if (!t && silent)
|
|
188
|
-
return;
|
|
189
|
-
if (t && t.kind === OpraSchema.Storage.Kind)
|
|
190
|
-
return t;
|
|
191
|
-
throw new NotAcceptableError(`Resource type "${t.name}" is not a Storage`);
|
|
21
|
+
return this.root.getStorage(path, silent);
|
|
192
22
|
}
|
|
193
23
|
/**
|
|
194
24
|
* Export as Opra schema definition object
|
|
195
25
|
*/
|
|
196
26
|
exportSchema(options) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
url: this.url,
|
|
202
|
-
info: cloneObject(this.info)
|
|
203
|
-
};
|
|
204
|
-
if (this.references.size) {
|
|
205
|
-
const references = {};
|
|
206
|
-
let i = 0;
|
|
207
|
-
for (const [ns, r] of this.references.entries()) {
|
|
208
|
-
if (ns.toLowerCase() === 'opra')
|
|
209
|
-
continue;
|
|
210
|
-
references[ns] = r.url ? r.url : r.exportSchema();
|
|
211
|
-
i++;
|
|
212
|
-
}
|
|
213
|
-
if (i)
|
|
214
|
-
schema.references = references;
|
|
215
|
-
}
|
|
216
|
-
if (this.resources.size) {
|
|
217
|
-
const resources = schema.resources = {};
|
|
218
|
-
for (const [name, r] of this.resources.entries()) {
|
|
219
|
-
resources[name] = r.exportSchema();
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
if (this.types.size) {
|
|
223
|
-
const types = schema.types = {};
|
|
224
|
-
for (const [name, r] of this.types.entries()) {
|
|
225
|
-
types[name] = r.exportSchema();
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
this._metadataCache = schema;
|
|
229
|
-
return this._metadataCache;
|
|
230
|
-
}
|
|
231
|
-
invalidate() {
|
|
232
|
-
this._metadataCache = undefined;
|
|
233
|
-
this._typeCache.clear();
|
|
234
|
-
this._typesCacheByCtor.clear();
|
|
27
|
+
const schema = super.exportSchema(options);
|
|
28
|
+
schema.root = this.root.exportSchema(options);
|
|
29
|
+
delete schema.root.kind;
|
|
30
|
+
return schema;
|
|
235
31
|
}
|
|
236
32
|
}
|