@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.
Files changed (197) hide show
  1. package/browser.js +1843 -1355
  2. package/cjs/document/api-document.js +16 -220
  3. package/cjs/document/data-type/complex-type-class.js +41 -19
  4. package/cjs/document/data-type/complex-type.js +1 -1
  5. package/cjs/document/data-type/data-type.js +2 -1
  6. package/cjs/document/data-type/enum-type-class.js +15 -12
  7. package/cjs/document/data-type/enum-type.js +28 -14
  8. package/cjs/document/data-type/field-class.js +21 -3
  9. package/cjs/document/data-type/field-decorator.js +0 -19
  10. package/cjs/document/data-type/mapped-type-class.js +8 -10
  11. package/cjs/document/data-type/mapped-type.js +1 -1
  12. package/cjs/document/data-type/simple-type-class.js +17 -8
  13. package/cjs/document/data-type/simple-type.js +1 -1
  14. package/cjs/document/data-type/union-type-class.js +15 -13
  15. package/cjs/document/document-base.js +24 -0
  16. package/cjs/document/factory/api-document-factory.js +231 -0
  17. package/cjs/document/factory/type-document-factory.js +324 -0
  18. package/cjs/document/index.js +7 -1
  19. package/cjs/document/resource/action-decorator.js +27 -0
  20. package/cjs/document/resource/collection-class.js +95 -44
  21. package/cjs/document/resource/collection-decorator.js +182 -0
  22. package/cjs/document/resource/collection.js +4 -4
  23. package/cjs/document/resource/container-class.js +82 -0
  24. package/cjs/document/resource/container-decorator.js +24 -0
  25. package/cjs/document/resource/container.js +29 -0
  26. package/cjs/document/resource/crud-resource.js +31 -0
  27. package/cjs/document/resource/endpoint.js +28 -21
  28. package/cjs/document/resource/operation-decorator.js +27 -0
  29. package/cjs/document/resource/parameter.js +51 -0
  30. package/cjs/document/{decorators/resource.decorator.js → resource/resource-decorator.js} +9 -14
  31. package/cjs/document/resource/resource.js +20 -5
  32. package/cjs/document/resource/singleton-class.js +60 -40
  33. package/cjs/document/resource/singleton-decorator.js +57 -0
  34. package/cjs/document/resource/singleton.js +4 -4
  35. package/cjs/document/resource/storage-class.js +11 -17
  36. package/cjs/document/resource/storage-decorator.js +66 -0
  37. package/cjs/document/resource/storage.js +3 -3
  38. package/cjs/document/type-document.js +195 -0
  39. package/cjs/exception/http-errors/internal-server.error.js +1 -0
  40. package/cjs/exception/opra-exception.js +18 -12
  41. package/cjs/exception/wrap-exception.js +2 -1
  42. package/cjs/helpers/responsive-map.js +3 -3
  43. package/cjs/http/opra-url-path.js +5 -1
  44. package/cjs/schema/document/document-base.interface.js +2 -0
  45. package/cjs/schema/document/type-document.interface.js +2 -0
  46. package/cjs/schema/opra-schema.ns.js +3 -1
  47. package/cjs/schema/type-guards.js +3 -3
  48. package/esm/document/api-document.js +16 -220
  49. package/esm/document/data-type/complex-type-class.js +40 -19
  50. package/esm/document/data-type/complex-type.js +1 -1
  51. package/esm/document/data-type/data-type.js +2 -1
  52. package/esm/document/data-type/enum-type-class.js +15 -12
  53. package/esm/document/data-type/enum-type.js +26 -12
  54. package/esm/document/data-type/field-class.js +20 -3
  55. package/esm/document/data-type/field-decorator.js +0 -19
  56. package/esm/document/data-type/mapped-type-class.js +9 -11
  57. package/esm/document/data-type/mapped-type.js +1 -1
  58. package/esm/document/data-type/simple-type-class.js +15 -6
  59. package/esm/document/data-type/simple-type.js +1 -1
  60. package/esm/document/data-type/union-type-class.js +11 -9
  61. package/esm/document/document-base.js +20 -0
  62. package/esm/document/factory/api-document-factory.js +227 -0
  63. package/esm/document/factory/type-document-factory.js +320 -0
  64. package/esm/document/index.js +7 -1
  65. package/esm/document/resource/action-decorator.js +23 -0
  66. package/esm/document/resource/collection-class.js +95 -44
  67. package/esm/document/resource/collection-decorator.js +178 -0
  68. package/esm/document/resource/collection.js +4 -4
  69. package/esm/document/resource/container-class.js +78 -0
  70. package/esm/document/resource/container-decorator.js +20 -0
  71. package/esm/document/resource/container.js +25 -0
  72. package/esm/document/resource/crud-resource.js +27 -0
  73. package/esm/document/resource/endpoint.js +27 -21
  74. package/esm/document/resource/operation-decorator.js +23 -0
  75. package/esm/document/resource/parameter.js +46 -0
  76. package/esm/document/resource/resource-decorator.js +28 -0
  77. package/esm/document/resource/resource.js +20 -5
  78. package/esm/document/resource/singleton-class.js +58 -38
  79. package/esm/document/resource/singleton-decorator.js +53 -0
  80. package/esm/document/resource/singleton.js +4 -4
  81. package/esm/document/resource/storage-class.js +9 -15
  82. package/esm/document/resource/storage-decorator.js +62 -0
  83. package/esm/document/resource/storage.js +3 -3
  84. package/esm/document/type-document.js +191 -0
  85. package/esm/exception/http-errors/internal-server.error.js +1 -0
  86. package/esm/exception/opra-exception.js +18 -12
  87. package/esm/exception/wrap-exception.js +2 -1
  88. package/esm/helpers/responsive-map.js +3 -3
  89. package/esm/http/opra-url-path.js +4 -1
  90. package/esm/schema/document/document-base.interface.js +1 -0
  91. package/esm/schema/document/type-document.interface.js +1 -0
  92. package/esm/schema/opra-schema.ns.js +3 -1
  93. package/esm/schema/type-guards.js +1 -1
  94. package/package.json +2 -2
  95. package/types/document/api-document.d.ts +32 -73
  96. package/types/document/data-type/complex-type-class.d.ts +5 -5
  97. package/types/document/data-type/complex-type.d.ts +8 -3
  98. package/types/document/{decorators → data-type}/complex-type.decorator.d.ts +1 -1
  99. package/types/document/data-type/data-type.d.ts +11 -1
  100. package/types/document/data-type/enum-type-class.d.ts +6 -6
  101. package/types/document/data-type/enum-type.d.ts +12 -6
  102. package/types/document/data-type/field-class.d.ts +9 -1
  103. package/types/document/data-type/field-decorator.d.ts +1 -1
  104. package/types/document/data-type/field.d.ts +4 -3
  105. package/types/document/data-type/mapped-type-class.d.ts +5 -9
  106. package/types/document/data-type/mapped-type.d.ts +6 -7
  107. package/types/document/data-type/simple-type-class.d.ts +6 -3
  108. package/types/document/{decorators → data-type}/simple-type.decorator.d.ts +1 -1
  109. package/types/document/data-type/union-type-class.d.ts +2 -8
  110. package/types/document/data-type/union-type.d.ts +7 -6
  111. package/types/document/document-base.d.ts +10 -0
  112. package/types/document/factory/api-document-factory.d.ts +50 -0
  113. package/types/document/factory/type-document-factory.d.ts +56 -0
  114. package/types/document/index.d.ts +7 -1
  115. package/types/document/resource/action-decorator.d.ts +6 -0
  116. package/types/document/resource/collection-class.d.ts +17 -12
  117. package/types/document/resource/collection-decorator.d.ts +192 -0
  118. package/types/document/resource/collection.d.ts +11 -19
  119. package/types/document/resource/container-class.d.ts +66 -0
  120. package/types/document/resource/container-decorator.d.ts +37 -0
  121. package/types/document/resource/container.d.ts +25 -0
  122. package/types/document/resource/crud-resource.d.ts +16 -0
  123. package/types/document/resource/endpoint.d.ts +26 -19
  124. package/types/document/resource/operation-decorator.d.ts +6 -0
  125. package/types/document/resource/parameter.d.ts +35 -0
  126. package/types/document/resource/resource-decorator.d.ts +32 -0
  127. package/types/document/resource/resource.d.ts +15 -13
  128. package/types/document/resource/singleton-class.d.ts +15 -14
  129. package/types/document/resource/singleton-decorator.d.ts +125 -0
  130. package/types/document/resource/singleton.d.ts +11 -16
  131. package/types/document/resource/storage-class.d.ts +13 -6
  132. package/types/document/resource/storage-decorator.d.ts +98 -0
  133. package/types/document/resource/storage.d.ts +9 -12
  134. package/types/document/type-document.d.ts +68 -0
  135. package/types/exception/opra-exception.d.ts +1 -1
  136. package/types/http/opra-url-path.d.ts +2 -1
  137. package/types/schema/data-type/complex-type.interface.d.ts +1 -1
  138. package/types/schema/data-type/data-type.interface.d.ts +3 -3
  139. package/types/schema/data-type/enum-type.interface.d.ts +6 -7
  140. package/types/schema/data-type/mapped-type.interface.d.ts +4 -3
  141. package/types/schema/data-type/simple-type.interface.d.ts +3 -3
  142. package/types/schema/data-type/union-type.interface.d.ts +6 -4
  143. package/types/schema/document/api-document.interface.d.ts +11 -0
  144. package/types/schema/document/document-base.interface.d.ts +24 -0
  145. package/types/schema/document/type-document.interface.d.ts +6 -0
  146. package/types/schema/opra-schema.ns.d.ts +3 -1
  147. package/types/schema/resource/collection.interface.d.ts +31 -31
  148. package/types/schema/resource/container.interface.d.ts +4 -3
  149. package/types/schema/resource/resource.interface.d.ts +1 -1
  150. package/types/schema/resource/singleton.interface.d.ts +15 -10
  151. package/types/schema/resource/storage.interface.d.ts +49 -45
  152. package/types/schema/type-guards.d.ts +3 -2
  153. package/cjs/document/decorators/collection-decorator.js +0 -38
  154. package/cjs/document/decorators/singleton.decorator.js +0 -36
  155. package/cjs/document/decorators/storage.decorator.js +0 -35
  156. package/cjs/document/factory/add-references.js +0 -20
  157. package/cjs/document/factory/create-document.js +0 -83
  158. package/cjs/document/factory/factory.js +0 -66
  159. package/cjs/document/factory/import-resource-class.js +0 -54
  160. package/cjs/document/factory/import-type-class.js +0 -146
  161. package/cjs/document/factory/index.js +0 -4
  162. package/cjs/document/factory/process-resources.js +0 -70
  163. package/cjs/document/factory/process-types.js +0 -191
  164. package/cjs/document/utils/generate-codec.js +0 -39
  165. package/esm/document/decorators/collection-decorator.js +0 -34
  166. package/esm/document/decorators/resource.decorator.js +0 -33
  167. package/esm/document/decorators/singleton.decorator.js +0 -32
  168. package/esm/document/decorators/storage.decorator.js +0 -31
  169. package/esm/document/factory/add-references.js +0 -16
  170. package/esm/document/factory/create-document.js +0 -77
  171. package/esm/document/factory/factory.js +0 -62
  172. package/esm/document/factory/import-resource-class.js +0 -48
  173. package/esm/document/factory/import-type-class.js +0 -136
  174. package/esm/document/factory/index.js +0 -1
  175. package/esm/document/factory/process-resources.js +0 -63
  176. package/esm/document/factory/process-types.js +0 -185
  177. package/esm/document/utils/generate-codec.js +0 -33
  178. package/types/document/decorators/collection-decorator.d.ts +0 -30
  179. package/types/document/decorators/resource.decorator.d.ts +0 -9
  180. package/types/document/decorators/singleton.decorator.d.ts +0 -25
  181. package/types/document/decorators/storage.decorator.d.ts +0 -22
  182. package/types/document/factory/add-references.d.ts +0 -4
  183. package/types/document/factory/create-document.d.ts +0 -12
  184. package/types/document/factory/factory.d.ts +0 -63
  185. package/types/document/factory/import-resource-class.d.ts +0 -10
  186. package/types/document/factory/import-type-class.d.ts +0 -17
  187. package/types/document/factory/index.d.ts +0 -1
  188. package/types/document/factory/process-resources.d.ts +0 -9
  189. package/types/document/factory/process-types.d.ts +0 -6
  190. package/types/document/utils/generate-codec.d.ts +0 -10
  191. package/types/schema/document.interface.d.ts +0 -34
  192. /package/cjs/document/{decorators → data-type}/complex-type.decorator.js +0 -0
  193. /package/cjs/document/{decorators → data-type}/simple-type.decorator.js +0 -0
  194. /package/cjs/schema/{document.interface.js → document/api-document.interface.js} +0 -0
  195. /package/esm/document/{decorators → data-type}/complex-type.decorator.js +0 -0
  196. /package/esm/document/{decorators → data-type}/simple-type.decorator.js +0 -0
  197. /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 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
