@api-client/core 0.6.13 → 0.6.16

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 (155) hide show
  1. package/build/browser.d.ts +9 -2
  2. package/build/browser.js +12 -2
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +9 -2
  5. package/build/index.js +12 -2
  6. package/build/index.js.map +1 -1
  7. package/build/src/amf/AmfMixin.d.ts +395 -0
  8. package/build/src/amf/AmfMixin.js +1146 -0
  9. package/build/src/amf/AmfMixin.js.map +1 -0
  10. package/build/src/amf/AmfSerializer.d.ts +136 -0
  11. package/build/src/amf/AmfSerializer.js +1913 -0
  12. package/build/src/amf/AmfSerializer.js.map +1 -0
  13. package/build/src/amf/AmfShapeGenerator.d.ts +85 -0
  14. package/build/src/amf/AmfShapeGenerator.js +385 -0
  15. package/build/src/amf/AmfShapeGenerator.js.map +1 -0
  16. package/build/src/amf/AmfTypes.d.ts +24 -0
  17. package/build/src/amf/AmfTypes.js +122 -0
  18. package/build/src/amf/AmfTypes.js.map +1 -0
  19. package/build/src/amf/ApiExampleGenerator.d.ts +36 -0
  20. package/build/src/amf/ApiExampleGenerator.js +109 -0
  21. package/build/src/amf/ApiExampleGenerator.js.map +1 -0
  22. package/build/src/amf/ApiMonacoSchemaGenerator.d.ts +67 -0
  23. package/build/src/amf/ApiMonacoSchemaGenerator.js +243 -0
  24. package/build/src/amf/ApiMonacoSchemaGenerator.js.map +1 -0
  25. package/build/src/amf/ApiSchemaGenerator.d.ts +55 -0
  26. package/build/src/amf/ApiSchemaGenerator.js +94 -0
  27. package/build/src/amf/ApiSchemaGenerator.js.map +1 -0
  28. package/build/src/amf/ApiSchemaValues.d.ts +98 -0
  29. package/build/src/amf/ApiSchemaValues.js +382 -0
  30. package/build/src/amf/ApiSchemaValues.js.map +1 -0
  31. package/build/src/amf/Utils.d.ts +41 -0
  32. package/build/src/amf/Utils.js +176 -0
  33. package/build/src/amf/Utils.js.map +1 -0
  34. package/build/src/amf/data-node/DataNodeBase.d.ts +31 -0
  35. package/build/src/amf/data-node/DataNodeBase.js +77 -0
  36. package/build/src/amf/data-node/DataNodeBase.js.map +1 -0
  37. package/build/src/amf/data-node/JsonDataNodeGenerator.d.ts +15 -0
  38. package/build/src/amf/data-node/JsonDataNodeGenerator.js +24 -0
  39. package/build/src/amf/data-node/JsonDataNodeGenerator.js.map +1 -0
  40. package/build/src/amf/data-node/UrlEncodedDataNodeGenerator.d.ts +13 -0
  41. package/build/src/amf/data-node/UrlEncodedDataNodeGenerator.js +42 -0
  42. package/build/src/amf/data-node/UrlEncodedDataNodeGenerator.js.map +1 -0
  43. package/build/src/amf/data-node/XmlDataNodeGenerator.d.ts +21 -0
  44. package/build/src/amf/data-node/XmlDataNodeGenerator.js +30 -0
  45. package/build/src/amf/data-node/XmlDataNodeGenerator.js.map +1 -0
  46. package/build/src/amf/definitions/Amf.d.ts +362 -0
  47. package/build/src/amf/definitions/Amf.js +2 -0
  48. package/build/src/amf/definitions/Amf.js.map +1 -0
  49. package/build/src/amf/definitions/Api.d.ts +381 -0
  50. package/build/src/amf/definitions/Api.js +2 -0
  51. package/build/src/amf/definitions/Api.js.map +1 -0
  52. package/build/src/amf/definitions/Base.d.ts +11 -0
  53. package/build/src/amf/definitions/Base.js +2 -0
  54. package/build/src/amf/definitions/Base.js.map +1 -0
  55. package/build/src/amf/definitions/Namespace.d.ts +311 -0
  56. package/build/src/amf/definitions/Namespace.js +330 -0
  57. package/build/src/amf/definitions/Namespace.js.map +1 -0
  58. package/build/src/amf/definitions/Shapes.d.ts +309 -0
  59. package/build/src/amf/definitions/Shapes.js +87 -0
  60. package/build/src/amf/definitions/Shapes.js.map +1 -0
  61. package/build/src/amf/models/AmfDataNode.d.ts +68 -0
  62. package/build/src/amf/models/AmfDataNode.js +188 -0
  63. package/build/src/amf/models/AmfDataNode.js.map +1 -0
  64. package/build/src/amf/shape/ShapeBase.d.ts +75 -0
  65. package/build/src/amf/shape/ShapeBase.js +90 -0
  66. package/build/src/amf/shape/ShapeBase.js.map +1 -0
  67. package/build/src/amf/shape/ShapeJsonSchemaGenerator.d.ts +46 -0
  68. package/build/src/amf/shape/ShapeJsonSchemaGenerator.js +406 -0
  69. package/build/src/amf/shape/ShapeJsonSchemaGenerator.js.map +1 -0
  70. package/build/src/amf/shape/ShapeXmlSchemaGenerator.d.ts +84 -0
  71. package/build/src/amf/shape/ShapeXmlSchemaGenerator.js +820 -0
  72. package/build/src/amf/shape/ShapeXmlSchemaGenerator.js.map +1 -0
  73. package/build/src/models/Thing.d.ts +17 -0
  74. package/build/src/models/Thing.js +19 -1
  75. package/build/src/models/Thing.js.map +1 -1
  76. package/build/src/models/data/Bindings.d.ts +161 -0
  77. package/build/src/models/data/Bindings.js +2 -0
  78. package/build/src/models/data/Bindings.js.map +1 -0
  79. package/build/src/models/data/DataAssociation.d.ts +135 -14
  80. package/build/src/models/data/DataAssociation.js +154 -21
  81. package/build/src/models/data/DataAssociation.js.map +1 -1
  82. package/build/src/models/data/DataAssociationSchema.d.ts +36 -0
  83. package/build/src/models/data/DataEntity.d.ts +76 -4
  84. package/build/src/models/data/DataEntity.js +136 -9
  85. package/build/src/models/data/DataEntity.js.map +1 -1
  86. package/build/src/models/data/DataFile.d.ts +3 -0
  87. package/build/src/models/data/DataFile.js +3 -0
  88. package/build/src/models/data/DataFile.js.map +1 -1
  89. package/build/src/models/data/DataModel.d.ts +1 -1
  90. package/build/src/models/data/DataModel.js.map +1 -1
  91. package/build/src/models/data/DataNamespace.d.ts +6 -0
  92. package/build/src/models/data/DataNamespace.js +11 -1
  93. package/build/src/models/data/DataNamespace.js.map +1 -1
  94. package/build/src/models/data/DataProperty.d.ts +131 -36
  95. package/build/src/models/data/DataProperty.js +200 -17
  96. package/build/src/models/data/DataProperty.js.map +1 -1
  97. package/data/apis/oas-date/oas-date.yaml +28 -0
  98. package/data/apis/oas-types/oas-types.yaml +159 -0
  99. package/data/apis/oas-unions/oas-unions.yaml +75 -0
  100. package/data/apis/raml-date/raml-date.raml +28 -0
  101. package/data/apis/recursive/recursive.raml +14 -0
  102. package/data/apis/schema-api/examples/person.json +14 -0
  103. package/data/apis/schema-api/examples/person.raml +14 -0
  104. package/data/apis/schema-api/examples/person.url.encoded +1 -0
  105. package/data/apis/schema-api/examples/person.xml +14 -0
  106. package/data/apis/schema-api/library/demo-types.raml +43 -0
  107. package/data/apis/schema-api/schema-api.raml +644 -0
  108. package/data/apis/schema-api/schemas/person.json +104 -0
  109. package/data/apis/schema-api/schemas/person.xsd +26 -0
  110. package/data/apis/schema-api/types/DemoPerson.raml +67 -0
  111. package/data/model.js +106 -0
  112. package/data/models/oas-date.json +637 -0
  113. package/data/models/oas-types.json +5352 -0
  114. package/data/models/oas-unions.json +1881 -0
  115. package/data/models/raml-date.json +1096 -0
  116. package/data/models/recursive.json +610 -0
  117. package/data/models/schema-api.json +37319 -0
  118. package/package.json +9 -6
  119. package/src/amf/AmfMixin.ts +1623 -0
  120. package/src/amf/AmfSerializer.ts +2028 -0
  121. package/src/amf/AmfShapeGenerator.ts +400 -0
  122. package/src/amf/AmfTypes.ts +126 -0
  123. package/src/amf/ApiExampleGenerator.ts +112 -0
  124. package/src/amf/ApiMonacoSchemaGenerator.ts +296 -0
  125. package/src/amf/ApiSchemaGenerator.ts +108 -0
  126. package/src/amf/ApiSchemaValues.ts +411 -0
  127. package/src/amf/Utils.ts +182 -0
  128. package/src/amf/data-node/DataNodeBase.ts +81 -0
  129. package/src/amf/data-node/JsonDataNodeGenerator.ts +26 -0
  130. package/src/amf/data-node/README.md +3 -0
  131. package/src/amf/data-node/UrlEncodedDataNodeGenerator.ts +43 -0
  132. package/src/amf/data-node/XmlDataNodeGenerator.ts +38 -0
  133. package/src/amf/definitions/Amf.ts +443 -0
  134. package/src/amf/definitions/Api.ts +427 -0
  135. package/src/amf/definitions/Base.ts +13 -0
  136. package/src/amf/definitions/Namespace.ts +341 -0
  137. package/src/amf/definitions/Shapes.ts +414 -0
  138. package/src/amf/models/AmfDataNode.ts +200 -0
  139. package/src/amf/shape/README.md +4 -0
  140. package/src/amf/shape/ShapeBase.ts +160 -0
  141. package/src/amf/shape/ShapeJsonSchemaGenerator.ts +422 -0
  142. package/src/amf/shape/ShapeXmlSchemaGenerator.ts +876 -0
  143. package/src/models/Thing.ts +25 -1
  144. package/src/models/data/Bindings.ts +186 -0
  145. package/src/models/data/DataAssociation.ts +226 -29
  146. package/src/models/data/DataAssociationSchema.ts +38 -0
  147. package/src/models/data/DataEntity.ts +170 -13
  148. package/src/models/data/DataFile.ts +3 -0
  149. package/src/models/data/DataModel.ts +1 -1
  150. package/src/models/data/DataNamespace.ts +16 -1
  151. package/src/models/data/DataProperty.ts +250 -47
  152. package/build/src/models/data/DataPropertySchema.d.ts +0 -125
  153. package/build/src/models/data/DataPropertySchema.js +0 -33
  154. package/build/src/models/data/DataPropertySchema.js.map +0 -1
  155. package/src/models/data/DataPropertySchema.ts +0 -156
