@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,109 @@
1
+ import { AmfNamespace as ns } from "./definitions/Namespace.js";
2
+ import { JsonDataNodeGenerator } from './data-node/JsonDataNodeGenerator.js';
3
+ import { XmlDataNodeGenerator } from './data-node/XmlDataNodeGenerator.js';
4
+ import { UrlEncodedDataNodeGenerator } from './data-node/UrlEncodedDataNodeGenerator.js';
5
+ import { formatXmlValue } from './Utils.js';
6
+ /**
7
+ * A class that processes AMF's Example object to read the example value
8
+ * or to generate the example for the given media type.
9
+ */
10
+ export class ApiExampleGenerator {
11
+ /**
12
+ * Reads or generates an example.
13
+ * When the `mime` is set then it tries to "guess" whether the mime type corresponds to the value.
14
+ * If it doesn't then it generates the example from the structured value, when possible.
15
+ * @param example The structured value of the example
16
+ * @param mime The optional mime type of the example. When not set it won't generate example from the structured value.
17
+ * @param shape The optional shape containing this example to use with the XML examples which needs wrapping into an element.
18
+ * @returns The read or generated example.
19
+ */
20
+ read(example, mime, shape) {
21
+ const { value, structuredValue } = example;
22
+ if (!value && !structuredValue) {
23
+ return undefined;
24
+ }
25
+ if (structuredValue && !value && mime) {
26
+ return this.fromStructuredValue(mime, structuredValue, shape);
27
+ }
28
+ if (!mime) {
29
+ return value;
30
+ }
31
+ if (this.mimeMatches(mime, value)) {
32
+ return value;
33
+ }
34
+ if (structuredValue) {
35
+ return this.fromStructuredValue(mime, structuredValue, shape);
36
+ }
37
+ return undefined;
38
+ }
39
+ /**
40
+ * Employs some basic heuristics to determine whether the given mime type patches the content.
41
+ * @param mime The mime type for the value.
42
+ * @param value The value.
43
+ * @returns True when the value matches the mime type.
44
+ */
45
+ mimeMatches(mime, value) {
46
+ const trimmed = String(value).trim();
47
+ if (mime.includes('json')) {
48
+ // JSON string has to start with either of these characters
49
+ return trimmed[0] === '{' || trimmed[0] === '[';
50
+ }
51
+ if (mime.includes('xml')) {
52
+ return trimmed.startsWith('<');
53
+ }
54
+ if (mime.includes('x-www-form-urlencoded')) {
55
+ return trimmed.includes('=') || trimmed.includes('&');
56
+ }
57
+ return true;
58
+ }
59
+ /**
60
+ * Generates the example for the given structured value and the media type.
61
+ * @param mime The mime type for the value.
62
+ * @param structuredValue The structuredValue of the example.
63
+ * @param shape The optional shape containing this example to use with the XML examples which needs wrapping into an element.
64
+ * @returns The generated example or null if couldn't process the data.
65
+ */
66
+ fromStructuredValue(mime, structuredValue, shape) {
67
+ if (mime.includes('json')) {
68
+ const generator = new JsonDataNodeGenerator();
69
+ return generator.generate(structuredValue);
70
+ }
71
+ let shapeName;
72
+ if (shape && shape.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
73
+ shapeName = shape.name;
74
+ }
75
+ else if (shape && shape.types.includes(ns.aml.vocabularies.shapes.ArrayShape)) {
76
+ const typed = shape;
77
+ if (typed.items && typed.items.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
78
+ shapeName = typed.items.name || shape.name;
79
+ }
80
+ }
81
+ if (mime.includes('xml')) {
82
+ const generator = new XmlDataNodeGenerator();
83
+ let value = generator.generate(structuredValue, shapeName);
84
+ if (shape && !shapeName) {
85
+ value = this.wrapXmlValue(value, shape);
86
+ }
87
+ return value;
88
+ }
89
+ if (mime.includes('x-www-form-urlencoded')) {
90
+ const generator = new UrlEncodedDataNodeGenerator();
91
+ return generator.generate(structuredValue, shapeName);
92
+ }
93
+ return undefined;
94
+ }
95
+ /**
96
+ * Wraps the generated XML example into an element according to the `shape` properties.
97
+ */
98
+ wrapXmlValue(value, shape) {
99
+ if (!value || !shape) {
100
+ return value;
101
+ }
102
+ const { name } = shape;
103
+ const parts = [`<${name}>`];
104
+ parts.push(formatXmlValue(' ', value.trim()));
105
+ parts.push(`</${name}>`);
106
+ return parts.join('\n');
107
+ }
108
+ }
109
+ //# sourceMappingURL=ApiExampleGenerator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiExampleGenerator.js","sourceRoot":"","sources":["../../../src/amf/ApiExampleGenerator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,IAAI,EAAE,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAC7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,EAAE,2BAA2B,EAAE,MAAM,4CAA4C,CAAC;AACzF,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAG5C;;;GAGG;AACH,MAAM,OAAO,mBAAmB;IAC9B;;;;;;;;OAQG;IACH,IAAI,CAAC,OAAqB,EAAE,IAAa,EAAE,KAAmB;QAC5D,MAAM,EAAE,KAAK,EAAE,eAAe,EAAE,GAAG,OAAO,CAAC;QAC3C,IAAI,CAAC,KAAK,IAAI,CAAC,eAAe,EAAE;YAC9B,OAAO,SAAS,CAAC;SAClB;QACD,IAAI,eAAe,IAAI,CAAC,KAAK,IAAI,IAAI,EAAE;YACrC,OAAO,IAAI,CAAC,mBAAmB,CAAC,IAAI,EAAE,eAAe,EAAE,KAAK,CAAC,CAAC;SAC/D;QACD,IAAI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QACvB,MAAM,KAAK,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,CAAC;QAC5B,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;QAC/C,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,GAAG,CAAC,CAAC;QACzB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;CACF"}
@@ -0,0 +1,67 @@
1
+ import { IArrayShape, INodeShape, IPropertyShape, IScalarShape, IShapeUnion } from "./definitions/Shapes.js";
2
+ export interface IMonacoSchema {
3
+ uri: string;
4
+ schema: IMonacoProperty;
5
+ fileMatch?: string[];
6
+ }
7
+ export interface IMonacoProperty {
8
+ $id?: string;
9
+ title: string;
10
+ type: string;
11
+ description?: string;
12
+ readOnly?: boolean;
13
+ writeOnly?: boolean;
14
+ }
15
+ export interface IMonacoScalarProperty extends IMonacoProperty {
16
+ default?: string;
17
+ pattern?: string;
18
+ format?: string;
19
+ exclusiveMaximum?: boolean;
20
+ exclusiveMinimum?: boolean;
21
+ maxLength?: number;
22
+ minLength?: number;
23
+ minimum?: number;
24
+ maximum?: number;
25
+ multipleOf?: number;
26
+ enum?: string[];
27
+ }
28
+ export interface IMonacoObjectProperty extends IMonacoProperty {
29
+ properties: Record<string, IMonacoProperty>;
30
+ required: string[];
31
+ additionalProperties?: boolean;
32
+ minProperties?: number;
33
+ maxProperties?: number;
34
+ }
35
+ export interface IMonacoArrayProperty extends IMonacoProperty {
36
+ additionalItems?: boolean;
37
+ items: {
38
+ anyOf: IMonacoProperty[];
39
+ };
40
+ uniqueItems?: boolean;
41
+ minItems?: number;
42
+ maxItems?: number;
43
+ required: string[];
44
+ }
45
+ /**
46
+ * A class to generate JSON schema from an ApiShapeUnion declaration to use with the Monaco editor schemas.
47
+ */
48
+ export declare class ApiMonacoSchemaGenerator {
49
+ schemas: IMonacoSchema[];
50
+ /**
51
+ * @param parentUri The URI for the fileMatch property.
52
+ */
53
+ generate(schema: IShapeUnion, parentUri: string): IMonacoSchema[];
54
+ /**
55
+ * @param parentUri The URI for the fileMatch property.
56
+ */
57
+ fromNodeShape(schema: INodeShape, parentUri?: string): IMonacoSchema[];
58
+ appendSchemaProperty(content: IMonacoObjectProperty, property: IPropertyShape): void;
59
+ rangeToPropertySchema(range: IShapeUnion): IMonacoScalarProperty | IMonacoObjectProperty | IMonacoArrayProperty | undefined;
60
+ scalarRangeToPropertySchema(schema: IScalarShape): IMonacoScalarProperty;
61
+ /**
62
+ * Translates AMF data type to JSON schema data type.
63
+ */
64
+ schemaTypeToJsonDataType(schemaType?: string): string;
65
+ nodeShapeRangeToPropertySchema(schema: INodeShape): IMonacoObjectProperty;
66
+ arrayShapeRangeToPropertySchema(schema: IArrayShape): IMonacoArrayProperty;
67
+ }
@@ -0,0 +1,243 @@
1
+ import { AmfNamespace as ns } from "./definitions/Namespace.js";
2
+ import { collectNodeProperties } from './Utils.js';
3
+ function cleanName(name) {
4
+ if (!name) {
5
+ return '';
6
+ }
7
+ return name.replace('?', '');
8
+ }
9
+ /**
10
+ * A class to generate JSON schema from an ApiShapeUnion declaration to use with the Monaco editor schemas.
11
+ */
12
+ export class ApiMonacoSchemaGenerator {
13
+ schemas = [];
14
+ /**
15
+ * @param parentUri The URI for the fileMatch property.
16
+ */
17
+ generate(schema, parentUri) {
18
+ this.schemas = [];
19
+ if (!schema) {
20
+ return [];
21
+ }
22
+ const { types } = schema;
23
+ if (types.includes(ns.w3.shacl.NodeShape)) {
24
+ return this.fromNodeShape(schema, parentUri);
25
+ }
26
+ return [];
27
+ }
28
+ /**
29
+ * @param parentUri The URI for the fileMatch property.
30
+ */
31
+ fromNodeShape(schema, parentUri) {
32
+ const { id, name } = schema;
33
+ const properties = collectNodeProperties(schema);
34
+ const content = {
35
+ title: cleanName(name),
36
+ type: "object",
37
+ properties: {},
38
+ required: [],
39
+ };
40
+ const result = {
41
+ uri: id,
42
+ schema: content,
43
+ };
44
+ if (parentUri) {
45
+ result.fileMatch = [parentUri];
46
+ }
47
+ this.schemas.push(result);
48
+ if (!Array.isArray(properties) || !properties.length) {
49
+ return this.schemas;
50
+ }
51
+ properties.forEach(property => this.appendSchemaProperty(content, property));
52
+ return this.schemas;
53
+ }
54
+ appendSchemaProperty(content, property) {
55
+ const { name, range, minCount } = property;
56
+ if (!range) {
57
+ return;
58
+ }
59
+ const value = this.rangeToPropertySchema(range);
60
+ if (value && name) {
61
+ content.properties[name] = value;
62
+ if (minCount === 1) {
63
+ content.required.push(name);
64
+ }
65
+ }
66
+ }
67
+ rangeToPropertySchema(range) {
68
+ const { types } = range;
69
+ if (types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
70
+ return this.scalarRangeToPropertySchema(range);
71
+ }
72
+ if (types.includes(ns.w3.shacl.NodeShape)) {
73
+ return this.nodeShapeRangeToPropertySchema(range);
74
+ }
75
+ if (types.includes(ns.aml.vocabularies.shapes.ArrayShape)) {
76
+ return this.arrayShapeRangeToPropertySchema(range);
77
+ }
78
+ return undefined;
79
+ }
80
+ scalarRangeToPropertySchema(schema) {
81
+ const { values, description, name, displayName, defaultValueStr, exclusiveMaximum, exclusiveMinimum, minimum, maximum, minLength, maxLength, id, multipleOf, pattern, readOnly, writeOnly } = schema;
82
+ const type = this.schemaTypeToJsonDataType(schema.dataType);
83
+ const result = {
84
+ '$id': id,
85
+ type,
86
+ title: cleanName(displayName || name),
87
+ };
88
+ if (description) {
89
+ result.description = description;
90
+ }
91
+ if (defaultValueStr) {
92
+ result.default = defaultValueStr;
93
+ }
94
+ if (typeof exclusiveMaximum === 'boolean') {
95
+ result.exclusiveMaximum = exclusiveMaximum;
96
+ }
97
+ if (typeof exclusiveMinimum === 'boolean') {
98
+ result.exclusiveMinimum = exclusiveMinimum;
99
+ }
100
+ if (typeof maxLength === 'number') {
101
+ result.maxLength = maxLength;
102
+ }
103
+ if (typeof minLength === 'number') {
104
+ result.minLength = minLength;
105
+ }
106
+ if (typeof minimum === 'number') {
107
+ result.minimum = minimum;
108
+ }
109
+ if (typeof maximum === 'number') {
110
+ result.maximum = maximum;
111
+ }
112
+ if (typeof multipleOf === 'number') {
113
+ result.multipleOf = multipleOf;
114
+ }
115
+ if (typeof pattern === 'string') {
116
+ result.pattern = pattern;
117
+ }
118
+ if (typeof readOnly === 'boolean') {
119
+ result.readOnly = readOnly;
120
+ }
121
+ if (typeof writeOnly === 'boolean') {
122
+ result.writeOnly = writeOnly;
123
+ }
124
+ switch (schema.dataType) {
125
+ case ns.aml.vocabularies.shapes.dateTimeOnly:
126
+ result.format = 'date-time';
127
+ break;
128
+ case ns.w3.xmlSchema.date:
129
+ result.format = 'date';
130
+ break;
131
+ case ns.w3.xmlSchema.time:
132
+ result.format = 'time';
133
+ break;
134
+ default:
135
+ }
136
+ if (Array.isArray(values) && values.length) {
137
+ // enum properties
138
+ result.enum = [];
139
+ values.forEach((value) => {
140
+ const { types } = value;
141
+ if (types.includes(ns.aml.vocabularies.data.Scalar)) {
142
+ const typed = value;
143
+ if (typed.value) {
144
+ result.enum.push(typed.value);
145
+ }
146
+ }
147
+ });
148
+ }
149
+ return result;
150
+ }
151
+ /**
152
+ * Translates AMF data type to JSON schema data type.
153
+ */
154
+ schemaTypeToJsonDataType(schemaType) {
155
+ switch (schemaType) {
156
+ case ns.aml.vocabularies.shapes.number:
157
+ case ns.aml.vocabularies.shapes.integer:
158
+ case ns.aml.vocabularies.shapes.float:
159
+ case ns.aml.vocabularies.shapes.long:
160
+ case ns.aml.vocabularies.shapes.double:
161
+ case ns.w3.xmlSchema.number:
162
+ case ns.w3.xmlSchema.integer:
163
+ case ns.w3.xmlSchema.float:
164
+ case ns.w3.xmlSchema.long:
165
+ case ns.w3.xmlSchema.double: return 'number';
166
+ case ns.w3.xmlSchema.boolean:
167
+ case ns.aml.vocabularies.shapes.boolean: return 'boolean';
168
+ case ns.aml.vocabularies.shapes.nil:
169
+ case ns.w3.xmlSchema.nil: return 'null';
170
+ default: return 'string';
171
+ }
172
+ }
173
+ nodeShapeRangeToPropertySchema(schema) {
174
+ const { description, name, displayName, id, readOnly, writeOnly, closed, minProperties, maxProperties } = schema;
175
+ const properties = collectNodeProperties(schema);
176
+ const result = {
177
+ '$id': id,
178
+ type: 'object',
179
+ title: cleanName(displayName || name),
180
+ properties: {},
181
+ required: [],
182
+ };
183
+ if (description) {
184
+ result.description = description;
185
+ }
186
+ if (typeof readOnly === 'boolean') {
187
+ result.readOnly = readOnly;
188
+ }
189
+ if (typeof writeOnly === 'boolean') {
190
+ result.writeOnly = writeOnly;
191
+ }
192
+ if (typeof closed === 'boolean') {
193
+ result.additionalProperties = !closed;
194
+ }
195
+ if (typeof minProperties === 'number') {
196
+ result.minProperties = minProperties;
197
+ }
198
+ if (typeof maxProperties === 'number') {
199
+ result.maxProperties = maxProperties;
200
+ }
201
+ properties.forEach(property => this.appendSchemaProperty(result, property));
202
+ return result;
203
+ }
204
+ arrayShapeRangeToPropertySchema(schema) {
205
+ const { description, name, displayName, id, readOnly, writeOnly, items, minItems, maxItems, uniqueItems } = schema;
206
+ const result = {
207
+ '$id': id,
208
+ type: 'array',
209
+ title: cleanName(displayName || name),
210
+ items: {
211
+ anyOf: [],
212
+ },
213
+ required: [],
214
+ additionalItems: false,
215
+ };
216
+ if (description) {
217
+ result.description = description;
218
+ }
219
+ if (typeof readOnly === 'boolean') {
220
+ result.readOnly = readOnly;
221
+ }
222
+ if (typeof writeOnly === 'boolean') {
223
+ result.writeOnly = writeOnly;
224
+ }
225
+ if (typeof uniqueItems === 'boolean') {
226
+ result.uniqueItems = uniqueItems;
227
+ }
228
+ if (items) {
229
+ const value = this.rangeToPropertySchema(items);
230
+ if (value) {
231
+ result.items.anyOf.push(value);
232
+ }
233
+ }
234
+ if (typeof minItems === 'number') {
235
+ result.minItems = minItems;
236
+ }
237
+ if (typeof maxItems === 'number') {
238
+ result.maxItems = maxItems;
239
+ }
240
+ return result;
241
+ }
242
+ }
243
+ //# sourceMappingURL=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,EAAE;YAChC,MAAM,CAAC,QAAQ,GAAG,QAAQ,CAAC;SAC5B;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
@@ -0,0 +1,55 @@
1
+ import { IDataExample, IShapeUnion } from "./definitions/Shapes.js";
2
+ import { ShapeBase, ShapeRenderOptions } from "./shape/ShapeBase.js";
3
+ export interface ISchemaExample extends IDataExample {
4
+ /**
5
+ * The value to render as the example value.
6
+ */
7
+ renderValue?: string | number | boolean | null | undefined;
8
+ label?: string;
9
+ }
10
+ /**
11
+ * A class that processes AMF's Shape to auto-generate a schema from examples/type for a given media type.
12
+ * This should be used when examples for the Shape are not available but the application still needs to
13
+ * render an example or a schema from the Shape.
14
+ * If examples can be found directly in the shape, use the `ApiExampleGenerator` instead.
15
+ */
16
+ export declare class ApiSchemaGenerator {
17
+ mime: string;
18
+ opts: Readonly<ShapeRenderOptions>;
19
+ generator?: ShapeBase;
20
+ /**
21
+ *
22
+ * @param mime The example mime type to format the generated example.
23
+ * @param opts Optional configuration.
24
+ */
25
+ constructor(mime: string, opts?: ShapeRenderOptions);
26
+ /**
27
+ * @param shape The Shape definition
28
+ * @param mime The mime type for the value.
29
+ * @returns Customized Example with the `renderValue` that is the generated Example value.
30
+ */
31
+ static asExample(shape: IShapeUnion, mime: string, opts?: ShapeRenderOptions): ISchemaExample | undefined;
32
+ /**
33
+ * @param shape The Shape definition
34
+ * @param mime The mime type for the value.
35
+ * @returns The generated schema
36
+ */
37
+ static asSchema(shape: IShapeUnion, mime: string, opts?: ShapeRenderOptions): string | number | boolean | null | undefined;
38
+ /**
39
+ * Generates the schema from the AMF shape.
40
+ *
41
+ * @param shape The Shape definition
42
+ */
43
+ generate(shape: IShapeUnion): string | number | boolean | null | undefined;
44
+ /**
45
+ * @link {#generate()}
46
+ * @param shape The Shape definition
47
+ */
48
+ toValue(shape: IShapeUnion): string | number | boolean | null | undefined;
49
+ /**
50
+ * Generates an API Example object with the value to render.
51
+ * @param shape The Shape definition
52
+ * @returns Customized Example with the `renderValue` that is the generated Example value.
53
+ */
54
+ toExample(shape: IShapeUnion): ISchemaExample | undefined;
55
+ }
@@ -0,0 +1,94 @@
1
+ import { AmfNamespace as ns } from "./definitions/Namespace.js";
2
+ import { ShapeJsonSchemaGenerator } from './shape/ShapeJsonSchemaGenerator.js';
3
+ import { ShapeXmlSchemaGenerator } from './shape/ShapeXmlSchemaGenerator.js';
4
+ /**
5
+ * A class that processes AMF's Shape to auto-generate a schema from examples/type for a given media type.
6
+ * This should be used when examples for the Shape are not available but the application still needs to
7
+ * render an example or a schema from the Shape.
8
+ * If examples can be found directly in the shape, use the `ApiExampleGenerator` instead.
9
+ */
10
+ export class ApiSchemaGenerator {
11
+ mime;
12
+ opts;
13
+ generator;
14
+ /**
15
+ *
16
+ * @param mime The example mime type to format the generated example.
17
+ * @param opts Optional configuration.
18
+ */
19
+ constructor(mime, opts = {}) {
20
+ this.mime = mime;
21
+ this.opts = Object.freeze({ ...opts });
22
+ if (mime.includes('json')) {
23
+ this.generator = new ShapeJsonSchemaGenerator(opts);
24
+ }
25
+ else if (mime.includes('xml')) {
26
+ this.generator = new ShapeXmlSchemaGenerator(opts);
27
+ }
28
+ }
29
+ /**
30
+ * @param shape The Shape definition
31
+ * @param mime The mime type for the value.
32
+ * @returns Customized Example with the `renderValue` that is the generated Example value.
33
+ */
34
+ static asExample(shape, mime, opts) {
35
+ const generator = new ApiSchemaGenerator(mime, opts);
36
+ return generator.toExample(shape);
37
+ }
38
+ /**
39
+ * @param shape The Shape definition
40
+ * @param mime The mime type for the value.
41
+ * @returns The generated schema
42
+ */
43
+ static asSchema(shape, mime, opts) {
44
+ const generator = new ApiSchemaGenerator(mime, opts);
45
+ return generator.toValue(shape);
46
+ }
47
+ /**
48
+ * Generates the schema from the AMF shape.
49
+ *
50
+ * @param shape The Shape definition
51
+ */
52
+ generate(shape) {
53
+ const { generator } = this;
54
+ if (!generator) {
55
+ return undefined;
56
+ }
57
+ return generator.generate(shape);
58
+ }
59
+ /**
60
+ * @link {#generate()}
61
+ * @param shape The Shape definition
62
+ */
63
+ toValue(shape) {
64
+ return this.generate(shape);
65
+ }
66
+ /**
67
+ * Generates an API Example object with the value to render.
68
+ * @param shape The Shape definition
69
+ * @returns Customized Example with the `renderValue` that is the generated Example value.
70
+ */
71
+ toExample(shape) {
72
+ const renderValue = this.generate(shape);
73
+ if (renderValue === null || renderValue === undefined) {
74
+ return undefined;
75
+ }
76
+ const result = {
77
+ id: `${shape.id}/generated`,
78
+ strict: true,
79
+ types: [ns.aml.vocabularies.apiContract.Example],
80
+ mediaType: this.mime,
81
+ renderValue,
82
+ customDomainProperties: [],
83
+ };
84
+ const typed = shape;
85
+ if (Array.isArray(typed.examples) && typed.examples.length) {
86
+ const [example] = typed.examples;
87
+ if (example.value) {
88
+ result.value = example.value;
89
+ }
90
+ }
91
+ return result;
92
+ }
93
+ }
94
+ //# sourceMappingURL=ApiSchemaGenerator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiSchemaGenerator.js","sourceRoot":"","sources":["../../../src/amf/ApiSchemaGenerator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,IAAI,EAAE,EAAE,MAAM,4BAA4B,CAAC;AAGhE,OAAO,EAAE,wBAAwB,EAAE,MAAM,qCAAqC,CAAC;AAC/E,OAAO,EAAE,uBAAuB,EAAE,MAAM,oCAAoC,CAAC;AAU7E;;;;;GAKG;AACH,MAAM,OAAO,kBAAkB;IAUV;IATnB,IAAI,CAA+B;IAEnC,SAAS,CAAa;IAEtB;;;;OAIG;IACH,YAAmB,IAAY,EAAE,OAA2B,EAAE;QAA3C,SAAI,GAAJ,IAAI,CAAQ;QAC7B,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,IAAI,EAAE,CAAC,CAAC;QACvC,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;YACzB,IAAI,CAAC,SAAS,GAAG,IAAI,wBAAwB,CAAC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