- 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
- this._designCtorMap = new Map();
12
- this._typeCache = new index_js_2.ResponsiveMap();
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
- let resource;
141
- const m = constants_js_1.NAMESPACE_PATTERN.exec(path);
142
- if (!m)
143
- throw new index_js_1.NotFoundError(`Invalid resource path "${path}"`);
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
- const t = this.getResource(path);
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
- const t = this.getResource(path);
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
- const t = this.getResource(path);
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
- if (this._metadataCache)
201
- return (0, index_js_2.cloneObject)(this._metadataCache, options?.webSafe);
202
- const schema = {
203
- version: index_js_3.OpraSchema.SpecVersion,
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?.ctor || init?.base?.ctor;
18
- own.abstract = init?.abstract;
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 = own.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 newEl = new field_js_1.ApiField(this, el);
33
- this.fields.set(k, newEl);
40
+ const field = new field_js_1.ApiField(this, el);
41
+ this.fields.set(k, field);
34
42
  }
35
43
  }
36
- }
37
- addField(init) {
38
- const field = new field_js_1.ApiField(this, init);
39
- this.own.fields.set(field.name, field);
40
- this.fields.set(field.name, field);
41
- return field;
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.own.abstract,
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.ctor;
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("../decorators/complex-type.decorator.js");
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
- exportSchema() {
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.meanings = { ...this.base?.meanings, ...this.ownMeanings };
18
- this.decode = vg.isEnum(Object.values(this.values));
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 = data_type_js_1.DataType.prototype.exportSchema.call(this);
23
- Object.assign(out, (0, index_js_1.omitUndefined)({
24
- base: this.base ?
25
- (this.base.name ? this.base.name : this.base.exportSchema()) : undefined,
26
- values: this.ownValues,
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("../../schema/index.js");
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
- const values = Array.isArray(enumSource)
19
- ? enumSource.reduce((obj, v) => {
20
- obj[v] = v;
21
- return obj;
22
- }, {})
23
- : enumSource;
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: index_js_1.OpraSchema.EnumType.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
- if (options)
29
- Object.assign(metadata, (0, lodash_omit_1.default)(options, ['kind', 'values']));
30
- Reflect.defineMetadata(constants_js_1.DATATYPE_METADATA, metadata, enumSource);
31
- return values;
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 data_type_js_1 = require("./data-type.js");
7
- class MappedTypeClass extends data_type_js_1.DataType {
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 [elemName, elem] of this.type.fields.entries()) {
22
- if (isInheritedPredicate(elemName))
23
- this.fields.set(elemName, elem);
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 = data_type_js_1.DataType.prototype.exportSchema.call(this);
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
- type: mappedSource
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("../../schema/index.js");
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 = index_js_1.OpraSchema.SimpleType.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
- // Object.assign(out, omitUndefined({
23
- // base: this.base ?
24
- // (this.base.name ? this.base.name : this.base.exportSchema()) : undefined,
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;