@@ -0,0 +1,160 @@
1
+ import { Time } from '@pawel-up/data-mock';
2
+ import { AmfNamespace as ns } from "../definitions/Namespace.js";
3
+ import { ApiSchemaValues } from '../ApiSchemaValues.js';
4
+ import { JsonDataNodeGenerator } from '../data-node/JsonDataNodeGenerator.js';
5
+ import { IAnyShape, IArrayShape, IDataExample, IFileShape, INodeShape, IPropertyShape, IScalarNode, IScalarShape, ISchemaShape, IShapeUnion, ITupleShape, IUnionShape } from '../definitions/Shapes.js';
6
+
7
+ export interface ShapeRenderOptions {
8
+ /**
9
+ * All selected unions in the current view.
10
+ * When the processor encounter an union it checks this array
11
+ * to pick the selected union.
12
+ * When the selected union cannot be determined it picks the first union.
13
+ */
14
+ selectedUnions?: string[];
15
+ /**
16
+ * Whether to include optional fields into the schema.
17
+ * @default false
18
+ */
19
+ renderOptional?: boolean;
20
+ /**
21
+ * When set it uses the data mocking library to generate the values
22
+ * when examples and default are not set.
23
+ */
24
+ renderMocked?: boolean;
25
+ /**
26
+ * The library **always** uses default values in the schema.
27
+ * When a default value is not set by default it inserts an empty value for
28
+ * the given data type ('', false, null, random date). When this is set
29
+ * it includes examples in the generated value.
30
+ */
31
+ renderExamples?: boolean;
32
+ }
33
+
34
+ /**
35
+ * A base class for generators that generates a schema from AMF's shape definition.
36
+ */
37
+ export abstract class ShapeBase {
38
+ opts: Readonly<ShapeRenderOptions>;
39
+ time = new Time();
40
+
41
+ constructor(opts: ShapeRenderOptions = {}) {
42
+ this.opts = Object.freeze({ ...opts });
43
+ }
44
+
45
+ protected _scalarValue(schema: IScalarShape): string | number | boolean {
46
+ const { defaultValue, examples, values, inherits, dataType } = schema;
47
+ // check examples
48
+ if (this.opts.renderExamples && examples && examples.length) {
49
+ const example = examples.find((item) => !!item.structuredValue);
50
+ const value = this._exampleToObject(example);
51
+ if (typeof value !== 'undefined') {
52
+ return ApiSchemaValues.readTypedValue(value, dataType);
53
+ }
54
+ }
55
+
56
+ // check the default value
57
+ if (defaultValue) {
58
+ const gen = new JsonDataNodeGenerator();
59
+ const processed = gen.processNode(defaultValue);
60
+ // return ApiSchemaValues.readTypedValue(processed, dataType);
61
+ return processed;
62
+ }
63
+ // check enum values
64
+ if (values && values.length) {
65
+ const typed = values[0] as IScalarNode;
66
+ if (typed.value !== undefined) {
67
+ return ApiSchemaValues.readTypedValue(typed.value, dataType);
68
+ }
69
+ }
70
+ // check parents
71
+ if (Array.isArray(inherits) && inherits.length) {
72
+ for (let i = 0, len = inherits.length; i < len; i += 1) {
73
+ const result = this._scalarValue(inherits[i] as IScalarShape);
74
+ if (result !== undefined) {
75
+ return result;
76
+ }
77
+ }
78
+ }
79
+ // return this[dataTypeToExample](dataType, format);
80
+ // create a default value.
81
+ return ApiSchemaValues.generateDefaultValue(schema);
82
+ }
83
+
84
+ /**
85
+ * Checks whether the union represents a scalar + nil which is equivalent
86
+ * to having scalar that is not required.
87
+ *
88
+ * See more about nil values in RAML:
89
+ * https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md#nil-type
90
+ *
91
+ * @param union The list of unions in the shape
92
+ */
93
+ protected _isNotRequiredUnion(union: IShapeUnion[]): boolean {
94
+ let scalars = 0;
95
+ let hasNil = false;
96
+ union.forEach((i) => {
97
+ if (i.types.includes(ns.aml.vocabularies.shapes.NilShape)) {
98
+ hasNil = true;
99
+ } else if (i.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
100
+ const scalar = i as IScalarShape;
101
+ if (scalar.dataType === ns.w3.xmlSchema.nil) {
102
+ hasNil = true;
103
+ } else {
104
+ scalars += 1;
105
+ }
106
+ } else if (i.types.includes(ns.aml.vocabularies.shapes.FileShape)) {
107
+ scalars += 1;
108
+ }
109
+ });
110
+ if (!hasNil) {
111
+ return false;
112
+ }
113
+ // size of union minus the nil union
114
+ if (scalars === union.length - 1) {
115
+ return true;
116
+ }
117
+ return false;
118
+ }
119
+
120
+ /**
121
+ * Generates a schema from AMF's shape.
122
+ * @param schema The Shape definition
123
+ * @returns The generated example
124
+ */
125
+ abstract generate(schema: IShapeUnion): string | number | boolean | null | undefined;
126
+
127
+ /**
128
+ * Serializes generated values into the final mime type related form.
129
+ *
130
+ * @returns The generated example
131
+ */
132
+ abstract serialize(value: any): string | undefined;
133
+
134
+ /**
135
+ * @abstract
136
+ * @param {ApiExample} example The example to turn into a JS object
137
+ * @returns {any}
138
+ */
139
+ protected abstract _exampleToObject(example?: IDataExample): any;
140
+
141
+ protected abstract _scalarShapeObject(schema: IScalarShape): any;
142
+
143
+ protected abstract _nilShapeObject(schema: IScalarShape): any;
144
+
145
+ protected abstract _nodeShapeObject(schema: INodeShape): any;
146
+
147
+ protected abstract _unionShapeObject(schema: IUnionShape): any;
148
+
149
+ protected abstract _fileShapeObject(schema?: IFileShape): any;
150
+
151
+ protected abstract _schemaShapeObject(schema?: ISchemaShape): any;
152
+
153
+ protected abstract _arrayShapeObject(schema: IArrayShape): any;
154
+
155
+ protected abstract _tupleShapeObject(schema?: ITupleShape): any;
156
+
157
+ protected abstract _anyShapeObject(schema: IAnyShape): any;
158
+
159
+ protected abstract _propertyShapeObject(schema: IPropertyShape): any;
160
+ }
@@ -0,0 +1,422 @@
1
+ import { AmfNamespace as ns } from "../definitions/Namespace.js";
2
+ import { ShapeBase } from './ShapeBase.js';
3
+ import { JsonDataNodeGenerator } from '../data-node/JsonDataNodeGenerator.js';
4
+ import { IAnyShape, IArrayShape, IDataExample, IDataNodeUnion, INodeShape, IPropertyShape, IScalarShape, IShapeUnion, ITupleShape, IUnionShape } from "../definitions/Shapes.js";
5
+
6
+ export class ShapeJsonSchemaGenerator extends ShapeBase {
7
+ /**
8
+ * Generates a schema from AMF's shape.
9
+ *
10
+ * @param schema The Shape definition
11
+ */
12
+ generate(schema: IShapeUnion): string | number | boolean | null | undefined {
13
+ const result = this.toObject(schema);
14
+ if (result !== null && typeof result === 'object') {
15
+ return this.serialize(result);
16
+ }
17
+ return result;
18
+ }
19
+
20
+ /**
21
+ * Processes the Shape definition and returns a JavaScript object or array.
22
+ */
23
+ toObject(schema: IShapeUnion): any {
24
+ const { types } = schema;
25
+ if (types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
26
+ return this._scalarShapeObject(schema as IScalarShape);
27
+ }
28
+ if (types.includes(ns.w3.shacl.NodeShape)) {
29
+ return this._nodeShapeObject(schema as INodeShape);
30
+ }
31
+ if (types.includes(ns.aml.vocabularies.shapes.UnionShape)) {
32
+ return this._unionShapeObject(schema as IUnionShape);
33
+ }
34
+ if (types.includes(ns.aml.vocabularies.shapes.FileShape)) {
35
+ return this._fileShapeObject();
36
+ }
37
+ if (types.includes(ns.aml.vocabularies.shapes.SchemaShape)) {
38
+ return this._schemaShapeObject();
39
+ }
40
+ if (types.includes(ns.aml.vocabularies.shapes.TupleShape)) {
41
+ return this._tupleShapeObject(schema as ITupleShape);
42
+ }
43
+ if (types.includes(ns.aml.vocabularies.shapes.ArrayShape) || types.includes(ns.aml.vocabularies.shapes.MatrixShape)) {
44
+ return this._arrayShapeObject(schema as IArrayShape);
45
+ }
46
+ return this._anyShapeObject(schema as IAnyShape);
47
+ }
48
+
49
+ /**
50
+ * Serializes generated JS value according to the mime type.
51
+ */
52
+ serialize(value: any): string {
53
+ return JSON.stringify(value, null, 2);
54
+ }
55
+
56
+ protected _scalarShapeObject(schema: IScalarShape): any|undefined {
57
+ return this._scalarValue(schema);
58
+ }
59
+
60
+ protected _nilShapeObject(): any|undefined {
61
+ return undefined;
62
+ }
63
+
64
+ protected _nodeShapeObject(schema: INodeShape): any {
65
+ const { inherits } = schema;
66
+ let { examples=[] } = schema;
67
+ if (Array.isArray(inherits) && inherits.length) {
68
+ inherits.forEach((parent) => {
69
+ const anyParent = parent as IAnyShape;
70
+ if (Array.isArray(anyParent.examples) && anyParent.examples.length) {
71
+ examples = examples.concat(anyParent.examples);
72
+ }
73
+ });
74
+ }
75
+ if (this.opts.renderExamples && examples.length) {
76
+ const example = examples.find((item) => !!item.structuredValue);
77
+ const value = this._exampleToObject(example);
78
+ if (value !== undefined) {
79
+ return value;
80
+ }
81
+ }
82
+ let result: any = {};
83
+ const { properties } = schema;
84
+ if (Array.isArray(inherits) && inherits.length) {
85
+ inherits.forEach(((s) => {
86
+ const part = this.toObject(s);
87
+ if (typeof part === 'object') {
88
+ result = { ...result, ...part };
89
+ }
90
+ }));
91
+ }
92
+ properties.forEach((property) => {
93
+ const { name } = property;
94
+ const value = this._propertyShapeObject(property);
95
+ if (typeof value !== 'undefined' && name) {
96
+ result[name] = value;
97
+ }
98
+ });
99
+ return result;
100
+ }
101
+
102
+ protected _unionShapeObject(schema: IUnionShape): any {
103
+ if (schema.and && schema.and.length) {
104
+ return this._allOfUnion(schema);
105
+ }
106
+ if (schema.xone && schema.xone.length) {
107
+ return this._oneOfUnion(schema);
108
+ }
109
+ // the default
110
+ return this._anyOfUnion(schema);
111
+ }
112
+
113
+ protected _anyOfUnion(schema: IUnionShape): any {
114
+ let { anyOf=[], examples=[] } = schema;
115
+ if (Array.isArray(schema.inherits) && schema.inherits) {
116
+ schema.inherits.forEach((parent) => {
117
+ const anyParent = parent as IAnyShape;
118
+ if (Array.isArray(anyParent.examples) && anyParent.examples.length) {
119
+ examples = examples.concat(anyParent.examples);
120
+ }
121
+ const typed = parent as IUnionShape;
122
+ if (Array.isArray(typed.anyOf) && typed.anyOf.length) {
123
+ anyOf = anyOf.concat(typed.anyOf);
124
+ }
125
+ });
126
+ }
127
+ const { opts } = this;
128
+ if (this._isNotRequiredUnion(anyOf)) {
129
+ // This generates schema for required values.
130
+ // This implicitly mean that the property is not required therefore the value should
131
+ // not be generated.
132
+ return undefined;
133
+ }
134
+ if (this.opts.renderExamples) {
135
+ const example = examples.find((item) => !!item.structuredValue);
136
+ const value = this._exampleToObject(example);
137
+ if (value !== undefined) {
138
+ return value;
139
+ }
140
+ }
141
+ if (schema.defaultValue) {
142
+ return this._unionDefaultValue(anyOf, schema.defaultValue);
143
+ }
144
+ const { selectedUnions } = opts;
145
+ let renderedItem: IShapeUnion | undefined;
146
+ if (selectedUnions && selectedUnions.length) {
147
+ renderedItem = anyOf.find((item) => selectedUnions.includes(item.id));
148
+ } else {
149
+ [renderedItem] = anyOf;
150
+ }
151
+ if (renderedItem) {
152
+ return this.toObject(renderedItem);
153
+ }
154
+ return undefined;
155
+ }
156
+
157
+ protected _oneOfUnion(schema: IUnionShape): any {
158
+ let { xone=[], examples=[] } = schema;
159
+ if (Array.isArray(schema.inherits) && schema.inherits) {
160
+ schema.inherits.forEach((parent) => {
161
+ const anyParent = parent as IAnyShape;
162
+ if (Array.isArray(anyParent.examples) && anyParent.examples.length) {
163
+ examples = examples.concat(anyParent.examples);
164
+ }
165
+ });
166
+ }
167
+ const { opts } = this;
168
+ if (this.opts.renderExamples) {
169
+ const example = examples.find((item) => !!item.structuredValue);
170
+ const value = this._exampleToObject(example);
171
+ if (value !== undefined) {
172
+ return value;
173
+ }
174
+ }
175
+ if (schema.defaultValue) {
176
+ return this._unionDefaultValue(xone, schema.defaultValue);
177
+ }
178
+ const { selectedUnions } = opts;
179
+ let renderedItem: IShapeUnion | undefined;
180
+ if (selectedUnions && selectedUnions.length) {
181
+ renderedItem = xone.find((item) => selectedUnions.includes(item.id));
182
+ } else {
183
+ [renderedItem] = xone;
184
+ }
185
+ if (renderedItem) {
186
+ return this.toObject(renderedItem);
187
+ }
188
+ return undefined;
189
+ }
190
+
191
+ /**
192
+ * Combines all properties from both all shapes in the union
193
+ */
194
+ protected _allOfUnion(schema: IUnionShape): any {
195
+ let { examples=[], and=[] } = schema;
196
+ if (Array.isArray(schema.inherits) && schema.inherits) {
197
+ schema.inherits.forEach((parent) => {
198
+ const anyParent = parent as IAnyShape;
199
+ if (Array.isArray(anyParent.examples) && anyParent.examples.length) {
200
+ examples = examples.concat(anyParent.examples);
201
+ }
202
+ });
203
+ }
204
+ if (this.opts.renderExamples) {
205
+ const example = examples.find((item) => !!item.structuredValue);
206
+ const value = this._exampleToObject(example);
207
+ if (value !== undefined) {
208
+ return value;
209
+ }
210
+ }
211
+ let result: any = {};
212
+ and.forEach((item) => {
213
+ const props = this.toObject(item);
214
+ if (typeof props === 'object') {
215
+ result = { ...result, ...props };
216
+ }
217
+ });
218
+ return result;
219
+ }
220
+
221
+ /**
222
+ * @param union The list of unions in the shape
223
+ * @param defaultValue The definition of a default value.
224
+ */
225
+ protected _unionDefaultValue(union: IShapeUnion[], defaultValue: IDataNodeUnion): any | undefined {
226
+ const gen = new JsonDataNodeGenerator();
227
+ const result = gen.generate(defaultValue);
228
+ let hasNumber = false;
229
+ let hasBoolean = false;
230
+ let hasNil = false;
231
+ union.forEach((i) => {
232
+ if (i.types.includes(ns.aml.vocabularies.shapes.NilShape)) {
233
+ hasNil = true;
234
+ }
235
+ if (!i.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
236
+ return;
237
+ }
238
+ const scalar = i as IScalarShape;
239
+ if (!hasBoolean) {
240
+ hasBoolean = scalar.dataType === ns.w3.xmlSchema.boolean;
241
+ }
242
+ if (!hasNumber) {
243
+ hasNumber = [
244
+ ns.w3.xmlSchema.number,
245
+ ns.w3.xmlSchema.long,
246
+ ns.w3.xmlSchema.integer,
247
+ ns.w3.xmlSchema.float,
248
+ ns.w3.xmlSchema.double,
249
+ ns.aml.vocabularies.shapes.number,
250
+ ns.aml.vocabularies.shapes.long,
251
+ ns.aml.vocabularies.shapes.integer,
252
+ ns.aml.vocabularies.shapes.float,
253
+ ns.aml.vocabularies.shapes.double,
254
+ ].includes(scalar.dataType!);
255
+ }
256
+ if (!hasNil) {
257
+ hasNil = scalar.dataType === ns.w3.xmlSchema.nil;
258
+ }
259
+ });
260
+ if (hasNumber) {
261
+ const parsed = Number(result);
262
+ if (!Number.isNaN(parsed)) {
263
+ return parsed;
264
+ }
265
+ }
266
+ if (hasBoolean) {
267
+ if (result === 'true') {
268
+ return true;
269
+ }
270
+ if (result === 'false') {
271
+ return false;
272
+ }
273
+ }
274
+ if (hasNil && (result === 'null' || result === 'nil')) {
275
+ return null;
276
+ }
277
+ return result;
278
+ }
279
+
280
+ protected _arrayShapeObject(schema: IArrayShape): any[] {
281
+ const { items } = schema;
282
+ const defaultValue = schema.defaultValue || items && items.defaultValue;
283
+
284
+ let { examples=[] } = schema;
285
+ const anyItems = items as IAnyShape;
286
+ if (Array.isArray(anyItems.examples)) {
287
+ examples = examples.concat(anyItems.examples);
288
+ }
289
+ if (this.opts.renderExamples && examples && examples.length) {
290
+ const example = examples.find((item) => !!item.structuredValue);
291
+ const value = this._exampleToObject(example);
292
+ if (Array.isArray(value)) {
293
+ return value;
294
+ }
295
+ if (typeof value !== 'undefined') {
296
+ return [value];
297
+ }
298
+ }
299
+ if (defaultValue) {
300
+ const gen = new JsonDataNodeGenerator();
301
+ const arr = gen.processNode(defaultValue);
302
+ if (Array.isArray(arr)) {
303
+ return arr;
304
+ } else if (arr) {
305
+ return [arr];
306
+ }
307
+ }
308
+ if (items) {
309
+ const value = this.toObject(items);
310
+ if (typeof value !== 'undefined') {
311
+ return [value];
312
+ }
313
+ }
314
+ return [];
315
+ }
316
+
317
+ protected _tupleShapeObject(schema: ITupleShape): any {
318
+ const { items, examples } = schema;
319
+ if (this.opts.renderExamples && examples && examples.length) {
320
+ const example = examples.find((item) => !!item.structuredValue);
321
+ const value = this._exampleToObject(example);
322
+ if (typeof value !== 'undefined') {
323
+ return [value];
324
+ }
325
+ }
326
+ if (schema.defaultValue) {
327
+ const gen = new JsonDataNodeGenerator();
328
+ const arr = gen.processNode(schema.defaultValue);
329
+ if (Array.isArray(arr)) {
330
+ return arr;
331
+ }
332
+ }
333
+ if (items.length) {
334
+ const result: any[] = [];
335
+ items.forEach((i) => {
336
+ const value = this.toObject(i);
337
+ if (typeof value !== 'undefined') {
338
+ result.push(value);
339
+ }
340
+ });
341
+ return result;
342
+ }
343
+ return [];
344
+ }
345
+
346
+ protected _anyShapeObject(schema: IAnyShape): any {
347
+ const { and=[], xone=[], or=[] } = schema;
348
+ if (and.length) {
349
+ let result: any = {};
350
+ and.forEach((item) => {
351
+ const props = this.toObject(item);
352
+ if (typeof props === 'object') {
353
+ result = { ...result, ...props };
354
+ }
355
+ });
356
+ return result;
357
+ }
358
+ if (xone.length) {
359
+ const { selectedUnions=[] } = this.opts;
360
+ let selected = xone.find(i => selectedUnions.includes(i.id));
361
+ if (!selected) {
362
+ // select firs available
363
+ selected = xone[0];
364
+ }
365
+ return this.toObject(selected);
366
+ }
367
+ if (or.length) {
368
+ const { selectedUnions=[] } = this.opts;
369
+ let selected = or.find(i => selectedUnions.includes(i.id));
370
+ if (!selected) {
371
+ // select firs available
372
+ selected = or[0];
373
+ }
374
+ return this.toObject(selected);
375
+ }
376
+ return this._scalarShapeObject(schema);
377
+ }
378
+
379
+ /**
380
+ * @returns The value for the property or undefined when cannot generate the value.
381
+ */
382
+ protected _propertyShapeObject(schema: IPropertyShape): any | undefined {
383
+ const { minCount=0 } = schema;
384
+ if (minCount === 0 && !this.opts.renderOptional) {
385
+ return undefined;
386
+ }
387
+ const range = schema.range!;
388
+ const { types } = range;
389
+ if (types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
390
+ // const defaultValue = schema.defaultValue || range.defaultValue;
391
+ // if (!this.opts.renderExamples && defaultValue) {
392
+ // const gen = new JsonDataNodeGenerator();
393
+ // const value = gen.generate(defaultValue);
394
+ // if (value) {
395
+ // return ApiSchemaValues.readTypedValue(value, /** @type IScalarShape */ (range).dataType);
396
+ // }
397
+ // }
398
+ const anyRange = range as IAnyShape;
399
+ return this._scalarShapeObject(anyRange);
400
+ }
401
+ return this.toObject(range);
402
+ }
403
+
404
+ /**
405
+ * @param example The example to turn into a JS object
406
+ */
407
+ protected _exampleToObject(example?: IDataExample): any {
408
+ if (example && example.structuredValue) {
409
+ const jsonGenerator = new JsonDataNodeGenerator();
410
+ return jsonGenerator.processNode(example.structuredValue);
411
+ }
412
+ return undefined;
413
+ }
414
+
415
+ protected _fileShapeObject(): any {
416
+ return undefined
417
+ }
418
+
419
+ protected _schemaShapeObject(): any {
420
+ return undefined;
421
+ }
422
+ }