@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.
- package/build/browser.d.ts +9 -2
- package/build/browser.js +12 -2
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +9 -2
- package/build/index.js +12 -2
- package/build/index.js.map +1 -1
- package/build/src/amf/AmfMixin.d.ts +395 -0
- package/build/src/amf/AmfMixin.js +1146 -0
- package/build/src/amf/AmfMixin.js.map +1 -0
- package/build/src/amf/AmfSerializer.d.ts +136 -0
- package/build/src/amf/AmfSerializer.js +1913 -0
- package/build/src/amf/AmfSerializer.js.map +1 -0
- package/build/src/amf/AmfShapeGenerator.d.ts +85 -0
- package/build/src/amf/AmfShapeGenerator.js +385 -0
- package/build/src/amf/AmfShapeGenerator.js.map +1 -0
- package/build/src/amf/AmfTypes.d.ts +24 -0
- package/build/src/amf/AmfTypes.js +122 -0
- package/build/src/amf/AmfTypes.js.map +1 -0
- package/build/src/amf/ApiExampleGenerator.d.ts +36 -0
- package/build/src/amf/ApiExampleGenerator.js +109 -0
- package/build/src/amf/ApiExampleGenerator.js.map +1 -0
- package/build/src/amf/ApiMonacoSchemaGenerator.d.ts +67 -0
- package/build/src/amf/ApiMonacoSchemaGenerator.js +243 -0
- package/build/src/amf/ApiMonacoSchemaGenerator.js.map +1 -0
- package/build/src/amf/ApiSchemaGenerator.d.ts +55 -0
- package/build/src/amf/ApiSchemaGenerator.js +94 -0
- package/build/src/amf/ApiSchemaGenerator.js.map +1 -0
- package/build/src/amf/ApiSchemaValues.d.ts +98 -0
- package/build/src/amf/ApiSchemaValues.js +382 -0
- package/build/src/amf/ApiSchemaValues.js.map +1 -0
- package/build/src/amf/Utils.d.ts +41 -0
- package/build/src/amf/Utils.js +176 -0
- package/build/src/amf/Utils.js.map +1 -0
- package/build/src/amf/data-node/DataNodeBase.d.ts +31 -0
- package/build/src/amf/data-node/DataNodeBase.js +77 -0
- package/build/src/amf/data-node/DataNodeBase.js.map +1 -0
- package/build/src/amf/data-node/JsonDataNodeGenerator.d.ts +15 -0
- package/build/src/amf/data-node/JsonDataNodeGenerator.js +24 -0
- package/build/src/amf/data-node/JsonDataNodeGenerator.js.map +1 -0
- package/build/src/amf/data-node/UrlEncodedDataNodeGenerator.d.ts +13 -0
- package/build/src/amf/data-node/UrlEncodedDataNodeGenerator.js +42 -0
- package/build/src/amf/data-node/UrlEncodedDataNodeGenerator.js.map +1 -0
- package/build/src/amf/data-node/XmlDataNodeGenerator.d.ts +21 -0
- package/build/src/amf/data-node/XmlDataNodeGenerator.js +30 -0
- package/build/src/amf/data-node/XmlDataNodeGenerator.js.map +1 -0
- package/build/src/amf/definitions/Amf.d.ts +362 -0
- package/build/src/amf/definitions/Amf.js +2 -0
- package/build/src/amf/definitions/Amf.js.map +1 -0
- package/build/src/amf/definitions/Api.d.ts +381 -0
- package/build/src/amf/definitions/Api.js +2 -0
- package/build/src/amf/definitions/Api.js.map +1 -0
- package/build/src/amf/definitions/Base.d.ts +11 -0
- package/build/src/amf/definitions/Base.js +2 -0
- package/build/src/amf/definitions/Base.js.map +1 -0
- package/build/src/amf/definitions/Namespace.d.ts +311 -0
- package/build/src/amf/definitions/Namespace.js +330 -0
- package/build/src/amf/definitions/Namespace.js.map +1 -0
- package/build/src/amf/definitions/Shapes.d.ts +309 -0
- package/build/src/amf/definitions/Shapes.js +87 -0
- package/build/src/amf/definitions/Shapes.js.map +1 -0
- package/build/src/amf/models/AmfDataNode.d.ts +68 -0
- package/build/src/amf/models/AmfDataNode.js +188 -0
- package/build/src/amf/models/AmfDataNode.js.map +1 -0
- package/build/src/amf/shape/ShapeBase.d.ts +75 -0
- package/build/src/amf/shape/ShapeBase.js +90 -0
- package/build/src/amf/shape/ShapeBase.js.map +1 -0
- package/build/src/amf/shape/ShapeJsonSchemaGenerator.d.ts +46 -0
- package/build/src/amf/shape/ShapeJsonSchemaGenerator.js +406 -0
- package/build/src/amf/shape/ShapeJsonSchemaGenerator.js.map +1 -0
- package/build/src/amf/shape/ShapeXmlSchemaGenerator.d.ts +84 -0
- package/build/src/amf/shape/ShapeXmlSchemaGenerator.js +820 -0
- package/build/src/amf/shape/ShapeXmlSchemaGenerator.js.map +1 -0
- package/build/src/models/Thing.d.ts +17 -0
- package/build/src/models/Thing.js +19 -1
- package/build/src/models/Thing.js.map +1 -1
- package/build/src/models/data/Bindings.d.ts +161 -0
- package/build/src/models/data/Bindings.js +2 -0
- package/build/src/models/data/Bindings.js.map +1 -0
- package/build/src/models/data/DataAssociation.d.ts +135 -14
- package/build/src/models/data/DataAssociation.js +154 -21
- package/build/src/models/data/DataAssociation.js.map +1 -1
- package/build/src/models/data/DataAssociationSchema.d.ts +36 -0
- package/build/src/models/data/DataEntity.d.ts +76 -4
- package/build/src/models/data/DataEntity.js +136 -9
- package/build/src/models/data/DataEntity.js.map +1 -1
- package/build/src/models/data/DataFile.d.ts +3 -0
- package/build/src/models/data/DataFile.js +3 -0
- package/build/src/models/data/DataFile.js.map +1 -1
- package/build/src/models/data/DataModel.d.ts +1 -1
- package/build/src/models/data/DataModel.js.map +1 -1
- package/build/src/models/data/DataNamespace.d.ts +6 -0
- package/build/src/models/data/DataNamespace.js +11 -1
- package/build/src/models/data/DataNamespace.js.map +1 -1
- package/build/src/models/data/DataProperty.d.ts +131 -36
- package/build/src/models/data/DataProperty.js +200 -17
- package/build/src/models/data/DataProperty.js.map +1 -1
- package/data/apis/oas-date/oas-date.yaml +28 -0
- package/data/apis/oas-types/oas-types.yaml +159 -0
- package/data/apis/oas-unions/oas-unions.yaml +75 -0
- package/data/apis/raml-date/raml-date.raml +28 -0
- package/data/apis/recursive/recursive.raml +14 -0
- package/data/apis/schema-api/examples/person.json +14 -0
- package/data/apis/schema-api/examples/person.raml +14 -0
- package/data/apis/schema-api/examples/person.url.encoded +1 -0
- package/data/apis/schema-api/examples/person.xml +14 -0
- package/data/apis/schema-api/library/demo-types.raml +43 -0
- package/data/apis/schema-api/schema-api.raml +644 -0
- package/data/apis/schema-api/schemas/person.json +104 -0
- package/data/apis/schema-api/schemas/person.xsd +26 -0
- package/data/apis/schema-api/types/DemoPerson.raml +67 -0
- package/data/model.js +106 -0
- package/data/models/oas-date.json +637 -0
- package/data/models/oas-types.json +5352 -0
- package/data/models/oas-unions.json +1881 -0
- package/data/models/raml-date.json +1096 -0
- package/data/models/recursive.json +610 -0
- package/data/models/schema-api.json +37319 -0
- package/package.json +9 -6
- package/src/amf/AmfMixin.ts +1623 -0
- package/src/amf/AmfSerializer.ts +2028 -0
- package/src/amf/AmfShapeGenerator.ts +400 -0
- package/src/amf/AmfTypes.ts +126 -0
- package/src/amf/ApiExampleGenerator.ts +112 -0
- package/src/amf/ApiMonacoSchemaGenerator.ts +296 -0
- package/src/amf/ApiSchemaGenerator.ts +108 -0
- package/src/amf/ApiSchemaValues.ts +411 -0
- package/src/amf/Utils.ts +182 -0
- package/src/amf/data-node/DataNodeBase.ts +81 -0
- package/src/amf/data-node/JsonDataNodeGenerator.ts +26 -0
- package/src/amf/data-node/README.md +3 -0
- package/src/amf/data-node/UrlEncodedDataNodeGenerator.ts +43 -0
- package/src/amf/data-node/XmlDataNodeGenerator.ts +38 -0
- package/src/amf/definitions/Amf.ts +443 -0
- package/src/amf/definitions/Api.ts +427 -0
- package/src/amf/definitions/Base.ts +13 -0
- package/src/amf/definitions/Namespace.ts +341 -0
- package/src/amf/definitions/Shapes.ts +414 -0
- package/src/amf/models/AmfDataNode.ts +200 -0
- package/src/amf/shape/README.md +4 -0
- package/src/amf/shape/ShapeBase.ts +160 -0
- package/src/amf/shape/ShapeJsonSchemaGenerator.ts +422 -0
- package/src/amf/shape/ShapeXmlSchemaGenerator.ts +876 -0
- package/src/models/Thing.ts +25 -1
- package/src/models/data/Bindings.ts +186 -0
- package/src/models/data/DataAssociation.ts +226 -29
- package/src/models/data/DataAssociationSchema.ts +38 -0
- package/src/models/data/DataEntity.ts +170 -13
- package/src/models/data/DataFile.ts +3 -0
- package/src/models/data/DataModel.ts +1 -1
- package/src/models/data/DataNamespace.ts +16 -1
- package/src/models/data/DataProperty.ts +250 -47
- package/build/src/models/data/DataPropertySchema.d.ts +0 -125
- package/build/src/models/data/DataPropertySchema.js +0 -33
- package/build/src/models/data/DataPropertySchema.js.map +0 -1
- package/src/models/data/DataPropertySchema.ts +0 -156
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { AmfNamespace as ns } from "./definitions/Namespace.js";
|
|
2
|
+
import { IArrayShape, INodeShape, IPropertyShape, IScalarNode, IScalarShape, IShapeUnion } from "./definitions/Shapes.js";
|
|
3
|
+
import { collectNodeProperties } from './Utils.js';
|
|
4
|
+
|
|
5
|
+
export interface IMonacoSchema {
|
|
6
|
+
uri: string;
|
|
7
|
+
schema: IMonacoProperty;
|
|
8
|
+
fileMatch?: string[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface IMonacoProperty {
|
|
12
|
+
$id?: string;
|
|
13
|
+
title: string;
|
|
14
|
+
type: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
readOnly?: boolean;
|
|
17
|
+
writeOnly?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface IMonacoScalarProperty extends IMonacoProperty {
|
|
21
|
+
default?: string;
|
|
22
|
+
pattern?: string;
|
|
23
|
+
format?: string;
|
|
24
|
+
exclusiveMaximum?: boolean;
|
|
25
|
+
exclusiveMinimum?: boolean;
|
|
26
|
+
maxLength?: number;
|
|
27
|
+
minLength?: number;
|
|
28
|
+
minimum?: number;
|
|
29
|
+
maximum?: number;
|
|
30
|
+
multipleOf?: number;
|
|
31
|
+
enum?: string[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface IMonacoObjectProperty extends IMonacoProperty {
|
|
35
|
+
properties: Record<string, IMonacoProperty>;
|
|
36
|
+
required: string[];
|
|
37
|
+
additionalProperties?: boolean;
|
|
38
|
+
minProperties?: number;
|
|
39
|
+
maxProperties?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface IMonacoArrayProperty extends IMonacoProperty {
|
|
43
|
+
additionalItems?: boolean;
|
|
44
|
+
items: {
|
|
45
|
+
anyOf: IMonacoProperty[]
|
|
46
|
+
}
|
|
47
|
+
uniqueItems?: boolean;
|
|
48
|
+
minItems?: number;
|
|
49
|
+
maxItems?: number;
|
|
50
|
+
required: string[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function cleanName(name?: string): string {
|
|
54
|
+
if (!name) {
|
|
55
|
+
return '';
|
|
56
|
+
}
|
|
57
|
+
return name.replace('?', '');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A class to generate JSON schema from an ApiShapeUnion declaration to use with the Monaco editor schemas.
|
|
62
|
+
*/
|
|
63
|
+
export class ApiMonacoSchemaGenerator {
|
|
64
|
+
schemas: IMonacoSchema[] = [];
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param parentUri The URI for the fileMatch property.
|
|
68
|
+
*/
|
|
69
|
+
generate(schema: IShapeUnion, parentUri: string): IMonacoSchema[] {
|
|
70
|
+
this.schemas = [];
|
|
71
|
+
if (!schema) {
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
const { types } = schema;
|
|
75
|
+
if (types.includes(ns.w3.shacl.NodeShape)) {
|
|
76
|
+
return this.fromNodeShape(schema as INodeShape, parentUri);
|
|
77
|
+
}
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @param parentUri The URI for the fileMatch property.
|
|
83
|
+
*/
|
|
84
|
+
fromNodeShape(schema: INodeShape, parentUri?: string): IMonacoSchema[] {
|
|
85
|
+
const { id, name } = schema;
|
|
86
|
+
const properties = collectNodeProperties(schema);
|
|
87
|
+
const content: IMonacoObjectProperty = {
|
|
88
|
+
title: cleanName(name),
|
|
89
|
+
type: "object",
|
|
90
|
+
properties: {},
|
|
91
|
+
required: [],
|
|
92
|
+
};
|
|
93
|
+
const result: IMonacoSchema = {
|
|
94
|
+
uri: id,
|
|
95
|
+
schema: content,
|
|
96
|
+
};
|
|
97
|
+
if (parentUri) {
|
|
98
|
+
result.fileMatch = [parentUri];
|
|
99
|
+
}
|
|
100
|
+
this.schemas.push(result);
|
|
101
|
+
if (!Array.isArray(properties) || !properties.length) {
|
|
102
|
+
return this.schemas;
|
|
103
|
+
}
|
|
104
|
+
properties.forEach(property => this.appendSchemaProperty(content, property));
|
|
105
|
+
return this.schemas;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
appendSchemaProperty(content: IMonacoObjectProperty, property: IPropertyShape): void {
|
|
109
|
+
const { name, range, minCount } = property;
|
|
110
|
+
if (!range) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const value = this.rangeToPropertySchema(range);
|
|
114
|
+
if (value && name) {
|
|
115
|
+
content.properties[name] = value;
|
|
116
|
+
if (minCount === 1) {
|
|
117
|
+
content.required.push(name);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
rangeToPropertySchema(range: IShapeUnion): IMonacoScalarProperty | IMonacoObjectProperty | IMonacoArrayProperty | undefined {
|
|
123
|
+
const { types } = range;
|
|
124
|
+
if (types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
125
|
+
return this.scalarRangeToPropertySchema(range as IScalarShape);
|
|
126
|
+
}
|
|
127
|
+
if (types.includes(ns.w3.shacl.NodeShape)) {
|
|
128
|
+
return this.nodeShapeRangeToPropertySchema(range as INodeShape);
|
|
129
|
+
}
|
|
130
|
+
if (types.includes(ns.aml.vocabularies.shapes.ArrayShape)) {
|
|
131
|
+
return this.arrayShapeRangeToPropertySchema(range as IArrayShape);
|
|
132
|
+
}
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
scalarRangeToPropertySchema(schema: IScalarShape): IMonacoScalarProperty {
|
|
137
|
+
const { values, description, name, displayName, defaultValueStr, exclusiveMaximum, exclusiveMinimum, minimum, maximum, minLength, maxLength, id, multipleOf, pattern, readOnly, writeOnly } = schema;
|
|
138
|
+
const type = this.schemaTypeToJsonDataType(schema.dataType);
|
|
139
|
+
const result: IMonacoScalarProperty = {
|
|
140
|
+
'$id': id,
|
|
141
|
+
type,
|
|
142
|
+
title: cleanName(displayName || name),
|
|
143
|
+
};
|
|
144
|
+
if (description) {
|
|
145
|
+
result.description = description;
|
|
146
|
+
}
|
|
147
|
+
if (defaultValueStr) {
|
|
148
|
+
result.default = defaultValueStr;
|
|
149
|
+
}
|
|
150
|
+
if (typeof exclusiveMaximum === 'boolean') {
|
|
151
|
+
result.exclusiveMaximum = exclusiveMaximum;
|
|
152
|
+
}
|
|
153
|
+
if (typeof exclusiveMinimum === 'boolean') {
|
|
154
|
+
result.exclusiveMinimum = exclusiveMinimum;
|
|
155
|
+
}
|
|
156
|
+
if (typeof maxLength === 'number') {
|
|
157
|
+
result.maxLength = maxLength;
|
|
158
|
+
}
|
|
159
|
+
if (typeof minLength === 'number') {
|
|
160
|
+
result.minLength = minLength;
|
|
161
|
+
}
|
|
162
|
+
if (typeof minimum === 'number') {
|
|
163
|
+
result.minimum = minimum;
|
|
164
|
+
}
|
|
165
|
+
if (typeof maximum === 'number') {
|
|
166
|
+
result.maximum = maximum;
|
|
167
|
+
}
|
|
168
|
+
if (typeof multipleOf === 'number') {
|
|
169
|
+
result.multipleOf = multipleOf;
|
|
170
|
+
}
|
|
171
|
+
if (typeof pattern === 'string') {
|
|
172
|
+
result.pattern = pattern;
|
|
173
|
+
}
|
|
174
|
+
if (typeof readOnly === 'boolean') {
|
|
175
|
+
result.readOnly = readOnly;
|
|
176
|
+
}
|
|
177
|
+
if (typeof writeOnly === 'boolean') {
|
|
178
|
+
result.writeOnly = writeOnly;
|
|
179
|
+
}
|
|
180
|
+
switch (schema.dataType) {
|
|
181
|
+
case ns.aml.vocabularies.shapes.dateTimeOnly: result.format = 'date-time'; break;
|
|
182
|
+
case ns.w3.xmlSchema.date: result.format = 'date'; break;
|
|
183
|
+
case ns.w3.xmlSchema.time: result.format = 'time'; break;
|
|
184
|
+
default:
|
|
185
|
+
}
|
|
186
|
+
if (Array.isArray(values) && values.length) {
|
|
187
|
+
// enum properties
|
|
188
|
+
result.enum = [];
|
|
189
|
+
values.forEach((value) => {
|
|
190
|
+
const { types } = value;
|
|
191
|
+
if (types.includes(ns.aml.vocabularies.data.Scalar)) {
|
|
192
|
+
const typed = value as IScalarNode;
|
|
193
|
+
if (typed.value) {
|
|
194
|
+
result.enum!.push(typed.value);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Translates AMF data type to JSON schema data type.
|
|
204
|
+
*/
|
|
205
|
+
schemaTypeToJsonDataType(schemaType?: string): string {
|
|
206
|
+
switch (schemaType) {
|
|
207
|
+
case ns.aml.vocabularies.shapes.number:
|
|
208
|
+
case ns.aml.vocabularies.shapes.integer:
|
|
209
|
+
case ns.aml.vocabularies.shapes.float:
|
|
210
|
+
case ns.aml.vocabularies.shapes.long:
|
|
211
|
+
case ns.aml.vocabularies.shapes.double:
|
|
212
|
+
case ns.w3.xmlSchema.number:
|
|
213
|
+
case ns.w3.xmlSchema.integer:
|
|
214
|
+
case ns.w3.xmlSchema.float:
|
|
215
|
+
case ns.w3.xmlSchema.long:
|
|
216
|
+
case ns.w3.xmlSchema.double: return 'number';
|
|
217
|
+
case ns.w3.xmlSchema.boolean:
|
|
218
|
+
case ns.aml.vocabularies.shapes.boolean: return 'boolean';
|
|
219
|
+
case ns.aml.vocabularies.shapes.nil:
|
|
220
|
+
case ns.w3.xmlSchema.nil: return 'null';
|
|
221
|
+
default: return 'string';
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
nodeShapeRangeToPropertySchema(schema: INodeShape): IMonacoObjectProperty {
|
|
226
|
+
const { description, name, displayName, id, readOnly, writeOnly, closed, minProperties, maxProperties } = schema;
|
|
227
|
+
const properties = collectNodeProperties(schema);
|
|
228
|
+
|
|
229
|
+
const result: IMonacoObjectProperty = {
|
|
230
|
+
'$id': id,
|
|
231
|
+
type: 'object',
|
|
232
|
+
title: cleanName(displayName || name),
|
|
233
|
+
properties: {},
|
|
234
|
+
required: [],
|
|
235
|
+
};
|
|
236
|
+
if (description) {
|
|
237
|
+
result.description = description;
|
|
238
|
+
}
|
|
239
|
+
if (typeof readOnly === 'boolean') {
|
|
240
|
+
result.readOnly = readOnly;
|
|
241
|
+
}
|
|
242
|
+
if (typeof writeOnly === 'boolean') {
|
|
243
|
+
result.writeOnly = writeOnly;
|
|
244
|
+
}
|
|
245
|
+
if (typeof closed === 'boolean') {
|
|
246
|
+
result.additionalProperties = !closed;
|
|
247
|
+
}
|
|
248
|
+
if (typeof minProperties === 'number') {
|
|
249
|
+
result.minProperties = minProperties;
|
|
250
|
+
}
|
|
251
|
+
if (typeof maxProperties === 'number') {
|
|
252
|
+
result.maxProperties = maxProperties;
|
|
253
|
+
}
|
|
254
|
+
properties.forEach(property => this.appendSchemaProperty(result, property));
|
|
255
|
+
return result;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
arrayShapeRangeToPropertySchema(schema: IArrayShape): IMonacoArrayProperty {
|
|
259
|
+
const { description, name, displayName, id, readOnly, writeOnly, items, minItems, maxItems, uniqueItems } = schema;
|
|
260
|
+
const result: IMonacoArrayProperty = {
|
|
261
|
+
'$id': id,
|
|
262
|
+
type: 'array',
|
|
263
|
+
title: cleanName(displayName || name),
|
|
264
|
+
items: {
|
|
265
|
+
anyOf: [],
|
|
266
|
+
},
|
|
267
|
+
required: [],
|
|
268
|
+
additionalItems: false,
|
|
269
|
+
};
|
|
270
|
+
if (description) {
|
|
271
|
+
result.description = description;
|
|
272
|
+
}
|
|
273
|
+
if (typeof readOnly === 'boolean') {
|
|
274
|
+
result.readOnly = readOnly;
|
|
275
|
+
}
|
|
276
|
+
if (typeof writeOnly === 'boolean') {
|
|
277
|
+
result.writeOnly = writeOnly;
|
|
278
|
+
}
|
|
279
|
+
if (typeof uniqueItems === 'boolean') {
|
|
280
|
+
result.uniqueItems = uniqueItems;
|
|
281
|
+
}
|
|
282
|
+
if (items) {
|
|
283
|
+
const value = this.rangeToPropertySchema(items);
|
|
284
|
+
if (value) {
|
|
285
|
+
result.items.anyOf.push(value);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (typeof minItems === 'number') {
|
|
289
|
+
result.minItems = minItems;
|
|
290
|
+
}
|
|
291
|
+
if (typeof maxItems === 'number') {
|
|
292
|
+
result.maxItems = maxItems;
|
|
293
|
+
}
|
|
294
|
+
return result;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { AmfNamespace as ns } from "./definitions/Namespace.js";
|
|
2
|
+
import { IAnyShape, IDataExample, IShapeUnion } from "./definitions/Shapes.js";
|
|
3
|
+
import { ShapeBase, ShapeRenderOptions } from "./shape/ShapeBase.js";
|
|
4
|
+
import { ShapeJsonSchemaGenerator } from './shape/ShapeJsonSchemaGenerator.js';
|
|
5
|
+
import { ShapeXmlSchemaGenerator } from './shape/ShapeXmlSchemaGenerator.js';
|
|
6
|
+
|
|
7
|
+
export interface ISchemaExample extends IDataExample {
|
|
8
|
+
/**
|
|
9
|
+
* The value to render as the example value.
|
|
10
|
+
*/
|
|
11
|
+
renderValue?: string | number | boolean | null | undefined;
|
|
12
|
+
label?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A class that processes AMF's Shape to auto-generate a schema from examples/type for a given media type.
|
|
17
|
+
* This should be used when examples for the Shape are not available but the application still needs to
|
|
18
|
+
* render an example or a schema from the Shape.
|
|
19
|
+
* If examples can be found directly in the shape, use the `ApiExampleGenerator` instead.
|
|
20
|
+
*/
|
|
21
|
+
export class ApiSchemaGenerator {
|
|
22
|
+
opts: Readonly<ShapeRenderOptions>;
|
|
23
|
+
|
|
24
|
+
generator?: ShapeBase;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
*
|
|
28
|
+
* @param mime The example mime type to format the generated example.
|
|
29
|
+
* @param opts Optional configuration.
|
|
30
|
+
*/
|
|
31
|
+
constructor(public mime: string, opts: ShapeRenderOptions = {}) {
|
|
32
|
+
this.opts = Object.freeze({ ...opts });
|
|
33
|
+
if (mime.includes('json')) {
|
|
34
|
+
this.generator = new ShapeJsonSchemaGenerator(opts);
|
|
35
|
+
} else if (mime.includes('xml')) {
|
|
36
|
+
this.generator = new ShapeXmlSchemaGenerator(opts);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param shape The Shape definition
|
|
42
|
+
* @param mime The mime type for the value.
|
|
43
|
+
* @returns Customized Example with the `renderValue` that is the generated Example value.
|
|
44
|
+
*/
|
|
45
|
+
static asExample(shape: IShapeUnion, mime: string, opts?: ShapeRenderOptions): ISchemaExample | undefined {
|
|
46
|
+
const generator = new ApiSchemaGenerator(mime, opts);
|
|
47
|
+
return generator.toExample(shape);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param shape The Shape definition
|
|
52
|
+
* @param mime The mime type for the value.
|
|
53
|
+
* @returns The generated schema
|
|
54
|
+
*/
|
|
55
|
+
static asSchema(shape: IShapeUnion, mime: string, opts?: ShapeRenderOptions): string | number | boolean | null | undefined {
|
|
56
|
+
const generator = new ApiSchemaGenerator(mime, opts);
|
|
57
|
+
return generator.toValue(shape);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Generates the schema from the AMF shape.
|
|
62
|
+
*
|
|
63
|
+
* @param shape The Shape definition
|
|
64
|
+
*/
|
|
65
|
+
generate(shape: IShapeUnion): string | number | boolean | null | undefined {
|
|
66
|
+
const { generator } = this;
|
|
67
|
+
if (!generator) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
return generator.generate(shape);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @link {#generate()}
|
|
75
|
+
* @param shape The Shape definition
|
|
76
|
+
*/
|
|
77
|
+
toValue(shape: IShapeUnion): string | number | boolean | null | undefined {
|
|
78
|
+
return this.generate(shape);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Generates an API Example object with the value to render.
|
|
83
|
+
* @param shape The Shape definition
|
|
84
|
+
* @returns Customized Example with the `renderValue` that is the generated Example value.
|
|
85
|
+
*/
|
|
86
|
+
toExample(shape: IShapeUnion): ISchemaExample | undefined {
|
|
87
|
+
const renderValue = this.generate(shape);
|
|
88
|
+
if (renderValue === null || renderValue === undefined) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
const result: ISchemaExample = {
|
|
92
|
+
id: `${shape.id}/generated`,
|
|
93
|
+
strict: true,
|
|
94
|
+
types: [ns.aml.vocabularies.apiContract.Example],
|
|
95
|
+
mediaType: this.mime,
|
|
96
|
+
renderValue,
|
|
97
|
+
customDomainProperties: [],
|
|
98
|
+
};
|
|
99
|
+
const typed = shape as IAnyShape;
|
|
100
|
+
if (Array.isArray(typed.examples) && typed.examples.length) {
|
|
101
|
+
const [example] = typed.examples;
|
|
102
|
+
if (example.value) {
|
|
103
|
+
result.value = example.value;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
}
|