@opra/common 1.0.0-alpha.2 → 1.0.0-alpha.4

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 (85) hide show
  1. package/browser.js +336 -136
  2. package/cjs/document/api-document.js +18 -12
  3. package/cjs/document/common/data-type-map.js +1 -1
  4. package/cjs/document/constants.js +1 -1
  5. package/cjs/document/data-type/api-field.js +5 -1
  6. package/cjs/document/data-type/complex-type-base.js +4 -0
  7. package/cjs/document/data-type/complex-type.js +4 -3
  8. package/cjs/document/data-type/enum-type.js +2 -0
  9. package/cjs/document/data-type/extended-types/base64.type.js +6 -1
  10. package/cjs/document/data-type/extended-types/date-string.type.js +10 -3
  11. package/cjs/document/data-type/extended-types/date-time-string.type.js +10 -3
  12. package/cjs/document/data-type/extended-types/date-time.type.js +10 -3
  13. package/cjs/document/data-type/extended-types/date.type.js +10 -3
  14. package/cjs/document/data-type/extended-types/email.type.js +24 -10
  15. package/cjs/document/data-type/extended-types/field-path.type.js +12 -4
  16. package/cjs/document/data-type/extended-types/filter.type.js +10 -3
  17. package/cjs/document/data-type/extended-types/object-id.type.js +6 -1
  18. package/cjs/document/data-type/extended-types/operation-result.type.js +20 -10
  19. package/cjs/document/data-type/extended-types/time.type.js +10 -3
  20. package/cjs/document/data-type/extended-types/url.type.js +6 -1
  21. package/cjs/document/data-type/extended-types/uuid.type.js +8 -2
  22. package/cjs/document/data-type/mapped-type.js +3 -0
  23. package/cjs/document/data-type/mixin-type.js +3 -0
  24. package/cjs/document/data-type/primitive-types/any.type.js +2 -1
  25. package/cjs/document/data-type/primitive-types/bigint.type.js +6 -1
  26. package/cjs/document/data-type/primitive-types/boolean.type.js +6 -1
  27. package/cjs/document/data-type/primitive-types/integer.type.js +6 -1
  28. package/cjs/document/data-type/primitive-types/null.type.js +6 -1
  29. package/cjs/document/data-type/primitive-types/number.type.js +10 -3
  30. package/cjs/document/data-type/primitive-types/object.type.js +2 -1
  31. package/cjs/document/data-type/primitive-types/string.type.js +14 -5
  32. package/cjs/document/data-type/simple-type.js +8 -1
  33. package/cjs/document/decorators/http-operation-entity.decorator.js +7 -7
  34. package/cjs/document/factory/api-document.factory.js +5 -0
  35. package/cjs/document/factory/data-type.factory.js +62 -39
  36. package/cjs/document/factory/http-api.factory.js +1 -1
  37. package/esm/document/api-document.js +18 -12
  38. package/esm/document/common/data-type-map.js +1 -1
  39. package/esm/document/constants.js +1 -1
  40. package/esm/document/data-type/api-field.js +5 -1
  41. package/esm/document/data-type/complex-type-base.js +4 -0
  42. package/esm/document/data-type/complex-type.js +4 -3
  43. package/esm/document/data-type/enum-type.js +2 -0
  44. package/esm/document/data-type/extended-types/base64.type.js +7 -2
  45. package/esm/document/data-type/extended-types/date-string.type.js +11 -4
  46. package/esm/document/data-type/extended-types/date-time-string.type.js +11 -4
  47. package/esm/document/data-type/extended-types/date-time.type.js +11 -4
  48. package/esm/document/data-type/extended-types/date.type.js +11 -4
  49. package/esm/document/data-type/extended-types/email.type.js +25 -11
  50. package/esm/document/data-type/extended-types/field-path.type.js +13 -5
  51. package/esm/document/data-type/extended-types/filter.type.js +11 -4
  52. package/esm/document/data-type/extended-types/object-id.type.js +7 -2
  53. package/esm/document/data-type/extended-types/operation-result.type.js +21 -11
  54. package/esm/document/data-type/extended-types/time.type.js +11 -4
  55. package/esm/document/data-type/extended-types/url.type.js +7 -2
  56. package/esm/document/data-type/extended-types/uuid.type.js +9 -3
  57. package/esm/document/data-type/mapped-type.js +3 -0
  58. package/esm/document/data-type/mixin-type.js +3 -0
  59. package/esm/document/data-type/primitive-types/any.type.js +3 -2
  60. package/esm/document/data-type/primitive-types/bigint.type.js +7 -2
  61. package/esm/document/data-type/primitive-types/boolean.type.js +7 -2
  62. package/esm/document/data-type/primitive-types/integer.type.js +7 -2
  63. package/esm/document/data-type/primitive-types/null.type.js +7 -2
  64. package/esm/document/data-type/primitive-types/number.type.js +11 -4
  65. package/esm/document/data-type/primitive-types/object.type.js +3 -2
  66. package/esm/document/data-type/primitive-types/string.type.js +15 -6
  67. package/esm/document/data-type/simple-type.js +8 -1
  68. package/esm/document/decorators/http-operation-entity.decorator.js +7 -7
  69. package/esm/document/factory/api-document.factory.js +4 -0
  70. package/esm/document/factory/data-type.factory.js +62 -39
  71. package/esm/document/factory/http-api.factory.js +1 -1
  72. package/package.json +4 -4
  73. package/types/document/api-document.d.ts +3 -6
  74. package/types/document/data-type/api-field.d.ts +2 -0
  75. package/types/document/data-type/complex-type.d.ts +1 -1
  76. package/types/document/data-type/data-type.d.ts +4 -2
  77. package/types/document/data-type/enum-type.d.ts +2 -2
  78. package/types/document/data-type/mapped-type.d.ts +2 -2
  79. package/types/document/data-type/mixin-type.d.ts +2 -2
  80. package/types/document/data-type/simple-type.d.ts +4 -2
  81. package/types/document/factory/api-document.factory.d.ts +1 -1
  82. package/types/document/factory/data-type.factory.d.ts +5 -0
  83. package/types/schema/data-type/simple-type.interface.d.ts +4 -0
  84. package/types/schema/document.interface.d.ts +13 -1
  85. package/types/schema/index.d.ts +1 -0
