@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
|
@@ -1,240 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ApiDocument = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
const constants_js_1 = require("./constants.js");
|
|
8
|
-
const complex_type_js_1 = require("./data-type/complex-type.js");
|
|
9
|
-
class ApiDocument {
|
|
4
|
+
const container_js_1 = require("./resource/container.js");
|
|
5
|
+
const type_document_js_1 = require("./type-document.js");
|
|
6
|
+
class ApiDocument extends type_document_js_1.TypeDocument {
|
|
10
7
|
constructor() {
|
|
11
|
-
|
|
12
|
-
this.
|
|
13
|
-
this._typesCacheByCtor = new Map();
|
|
14
|
-
this.references = new index_js_2.ResponsiveMap();
|
|
15
|
-
this.types = new index_js_2.ResponsiveMap();
|
|
16
|
-
this.resources = new index_js_2.ResponsiveMap();
|
|
17
|
-
this.info = {
|
|
18
|
-
version: '',
|
|
19
|
-
title: ''
|
|
20
|
-
};
|
|
21
|
-
const BigIntConstructor = Object.getPrototypeOf(BigInt(0)).constructor;
|
|
22
|
-
const BufferConstructor = Object.getPrototypeOf(Buffer.from([]));
|
|
23
|
-
this._designCtorMap.set(String, 'string');
|
|
24
|
-
this._designCtorMap.set(Number, 'number');
|
|
25
|
-
this._designCtorMap.set(Boolean, 'boolean');
|
|
26
|
-
this._designCtorMap.set(Object, 'any');
|
|
27
|
-
this._designCtorMap.set(Date, 'timestamp');
|
|
28
|
-
this._designCtorMap.set(BigIntConstructor, 'bigint');
|
|
29
|
-
this._designCtorMap.set(ArrayBuffer, 'base64');
|
|
30
|
-
this._designCtorMap.set(SharedArrayBuffer, 'base64');
|
|
31
|
-
this._designCtorMap.set(BufferConstructor, 'base64');
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
*
|
|
35
|
-
*/
|
|
36
|
-
getDataType(arg0, silent) {
|
|
37
|
-
let dataType;
|
|
38
|
-
const name = typeof arg0 === 'function' ? arg0.name : String(arg0);
|
|
39
|
-
// Try to get instance from cache
|
|
40
|
-
const t = typeof arg0 === 'string'
|
|
41
|
-
? this._typeCache.get(arg0)
|
|
42
|
-
: this._typesCacheByCtor.get(arg0);
|
|
43
|
-
if (t)
|
|
44
|
-
return t;
|
|
45
|
-
// If cached as null, it means "not found"
|
|
46
|
-
if (t === null) {
|
|
47
|
-
if (silent)
|
|
48
|
-
return;
|
|
49
|
-
throw new Error(`Data type "${name}" does not exists`);
|
|
50
|
-
}
|
|
51
|
-
// Convert design ctor to type name (String -> 'string')
|
|
52
|
-
if (typeof arg0 === 'function') {
|
|
53
|
-
const x = this._designCtorMap.get(arg0);
|
|
54
|
-
if (x)
|
|
55
|
-
arg0 = x;
|
|
56
|
-
}
|
|
57
|
-
if (typeof arg0 === 'string') {
|
|
58
|
-
const m = constants_js_1.NAMESPACE_PATTERN.exec(arg0);
|
|
59
|
-
if (!m)
|
|
60
|
-
throw new TypeError(`Invalid data type name "${name}"`);
|
|
61
|
-
// If given string has namespace pattern (ns:type_name)
|
|
62
|
-
if (m[2]) {
|
|
63
|
-
const ref = this.references.get(m[1]);
|
|
64
|
-
if (!ref) {
|
|
65
|
-
if (silent)
|
|
66
|
-
return;
|
|
67
|
-
throw new Error(`No referenced document found for given namespace "${m[1]}"`);
|
|
68
|
-
}
|
|
69
|
-
dataType = ref.getDataType(m[2], silent);
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
// Get instance from own types
|
|
73
|
-
dataType = this.types.get(arg0);
|
|
74
|
-
// if not found, search in references (from last to first)
|
|
75
|
-
if (!dataType) {
|
|
76
|
-
const references = Array.from(this.references.values()).reverse();
|
|
77
|
-
for (const ref of references) {
|
|
78
|
-
dataType = ref.getDataType(name, true);
|
|
79
|
-
if (dataType)
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
if (dataType) {
|
|
85
|
-
this._typeCache.set(arg0, dataType);
|
|
86
|
-
return dataType;
|
|
87
|
-
}
|
|
88
|
-
if (!silent)
|
|
89
|
-
throw new index_js_1.NotFoundError(`Data type "${arg0}" does not exists`);
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
if (typeof arg0 === 'function') {
|
|
93
|
-
const types = Array.from(this.types.values()).reverse();
|
|
94
|
-
for (const dt of types) {
|
|
95
|
-
if (dt instanceof complex_type_js_1.ComplexType && dt.isTypeOf(arg0)) {
|
|
96
|
-
dataType = dt;
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
// if not found, search in references (from last to first)
|
|
101
|
-
if (!dataType) {
|
|
102
|
-
const references = Array.from(this.references.values()).reverse();
|
|
103
|
-
for (const ref of references) {
|
|
104
|
-
dataType = ref.getDataType(arg0, true);
|
|
105
|
-
if (dataType)
|
|
106
|
-
break;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
if (dataType)
|
|
110
|
-
this._typesCacheByCtor.set(arg0, dataType);
|
|
111
|
-
if (dataType)
|
|
112
|
-
return dataType;
|
|
113
|
-
if (!silent)
|
|
114
|
-
throw new index_js_1.NotFoundError(`No data type mapping found for class "${name}"`);
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
/* istanbul ignore next */
|
|
118
|
-
if (!silent)
|
|
119
|
-
throw new TypeError('Invalid argument');
|
|
120
|
-
}
|
|
121
|
-
getComplexType(nameOrCtor, silent) {
|
|
122
|
-
if (nameOrCtor === Object)
|
|
123
|
-
nameOrCtor = 'object';
|
|
124
|
-
const t = this.getDataType(nameOrCtor);
|
|
125
|
-
if (!t && silent)
|
|
126
|
-
return;
|
|
127
|
-
if (t && t.kind === index_js_3.OpraSchema.ComplexType.Kind)
|
|
128
|
-
return t;
|
|
129
|
-
throw new index_js_1.NotAcceptableError(`Data type "${t.name}" is not a ComplexType`);
|
|
130
|
-
}
|
|
131
|
-
getSimpleType(nameOrCtor, silent) {
|
|
132
|
-
const t = this.getDataType(nameOrCtor);
|
|
133
|
-
if (!t && silent)
|
|
134
|
-
return;
|
|
135
|
-
if (t && t.kind === index_js_3.OpraSchema.SimpleType.Kind)
|
|
136
|
-
return t;
|
|
137
|
-
throw new index_js_1.NotAcceptableError(`Data type "${t.name || t}" is not a SimpleType`);
|
|
8
|
+
super(...arguments);
|
|
9
|
+
this.root = new container_js_1.Container(this, { name: '' });
|
|
138
10
|
}
|
|
139
11
|
getResource(path, silent) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
// If given string has namespace pattern (ns:type_name)
|
|
145
|
-
if (m[2]) {
|
|
146
|
-
const ref = this.references.get(m[1]);
|
|
147
|
-
if (!ref) {
|
|
148
|
-
if (silent)
|
|
149
|
-
return;
|
|
150
|
-
throw new index_js_1.NotFoundError(`Reference "${m[1]}" not found`);
|
|
151
|
-
}
|
|
152
|
-
return ref.getResource(m[2]);
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
const name = m[1];
|
|
156
|
-
// Get instance from own types
|
|
157
|
-
resource = this.resources.get(name);
|
|
158
|
-
if (resource)
|
|
159
|
-
return resource;
|
|
160
|
-
// if not found, search in references (from last to first)
|
|
161
|
-
const references = Array.from(this.references.values()).reverse();
|
|
162
|
-
for (const ref of references) {
|
|
163
|
-
resource = silent ? ref.getResource(name, silent) : ref.getResource(name);
|
|
164
|
-
if (resource)
|
|
165
|
-
return resource;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
if (silent)
|
|
169
|
-
return;
|
|
170
|
-
throw new index_js_1.ResourceNotFoundError(path);
|
|
12
|
+
return this.root.getResource(path, silent);
|
|
13
|
+
}
|
|
14
|
+
getContainer(path, silent) {
|
|
15
|
+
return this.root.getContainer(path, silent);
|
|
171
16
|
}
|
|
172
17
|
getCollection(path, silent) {
|
|
173
|
-
|
|
174
|
-
if (!t && silent)
|
|
175
|
-
return;
|
|
176
|
-
if (t && t.kind === index_js_3.OpraSchema.Collection.Kind)
|
|
177
|
-
return t;
|
|
178
|
-
throw new index_js_1.NotAcceptableError(`Resource type "${t.name}" is not a Collection`);
|
|
18
|
+
return this.root.getCollection(path, silent);
|
|
179
19
|
}
|
|
180
20
|
getSingleton(path, silent) {
|
|
181
|
-
|
|
182
|
-
if (!t && silent)
|
|
183
|
-
return;
|
|
184
|
-
if (t && t.kind === index_js_3.OpraSchema.Singleton.Kind)
|
|
185
|
-
return t;
|
|
186
|
-
throw new index_js_1.NotAcceptableError(`Resource type "${t.name}" is not a Singleton`);
|
|
21
|
+
return this.root.getSingleton(path, silent);
|
|
187
22
|
}
|
|
188
23
|
getStorage(path, silent) {
|
|
189
|
-
|
|
190
|
-
if (!t && silent)
|
|
191
|
-
return;
|
|
192
|
-
if (t && t.kind === index_js_3.OpraSchema.Storage.Kind)
|
|
193
|
-
return t;
|
|
194
|
-
throw new index_js_1.NotAcceptableError(`Resource type "${t.name}" is not a Storage`);
|
|
24
|
+
return this.root.getStorage(path, silent);
|
|
195
25
|
}
|
|
196
26
|
/**
|
|
197
27
|
* Export as Opra schema definition object
|
|
198
28
|
*/
|
|
199
29
|
exportSchema(options) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
url: this.url,
|
|
205
|
-
info: (0, index_js_2.cloneObject)(this.info)
|
|
206
|
-
};
|
|
207
|
-
if (this.references.size) {
|
|
208
|
-
const references = {};
|
|
209
|
-
let i = 0;
|
|
210
|
-
for (const [ns, r] of this.references.entries()) {
|
|
211
|
-
if (ns.toLowerCase() === 'opra')
|
|
212
|
-
continue;
|
|
213
|
-
references[ns] = r.url ? r.url : r.exportSchema();
|
|
214
|
-
i++;
|
|
215
|
-
}
|
|
216
|
-
if (i)
|
|
217
|
-
schema.references = references;
|
|
218
|
-
}
|
|
219
|
-
if (this.resources.size) {
|
|
220
|
-
const resources = schema.resources = {};
|
|
221
|
-
for (const [name, r] of this.resources.entries()) {
|
|
222
|
-
resources[name] = r.exportSchema();
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
if (this.types.size) {
|
|
226
|
-
const types = schema.types = {};
|
|
227
|
-
for (const [name, r] of this.types.entries()) {
|
|
228
|
-
types[name] = r.exportSchema();
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
this._metadataCache = schema;
|
|
232
|
-
return this._metadataCache;
|
|
233
|
-
}
|
|
234
|
-
invalidate() {
|
|
235
|
-
this._metadataCache = undefined;
|
|
236
|
-
this._typeCache.clear();
|
|
237
|
-
this._typesCacheByCtor.clear();
|
|
30
|
+
const schema = super.exportSchema(options);
|
|
31
|
+
schema.root = this.root.exportSchema(options);
|
|
32
|
+
delete schema.root.kind;
|
|
33
|
+
return schema;
|
|
238
34
|
}
|
|
239
35
|
}
|
|
240
36
|
exports.ApiDocument = ApiDocument;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ComplexTypeClass = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const vg = tslib_1.__importStar(require("valgen"));
|
|
4
6
|
const index_js_1 = require("../../helpers/index.js");
|
|
5
7
|
const index_js_2 = require("../../i18n/index.js");
|
|
6
8
|
const index_js_3 = require("../../schema/index.js");
|
|
@@ -14,31 +16,38 @@ class ComplexTypeClass extends data_type_js_1.DataType {
|
|
|
14
16
|
super(document, init);
|
|
15
17
|
this.kind = index_js_3.OpraSchema.ComplexType.Kind;
|
|
16
18
|
const own = this.own = {};
|
|
17
|
-
own.ctor = init
|
|
18
|
-
|
|
19
|
+
own.ctor = init.ctor;
|
|
20
|
+
if (init.base) {
|
|
21
|
+
if (!(init.base.kind === 'ComplexType' || init.base.kind === 'MappedType' || init.base.kind === 'UnionType'))
|
|
22
|
+
throw new TypeError('"base" argument must be one of ComplexType or MappedType or UnionType');
|
|
23
|
+
own.ctor = own.ctor || init.base.ctor;
|
|
24
|
+
}
|
|
19
25
|
own.additionalFields = init?.additionalFields;
|
|
20
26
|
own.fields = new index_js_1.ResponsiveMap();
|
|
21
27
|
this.kind = index_js_3.OpraSchema.ComplexType.Kind;
|
|
22
28
|
this.base = init?.base;
|
|
23
29
|
this.ctor = own.ctor || Object;
|
|
24
|
-
this.abstract =
|
|
30
|
+
this.abstract = init.abstract;
|
|
25
31
|
this.additionalFields = own.additionalFields;
|
|
32
|
+
if (this.base?.additionalFields === true && this.additionalFields !== true)
|
|
33
|
+
this.additionalFields = true;
|
|
34
|
+
else if (this.base?.additionalFields === 'error' && !this.additionalFields)
|
|
35
|
+
this.additionalFields = 'error';
|
|
26
36
|
this.fields = new index_js_1.ResponsiveMap();
|
|
27
37
|
if (this.base) {
|
|
28
|
-
if (this.additionalFields == null)
|
|
29
|
-
this.additionalFields = this.base.additionalFields;
|
|
30
38
|
if (this.base.fields)
|
|
31
39
|
for (const [k, el] of this.base.fields.entries()) {
|
|
32
|
-
const
|
|
33
|
-
this.fields.set(k,
|
|
40
|
+
const field = new field_js_1.ApiField(this, el);
|
|
41
|
+
this.fields.set(k, field);
|
|
34
42
|
}
|
|
35
43
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
if (init.fields) {
|
|
45
|
+
for (const [k, el] of Object.entries(init.fields)) {
|
|
46
|
+
const field = new field_js_1.ApiField(this, el);
|
|
47
|
+
this.own.fields.set(field.name, field);
|
|
48
|
+
this.fields.set(k, field);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
42
51
|
}
|
|
43
52
|
findField(nameOrPath) {
|
|
44
53
|
let field;
|
|
@@ -119,24 +128,24 @@ class ComplexTypeClass extends data_type_js_1.DataType {
|
|
|
119
128
|
}).flat();
|
|
120
129
|
return array.length ? array : undefined;
|
|
121
130
|
}
|
|
122
|
-
exportSchema() {
|
|
123
|
-
const out = super.exportSchema();
|
|
131
|
+
exportSchema(options) {
|
|
132
|
+
const out = super.exportSchema(options);
|
|
124
133
|
Object.assign(out, (0, index_js_1.omitUndefined)({
|
|
125
134
|
base: this.base ?
|
|
126
|
-
(this.base.name ? this.base.name : this.base.exportSchema()) : undefined,
|
|
127
|
-
abstract: this.
|
|
135
|
+
(this.base.name ? this.base.name : this.base.exportSchema(options)) : undefined,
|
|
136
|
+
abstract: this.abstract,
|
|
128
137
|
additionalFields: this.own.additionalFields
|
|
129
138
|
}));
|
|
130
139
|
if (this.own.fields.size) {
|
|
131
140
|
const fields = out.fields = {};
|
|
132
141
|
for (const field of this.own.fields.values()) {
|
|
133
|
-
fields[field.name] = field.exportSchema();
|
|
142
|
+
fields[field.name] = field.exportSchema(options);
|
|
134
143
|
}
|
|
135
144
|
}
|
|
136
145
|
return (0, index_js_1.omitUndefined)(out);
|
|
137
146
|
}
|
|
138
147
|
isTypeOf(t) {
|
|
139
|
-
return t === this.own
|
|
148
|
+
return t === this.own?.ctor;
|
|
140
149
|
}
|
|
141
150
|
extendsFrom(t) {
|
|
142
151
|
const base = t instanceof data_type_js_1.DataType ? t : this.document.getDataType(t);
|
|
@@ -147,5 +156,18 @@ class ComplexTypeClass extends data_type_js_1.DataType {
|
|
|
147
156
|
}
|
|
148
157
|
return false;
|
|
149
158
|
}
|
|
159
|
+
generateCodec(codec, options) {
|
|
160
|
+
const schema = {};
|
|
161
|
+
for (const f of this.fields.values()) {
|
|
162
|
+
// todo omit, pick
|
|
163
|
+
schema[f.name] = f.generateCodec(codec, options);
|
|
164
|
+
}
|
|
165
|
+
return vg.isObject(schema, {
|
|
166
|
+
ctor: this.ctor,
|
|
167
|
+
additionalFields: this.additionalFields ?? false,
|
|
168
|
+
name: this.name,
|
|
169
|
+
caseInSensitive: !options?.caseSensitive
|
|
170
|
+
});
|
|
171
|
+
}
|
|
150
172
|
}
|
|
151
173
|
exports.ComplexTypeClass = ComplexTypeClass;
|
|
@@ -5,7 +5,7 @@ const tslib_1 = require("tslib");
|
|
|
5
5
|
require("reflect-metadata");
|
|
6
6
|
const putil_merge_1 = tslib_1.__importDefault(require("putil-merge"));
|
|
7
7
|
const constants_js_1 = require("../constants.js");
|
|
8
|
-
const complex_type_decorator_js_1 = require("
|
|
8
|
+
const complex_type_decorator_js_1 = require("./complex-type.decorator.js");
|
|
9
9
|
const complex_type_class_js_1 = require("./complex-type-class.js");
|
|
10
10
|
/**
|
|
11
11
|
* @class ComplexType
|
|
@@ -15,7 +15,8 @@ class DataType {
|
|
|
15
15
|
this.description = init?.description;
|
|
16
16
|
this.isAnonymous = !this.name;
|
|
17
17
|
}
|
|
18
|
-
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
19
|
+
exportSchema(options) {
|
|
19
20
|
return (0, index_js_1.omitUndefined)({
|
|
20
21
|
kind: this.kind,
|
|
21
22
|
description: this.description
|
|
@@ -10,23 +10,26 @@ class EnumTypeClass extends data_type_js_1.DataType {
|
|
|
10
10
|
constructor(document, init) {
|
|
11
11
|
super(document, init);
|
|
12
12
|
this.kind = index_js_2.OpraSchema.EnumType.Kind;
|
|
13
|
+
this.enumObject = init.enumObject;
|
|
13
14
|
this.base = init.base;
|
|
14
|
-
this.ownValues = init.values;
|
|
15
|
-
this.ownMeanings = init.meanings || {};
|
|
15
|
+
this.ownValues = { ...init.values };
|
|
16
16
|
this.values = { ...this.base?.values, ...this.ownValues };
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
this.encode = vg.isEnum(Object.values(this.values));
|
|
17
|
+
this.decode = vg.isEnum(Object.keys(this.values));
|
|
18
|
+
this.encode = vg.isEnum(Object.keys(this.values));
|
|
20
19
|
}
|
|
21
20
|
exportSchema() {
|
|
22
|
-
const out =
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
meanings: this.ownMeanings
|
|
28
|
-
}));
|
|
21
|
+
const out = super.exportSchema();
|
|
22
|
+
out.values = {};
|
|
23
|
+
Object.entries(this.values).forEach(([k, i]) => {
|
|
24
|
+
out.values[k] = (0, index_js_1.omitUndefined)({ key: i.key, description: i.description });
|
|
25
|
+
});
|
|
29
26
|
return out;
|
|
30
27
|
}
|
|
28
|
+
generateCodec(codec) {
|
|
29
|
+
if (codec === 'encode')
|
|
30
|
+
return this.encode;
|
|
31
|
+
else
|
|
32
|
+
return this.decode;
|
|
33
|
+
}
|
|
31
34
|
}
|
|
32
35
|
exports.EnumTypeClass = EnumTypeClass;
|
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.EnumType = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
require("reflect-metadata");
|
|
6
|
-
const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
|
|
7
6
|
const putil_merge_1 = tslib_1.__importDefault(require("putil-merge"));
|
|
8
|
-
const index_js_1 = require("../../
|
|
7
|
+
const index_js_1 = require("../../helpers/index.js");
|
|
8
|
+
const index_js_2 = require("../../schema/index.js");
|
|
9
9
|
const constants_js_1 = require("../constants.js");
|
|
10
10
|
const enum_type_class_js_1 = require("./enum-type-class.js");
|
|
11
11
|
/**
|
|
@@ -15,20 +15,34 @@ exports.EnumType = function (...args) {
|
|
|
15
15
|
// Injector
|
|
16
16
|
if (!this) {
|
|
17
17
|
const [enumSource, options] = args;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
let values = {};
|
|
19
|
+
if (Array.isArray(enumSource)) {
|
|
20
|
+
values = {};
|
|
21
|
+
enumSource.forEach(k => {
|
|
22
|
+
const description = options?.meanings?.[k];
|
|
23
|
+
values[k] = (0, index_js_1.omitUndefined)({ description });
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
Object.keys(enumSource).forEach(k => {
|
|
28
|
+
const description = options?.meanings?.[k];
|
|
29
|
+
values[enumSource[k]] = (0, index_js_1.omitUndefined)({ key: k, description });
|
|
30
|
+
});
|
|
31
|
+
}
|
|
24
32
|
const metadata = {
|
|
25
|
-
kind:
|
|
26
|
-
values
|
|
33
|
+
kind: index_js_2.OpraSchema.EnumType.Kind,
|
|
34
|
+
values,
|
|
35
|
+
base: options?.base,
|
|
36
|
+
name: options?.name,
|
|
37
|
+
description: options?.description
|
|
27
38
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
39
|
+
Object.defineProperty(enumSource, constants_js_1.DATATYPE_METADATA, {
|
|
40
|
+
value: metadata,
|
|
41
|
+
enumerable: false,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true
|
|
44
|
+
});
|
|
45
|
+
return metadata;
|
|
32
46
|
}
|
|
33
47
|
// Constructor
|
|
34
48
|
const [document, init] = args;
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FieldClass = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const vg = tslib_1.__importStar(require("valgen"));
|
|
4
6
|
const index_js_1 = require("../../helpers/index.js");
|
|
5
7
|
class FieldClass {
|
|
6
8
|
constructor(owner, init) {
|
|
7
9
|
this.owner = owner;
|
|
8
|
-
this.name = init.name;
|
|
9
10
|
this.origin = init.origin || owner;
|
|
10
11
|
this.type = init.type;
|
|
12
|
+
this.name = init.name;
|
|
11
13
|
this.description = init.description;
|
|
12
14
|
this.isArray = init.isArray;
|
|
13
15
|
this.default = init.default;
|
|
@@ -17,9 +19,9 @@ class FieldClass {
|
|
|
17
19
|
this.deprecated = init.deprecated;
|
|
18
20
|
this.examples = init.examples;
|
|
19
21
|
}
|
|
20
|
-
exportSchema() {
|
|
22
|
+
exportSchema(options) {
|
|
21
23
|
return (0, index_js_1.omitUndefined)({
|
|
22
|
-
type: this.type.name ? this.type.name : this.type.exportSchema(),
|
|
24
|
+
type: this.type.name ? this.type.name : this.type.exportSchema(options),
|
|
23
25
|
description: this.description,
|
|
24
26
|
isArray: this.isArray,
|
|
25
27
|
default: this.default,
|
|
@@ -30,5 +32,21 @@ class FieldClass {
|
|
|
30
32
|
examples: this.examples
|
|
31
33
|
});
|
|
32
34
|
}
|
|
35
|
+
getDecoder() {
|
|
36
|
+
if (!this._decoder)
|
|
37
|
+
this._decoder = this.generateCodec('decode');
|
|
38
|
+
return this._decoder;
|
|
39
|
+
}
|
|
40
|
+
getEncoder() {
|
|
41
|
+
if (!this._encoder)
|
|
42
|
+
this._encoder = this.generateCodec('encode');
|
|
43
|
+
return this._encoder;
|
|
44
|
+
}
|
|
45
|
+
generateCodec(codec, options) {
|
|
46
|
+
let fn = this.type.generateCodec(codec, options);
|
|
47
|
+
if (this.isArray)
|
|
48
|
+
fn = vg.isArray(fn);
|
|
49
|
+
return !options?.partial && this.required ? vg.required(fn) : vg.optional(fn);
|
|
50
|
+
}
|
|
33
51
|
}
|
|
34
52
|
exports.FieldClass = FieldClass;
|
|
@@ -4,7 +4,6 @@ exports.FieldDecorator = void 0;
|
|
|
4
4
|
const index_js_1 = require("../../helpers/index.js");
|
|
5
5
|
const index_js_2 = require("../../schema/index.js");
|
|
6
6
|
const constants_js_1 = require("../constants.js");
|
|
7
|
-
const enum_type_js_1 = require("./enum-type.js");
|
|
8
7
|
function FieldDecorator(options) {
|
|
9
8
|
return function (target, propertyKey) {
|
|
10
9
|
if (typeof propertyKey !== 'string')
|
|
@@ -15,30 +14,12 @@ function FieldDecorator(options) {
|
|
|
15
14
|
const designType = Reflect.getMetadata('design:type', target, propertyKey);
|
|
16
15
|
const elemMeta = metadata.fields[propertyKey] = {
|
|
17
16
|
...options,
|
|
18
|
-
enum: undefined,
|
|
19
17
|
designType
|
|
20
18
|
};
|
|
21
19
|
if (designType === Array) {
|
|
22
20
|
elemMeta.isArray = true;
|
|
23
21
|
delete elemMeta.designType;
|
|
24
22
|
}
|
|
25
|
-
if (options?.enum) {
|
|
26
|
-
elemMeta.type = undefined;
|
|
27
|
-
if (Array.isArray(options.enum)) {
|
|
28
|
-
const enumObj = options.enum.reduce((o, v) => {
|
|
29
|
-
o[v] = v;
|
|
30
|
-
return o;
|
|
31
|
-
}, {});
|
|
32
|
-
(0, enum_type_js_1.EnumType)(enumObj);
|
|
33
|
-
elemMeta.enum = enumObj;
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
const m = Reflect.getOwnMetadata(constants_js_1.DATATYPE_METADATA, options?.enum);
|
|
37
|
-
if (!index_js_2.OpraSchema.isEnumType(m))
|
|
38
|
-
throw new TypeError(`Invalid "enum" value. Did you forget to set metadata using EnumType() method?`);
|
|
39
|
-
elemMeta.enum = options.enum;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
23
|
Reflect.defineMetadata(constants_js_1.DATATYPE_METADATA, (0, index_js_1.omitUndefined)(metadata), target.constructor);
|
|
43
24
|
};
|
|
44
25
|
}
|
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getIsInheritedPredicateFn = exports.MappedTypeClass = void 0;
|
|
4
4
|
const index_js_1 = require("../../helpers/index.js");
|
|
5
5
|
const index_js_2 = require("../../schema/index.js");
|
|
6
|
-
const
|
|
7
|
-
class MappedTypeClass extends
|
|
6
|
+
const complex_type_class_js_1 = require("./complex-type-class.js");
|
|
7
|
+
class MappedTypeClass extends complex_type_class_js_1.ComplexTypeClass {
|
|
8
8
|
constructor(document, init) {
|
|
9
9
|
super(document, init);
|
|
10
10
|
this.kind = index_js_2.OpraSchema.MappedType.Kind;
|
|
@@ -12,21 +12,19 @@ class MappedTypeClass extends data_type_js_1.DataType {
|
|
|
12
12
|
own.pick = init.pick;
|
|
13
13
|
own.omit = init.omit;
|
|
14
14
|
this.kind = index_js_2.OpraSchema.MappedType.Kind;
|
|
15
|
-
this.type = init.type;
|
|
16
15
|
this.pick = own.pick;
|
|
17
16
|
this.omit = own.omit;
|
|
18
|
-
this.fields = new index_js_1.ResponsiveMap();
|
|
19
|
-
this.additionalFields = this.type.additionalFields;
|
|
20
17
|
const isInheritedPredicate = getIsInheritedPredicateFn(init.pick, init.omit);
|
|
21
|
-
for (const
|
|
22
|
-
if (isInheritedPredicate(
|
|
23
|
-
this.fields.
|
|
18
|
+
for (const fieldName of this.fields.keys()) {
|
|
19
|
+
if (!isInheritedPredicate(fieldName)) {
|
|
20
|
+
this.fields.delete(fieldName);
|
|
21
|
+
}
|
|
24
22
|
}
|
|
25
23
|
}
|
|
24
|
+
// @ts-ignore
|
|
26
25
|
exportSchema() {
|
|
27
|
-
const out =
|
|
26
|
+
const out = super.exportSchema();
|
|
28
27
|
Object.assign(out, (0, index_js_1.omitUndefined)({
|
|
29
|
-
type: this.type.name ? this.type.name : this.type.exportSchema(),
|
|
30
28
|
pick: this.own.pick,
|
|
31
29
|
omit: this.own.omit,
|
|
32
30
|
}));
|
|
@@ -49,7 +49,7 @@ exports.MappedType = function (...args) {
|
|
|
49
49
|
throw new TypeError(`Class "${mappedSource}" is not a ${index_js_2.OpraSchema.ComplexType.Kind}`);
|
|
50
50
|
const metadata = {
|
|
51
51
|
kind: 'MappedType',
|
|
52
|
-
|
|
52
|
+
base: mappedSource
|
|
53
53
|
};
|
|
54
54
|
if (options.pick)
|
|
55
55
|
metadata.pick = options.pick;
|
|
@@ -3,7 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SimpleTypeClass = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const vg = tslib_1.__importStar(require("valgen"));
|
|
6
|
-
const index_js_1 = require("../../
|
|
6
|
+
const index_js_1 = require("../../helpers/index.js");
|
|
7
|
+
const index_js_2 = require("../../schema/index.js");
|
|
7
8
|
const data_type_js_1 = require("./data-type.js");
|
|
8
9
|
/**
|
|
9
10
|
* @class SimpleType
|
|
@@ -11,19 +12,27 @@ const data_type_js_1 = require("./data-type.js");
|
|
|
11
12
|
class SimpleTypeClass extends data_type_js_1.DataType {
|
|
12
13
|
constructor(document, init) {
|
|
13
14
|
super(document, init);
|
|
14
|
-
this.kind =
|
|
15
|
+
this.kind = index_js_2.OpraSchema.SimpleType.Kind;
|
|
15
16
|
this.base = init.base;
|
|
16
17
|
this.decode = init.decoder || init.base?.decode || vg.isAny();
|
|
17
18
|
this.encode = init.encoder || init.base?.encode || vg.isAny();
|
|
18
19
|
}
|
|
19
|
-
exportSchema() {
|
|
20
|
+
exportSchema(options) {
|
|
20
21
|
// noinspection UnnecessaryLocalVariableJS
|
|
21
|
-
const out = super.exportSchema();
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
const out = super.exportSchema(options);
|
|
23
|
+
Object.assign(out, (0, index_js_1.omitUndefined)({
|
|
24
|
+
base: this.base ?
|
|
25
|
+
(this.base.name ? this.base.name : this.base.exportSchema(options)) : undefined,
|
|
26
|
+
decode: !options?.webSafe ? this.decode : undefined,
|
|
27
|
+
encode: !options?.webSafe ? this.encode : undefined
|
|
28
|
+
}));
|
|
26
29
|
return out;
|
|
27
30
|
}
|
|
31
|
+
generateCodec(codec) {
|
|
32
|
+
if (codec === 'encode')
|
|
33
|
+
return this.encode;
|
|
34
|
+
else
|
|
35
|
+
return this.decode;
|
|
36
|
+
}
|
|
28
37
|
}
|
|
29
38
|
exports.SimpleTypeClass = SimpleTypeClass;
|