@opra/common 0.18.4 → 0.19.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 (122) hide show
  1. package/browser.js +1225 -910
  2. package/cjs/document/api-document.js +55 -38
  3. package/cjs/document/data-type/api-field.js +5 -5
  4. package/cjs/document/data-type/builtin/any.type.js +3 -5
  5. package/cjs/document/data-type/builtin/base64.type.js +5 -28
  6. package/cjs/document/data-type/builtin/bigint.type.js +7 -25
  7. package/cjs/document/data-type/builtin/boolean.type.js +5 -18
  8. package/cjs/document/data-type/builtin/date.type.js +13 -9
  9. package/cjs/document/data-type/builtin/index.js +3 -1
  10. package/cjs/document/data-type/builtin/integer.type.js +5 -18
  11. package/cjs/document/data-type/builtin/number.type.js +5 -23
  12. package/cjs/document/data-type/builtin/object.type.js +4 -9
  13. package/cjs/document/data-type/builtin/string.type.js +5 -14
  14. package/cjs/document/data-type/builtin/time.type.js +11 -23
  15. package/cjs/document/data-type/builtin/timestamp.type.js +8 -20
  16. package/cjs/document/data-type/builtin/uuid.type.js +15 -0
  17. package/cjs/document/data-type/complex-type.js +151 -92
  18. package/cjs/document/data-type/data-type.js +32 -31
  19. package/cjs/document/data-type/enum-type.js +49 -43
  20. package/cjs/document/data-type/mapped-type.js +115 -70
  21. package/cjs/document/data-type/simple-type.js +46 -45
  22. package/cjs/document/data-type/union-type.js +69 -36
  23. package/cjs/document/factory/create-document.js +5 -3
  24. package/cjs/document/factory/factory.js +23 -20
  25. package/cjs/document/factory/import-type-class.js +13 -4
  26. package/cjs/document/resource/collection.js +55 -59
  27. package/cjs/document/resource/resource.js +10 -21
  28. package/cjs/document/resource/singleton.js +36 -39
  29. package/cjs/exception/common-errors/validation.error.js +11 -0
  30. package/cjs/exception/index.js +2 -1
  31. package/cjs/exception/{enums/issue-severity.enum.js → issue-severity.enum.js} +1 -1
  32. package/cjs/exception/opra-exception.js +2 -2
  33. package/cjs/filter/antlr/OpraFilterLexer.js +1 -1
  34. package/cjs/filter/antlr/OpraFilterParser.js +1 -1
  35. package/cjs/filter/errors.js +5 -0
  36. package/cjs/http/enums/http-headers-codes.enum.js +1 -1
  37. package/cjs/http/enums/http-status-codes.enum.js +1 -1
  38. package/cjs/http/http-request-message.js +1 -1
  39. package/cjs/http/http-response-message.js +1 -1
  40. package/cjs/schema/data-type/complex-type.interface.js +1 -1
  41. package/cjs/schema/data-type/enum-type.interface.js +1 -1
  42. package/cjs/schema/data-type/mapped-type.interface.js +1 -1
  43. package/cjs/schema/data-type/simple-type.interface.js +1 -1
  44. package/cjs/schema/data-type/union-type.interface.js +1 -1
  45. package/cjs/schema/index.js +3 -41
  46. package/cjs/schema/opra-schema.ns.js +18 -0
  47. package/cjs/schema/resource/collection.interface.js +1 -1
  48. package/cjs/schema/resource/container.interface.js +1 -1
  49. package/cjs/schema/resource/singleton.interface.js +1 -1
  50. package/esm/document/api-document.js +55 -38
  51. package/esm/document/data-type/api-field.js +5 -5
  52. package/esm/document/data-type/builtin/any.type.js +2 -4
  53. package/esm/document/data-type/builtin/base64.type.js +4 -27
  54. package/esm/document/data-type/builtin/bigint.type.js +6 -24
  55. package/esm/document/data-type/builtin/boolean.type.js +4 -17
  56. package/esm/document/data-type/builtin/date.type.js +12 -8
  57. package/esm/document/data-type/builtin/index.js +3 -1
  58. package/esm/document/data-type/builtin/integer.type.js +4 -17
  59. package/esm/document/data-type/builtin/number.type.js +4 -22
  60. package/esm/document/data-type/builtin/object.type.js +3 -8
  61. package/esm/document/data-type/builtin/string.type.js +4 -13
  62. package/esm/document/data-type/builtin/time.type.js +10 -22
  63. package/esm/document/data-type/builtin/timestamp.type.js +7 -19
  64. package/esm/document/data-type/builtin/uuid.type.js +12 -0
  65. package/esm/document/data-type/complex-type.js +151 -92
  66. package/esm/document/data-type/data-type.js +31 -31
  67. package/esm/document/data-type/enum-type.js +49 -43
  68. package/esm/document/data-type/mapped-type.js +114 -70
  69. package/esm/document/data-type/simple-type.js +45 -44
  70. package/esm/document/data-type/union-type.js +68 -36
  71. package/esm/document/factory/create-document.js +6 -4
  72. package/esm/document/factory/factory.js +23 -20
  73. package/esm/document/factory/import-type-class.js +13 -4
  74. package/esm/document/resource/collection.js +55 -59
  75. package/esm/document/resource/resource.js +9 -21
  76. package/esm/document/resource/singleton.js +36 -39
  77. package/esm/exception/common-errors/validation.error.js +7 -0
  78. package/esm/exception/index.js +2 -1
  79. package/esm/exception/opra-exception.js +2 -2
  80. package/esm/filter/antlr/OpraFilterLexer.js +2 -1
  81. package/esm/filter/antlr/OpraFilterParser.js +2 -1
  82. package/esm/filter/errors.js +5 -0
  83. package/esm/schema/index.js +2 -41
  84. package/esm/schema/opra-schema.ns.js +15 -0
  85. package/package.json +5 -3
  86. package/types/document/api-document.d.ts +3 -2
  87. package/types/document/data-type/builtin/base64.type.d.ts +0 -4
  88. package/types/document/data-type/builtin/bigint.type.d.ts +1 -5
  89. package/types/document/data-type/builtin/boolean.type.d.ts +0 -3
  90. package/types/document/data-type/builtin/date.type.d.ts +1 -3
  91. package/types/document/data-type/builtin/index.d.ts +3 -1
  92. package/types/document/data-type/builtin/integer.type.d.ts +1 -5
  93. package/types/document/data-type/builtin/number.type.d.ts +0 -4
  94. package/types/document/data-type/builtin/object.type.d.ts +0 -1
  95. package/types/document/data-type/builtin/string.type.d.ts +0 -3
  96. package/types/document/data-type/builtin/time.type.d.ts +1 -6
  97. package/types/document/data-type/builtin/timestamp.type.d.ts +0 -3
  98. package/types/document/data-type/builtin/uuid.type.d.ts +2 -0
  99. package/types/document/data-type/complex-type.d.ts +29 -16
  100. package/types/document/data-type/data-type.d.ts +19 -15
  101. package/types/document/data-type/enum-type.d.ts +15 -5
  102. package/types/document/data-type/mapped-type.d.ts +27 -5
  103. package/types/document/data-type/simple-type.d.ts +26 -12
  104. package/types/document/data-type/union-type.d.ts +20 -5
  105. package/types/document/factory/factory.d.ts +3 -2
  106. package/types/document/resource/collection.d.ts +11 -9
  107. package/types/document/resource/resource.d.ts +3 -9
  108. package/types/document/resource/singleton.d.ts +7 -5
  109. package/types/exception/common-errors/validation.error.d.ts +4 -0
  110. package/types/exception/error-issue.d.ts +1 -1
  111. package/types/exception/index.d.ts +2 -1
  112. package/types/exception/opra-exception.d.ts +4 -2
  113. package/types/filter/errors.d.ts +3 -0
  114. package/types/schema/data-type/enum-type.interface.d.ts +5 -5
  115. package/types/schema/data-type/simple-type.interface.d.ts +5 -11
  116. package/types/schema/index.d.ts +2 -51
  117. package/types/schema/opra-schema.ns.d.ts +15 -0
  118. package/cjs/document/data-type/builtin/guid.type.js +0 -35
  119. package/esm/document/data-type/builtin/guid.type.js +0 -32
  120. package/types/document/data-type/builtin/guid.type.d.ts +0 -7
  121. /package/esm/exception/{enums/issue-severity.enum.js → issue-severity.enum.js} +0 -0
  122. /package/types/exception/{enums/issue-severity.enum.d.ts → issue-severity.enum.d.ts} +0 -0