@@ -21,5 +21,10 @@ exports.NullType = NullType;
21
21
  exports.NullType = NullType = tslib_1.__decorate([
22
22
  (0, simple_type_js_1.SimpleType)({
23
23
  description: 'A Null value',
24
- })
24
+ nameMappings: {
25
+ js: 'null',
26
+ json: 'null',
27
+ },
28
+ }),
29
+ tslib_1.__metadata("design:paramtypes", [Object])
25
30
  ], NullType);
@@ -26,15 +26,22 @@ exports.NumberType = NumberType;
26
26
  tslib_1.__decorate([
27
27
  simple_type_js_1.SimpleType.Attribute({
28
28
  description: 'Determines the minimum value',
29
- })
29
+ }),
30
+ tslib_1.__metadata("design:type", Number)
30
31
  ], NumberType.prototype, "minValue", void 0);
31
32
  tslib_1.__decorate([
32
33
  simple_type_js_1.SimpleType.Attribute({
33
34
  description: 'Determines the maximum value',
34
- })
35
+ }),
36
+ tslib_1.__metadata("design:type", Number)
35
37
  ], NumberType.prototype, "maxValue", void 0);
36
38
  exports.NumberType = NumberType = tslib_1.__decorate([
37
39
  (0, simple_type_js_1.SimpleType)({
38
40
  description: 'Both Integer as well as Floating-Point numbers',
39
- })
41
+ nameMappings: {
42
+ js: 'number',
43
+ json: 'number',
44
+ },
45
+ }),
46
+ tslib_1.__metadata("design:paramtypes", [Object])
40
47
  ], NumberType);
@@ -16,7 +16,8 @@ exports.ObjectType = ObjectType = tslib_1.__decorate([
16
16
  name: 'object',
17
17
  description: 'A non modelled object',
18
18
  additionalFields: true,
19
- })
19
+ }),
20
+ tslib_1.__metadata("design:paramtypes", [Object])
20
21
  ], ObjectType);
21
22
  const metadata = Reflect.getMetadata(constants_js_1.DATATYPE_METADATA, ObjectType);
22
23
  metadata.ctor = Object;
@@ -34,25 +34,34 @@ exports.StringType = StringType;
34
34
  tslib_1.__decorate([
35
35
  simple_type_js_1.SimpleType.Attribute({
36
36
  description: 'Regex pattern to be used for validation',
37
- })
37
+ }),
38
+ tslib_1.__metadata("design:type", Object)
38
39
  ], StringType.prototype, "pattern", void 0);
39
40
  tslib_1.__decorate([
40
41
  simple_type_js_1.SimpleType.Attribute({
41
42
  description: 'Name of the pattern',
42
- })
43
+ }),
44
+ tslib_1.__metadata("design:type", String)
43
45
  ], StringType.prototype, "patternName", void 0);
44
46
  tslib_1.__decorate([
45
47
  simple_type_js_1.SimpleType.Attribute({
46
48
  description: 'Minimum number of characters',
47
- })
49
+ }),
50
+ tslib_1.__metadata("design:type", Number)
48
51
  ], StringType.prototype, "minLength", void 0);