@@ -1,58 +1,64 @@
1
1
  import omit from 'lodash.omit';
2
+ import merge from 'putil-merge';
3
+ import * as vg from 'valgen';
2
4
  import { omitUndefined } from '../../helpers/index.js';
3
5
  import { OpraSchema } from '../../schema/index.js';
4
6
  import { METADATA_KEY } from '../constants.js';
5
7
  import { DataType } from './data-type.js';
6
- /**
7
- * @class EnumType
8
- */
9
- export const EnumType = function (...args) {
10
- // Injector
11
- if (!this) {
12
- const [source, options] = args;
13
- const values = Array.isArray(source)
14
- ? source.reduce((obj, v) => {
15
- obj[v] = v;
16
- return obj;
17
- }, {})
18
- : source;
19
- const metadata = {
20
- kind: OpraSchema.EnumType.Kind,
21
- values
22
- };
23
- if (options)
24
- Object.assign(metadata, omit(options, ['kind', 'values']));
25
- Reflect.defineMetadata(METADATA_KEY, metadata, source);
26
- return values;
8
+ class EnumTypeClass extends DataType {
9
+ constructor(document, init) {
10
+ super(document, init);
11
+ this.kind = OpraSchema.EnumType.Kind;
12
+ this.base = init.base;
13
+ this.ownValues = init.values;
14
+ this.ownMeanings = init.meanings || {};
15
+ this.values = { ...this.base?.values, ...this.ownValues };
16
+ this.meanings = { ...this.base?.meanings, ...this.ownMeanings };
27
17
  }
28
- // Constructor
29
- const [, init] = args;
30
- // call super()
31
- DataType.apply(this, args);
32
- const _this = this;
33
- _this.kind = OpraSchema.EnumType.Kind;
34
- _this.base = init.base;
35
- const own = _this.own;
36
- own.values = init.values;
37
- own.meanings = init.meanings || {};
38
- _this.values = { ..._this.base?.values, ...own.values };
39
- _this.meanings = { ..._this.base?.meanings, ...own.meanings };
40
- };
41
- const proto = {
42
18
  exportSchema() {
43
19
  const out = DataType.prototype.exportSchema.call(this);
44
20
  Object.assign(out, omitUndefined({
45
21
  base: this.base ?
46
22
  (this.base.name ? this.base.name : this.base.exportSchema()) : undefined,
47
- values: this.own.values,
48
- meanings: this.own.meanings
23
+ values: this.ownValues,
24
+ meanings: this.ownMeanings
49
25
  }));
50
26
  return out;
51
- },
52
- validate(v) {
53
- if (!this.values[v])
54
- throw new Error(`'${v}' is not a valid'${this.name ? ' ' + this.name : ''}' value`);
55
27
  }
28
+ _getDecoder() {
29
+ if (!this._decoder)
30
+ this._decoder = vg.isEnum(Object.values(this.values), { enumName: this.name });
31
+ return this._decoder;
32
+ }
33
+ _getEncoder() {
34
+ return this._getDecoder();
35
+ }
36
+ }
37
+ /**
38
+ * @class EnumType
39
+ */
40
+ export const EnumType = function (...args) {
41
+ if (this) {
42
+ // Constructor
43
+ const [document, init] = args;
44
+ merge(this, new EnumTypeClass(document, init), { descriptor: true });
45
+ return;
46
+ }
47
+ // Injector
48
+ const [source, options] = args;
49
+ const values = Array.isArray(source)
50
+ ? source.reduce((obj, v) => {
51
+ obj[v] = v;
52
+ return obj;
53
+ }, {})
54
+ : source;
55
+ const metadata = {
56
+ kind: OpraSchema.EnumType.Kind,
57
+ values
58
+ };
59
+ if (options)
60
+ Object.assign(metadata, omit(options, ['kind', 'values']));
61
+ Reflect.defineMetadata(METADATA_KEY, metadata, source);
62
+ return values;
56
63
  };
57
- Object.assign(EnumType.prototype, proto);
58
- Object.setPrototypeOf(EnumType.prototype, DataType.prototype);
64
+ EnumType.prototype = EnumTypeClass.prototype;
@@ -1,91 +1,135 @@
1
+ import merge from 'putil-merge';
2
+ import * as vg from 'valgen';
1
3
  import { applyMixins, inheritPropertyInitializers, omitUndefined, ResponsiveMap } from '../../helpers/index.js';
2
4
  import { OpraSchema } from '../../schema/index.js';
3
5
  import { METADATA_KEY } from '../constants.js';
4
6
  import { DataType } from './data-type.js';
5
- /**
6
- * @class MappedType
7
- */
8
- export const MappedType = function (...args) {
9
- let pickKeys;
10
- let omitKeys;
11
- const isInheritedPredicate = (propertyName) => {
12
- if (omitKeys && omitKeys.includes(propertyName.toLowerCase()))
13
- return false;
14
- if (pickKeys)
15
- return pickKeys.includes(propertyName.toLowerCase());
16
- return true;
17
- };
18
- // MappedType helper
19
- if (!this) {
20
- const [source, options] = args;
21
- pickKeys = options.pick && options.pick.map(x => String(x).toLowerCase());
22
- omitKeys = options.omit && options.omit.map(x => String(x).toLowerCase());
23
- class MappedClass {
24
- constructor() {
25
- inheritPropertyInitializers(this, source, isInheritedPredicate);
26
- }
7
+ class MappedTypeClass extends DataType {
8
+ constructor(document, init) {
9
+ super(document, init);
10
+ this.kind = OpraSchema.MappedType.Kind;
11
+ const own = this.own;
12
+ own.pick = init.pick;
13
+ own.omit = init.omit;
14
+ this.kind = OpraSchema.MappedType.Kind;
15
+ this.type = init.type;
16
+ this.pick = own.pick;
17
+ this.omit = own.omit;
18
+ this.fields = new ResponsiveMap();
19
+ this.additionalFields = this.type.additionalFields;
20
+ 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);
27
24
  }
28
- applyMixins(MappedClass, source);
29
- // const mappedTypeMetadata: MappedType.TypeMapping[] = [];
30
- const m = Reflect.getOwnMetadata(METADATA_KEY, source);
31
- if (!m)
32
- throw new TypeError(`Class "${source}" doesn't have datatype metadata information`);
33
- if (!(m.kind === OpraSchema.ComplexType.Kind))
34
- throw new TypeError(`Class "${source}" is not a ${OpraSchema.ComplexType.Kind}`);
35
- const metadata = {
36
- kind: 'MappedType',
37
- type: source
38
- };
39
- if (options.pick)
40
- metadata.pick = options.pick;
41
- if (options.omit)
42
- metadata.omit = options.omit;
43
- Reflect.defineMetadata(METADATA_KEY, metadata, MappedClass);
44
- MappedType._applyMixin(MappedClass, source, {
45
- ...options,
46
- isInheritedPredicate
47
- });
48
- return MappedClass;
49
25
  }
50
- // Constructor
51
- // call super()
52
- DataType.apply(this, args);
53
- const [, init] = args;
54
- pickKeys = init.pick && init.pick.map(x => String(x).toLowerCase());
55
- omitKeys = init.omit && init.omit.map(x => String(x).toLowerCase());
56
- const _this = this;
57
- const own = _this.own;
58
- own.type = init.type;
59
- own.pick = init.pick;
60
- own.omit = init.omit;
61
- _this.kind = OpraSchema.MappedType.Kind;
62
- _this.type = own.type;
63
- _this.pick = own.pick;
64
- _this.omit = own.omit;
65
- _this.fields = new ResponsiveMap();
66
- _this.additionalFields = _this.type.additionalFields;
67
- for (const [elemName, elem] of own.type.fields.entries()) {
68
- if (isInheritedPredicate(elemName))
69
- _this.fields.set(elemName, elem);
70
- }
71
- };
72
- MappedType._applyMixin = () => void 0;
73
- const proto = {
74
26
  exportSchema() {
75
27
  const out = DataType.prototype.exportSchema.call(this);
76
28
  Object.assign(out, omitUndefined({
77
- type: this.own.type.name ? this.own.type.name : this.own.type.exportSchema(),
29
+ type: this.type.name ? this.type.name : this.type.exportSchema(),
78
30
  pick: this.own.pick,
79
31
  omit: this.own.omit,
80
32
  }));
81
33
  return out;
82
34
  }
35
+ _getDecoder() {
36
+ if (this._decoder)
37
+ return this._decoder;
38
+ const schema = {};
39
+ for (const f of this.fields.values()) {
40
+ let t = f.type.getDecoder();
41
+ if (f.isArray)
42
+ t = vg.isArray(t);
43
+ schema[f.name] = t;
44
+ }
45
+ this._decoder = vg.isObject(schema, {
46
+ additionalFields: this.additionalFields,
47
+ name: this.name,
48
+ caseInSensitive: true
49
+ });
50
+ return this._decoder;
51
+ }
52
+ _getEncoder() {
53
+ if (this._encoder)
54
+ return this._encoder;
55
+ const schema = {};
56
+ for (const f of this.fields.values()) {
57
+ let t = f.type.getEncoder();
58
+ if (f.isArray)
59
+ t = vg.isArray(t);
60
+ schema[f.name] = t;
61
+ }
62
+ this._encoder = vg.isObject(schema, {
63
+ additionalFields: this.additionalFields,
64
+ name: this.name,
65
+ caseInSensitive: true,
66
+ detectCircular: true
67
+ });
68
+ return this._encoder;
69
+ }
70
+ }
71
+ /**
72
+ * @class MappedType
73
+ */
74
+ export const MappedType = function (...args) {
75
+ // Constructor
76
+ if (this) {
77
+ const [document, init] = args;
78
+ merge(this, new MappedTypeClass(document, init), { descriptor: true });
79
+ return;
80
+ }
81
+ // MappedType helper
82
+ const [source, options] = args;
83
+ const isInheritedPredicate = getIsInheritedPredicateFn(options.pick, options.omit);
84
+ class MappedClass {
85
+ constructor() {
86
+ inheritPropertyInitializers(this, source, isInheritedPredicate);
87
+ }
88
+ }
89
+ applyMixins(MappedClass, source);
90
+ // const mappedTypeMetadata: MappedType.TypeMapping[] = [];
91
+ const m = Reflect.getOwnMetadata(METADATA_KEY, source);
92
+ if (!m)
93
+ throw new TypeError(`Class "${source}" doesn't have datatype metadata information`);
94
+ if (!(m.kind === OpraSchema.ComplexType.Kind))
95
+ throw new TypeError(`Class "${source}" is not a ${OpraSchema.ComplexType.Kind}`);
96
+ const metadata = {
97
+ kind: 'MappedType',
98
+ type: source
99
+ };
100
+ if (options.pick)
101
+ metadata.pick = options.pick;
102
+ if (options.omit)
103
+ metadata.omit = options.omit;
104
+ Reflect.defineMetadata(METADATA_KEY, metadata, MappedClass);
105
+ MappedType._applyMixin(MappedClass, source, {
106
+ ...options,
107
+ isInheritedPredicate
108
+ });
109
+ return MappedClass;
83
110
  };
84
- Object.assign(MappedType.prototype, proto);
85
- Object.setPrototypeOf(MappedType.prototype, DataType.prototype);
111
+ MappedType.prototype = MappedTypeClass.prototype;
112
+ MappedType._applyMixin = () => void 0;
113
+ function getIsInheritedPredicateFn(pick, omit) {
114
+ const pickKeys = pick?.map(x => String(x).toLowerCase());
115
+ const omitKeys = omit?.map(x => String(x).toLowerCase());
116
+ return (propertyName) => {
117
+ if (omitKeys && omitKeys.includes(propertyName.toLowerCase()))
118
+ return false;
119
+ if (pickKeys)
120
+ return pickKeys.includes(propertyName.toLowerCase());
121
+ return true;
122
+ };
123
+ }
124
+ /**
125
+ *
126
+ */
86
127
  export function PickType(classRef, keys) {
87
128
  return MappedType(classRef, { pick: keys });
88
129
  }
130
+ /**
131
+ *
132
+ */
89
133
  export function OmitType(classRef, keys) {
90
134
  return MappedType(classRef, { omit: keys });
91
135
  }
@@ -1,56 +1,57 @@
1
1
  import omit from 'lodash.omit';
2
- import { omitUndefined } from '../../helpers/index.js';
2
+ import merge from 'putil-merge';
3
+ import * as vg from 'valgen';
3
4
  import { OpraSchema } from '../../schema/index.js';
4
5
  import { METADATA_KEY, TYPENAME_PATTERN } from '../constants.js';
5
6
  import { DataType } from './data-type.js';
6
7
  /**
7
8
  * @class SimpleType
8
9
  */
9
- export const SimpleType = function (...args) {
10
- // ClassDecorator
11
- if (!this) {
12
- const [options] = args;
13
- return function (target) {
14
- let name = options?.name || target.name.match(TYPENAME_PATTERN)?.[1] || target.name;
15
- name = name.charAt(0).toLowerCase() + name.substring(1);
16
- const metadata = Reflect.getOwnMetadata(METADATA_KEY, target) || {};
17
- metadata.kind = OpraSchema.SimpleType.Kind;
18
- metadata.name = name;
19
- if (options)
20
- Object.assign(metadata, omit(options, ['kind', 'name', 'base']));
21
- Reflect.defineMetadata(METADATA_KEY, metadata, target);
22
- };
10
+ class SimpleTypeClass extends DataType {
11
+ constructor(document, init) {
12
+ super(document, init);
13
+ this.kind = OpraSchema.SimpleType.Kind;
14
+ this.base = init.base;
15
+ this._decoder = init.decoder || init.base?._decoder || vg.isAny();
16
+ this._encoder = init.encoder || init.base?._encoder || vg.isAny();
17
+ }
18
+ _getDecoder() {
19
+ return this._decoder;
20
+ }
21
+ _getEncoder() {
22
+ return this._encoder;
23
23
  }
24
- // Constructor
25
- const [, options] = args;
26
- // call super()
27
- DataType.apply(this, args);
28
- const _this = this;
29
- _this.kind = OpraSchema.SimpleType.Kind;
30
- _this.base = options?.base;
31
- const own = _this.own;
32
- own.codec = options?.codec;
33
- own.ctor = options?.ctor;
34
- if (options?.pattern)
35
- own.pattern = (options?.pattern instanceof RegExp ? options.pattern : new RegExp(options.pattern));
36
- const ctor = own.ctor || _this.base?.ctor;
37
- _this.ctor = ctor || Object;
38
- _this.decode = own.codec?.decode || _this.base?.decode || ((v) => v);
39
- _this.encode = own.codec?.encode || _this.base?.encode || ((v) => v);
40
- _this.coerce = own.codec?.coerce || _this.base?.coerce || ((v) => v);
41
- _this.validate = own.codec?.validate || _this.base?.validate || (() => true);
42
- _this.pattern = own.pattern || _this.base?.pattern;
43
- };
44
- const proto = {
45
24
  exportSchema() {
46
- const out = DataType.prototype.exportSchema.call(this);
47
- Object.assign(out, omitUndefined({
48
- base: this.base ?
49
- (this.base.name ? this.base.name : this.base.exportSchema()) : undefined,
50
- pattern: this.own.pattern
51
- }));
25
+ // noinspection UnnecessaryLocalVariableJS
26
+ const out = super.exportSchema();
27
+ // Object.assign(out, omitUndefined({
28
+ // base: this.base ?
29
+ // (this.base.name ? this.base.name : this.base.exportSchema()) : undefined,
30
+ // }));
52
31
  return out;
53
32
  }
33
+ }
34
+ /**
35
+ * @class SimpleType
36
+ */
37
+ export const SimpleType = function (...args) {
38
+ // Constructor
39
+ if (this) {
40
+ const [document, init] = args;
41
+ merge(this, new SimpleTypeClass(document, init), { descriptor: true });
42
+ return;
43
+ }
44
+ // ClassDecorator
45
+ const [options] = args;
46
+ return function (target) {
47
+ let name = options?.name || target.name.match(TYPENAME_PATTERN)?.[1] || target.name;
48
+ name = name.charAt(0).toLowerCase() + name.substring(1);
49
+ const metadata = Reflect.getOwnMetadata(METADATA_KEY, target) || {};
50
+ metadata.kind = OpraSchema.SimpleType.Kind;
51
+ metadata.name = name;
52
+ if (options)
53
+ Object.assign(metadata, omit(options, ['kind', 'name']));
54
+ Reflect.defineMetadata(METADATA_KEY, metadata, target);
55
+ };
54
56
  };
55
- Object.assign(SimpleType.prototype, proto);
56
- Object.setPrototypeOf(SimpleType.prototype, DataType.prototype);
57
+ SimpleType.prototype = SimpleTypeClass.prototype;
@@ -1,53 +1,83 @@
1
+ import merge from 'putil-merge';
2
+ import * as vg from 'valgen';
1
3
  import { applyMixins, inheritPropertyInitializers, omitUndefined, ResponsiveMap } from '../../helpers/index.js';
2
4
  import { OpraSchema } from '../../schema/index.js';
3
5
  import { METADATA_KEY } from '../constants.js';
4
6
  import { ComplexType } from './complex-type.js';
5
7
  import { DataType } from './data-type.js';
6
8
  import { MappedType } from './mapped-type.js';
7
- /**
8
- * @class UnionType
9
- */
10
- export const UnionType = function (...args) {
11
- // UnionType helper
12
- if (!this) {
13
- return mapToUnionType(...args);
14
- }
15
- // Constructor
16
- // call super()
17
- DataType.apply(this, args);
18
- const [, init] = args;
19
- const _this = this;
20
- _this.fields = new ResponsiveMap();
21
- const own = _this.own;
22
- own.types = [];
23
- for (const base of init.types) {
24
- if (!(base instanceof ComplexType || base instanceof UnionType || base instanceof MappedType))
25
- throw new TypeError(`${OpraSchema.UnionType.Kind} shall contain ${OpraSchema.ComplexType.Kind}, ` +
26
- `${OpraSchema.UnionType.Kind} of ${OpraSchema.MappedType.Kind} types.`);
27
- own.types.push(base);
28
- if (base.additionalFields)
29
- _this.additionalFields = true;
30
- _this.fields.setAll(base.fields);
9
+ class UnionTypeClass extends DataType {
10
+ constructor(document, init) {
11
+ super(document, init);
12
+ this.kind = OpraSchema.UnionType.Kind;
13
+ this.fields = new ResponsiveMap();
14
+ const own = this.own;
15
+ own.types = [];
16
+ for (const base of init.types) {
17
+ if (!(base instanceof ComplexType || base instanceof UnionType || base instanceof MappedType))
18
+ throw new TypeError(`${OpraSchema.UnionType.Kind} shall contain ${OpraSchema.ComplexType.Kind}, ` +
19
+ `${OpraSchema.UnionType.Kind} of ${OpraSchema.MappedType.Kind} types.`);
20
+ own.types.push(base);
21
+ if (base.additionalFields)
22
+ this.additionalFields = true;
23
+ this.fields.setAll(base.fields);
24
+ }
25
+ this.types = [...own.types];
31
26
  }
32
- _this.kind = 'UnionType';
33
- _this.types = [...own.types];
34
- };
35
- UnionType._applyMixin = () => void 0;
36
- const proto = {
37
27
  exportSchema() {
38
- const out = DataType.prototype.exportSchema.call(this);
28
+ const out = super.exportSchema();
39
29
  Object.assign(out, omitUndefined({
40
30
  types: this.own.types.map(t => t.name ? t.name : t.exportSchema())
41
31
  }));
42
32
  return out;
43
33
  }
44
- };
45
- Object.assign(UnionType.prototype, proto);
46
- Object.setPrototypeOf(UnionType.prototype, DataType.prototype);
34
+ _getDecoder() {
35
+ if (this._decoder)
36
+ return this._decoder;
37
+ const schema = {};
38
+ for (const f of this.fields.values()) {
39
+ let t = f.type.getDecoder();
40
+ if (f.isArray)
41
+ t = vg.isArray(t);
42
+ schema[f.name] = t;
43
+ }
44
+ this._decoder = vg.isObject(schema, {
45
+ additionalFields: this.additionalFields,
46
+ name: this.name,
47
+ caseInSensitive: true
48
+ });
49
+ return this._decoder;
50
+ }
51
+ _getEncoder() {
52
+ if (this._encoder)
53
+ return this._encoder;
54
+ const schema = {};
55
+ for (const f of this.fields.values()) {
56
+ let t = f.type.getEncoder();
57
+ if (f.isArray)
58
+ t = vg.isArray(t);
59
+ schema[f.name] = t;
60
+ }
61
+ this._encoder = vg.isObject(schema, {
62
+ additionalFields: this.additionalFields,
63
+ name: this.name,
64
+ caseInSensitive: true,
65
+ detectCircular: true
66
+ });
67
+ return this._encoder;
68
+ }
69
+ }
47
70
  /**
48
- *
71
+ * @class UnionType
49
72
  */
50
- function mapToUnionType(...args) {
73
+ export const UnionType = function (...args) {
74
+ // Constructor
75
+ if (this) {
76
+ const [document, init] = args;
77
+ merge(this, new UnionTypeClass(document, init), { descriptor: true });
78
+ return;
79
+ }
80
+ // UnionType helper
51
81
  // Filter undefined items
52
82
  const clasRefs = [...args].filter(x => !!x);
53
83
  if (!clasRefs.length)
@@ -75,4 +105,6 @@ function mapToUnionType(...args) {
75
105
  }
76
106
  UnionType._applyMixin(UnionClass, ...clasRefs);
77
107
  return UnionClass;
78
- }
108
+ };
109
+ UnionType.prototype = UnionTypeClass.prototype;
110
+ UnionType._applyMixin = () => void 0;
@@ -1,5 +1,5 @@
1
1
  import { OpraSchema } from '../../schema/index.js';
2
- import { AnyType, Base64Type, BigIntType, BooleanType, DateType, GuidType, IntegerType, NumberType, ObjectType, StringType } from '../data-type/builtin/index.js';
2
+ import { AnyType, Base64Type, BigintType, BooleanType, DateType, IntegerType, NumberType, ObjectType, StringType, TimestampType, TimeType, UuidType } from '../data-type/builtin/index.js';
3
3
  export async function createDocument(init, options) {
4
4
  // const {document, typeQueue, curPath, resourceQueue} = this;
5
5
  this.document.url = init.url;
@@ -8,6 +8,8 @@ export async function createDocument(init, options) {
8
8
  if (!options?.noBuiltinTypes) {
9
9
  const builtinDocument = await this.createBuiltinTypeDocument();
10
10
  this.document.references.set('Opra', builtinDocument);
11
+ for (const [c, s] of Object.getPrototypeOf(this).constructor.designTypeMap.entries())
12
+ this.document._designTypeMap.set(c, s);
11
13
  }
12
14
  if (init.references)
13
15
  await this.addReferences(init.references);
@@ -64,9 +66,9 @@ export async function createBuiltinTypeDocument() {
64
66
  name: 'MIT'
65
67
  }
66
68
  },
67
- types: [AnyType, Base64Type, BigIntType, BooleanType,
68
- DateType, GuidType, IntegerType, NumberType,
69
- ObjectType, StringType
69
+ types: [AnyType, Base64Type, BigintType, BooleanType,
70
+ DateType, UuidType, IntegerType, NumberType,
71
+ ObjectType, StringType, TimeType, TimestampType
70
72
  ]
71
73
  };
72
74
  const factoryClass = Object.getPrototypeOf(this).constructor;
@@ -1,3 +1,4 @@
1
+ var _a;
1
2
  import { ResponsiveMap } from '../../helpers/index.js';
2
3
  import { ApiDocument } from '../api-document.js';
3
4
  import { addReferences } from './add-references.js';
@@ -34,25 +35,27 @@ export class DocumentFactory {
34
35
  return factory.createDocumentFromUrl(url);
35
36
  }
36
37
  }
38
+ _a = DocumentFactory;
39
+ DocumentFactory.designTypeMap = new Map();
37
40
  (() => {
38
- DocumentFactory.prototype.createDocument = createDocument;
39
- DocumentFactory.prototype.createDocumentFromUrl = createDocumentFromUrl;
40
- DocumentFactory.prototype.createBuiltinTypeDocument = createBuiltinTypeDocument;
41
- DocumentFactory.prototype.addReferences = addReferences;
42
- DocumentFactory.prototype.importTypeClass = importTypeClass;
43
- DocumentFactory.prototype.extractSimpleTypeSchema = extractSimpleTypeSchema;
44
- DocumentFactory.prototype.extractComplexTypeSchema = extractComplexTypeSchema;
45
- DocumentFactory.prototype.extractMappedTypeSchema = extractMappedTypeSchema;
46
- DocumentFactory.prototype.extractUnionTypeSchema = extractUnionTypeSchema;
47
- DocumentFactory.prototype.extractEnumTypeSchema = extractEnumTypeSchema;
48
- DocumentFactory.prototype.extractFieldSchema = extractFieldSchema;
49
- DocumentFactory.prototype.processTypes = processTypes;
50
- DocumentFactory.prototype.createDataTypeInstance = createDataTypeInstance;
51
- DocumentFactory.prototype.addDataType = addDataType;
52
- DocumentFactory.prototype.importResourceClass = importResourceClass;
53
- DocumentFactory.prototype.extractSingletonSchema = extractSingletonSchema;
54
- DocumentFactory.prototype.extractCollectionSchema = extractCollectionSchema;
55
- DocumentFactory.prototype.processResourceQueue = processResourceQueue;
56
- DocumentFactory.prototype.createCollection = createCollection;
57
- DocumentFactory.prototype.createSingleton = createSingleton;
41
+ _a.prototype.createDocument = createDocument;
42
+ _a.prototype.createDocumentFromUrl = createDocumentFromUrl;
43
+ _a.prototype.createBuiltinTypeDocument = createBuiltinTypeDocument;
44
+ _a.prototype.addReferences = addReferences;
45
+ _a.prototype.importTypeClass = importTypeClass;
46
+ _a.prototype.extractSimpleTypeSchema = extractSimpleTypeSchema;
47
+ _a.prototype.extractComplexTypeSchema = extractComplexTypeSchema;
48
+ _a.prototype.extractMappedTypeSchema = extractMappedTypeSchema;
49
+ _a.prototype.extractUnionTypeSchema = extractUnionTypeSchema;
50
+ _a.prototype.extractEnumTypeSchema = extractEnumTypeSchema;
51
+ _a.prototype.extractFieldSchema = extractFieldSchema;
52
+ _a.prototype.processTypes = processTypes;
53
+ _a.prototype.createDataTypeInstance = createDataTypeInstance;
54
+ _a.prototype.addDataType = addDataType;
55
+ _a.prototype.importResourceClass = importResourceClass;
56
+ _a.prototype.extractSingletonSchema = extractSingletonSchema;
57
+ _a.prototype.extractCollectionSchema = extractCollectionSchema;
58
+ _a.prototype.processResourceQueue = processResourceQueue;
59
+ _a.prototype.createCollection = createCollection;
60
+ _a.prototype.createSingleton = createSingleton;
58
61
  })();
@@ -1,3 +1,4 @@
1
+ import { validator } from 'valgen';
1
2
  import { cloneObject, isConstructor, resolveThunk } from '../../helpers/index.js';
2
3
  import { OpraSchema } from '../../schema/index.js';
3
4
  import { METADATA_KEY } from '../constants.js';
@@ -63,13 +64,16 @@ metadata) {
63
64
  const baseClass = Object.getPrototypeOf(ctor.prototype).constructor;
64
65
  if (Reflect.hasMetadata(METADATA_KEY, baseClass))
65
66
  target.base = await this.importTypeClass(baseClass);
66
- target.codec = Object.create(ctor.prototype);
67
+ if (typeof ctor.prototype.decode === 'function')
68
+ target.decoder = validator(metadata.name, ctor.prototype.decode);
69
+ if (typeof ctor.prototype.encode === 'function')
70
+ target.encoder = validator(metadata.name, ctor.prototype.encode);
67
71
  }
68
72
  export async function extractComplexTypeSchema(target, ctor, metadata) {
69
73
  const baseClass = Object.getPrototypeOf(ctor.prototype).constructor;
70
74
  if (Reflect.hasMetadata(METADATA_KEY, baseClass))
71
75
  target.base = await this.importTypeClass(baseClass);
72
- target.ctor = ctor;
76
+ target.ctor = target.ctor || ctor;
73
77
  // Fields
74
78
  if (metadata.fields) {
75
79
  const fields = target.fields = {};
@@ -85,8 +89,13 @@ export async function extractComplexTypeSchema(target, ctor, metadata) {
85
89
  };
86
90
  if (elemMeta.enum)
87
91
  elemSchema.type = await this.importTypeClass(elemMeta.enum);
88
- if (!elemSchema.type && elemMeta.designType)
89
- elemSchema.type = await this.importTypeClass(elemMeta.designType);
92
+ if (!elemSchema.type && elemMeta.designType) {
93
+ const mappingType = this.document.getDataType(elemMeta.designType, true);
94
+ if (mappingType)
95
+ elemSchema.type = mappingType.name;
96
+ else
97
+ elemSchema.type = await this.importTypeClass(elemMeta.designType);
98
+ }
90
99
  await this.extractFieldSchema(elemSchema, ctor, elemMeta, elemName);
91
100
  // Check enum again. External packages may modify enum value
92
101
  if (elemMeta.enum)