49
52
  tslib_1.__decorate([
50
53
  simple_type_js_1.SimpleType.Attribute({
51
54
  description: 'Minimum number of characters',
52
- })
55
+ }),
56
+ tslib_1.__metadata("design:type", Number)
53
57
  ], StringType.prototype, "maxLength", void 0);
54
58
  exports.StringType = StringType = tslib_1.__decorate([
55
59
  (0, simple_type_js_1.SimpleType)({
56
60
  description: 'A sequence of characters',
57
- })
61
+ nameMappings: {
62
+ js: 'string',
63
+ json: 'string',
64
+ },
65
+ }),
66
+ tslib_1.__metadata("design:paramtypes", [Object])
58
67
  ], StringType);
@@ -28,6 +28,8 @@ exports.SimpleType = function (...args) {
28
28
  _this.base = initArgs.base;
29
29
  }
30
30
  _this.properties = initArgs.properties;
31
+ _this.ownNameMappings = { ...initArgs.nameMappings };
32
+ _this.nameMappings = { ..._this.base?.nameMappings, ...initArgs.nameMappings };
31
33
  _this.ownAttributes = (0, index_js_1.cloneObject)(initArgs.attributes || {});
32
34
  _this.attributes = _this.base ? (0, index_js_1.cloneObject)(_this.base.attributes) : {};
33
35
  if (_this.ownAttributes) {
@@ -46,6 +48,8 @@ exports.SimpleType = function (...args) {
46
48
  */
47
49
  class SimpleTypeClass extends data_type_js_1.DataType {
48
50
  extendsFrom(baseType) {
51
+ if (!(baseType instanceof data_type_js_1.DataType))
52
+ baseType = this.node.getDataType(baseType);
49
53
  if (!(baseType instanceof exports.SimpleType))
50
54
  return false;
51
55
  if (baseType === this)
@@ -90,12 +94,15 @@ class SimpleTypeClass extends data_type_js_1.DataType {
90
94
  return o;
91
95
  }, {});
92
96
  const baseName = this.base ? this.node.getDataTypeNameWithNs(this.base) : undefined;
93
- return (0, index_js_1.omitUndefined)({
97
+ const out = (0, index_js_1.omitUndefined)({
94
98
  ...data_type_js_1.DataType.prototype.toJSON.apply(this),
95
99
  base: this.base ? (baseName ? baseName : this.base.toJSON()) : undefined,
96
100
  attributes: attributes && Object.keys(attributes).length ? attributes : undefined,
97
101
  properties: Object.keys(properties).length ? properties : undefined,
98
102
  });
103
+ if (Object.keys(this.ownNameMappings).length)
104
+ out.nameMappings = { ...this.ownNameMappings };
105
+ return out;
99
106
  }
100
107
  }
101
108
  exports.SimpleType.prototype = SimpleTypeClass.prototype;
@@ -23,8 +23,8 @@ http_operation_js_1.HttpOperation.Entity.Create = function (arg0, arg1) {
23
23
  /** Initialize the decorator and the chain */
24
24
  const decoratorChain = [];
25
25
  const decorator = (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)(decoratorChain, (0, index_js_1.omitUndefined)({
26
- description: args.description,
27
26
  method: 'POST',
27
+ ...args,
28
28
  composition: 'Entity.Create',
29
29
  requestBody: {
30
30
  immediateFetch: true,
@@ -74,8 +74,8 @@ http_operation_js_1.HttpOperation.Entity.Delete = function (arg0, arg1) {
74
74
  /** Initialize the decorator and the chain */
75
75
  const decoratorChain = [];
76
76
  const decorator = (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)(decoratorChain, (0, index_js_1.omitUndefined)({
77
- description: args.description,
78
77
  method: 'DELETE',
78
+ ...args,
79
79
  composition: 'Entity.Delete',
80
80
  }));
81
81
  decorator
@@ -123,8 +123,8 @@ http_operation_js_1.HttpOperation.Entity.DeleteMany = function (arg0, arg1) {
123
123
  const filterType = new index_js_3.FilterType({ dataType: args.type });
124
124
  filterType.rules = {};
125
125
  const decorator = (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)(decoratorChain, (0, index_js_1.omitUndefined)({
126
- description: args.description,
127
126
  method: 'DELETE',
127
+ ...args,
128
128
  composition: 'Entity.DeleteMany',
129
129
  }));
130
130
  decorator
@@ -171,8 +171,8 @@ http_operation_js_1.HttpOperation.Entity.FindMany = function (arg0, arg1) {
171
171
  const filterType = new index_js_3.FilterType({ dataType: args.type });
172
172
  filterType.rules = {};
173
173
  const decorator = (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)(decoratorChain, (0, index_js_1.omitUndefined)({
174
- description: args.description,
175
174
  method: 'GET',
175
+ ...args,
176
176
  composition: 'Entity.FindMany',
177
177
  }));
178
178
  decorator
@@ -263,8 +263,8 @@ http_operation_js_1.HttpOperation.Entity.Get = function (arg0, arg1) {
263
263
  /** Initialize the decorator and the chain */
264
264
  const decoratorChain = [];
265
265
  const decorator = (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)(decoratorChain, (0, index_js_1.omitUndefined)({
266
- description: args.description,
267
266
  method: 'GET',
267
+ ...args,
268
268
  composition: 'Entity.Get',
269
269
  }));
270
270
  decorator
@@ -326,8 +326,8 @@ http_operation_js_1.HttpOperation.Entity.UpdateMany = function (arg0, arg1) {
326
326
  filterType.rules = {};
327
327
  const filterRules = new filter_rules_js_1.FilterRules();
328
328
  const decorator = (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)(decoratorChain, (0, index_js_1.omitUndefined)({
329
- description: args.description,
330
329
  method: 'PATCH',
330
+ ...args,
331
331
  composition: 'Entity.UpdateMany',
332
332
  requestBody: {
333
333
  immediateFetch: true,
@@ -381,8 +381,8 @@ http_operation_js_1.HttpOperation.Entity.Update = function (arg0, arg1) {
381
381
  const filterType = new index_js_3.FilterType({ dataType: args.type });
382
382
  filterType.rules = {};
383
383
  const decorator = (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)(decoratorChain, (0, index_js_1.omitUndefined)({
384
- description: args.description,
385
384
  method: 'PATCH',
385
+ ...args,
386
386
  composition: 'Entity.Update',
387
387
  requestBody: {
388
388
  partial: 'deep',
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ApiDocumentFactory = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const node_crypto_1 = tslib_1.__importDefault(require("node:crypto"));
6
+ const ts_gems_1 = require("ts-gems");
4
7
  const index_js_1 = require("../../helpers/index.js");
5
8
  const index_js_2 = require("../../schema/index.js");
6
9
  const api_document_js_1 = require("../api-document.js");
@@ -112,6 +115,8 @@ class ApiDocumentFactory {
112
115
  context.addError(`Unknown service protocol (${init.api.protocol})`);
113
116
  });
114
117
  }
118
+ const x = document.export();
119
+ (0, ts_gems_1.asMutable)(document).id = node_crypto_1.default.createHash('md5').update(JSON.stringify(x)).digest('base64url');
115
120
  }
116
121
  /**
117
122
  *
@@ -7,6 +7,7 @@ const document_init_context_js_1 = require("../common/document-init-context.js")
7
7
  const constants_js_1 = require("../constants.js");
8
8
  const complex_type_js_1 = require("../data-type/complex-type.js");
9
9
  const complex_type_base_js_1 = require("../data-type/complex-type-base.js");
10
+ const data_type_js_1 = require("../data-type/data-type.js");
10
11
  const enum_type_js_1 = require("../data-type/enum-type.js");
11
12
  const mapped_type_js_1 = require("../data-type/mapped-type.js");
12
13
  const mixin_type_js_1 = require("../data-type/mixin-type.js");
@@ -133,7 +134,7 @@ class DataTypeFactory {
133
134
  const { importQueue, initArgsMap } = context;
134
135
  // Check if data type already exist (maybe a builtin type or already imported)
135
136
  const dataType = owner.node.findDataType(thunk);
136
- if (dataType)
137
+ if (dataType instanceof data_type_js_1.DataType)
137
138
  return dataType.name;
138
139
  let metadata;
139
140
  let ctor;
@@ -212,11 +213,15 @@ class DataTypeFactory {
212
213
  /** Mark "out" object as initializing. This will help us to detect circular dependencies */
213
214
  out[initializingSymbol] = true;
214
215
  try {
215
- if (out.name && importQueue?.has(out.name)) {
216
- initArgsMap?.set(metadata.name, out);
216
+ if (out.name) {
217
+ if (importQueue?.has(out.name)) {
218
+ initArgsMap?.set(metadata.name, out);
219
+ out._instance = { name: metadata.name };
220
+ out[constants_js_1.kDataTypeMap] = owner.node[constants_js_1.kDataTypeMap];
221
+ }
222
+ else
223
+ return context.addError(`Data Type (${out.name}) must be explicitly added to type list in the document scope`);
217
224
  }
218
- else if (out.name)
219
- return context.addError(`Data Type (${out.name}) must be explicitly added to type list in the document scope`);
220
225
  switch (out.kind) {
221
226
  case index_js_2.OpraSchema.ComplexType.Kind:
222
227
  out.ctor = ctor;
@@ -290,7 +295,7 @@ class DataTypeFactory {
290
295
  const fieldMeta = typeof v === 'string' ? { type: v } : v;
291
296
  if (fieldMeta.isArray && !fieldMeta.type)
292
297
  return context.addError(`"type" must be defined explicitly for array fields`);
293
- const t = await this._importDataTypeArgs(context, owner, fieldMeta.type);
298
+ const t = await this._importDataTypeArgs(context, owner, fieldMeta.type || 'any');
294
299
  if (!t)
295
300
  return;
296
301
  initArgs.fields[k] = {
@@ -332,6 +337,7 @@ class DataTypeFactory {
332
337
  initArgs.ctor = initArgs.ctor || baseArgs.ctor;
333
338
  });
334
339
  initArgs.properties = metadata.properties;
340
+ initArgs.nameMappings = metadata.nameMappings;
335
341
  if (!initArgs.properties && initArgs.ctor)
336
342
  initArgs.properties = new initArgs.ctor();
337
343
  if (metadata.attributes)
@@ -384,27 +390,40 @@ class DataTypeFactory {
384
390
  });
385
391
  }
386
392
  static _createDataType(context, owner, args) {
393
+ let dataType = owner.node.findDataType(typeof args === 'string' ? args : args.name || '');
394
+ if (dataType instanceof data_type_js_1.DataType)
395
+ return dataType;
387
396
  const initArgs = typeof args === 'string' ? context.initArgsMap?.get(args) : args;
388
- switch (initArgs?.kind) {
389
- case index_js_2.OpraSchema.ComplexType.Kind:
390
- return this._createComplexType(context, owner, initArgs);
391
- case index_js_2.OpraSchema.EnumType.Kind:
392
- return this._createEnumType(context, owner, initArgs);
393
- case index_js_2.OpraSchema.MappedType.Kind:
394
- return this._createMappedType(context, owner, initArgs);
395
- case index_js_2.OpraSchema.MixinType.Kind:
396
- return this._createMixinType(context, owner, initArgs);
397
- case index_js_2.OpraSchema.SimpleType.Kind:
398
- return this._createSimpleType(context, owner, initArgs);
397
+ if (initArgs) {
398
+ const dataTypeMap = initArgs[constants_js_1.kDataTypeMap];
399
+ if (!dataTypeMap)
400
+ delete initArgs._instance;
401
+ dataType = initArgs._instance;
402
+ if (dataType?.name && dataTypeMap) {
403
+ dataTypeMap.set(dataType.name, dataType);
404
+ }
405
+ switch (initArgs?.kind) {
406
+ case index_js_2.OpraSchema.ComplexType.Kind:
407
+ return this._createComplexType(context, owner, initArgs);
408
+ case index_js_2.OpraSchema.EnumType.Kind:
409
+ return this._createEnumType(context, owner, initArgs);
410
+ case index_js_2.OpraSchema.MappedType.Kind:
411
+ return this._createMappedType(context, owner, initArgs);
412
+ case index_js_2.OpraSchema.MixinType.Kind:
413
+ return this._createMixinType(context, owner, initArgs);
414
+ case index_js_2.OpraSchema.SimpleType.Kind:
415
+ return this._createSimpleType(context, owner, initArgs);
416
+ }
399
417
  }
400
418
  context.addError(`Unknown data type (${String(args)})`);
401
419
  }
402
420
  static _createComplexType(context, owner, args) {
421
+ const dataType = args._instance || {};
422
+ Object.setPrototypeOf(dataType, complex_type_js_1.ComplexType.prototype);
403
423
  const initArgs = (0, index_js_1.cloneObject)(args);
404
424
  if (args.base) {
405
425
  context.enter('.base', () => {
406
- initArgs.base = (owner.node.findDataType(args.base) ||
407
- this._createDataType(context, owner, args.base));
426
+ initArgs.base = this._createDataType(context, owner, args.base);
408
427
  });
409
428
  }
410
429
  /** Set additionalFields */
@@ -413,14 +432,7 @@ class DataTypeFactory {
413
432
  if (typeof args.additionalFields === 'boolean' || Array.isArray(args.additionalFields))
414
433
  initArgs.additionalFields = args.additionalFields;
415
434
  else {
416
- if (typeof args.additionalFields === 'string') {
417
- initArgs.additionalFields =
418
- owner.node.findDataType(args.additionalFields) ||
419
- this._createDataType(context, owner, args.additionalFields);
420
- }
421
- else {
422
- initArgs.additionalFields = this._createDataType(context, owner, args.additionalFields);
423
- }
435
+ initArgs.additionalFields = this._createDataType(context, owner, args.additionalFields);
424
436
  }
425
437
  });
426
438
  }
@@ -430,7 +442,7 @@ class DataTypeFactory {
430
442
  context.enter('.fields', () => {
431
443
  for (const [k, v] of Object.entries(args.fields)) {
432
444
  context.enter(`[${k}]`, () => {
433
- const type = owner.node.findDataType(v.type) || this._createDataType(context, owner, v.type);
445
+ const type = this._createDataType(context, owner, v.type);
434
446
  if (type)
435
447
  initArgs.fields[k] = {
436
448
  ...v,
@@ -441,29 +453,37 @@ class DataTypeFactory {
441
453
  }
442
454
  });
443
455
  }
444
- return new complex_type_js_1.ComplexType(owner, initArgs);
456
+ complex_type_js_1.ComplexType.apply(dataType, [owner, initArgs]);
457
+ return dataType;
445
458
  }
446
459
  static _createEnumType(context, owner, args) {
460
+ const dataType = args._instance || {};
461
+ Object.setPrototypeOf(dataType, enum_type_js_1.EnumType.prototype);
447
462
  const initArgs = (0, index_js_1.cloneObject)(args);
448
463
  if (args.base) {
449
464
  context.enter('.base', () => {
450
- initArgs.base = (owner.node.findDataType(args.base) ||
451
- this._createDataType(context, owner, args.base));
465
+ initArgs.base = this._createDataType(context, owner, args.base);
452
466
  });
453
467
  }
454
468
  initArgs.attributes = args.attributes;
455
- return new enum_type_js_1.EnumType(owner, initArgs);
469
+ enum_type_js_1.EnumType.apply(dataType, [owner, initArgs]);
470
+ return dataType;
456
471
  }
457
472
  static _createMappedType(context, owner, args) {
473
+ const dataType = args._instance || {};
474
+ Object.setPrototypeOf(dataType, mapped_type_js_1.MappedType.prototype);
458
475
  const initArgs = (0, index_js_1.cloneObject)(args);
459
476
  if (args.base) {
460
477
  context.enter('.base', () => {
461
- initArgs.base = (owner.node.findDataType(args.base) || this._createDataType(context, owner, args.base));
478
+ initArgs.base = this._createDataType(context, owner, args.base);
462
479
  });
463
480
  }
464
- return new mapped_type_js_1.MappedType(owner, initArgs);
481
+ mapped_type_js_1.MappedType.apply(dataType, [owner, initArgs]);
482
+ return dataType;
465
483
  }
466
484
  static _createMixinType(context, owner, args) {
485
+ const dataType = args._instance || {};
486
+ Object.setPrototypeOf(dataType, mixin_type_js_1.MixinType.prototype);
467
487
  const initArgs = (0, index_js_1.cloneObject)(args);
468
488
  if (args.types) {
469
489
  context.enter('.types', () => {
@@ -471,7 +491,7 @@ class DataTypeFactory {
471
491
  let i = 0;
472
492
  for (const t of args.types) {
473
493
  context.enter(`[${i++}]`, () => {
474
- const base = owner.node.findDataType(t) || this._createDataType(context, owner, t);
494
+ const base = this._createDataType(context, owner, t);
475
495
  if (!(base instanceof complex_type_base_js_1.ComplexTypeBase))
476
496
  throw new TypeError(`"${base?.kind}" can't be set as base for a "${initArgs.kind}"`);
477
497
  initArgs.types.push(base);
@@ -479,17 +499,20 @@ class DataTypeFactory {
479
499
  }
480
500
  });
481
501
  }
482
- return new mixin_type_js_1.MixinType(owner, initArgs);
502
+ mixin_type_js_1.MixinType.apply(dataType, [owner, initArgs]);
503
+ return dataType;
483
504
  }
484
505
  static _createSimpleType(context, owner, args) {
506
+ const dataType = args._instance || {};
507
+ Object.setPrototypeOf(dataType, simple_type_js_1.SimpleType.prototype);
485
508
  const initArgs = (0, index_js_1.cloneObject)(args);
486
509
  if (args.base) {
487
510
  context.enter('.base', () => {
488
- initArgs.base = (owner.node.findDataType(args.base) ||
489
- this._createDataType(context, owner, args.base));
511
+ initArgs.base = this._createDataType(context, owner, args.base);
490
512
  });
491
513
  }
492
- return new simple_type_js_1.SimpleType(owner, initArgs);
514
+ simple_type_js_1.SimpleType.apply(dataType, [owner, initArgs]);
515
+ return dataType;
493
516
  }
494
517
  }
495
518
  exports.DataTypeFactory = DataTypeFactory;
@@ -208,7 +208,7 @@ class HttpApiFactory {
208
208
  });
209
209
  }
210
210
  if (metadata.requestBody) {
211
- await context.enter('.requestBody', async () => {
211
+ await context.enterAsync('.requestBody', async () => {
212
212
  const requestBody = new http_request_body_js_1.HttpRequestBody(operation);
213
213
  await this._initHttpRequestBody(context, requestBody, metadata.requestBody);
214
214
  operation.requestBody = requestBody;
@@ -27,18 +27,29 @@ export class ApiDocument extends DocumentElement {
27
27
  if (dt)
28
28
  return this[kTypeNSMap].get(dt);
29
29
  }
30
+ findDocument(id) {
31
+ if (this.id === id)
32
+ return this;
33
+ for (const doc of this.references.values()) {
34
+ if (doc.id === id)
35
+ return doc;
36
+ const d = doc.findDocument(id);
37
+ if (d)
38
+ return d;
39
+ }
40
+ }
30
41
  toJSON() {
31
42
  return this.export();
32
43
  }
33
44
  /**
34
45
  * Export as Opra schema definition object
35
46
  */
36
- export(options) {
47
+ export() {
37
48
  const out = omitUndefined({
38
49
  spec: OpraSchema.SpecVersion,
50
+ id: this.id,
39
51
  url: this.url,
40
52
  info: cloneObject(this.info, true),
41
- // api: this.api ? this.api.toJSON() : undefined,
42
53
  });
43
54
  if (this.references.size) {
44
55
  let i = 0;
@@ -46,16 +57,11 @@ export class ApiDocument extends DocumentElement {
46
57
  for (const [ns, doc] of this.references.entries()) {
47
58
  if (doc[BUILTIN])
48
59
  continue;
49
- if (options?.references === 'external-url') {
50
- if (doc.url)
51
- references[ns] = doc.url;
52
- else
53
- references[ns] = `/$schema?ns=${ns}`;
54
- }
55
- else if (options?.references === 'relative-url')
56
- references[ns] = `/$schema?ns=${ns}`;
57
- else
58
- references[ns] = doc.export(options);
60
+ references[ns] = {
61
+ id: doc.id,
62
+ url: doc.url,
63
+ info: cloneObject(doc.info, true),
64
+ };
59
65
  i++;
60
66
  }
61
67
  if (i)
@@ -20,7 +20,7 @@ export class DataTypeMap {
20
20
  }
21
21
  get(nameOrCtor) {
22
22
  let name = typeof nameOrCtor === 'string' ? nameOrCtor : this[kCtorMap].get(nameOrCtor);
23
- if (!name) {
23
+ if (!name && typeof nameOrCtor === 'function') {
24
24
  const metadata = Reflect.getMetadata(DATATYPE_METADATA, nameOrCtor);
25
25
  name = metadata?.name;
26
26
  }
@@ -4,7 +4,7 @@ export const DECODER = Symbol.for('opra.type.decoder');
4
4
  export const ENCODER = Symbol('opra.type.encoder');
5
5
  export const DECORATOR = Symbol.for('DECORATOR');
6
6
  export const BUILTIN = Symbol.for('BUILTIN');
7
- export const NAMESPACE_PATTERN = /([a-z$_]\w+)(:.+)/i;
7
+ export const NAMESPACE_PATTERN = /([a-z$_]\w+):(.+)/i;
8
8
  export const CLASS_NAME_PATTERN = /^[a-z][\w_]*$/i;
9
9
  export const EXTRACT_TYPENAME_PATTERN = /^(.*)Type(\d*)$/;
10
10
  export const kDataTypeMap = Symbol.for('kDataTypeMap');
@@ -33,6 +33,8 @@ export const ApiField = function (...args) {
33
33
  _this.exclusive = initArgs.exclusive;
34
34
  _this.translatable = initArgs.translatable;
35
35
  _this.deprecated = initArgs.deprecated;
36
+ _this.readonly = initArgs.readonly;
37
+ _this.writeonly = initArgs.writeonly;
36
38
  _this.examples = initArgs.examples;
37
39
  };
38
40
  /**
@@ -43,7 +45,7 @@ class ApiFieldClass extends DocumentElement {
43
45
  toJSON() {
44
46
  const typeName = this.type ? this.node.getDataTypeNameWithNs(this.type) : undefined;
45
47
  return omitUndefined({
46
- type: this.type ? (typeName ? typeName : this.type?.toJSON()) : undefined,
48
+ type: typeName ? typeName : this.type?.toJSON(),
47
49
  description: this.description,
48
50
  isArray: this.isArray,
49
51
  default: this.default,
@@ -52,6 +54,8 @@ class ApiFieldClass extends DocumentElement {
52
54
  exclusive: this.exclusive,
53
55
  translatable: this.translatable,
54
56
  deprecated: this.deprecated,
57
+ readonly: this.readonly,
58
+ writeonly: this.writeonly,
55
59
  examples: this.examples,
56
60
  });
57
61
  }
@@ -143,6 +143,10 @@ class ComplexTypeBaseClass extends DataType {
143
143
  // Process fields
144
144
  let fieldName;
145
145
  for (const field of this.fields.values()) {
146
+ if ((context.ignoreReadonlyFields && field.readonly) || (context.ignoreWriteonlyFields && field.writeonly)) {
147
+ schema[field.name] = vg.isUndefined({ coerce: true });
148
+ continue;
149
+ }
146
150
  fieldName = field.name;
147
151
  let p;
148
152
  if (projection !== '*') {
@@ -57,6 +57,8 @@ export const ComplexType = function (...args) {
57
57
  */
58
58
  class ComplexTypeClass extends ComplexTypeBase {
59
59
  extendsFrom(baseType) {
60
+ if (!(baseType instanceof DataType))
61
+ baseType = this.node.getDataType(baseType);
60
62
  if (!(baseType instanceof ComplexTypeBase))
61
63
  return false;
62
64
  if (baseType === this)
@@ -72,9 +74,8 @@ class ComplexTypeClass extends ComplexTypeBase {
72
74
  });
73
75
  if (this.additionalFields) {
74
76
  if (this.additionalFields instanceof DataType) {
75
- out.additionalFields = this.additionalFields[1].name
76
- ? this.additionalFields[1].name
77
- : this.additionalFields[1].toJSON();
77
+ const typeName = this.node.getDataTypeNameWithNs(this.additionalFields);
78
+ out.additionalFields = typeName ? typeName : this.additionalFields.toJSON();
78
79
  }
79
80
  else
80
81
  out.additionalFields = this.additionalFields;
@@ -35,6 +35,8 @@ export const EnumType = function (...args) {
35
35
  */
36
36
  class EnumTypeClass extends DataType {
37
37
  extendsFrom(baseType) {
38
+ if (!(baseType instanceof DataType))
39
+ baseType = this.node.getDataType(baseType);
38
40
  if (!(baseType instanceof EnumType))
39
41
  return false;
40
42
  if (baseType === this)
@@ -1,4 +1,4 @@
1
- import { __decorate } from "tslib";
1
+ import { __decorate, __metadata } from "tslib";
2
2
  import { vg } from 'valgen';
3
3
  import { DECODER, ENCODER } from '../../constants.js';
4
4
  import { SimpleType } from '../simple-type.js';
@@ -17,6 +17,11 @@ let Base64Type = class Base64Type {
17
17
  Base64Type = __decorate([
18
18
  SimpleType({
19
19
  description: 'A stream of bytes, base64 encoded',
20
- })
20
+ nameMappings: {
21
+ js: 'string',
22
+ json: 'string',
23
+ },
24
+ }),
25
+ __metadata("design:paramtypes", [Object])
21
26
  ], Base64Type);
22
27
  export { Base64Type };
@@ -1,4 +1,4 @@
1
- import { __decorate } from "tslib";
1
+ import { __decorate, __metadata } from "tslib";
2
2
  import { vg } from 'valgen';
3
3
  import { DECODER, ENCODER } from '../../constants.js';
4
4
  import { SimpleType } from '../simple-type.js';
@@ -23,19 +23,26 @@ let DateStringType = class DateStringType {
23
23
  __decorate([
24
24
  SimpleType.Attribute({
25
25
  description: 'Minimum value',
26
- })
26
+ }),
27
+ __metadata("design:type", String)
27
28
  ], DateStringType.prototype, "minValue", void 0);
28
29
  __decorate([
29
30
  SimpleType.Attribute({
30
31
  description: 'Maximum value',
31
- })
32
+ }),
33
+ __metadata("design:type", String)
32
34
  ], DateStringType.prototype, "maxValue", void 0);
33
35
  DateStringType = __decorate([
34
36
  SimpleType({
35
37
  description: 'Date string value',
38
+ nameMappings: {
39
+ js: 'string',
40
+ json: 'string',
41
+ },
36
42
  })
37
43
  .Example('2021-04-18', 'Full date value')
38
44
  .Example('2021-04', 'Date value without day')
39
- .Example('2021', 'Year only value')
45
+ .Example('2021', 'Year only value'),
46
+ __metadata("design:paramtypes", [Object])
40
47
  ], DateStringType);
41
48
  export { DateStringType };