@api-client/core 0.6.14 → 0.6.17
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/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 +71 -15
- package/build/src/models/data/DataEntity.js +133 -53
- 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 +14 -0
- package/build/src/models/data/DataNamespace.js +50 -0
- package/build/src/models/data/DataNamespace.js.map +1 -1
- package/build/src/models/data/DataProperty.d.ts +107 -36
- package/build/src/models/data/DataProperty.js +147 -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/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 +162 -60
- package/src/models/data/DataFile.ts +3 -0
- package/src/models/data/DataModel.ts +1 -1
- package/src/models/data/DataNamespace.ts +54 -0
- package/src/models/data/DataProperty.ts +191 -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,400 @@
|
|
|
1
|
+
import { IPropertyBindings, IPropertySchema, IPropertyWebBindings } from "../models/data/Bindings.js";
|
|
2
|
+
import { DataAssociation } from "../models/data/DataAssociation.js";
|
|
3
|
+
import { DataEntity } from "../models/data/DataEntity.js";
|
|
4
|
+
import { DataProperty } from "../models/data/DataProperty.js";
|
|
5
|
+
import { ExampleTypes, modelTypeToAmfDataType } from "./AmfTypes.js";
|
|
6
|
+
import { AmfNamespace } from "./definitions/Namespace.js";
|
|
7
|
+
import { anyShape, arrayShape, fileShape, IAnyShape, IArrayShape, IDataExample, IFileShape, INodeShape, IPropertyShape, IRecursiveShape, IScalarShape, IShape, IShapeUnion, nodeShape, propertyShape, recursiveShape, scalarShape, unionShape } from "./definitions/Shapes.js";
|
|
8
|
+
import { AmfDataNode } from "./models/AmfDataNode.js";
|
|
9
|
+
import v4 from '../lib/uuid.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Serializes Data shapes (DataEntity, DataProperty, DataAssociation) to AMF-related shapes.
|
|
13
|
+
*/
|
|
14
|
+
export class AmfShapeGenerator {
|
|
15
|
+
/**
|
|
16
|
+
* Serializes the Entity to the AMF node shape.
|
|
17
|
+
*
|
|
18
|
+
* @param input The Property to serialize.
|
|
19
|
+
* @param generatedEntities The list keys of already generated entities. This prohibits recursive shape generation.
|
|
20
|
+
*/
|
|
21
|
+
entity(input: DataEntity, generatedEntities: string[] = []): INodeShape | IRecursiveShape {
|
|
22
|
+
// const adapted = input.readAdapted();
|
|
23
|
+
if (generatedEntities.includes(input.key)) {
|
|
24
|
+
// create a recursive shape.
|
|
25
|
+
return this._recursiveShape(input);
|
|
26
|
+
}
|
|
27
|
+
generatedEntities.push(input.key);
|
|
28
|
+
const result = nodeShape(input.key);
|
|
29
|
+
result.id = input.key;
|
|
30
|
+
this._updateBaseProperties(input, result);
|
|
31
|
+
result.properties = [];
|
|
32
|
+
input.properties.forEach((item) => {
|
|
33
|
+
// TODO: find adapted property in the adapted schema
|
|
34
|
+
// and check whether the property is disabled.
|
|
35
|
+
|
|
36
|
+
// we check whether the property is hidden.
|
|
37
|
+
// This is not happening when calling the `property()` because this method
|
|
38
|
+
// always returns the AMF shape.
|
|
39
|
+
const adapted = item.readAdapted();
|
|
40
|
+
if (adapted && adapted.hidden) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const shape = this.property(item);
|
|
44
|
+
result.properties.push(shape);
|
|
45
|
+
});
|
|
46
|
+
input.associations.forEach((assoc) => {
|
|
47
|
+
const prop = this.associationProperty(assoc, generatedEntities);
|
|
48
|
+
result.properties.push(prop);
|
|
49
|
+
});
|
|
50
|
+
input.getComputedParents().forEach((parent) => {
|
|
51
|
+
const shape = this.entity(parent, generatedEntities);
|
|
52
|
+
result.inherits.push(shape);
|
|
53
|
+
});
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Serializes an Entity Property to the AMF property shape.
|
|
59
|
+
*
|
|
60
|
+
* @param input The Property to serialize.
|
|
61
|
+
*/
|
|
62
|
+
property(input: DataProperty): IPropertyShape {
|
|
63
|
+
const { required, key } = input;
|
|
64
|
+
const result = propertyShape(key);
|
|
65
|
+
result.path = `${AmfNamespace.aml.vocabularies.data.key}${input.info.name}`;
|
|
66
|
+
if (required) {
|
|
67
|
+
result.minCount = 1;
|
|
68
|
+
}
|
|
69
|
+
result.range = this._readPropertyRange(input);
|
|
70
|
+
|
|
71
|
+
// for example, Example generator needs to know the name of the property
|
|
72
|
+
// as it does not look into the "range" object.
|
|
73
|
+
this._updateBaseProperties(input, result);
|
|
74
|
+
|
|
75
|
+
// sync the name of the property shape with the range, in case it was changed by the bindings
|
|
76
|
+
if (result.range!.name) {
|
|
77
|
+
result.name = result.range!.name;
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Serializes an Entity property to the AMF property shape with association targets as defined in the schema configuration.
|
|
84
|
+
*
|
|
85
|
+
* @param input The Property to serialize.
|
|
86
|
+
*/
|
|
87
|
+
associationProperty(input: DataAssociation, generatedEntities: string[] = []): IPropertyShape {
|
|
88
|
+
const { required, key } = input;
|
|
89
|
+
const result = propertyShape(key);
|
|
90
|
+
result.path = `${AmfNamespace.aml.vocabularies.data.key}${input.info.name}`;
|
|
91
|
+
if (required) {
|
|
92
|
+
result.minCount = 1;
|
|
93
|
+
}
|
|
94
|
+
result.range = this.associationShape(input, generatedEntities);
|
|
95
|
+
this._updateBaseProperties(input, result);
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Generates a shape for an association. Most likely you want to use the `associationProperty()` method instead.
|
|
101
|
+
*
|
|
102
|
+
* @param input The data association instance.
|
|
103
|
+
* @returns The range value for the PropertyShape.
|
|
104
|
+
*/
|
|
105
|
+
associationShape(input: DataAssociation, generatedEntities: string[] = []): IShapeUnion | undefined {
|
|
106
|
+
const adapted = input.readAdapted();
|
|
107
|
+
let schema = adapted && adapted.schema;
|
|
108
|
+
if (schema && schema.linked) {
|
|
109
|
+
// This is a link to the schema. In an API that would be the id
|
|
110
|
+
// of a resource to request the data from.
|
|
111
|
+
const range = scalarShape(input.key);
|
|
112
|
+
range.id = `link-${input.key}`,
|
|
113
|
+
range.dataType = modelTypeToAmfDataType('string');
|
|
114
|
+
return range;
|
|
115
|
+
}
|
|
116
|
+
const items = this.associationUnion(input, generatedEntities);
|
|
117
|
+
if (!items) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const unionType = schema && schema.unionType || 'anyOf';
|
|
121
|
+
if (Array.isArray(items)) {
|
|
122
|
+
const range = unionShape(input.key);
|
|
123
|
+
this._updateBaseProperties(input, range);
|
|
124
|
+
range.anyOf = [];
|
|
125
|
+
if (unionType === 'anyOf') {
|
|
126
|
+
range.anyOf = items;
|
|
127
|
+
} else if (unionType === 'allOf') {
|
|
128
|
+
range.and = items;
|
|
129
|
+
} else if (unionType === 'oneOf') {
|
|
130
|
+
range.xone = items;
|
|
131
|
+
} else { // not.
|
|
132
|
+
range.not = items[0]; // ?
|
|
133
|
+
}
|
|
134
|
+
if (input.multiple) {
|
|
135
|
+
return this.refactorShapeToArray(input.key, range);
|
|
136
|
+
}
|
|
137
|
+
return range;
|
|
138
|
+
}
|
|
139
|
+
if (unionType === 'not') {
|
|
140
|
+
const wrapper = anyShape(input.key);
|
|
141
|
+
wrapper.id = `not-shape-${input.key}`
|
|
142
|
+
wrapper.not = items;
|
|
143
|
+
return wrapper;
|
|
144
|
+
}
|
|
145
|
+
if (input.multiple) {
|
|
146
|
+
return this.refactorShapeToArray(input.key, items);
|
|
147
|
+
}
|
|
148
|
+
return items;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Generates a shape list for an union. Most likely you want to use the `associationProperty()` method instead.
|
|
153
|
+
*
|
|
154
|
+
* @param input The data association instance.
|
|
155
|
+
* @returns The range value for the PropertyShape.
|
|
156
|
+
*/
|
|
157
|
+
associationUnion(input: DataAssociation, generatedEntities: string[] = []): IShapeUnion | IShapeUnion[] | undefined {
|
|
158
|
+
const targets = input.getTargets().map(i => this.entity(i, generatedEntities));
|
|
159
|
+
if (!targets.length) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
if (targets.length > 1) {
|
|
163
|
+
return targets;
|
|
164
|
+
}
|
|
165
|
+
return targets[0];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The DataProperty may have both the `schema` and the `bindings`. For AMF shape we read `schema` for
|
|
170
|
+
* default value, examples, and enum values. We also look for the `web` bindings for more detailed definition of a shape.
|
|
171
|
+
*
|
|
172
|
+
* @param input
|
|
173
|
+
* @returns
|
|
174
|
+
*/
|
|
175
|
+
protected _readPropertyRange(input: DataProperty): IArrayShape | IFileShape | IScalarShape {
|
|
176
|
+
const adapted = input.readAdapted();
|
|
177
|
+
let bindings: IPropertyWebBindings | undefined;
|
|
178
|
+
let schema: IPropertySchema | undefined;
|
|
179
|
+
if (adapted) {
|
|
180
|
+
if (adapted.schema) {
|
|
181
|
+
schema = adapted.schema;
|
|
182
|
+
}
|
|
183
|
+
const findResult = adapted.bindings.find(b => b.type === 'web') as IPropertyBindings | undefined;
|
|
184
|
+
if (findResult) {
|
|
185
|
+
bindings = findResult.schema;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return this._createAmfSchema(input, schema, bindings);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
protected _createAmfSchema(input: DataProperty, schema?: IPropertySchema, bindings?: IPropertyWebBindings): IArrayShape | IFileShape | IScalarShape {
|
|
192
|
+
const { multiple, type } = input;
|
|
193
|
+
if (multiple) {
|
|
194
|
+
return this._generateArrayShape(input, schema, bindings);
|
|
195
|
+
}
|
|
196
|
+
if (type === 'binary' && !(bindings && bindings.format === 'binary')) {
|
|
197
|
+
return this._generateFileShape(input, schema, bindings);
|
|
198
|
+
}
|
|
199
|
+
return this._generateScalarShape(input, schema, bindings);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Normally this would be part of generating a scalar schema but the the property is an array this
|
|
204
|
+
* is generated on the array and not on the range.
|
|
205
|
+
*
|
|
206
|
+
* @param result The scalar or array shape.
|
|
207
|
+
* @param schema The adapted property schema
|
|
208
|
+
* @param type The data type of the parent property as set on the `range`
|
|
209
|
+
* @param isArray Whether the DataProperty is multiple
|
|
210
|
+
*/
|
|
211
|
+
protected _setShapeSchema(result: IAnyShape, schema: IPropertySchema, type: string, isArray?: boolean): void {
|
|
212
|
+
if (schema.defaultValue) {
|
|
213
|
+
const dt = AmfDataNode.scalar(schema.defaultValue, type);
|
|
214
|
+
result.defaultValue = dt.toJSON();
|
|
215
|
+
}
|
|
216
|
+
if (Array.isArray(schema.enum)) {
|
|
217
|
+
result.values = schema.enum.map(i => AmfDataNode.scalar(i, type).toJSON());
|
|
218
|
+
}
|
|
219
|
+
if (Array.isArray(schema.examples)) {
|
|
220
|
+
if (isArray) {
|
|
221
|
+
result.examples = this._generateArrayExamples(schema.examples, type);
|
|
222
|
+
} else {
|
|
223
|
+
result.examples = this._generateExamples(schema.examples, type);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
protected _generateArrayShape(input: DataProperty, schema?: IPropertySchema, bindings?: IPropertyWebBindings): IArrayShape {
|
|
229
|
+
const result = arrayShape(input.key);
|
|
230
|
+
const { type } = input;
|
|
231
|
+
if (type === 'binary') {
|
|
232
|
+
// we do not pass schema to the range generator as we set schema's properties on the array shape.
|
|
233
|
+
result.items = this._generateFileShape(input, undefined, bindings);
|
|
234
|
+
} else {
|
|
235
|
+
result.items = this._generateScalarShape(input, undefined, bindings);
|
|
236
|
+
}
|
|
237
|
+
if (schema) {
|
|
238
|
+
const type = (result.items as IScalarShape).dataType as string;
|
|
239
|
+
this._setShapeSchema(result, schema, type, input.multiple);
|
|
240
|
+
}
|
|
241
|
+
return result;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
protected _generateScalarShape(input: DataProperty, schema?: IPropertySchema, bindings?: IPropertyWebBindings): IScalarShape {
|
|
245
|
+
const result = scalarShape(input.key);
|
|
246
|
+
this._updateBaseProperties(input, result);
|
|
247
|
+
if (bindings) {
|
|
248
|
+
if (bindings.dataType) {
|
|
249
|
+
result.dataType = bindings.dataType;
|
|
250
|
+
}
|
|
251
|
+
this._fillScalarShapeCommonProperties(result, input, bindings);
|
|
252
|
+
}
|
|
253
|
+
if (!result.dataType) {
|
|
254
|
+
result.dataType = modelTypeToAmfDataType(input.type, bindings);
|
|
255
|
+
}
|
|
256
|
+
if (schema) {
|
|
257
|
+
this._setShapeSchema(result, schema, result.dataType as string, input.multiple);
|
|
258
|
+
}
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
protected _generateExamples(examples: string[], type: string): IDataExample[] {
|
|
263
|
+
return examples.map((current) => {
|
|
264
|
+
const value = AmfDataNode.scalar(current, type).toJSON();
|
|
265
|
+
const item: IDataExample = {
|
|
266
|
+
id: v4(),
|
|
267
|
+
customDomainProperties: [],
|
|
268
|
+
strict: true,
|
|
269
|
+
types: ExampleTypes,
|
|
270
|
+
structuredValue: value,
|
|
271
|
+
};
|
|
272
|
+
return item;
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
protected _generateArrayExamples(examples: string[], type: string): IDataExample[] {
|
|
277
|
+
const item: IDataExample = {
|
|
278
|
+
id: v4(),
|
|
279
|
+
customDomainProperties: [],
|
|
280
|
+
strict: true,
|
|
281
|
+
types: ExampleTypes,
|
|
282
|
+
};
|
|
283
|
+
const value = new AmfDataNode('array');
|
|
284
|
+
examples.forEach((item) => {
|
|
285
|
+
const member = AmfDataNode.scalar(item, type);
|
|
286
|
+
value.addMember(member);
|
|
287
|
+
});
|
|
288
|
+
item.structuredValue = value.toJSON();
|
|
289
|
+
return [item];
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
protected _generateFileShape(input: DataProperty, schema?: IPropertySchema, bindings?: IPropertyWebBindings): IFileShape {
|
|
293
|
+
if (bindings && bindings.dataType === AmfNamespace.w3.xmlSchema.base64Binary) {
|
|
294
|
+
// this is a binary format of a string shape
|
|
295
|
+
}
|
|
296
|
+
const result = fileShape(input.key);
|
|
297
|
+
this._updateBaseProperties(input, result);
|
|
298
|
+
if (bindings) {
|
|
299
|
+
if (Array.isArray(bindings.fileTypes)) {
|
|
300
|
+
result.fileTypes = bindings.fileTypes;
|
|
301
|
+
}
|
|
302
|
+
this._fillScalarShapeCommonProperties(result, input, bindings);
|
|
303
|
+
}
|
|
304
|
+
return result;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
protected _fillScalarShapeCommonProperties(result: IFileShape | IScalarShape, input: DataProperty, bindings: IPropertyWebBindings): void {
|
|
308
|
+
if (bindings.name) {
|
|
309
|
+
result.name = bindings.name;
|
|
310
|
+
}
|
|
311
|
+
if (bindings.xml) {
|
|
312
|
+
result.xmlSerialization = bindings.xml;
|
|
313
|
+
}
|
|
314
|
+
if (bindings.pattern) {
|
|
315
|
+
result.pattern = bindings.pattern;
|
|
316
|
+
}
|
|
317
|
+
if (typeof bindings.minLength === 'number') {
|
|
318
|
+
result.minLength = bindings.minLength;
|
|
319
|
+
}
|
|
320
|
+
if (typeof bindings.maxLength === 'number') {
|
|
321
|
+
result.maxLength = bindings.maxLength;
|
|
322
|
+
}
|
|
323
|
+
if (typeof bindings.minimum === 'number') {
|
|
324
|
+
result.minimum = bindings.minimum;
|
|
325
|
+
}
|
|
326
|
+
if (typeof bindings.maximum === 'number') {
|
|
327
|
+
result.maximum = bindings.maximum;
|
|
328
|
+
}
|
|
329
|
+
if (typeof bindings.multipleOf === 'number') {
|
|
330
|
+
result.multipleOf = bindings.multipleOf;
|
|
331
|
+
}
|
|
332
|
+
if (typeof bindings.exclusiveMinimum === 'boolean') {
|
|
333
|
+
result.exclusiveMinimum = bindings.exclusiveMinimum;
|
|
334
|
+
}
|
|
335
|
+
if (typeof bindings.exclusiveMaximum === 'boolean') {
|
|
336
|
+
result.exclusiveMaximum = bindings.exclusiveMaximum;
|
|
337
|
+
}
|
|
338
|
+
if (bindings.format) {
|
|
339
|
+
result.format = bindings.format;
|
|
340
|
+
}
|
|
341
|
+
if (typeof input.readOnly === 'boolean') {
|
|
342
|
+
result.readOnly = input.readOnly;
|
|
343
|
+
}
|
|
344
|
+
if (typeof input.writeOnly === 'boolean') {
|
|
345
|
+
result.writeOnly = input.writeOnly;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
protected _updateBaseProperties(input: DataProperty | DataAssociation | DataEntity, target: IShape): void {
|
|
350
|
+
const adopted = input.readAdapted();
|
|
351
|
+
if (adopted && adopted.info.name) {
|
|
352
|
+
target.name = adopted.info.name;
|
|
353
|
+
} else if (input.info.name) {
|
|
354
|
+
target.name = input.info.name;
|
|
355
|
+
}
|
|
356
|
+
if (adopted && adopted.info.displayName) {
|
|
357
|
+
target.displayName = adopted.info.displayName;
|
|
358
|
+
} else if (input.info.displayName) {
|
|
359
|
+
target.displayName = input.info.displayName;
|
|
360
|
+
}
|
|
361
|
+
if (adopted && adopted.info.description) {
|
|
362
|
+
target.description = adopted.info.description;
|
|
363
|
+
} else if (input.info.description) {
|
|
364
|
+
target.description = input.info.description;
|
|
365
|
+
}
|
|
366
|
+
if (adopted && (adopted as DataProperty).deprecated) {
|
|
367
|
+
target.deprecated = (adopted as DataProperty).deprecated;
|
|
368
|
+
} else if ((input as DataProperty).deprecated) {
|
|
369
|
+
target.deprecated = (input as DataProperty).deprecated;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
protected _recursiveShape(input: DataEntity): IRecursiveShape {
|
|
374
|
+
return recursiveShape(input.key, input.key);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Translates generated schema from an array shape to the shape defined in the `items` of the array.
|
|
379
|
+
*
|
|
380
|
+
* @param array The source array.
|
|
381
|
+
* @returns The definition of the `items` of the array.
|
|
382
|
+
*/
|
|
383
|
+
refactorArrayToShape(array: IArrayShape): IShapeUnion {
|
|
384
|
+
const { items } = array;
|
|
385
|
+
return items!;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Translates the shape to an array shape. This happens when data model property is changed from 'multiple' to not-multiple and back.
|
|
390
|
+
*
|
|
391
|
+
* @param id The key of the parameter or an association
|
|
392
|
+
* @param shape The shape to wrap as an array.
|
|
393
|
+
* @returns Array shape.
|
|
394
|
+
*/
|
|
395
|
+
refactorShapeToArray(id: string, shape: IShapeUnion): IArrayShape {
|
|
396
|
+
const result = arrayShape(id);
|
|
397
|
+
result.items = shape;
|
|
398
|
+
return result;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { DataPropertyType } from "../models/data/DataProperty.js";
|
|
2
|
+
import { IPropertyWebBindings } from "../models/data/Bindings.js";
|
|
3
|
+
import { AmfNamespace } from "./definitions/Namespace.js";
|
|
4
|
+
|
|
5
|
+
export const ScalarTypes = [
|
|
6
|
+
AmfNamespace.aml.vocabularies.shapes.ScalarShape,
|
|
7
|
+
AmfNamespace.aml.vocabularies.shapes.AnyShape,
|
|
8
|
+
AmfNamespace.w3.shacl.Shape,
|
|
9
|
+
AmfNamespace.aml.vocabularies.shapes.Shape,
|
|
10
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
11
|
+
];
|
|
12
|
+
export const ArrayTypes = [
|
|
13
|
+
AmfNamespace.aml.vocabularies.shapes.ArrayShape,
|
|
14
|
+
AmfNamespace.aml.vocabularies.shapes.AnyShape,
|
|
15
|
+
AmfNamespace.w3.shacl.Shape,
|
|
16
|
+
AmfNamespace.aml.vocabularies.shapes.Shape,
|
|
17
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
18
|
+
];
|
|
19
|
+
export const NodeTypes = [
|
|
20
|
+
AmfNamespace.w3.shacl.NodeShape,
|
|
21
|
+
AmfNamespace.aml.vocabularies.shapes.AnyShape,
|
|
22
|
+
AmfNamespace.w3.shacl.Shape,
|
|
23
|
+
AmfNamespace.aml.vocabularies.shapes.Shape,
|
|
24
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
25
|
+
];
|
|
26
|
+
export const PropertyTypes = [
|
|
27
|
+
AmfNamespace.w3.shacl.PropertyShape,
|
|
28
|
+
AmfNamespace.w3.shacl.Shape,
|
|
29
|
+
AmfNamespace.aml.vocabularies.shapes.Shape,
|
|
30
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
31
|
+
];
|
|
32
|
+
export const NilTypes = [
|
|
33
|
+
AmfNamespace.aml.vocabularies.shapes.NilShape,
|
|
34
|
+
AmfNamespace.w3.shacl.Shape,
|
|
35
|
+
AmfNamespace.aml.vocabularies.shapes.Shape,
|
|
36
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
37
|
+
];
|
|
38
|
+
export const UnionTypes = [
|
|
39
|
+
AmfNamespace.aml.vocabularies.shapes.UnionShape,
|
|
40
|
+
AmfNamespace.aml.vocabularies.shapes.AnyShape,
|
|
41
|
+
AmfNamespace.w3.shacl.Shape,
|
|
42
|
+
AmfNamespace.aml.vocabularies.shapes.Shape,
|
|
43
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
44
|
+
];
|
|
45
|
+
export const FileTypes = [
|
|
46
|
+
AmfNamespace.aml.vocabularies.shapes.FileShape,
|
|
47
|
+
AmfNamespace.aml.vocabularies.shapes.AnyShape,
|
|
48
|
+
AmfNamespace.w3.shacl.Shape,
|
|
49
|
+
AmfNamespace.aml.vocabularies.shapes.Shape,
|
|
50
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
51
|
+
];
|
|
52
|
+
export const AnyTypes = [
|
|
53
|
+
AmfNamespace.aml.vocabularies.shapes.AnyShape,
|
|
54
|
+
AmfNamespace.w3.shacl.Shape,
|
|
55
|
+
AmfNamespace.aml.vocabularies.shapes.Shape,
|
|
56
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
57
|
+
];
|
|
58
|
+
export const SchemaTypes = [
|
|
59
|
+
AmfNamespace.aml.vocabularies.shapes.SchemaShape,
|
|
60
|
+
AmfNamespace.aml.vocabularies.shapes.AnyShape,
|
|
61
|
+
AmfNamespace.w3.shacl.Shape,
|
|
62
|
+
AmfNamespace.aml.vocabularies.shapes.Shape,
|
|
63
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
64
|
+
];
|
|
65
|
+
export const TupleTypes = [
|
|
66
|
+
AmfNamespace.aml.vocabularies.shapes.TupleShape,
|
|
67
|
+
AmfNamespace.aml.vocabularies.shapes.ArrayShape,
|
|
68
|
+
AmfNamespace.aml.vocabularies.shapes.AnyShape,
|
|
69
|
+
AmfNamespace.w3.shacl.Shape,
|
|
70
|
+
AmfNamespace.aml.vocabularies.shapes.Shape,
|
|
71
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
72
|
+
];
|
|
73
|
+
export const RecursiveTypes = [
|
|
74
|
+
AmfNamespace.aml.vocabularies.shapes.RecursiveShape,
|
|
75
|
+
AmfNamespace.w3.shacl.Shape,
|
|
76
|
+
AmfNamespace.aml.vocabularies.shapes.Shape,
|
|
77
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
78
|
+
];
|
|
79
|
+
export const XmlSerializationTypes = [
|
|
80
|
+
AmfNamespace.aml.vocabularies.shapes.XMLSerializer,
|
|
81
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
export const ScalarNodeTypes = [
|
|
85
|
+
AmfNamespace.aml.vocabularies.data.Scalar,
|
|
86
|
+
AmfNamespace.aml.vocabularies.data.Node,
|
|
87
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
88
|
+
];
|
|
89
|
+
export const ArrayNodeTypes = [
|
|
90
|
+
AmfNamespace.aml.vocabularies.data.Array,
|
|
91
|
+
AmfNamespace.w3.rdfSchema.Seq,
|
|
92
|
+
AmfNamespace.aml.vocabularies.data.Node,
|
|
93
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
94
|
+
];
|
|
95
|
+
export const ObjectNodeTypes = [
|
|
96
|
+
AmfNamespace.aml.vocabularies.data.Object,
|
|
97
|
+
AmfNamespace.aml.vocabularies.data.Node,
|
|
98
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
99
|
+
];
|
|
100
|
+
export const ExampleTypes = [
|
|
101
|
+
AmfNamespace.aml.vocabularies.apiContract.Example,
|
|
102
|
+
AmfNamespace.aml.vocabularies.document.DomainElement,
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Translates the DataProperty type to an AMF data type.
|
|
107
|
+
* @param type The property data type
|
|
108
|
+
* @returns AMF shape dataType
|
|
109
|
+
*/
|
|
110
|
+
export function modelTypeToAmfDataType(type: DataPropertyType, bindings?: IPropertyWebBindings): string {
|
|
111
|
+
switch (type) {
|
|
112
|
+
case 'boolean': return AmfNamespace.aml.vocabularies.shapes.boolean;
|
|
113
|
+
case 'nil': return AmfNamespace.aml.vocabularies.shapes.nil;
|
|
114
|
+
case 'date': return AmfNamespace.w3.xmlSchema.date;
|
|
115
|
+
case 'datetime': return AmfNamespace.w3.xmlSchema.dateTime;
|
|
116
|
+
case 'time': return AmfNamespace.aml.vocabularies.shapes.dateTimeOnly;
|
|
117
|
+
case 'number': return AmfNamespace.w3.xmlSchema.number;
|
|
118
|
+
case 'integer': return AmfNamespace.w3.xmlSchema.integer;
|
|
119
|
+
case 'any': return AmfNamespace.aml.vocabularies.shapes.AnyShape;
|
|
120
|
+
default:
|
|
121
|
+
if (bindings && bindings.format === 'binary') {
|
|
122
|
+
return AmfNamespace.w3.xmlSchema.base64Binary;
|
|
123
|
+
}
|
|
124
|
+
return AmfNamespace.w3.xmlSchema.string;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
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
|
+
import { IArrayShape, IDataExample, IDataNode, IShapeUnion } from "./definitions/Shapes.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A class that processes AMF's Example object to read the example value
|
|
10
|
+
* or to generate the example for the given media type.
|
|
11
|
+
*/
|
|
12
|
+
export class ApiExampleGenerator {
|
|
13
|
+
/**
|
|
14
|
+
* Reads or generates an example.
|
|
15
|
+
* When the `mime` is set then it tries to "guess" whether the mime type corresponds to the value.
|
|
16
|
+
* If it doesn't then it generates the example from the structured value, when possible.
|
|
17
|
+
* @param example The structured value of the example
|
|
18
|
+
* @param mime The optional mime type of the example. When not set it won't generate example from the structured value.
|
|
19
|
+
* @param shape The optional shape containing this example to use with the XML examples which needs wrapping into an element.
|
|
20
|
+
* @returns The read or generated example.
|
|
21
|
+
*/
|
|
22
|
+
read(example: IDataExample, mime?: string, shape?: IShapeUnion): string | undefined {
|
|
23
|
+
const { value, structuredValue } = example;
|
|
24
|
+
if (!value && !structuredValue) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
if (structuredValue && !value && mime) {
|
|
28
|
+
return this.fromStructuredValue(mime, structuredValue, shape);
|
|
29
|
+
}
|
|
30
|
+
if (!mime) {
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
if (this.mimeMatches(mime, value)) {
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
if (structuredValue) {
|
|
37
|
+
return this.fromStructuredValue(mime, structuredValue, shape);
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Employs some basic heuristics to determine whether the given mime type patches the content.
|
|
44
|
+
* @param mime The mime type for the value.
|
|
45
|
+
* @param value The value.
|
|
46
|
+
* @returns True when the value matches the mime type.
|
|
47
|
+
*/
|
|
48
|
+
mimeMatches(mime: string, value?: string): boolean {
|
|
49
|
+
const trimmed = String(value).trim();
|
|
50
|
+
if (mime.includes('json')) {
|
|
51
|
+
// JSON string has to start with either of these characters
|
|
52
|
+
return trimmed[0] === '{' || trimmed[0] === '[';
|
|
53
|
+
}
|
|
54
|
+
if (mime.includes('xml')) {
|
|
55
|
+
return trimmed.startsWith('<');
|
|
56
|
+
}
|
|
57
|
+
if (mime.includes('x-www-form-urlencoded')) {
|
|
58
|
+
return trimmed.includes('=') || trimmed.includes('&');
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Generates the example for the given structured value and the media type.
|
|
65
|
+
* @param mime The mime type for the value.
|
|
66
|
+
* @param structuredValue The structuredValue of the example.
|
|
67
|
+
* @param shape The optional shape containing this example to use with the XML examples which needs wrapping into an element.
|
|
68
|
+
* @returns The generated example or null if couldn't process the data.
|
|
69
|
+
*/
|
|
70
|
+
fromStructuredValue(mime: string, structuredValue: IDataNode, shape?: IShapeUnion): string | undefined {
|
|
71
|
+
if (mime.includes('json')) {
|
|
72
|
+
const generator = new JsonDataNodeGenerator();
|
|
73
|
+
return generator.generate(structuredValue);
|
|
74
|
+
}
|
|
75
|
+
let shapeName;
|
|
76
|
+
if (shape && shape.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
77
|
+
shapeName = shape.name;
|
|
78
|
+
} else if (shape && shape.types.includes(ns.aml.vocabularies.shapes.ArrayShape)) {
|
|
79
|
+
const typed = shape as IArrayShape;
|
|
80
|
+
if (typed.items && typed.items.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
81
|
+
shapeName = typed.items.name || shape.name;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (mime.includes('xml')) {
|
|
85
|
+
const generator = new XmlDataNodeGenerator();
|
|
86
|
+
let value = generator.generate(structuredValue, shapeName);
|
|
87
|
+
if (shape && !shapeName) {
|
|
88
|
+
value = this.wrapXmlValue(value, shape);
|
|
89
|
+
}
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
if (mime.includes('x-www-form-urlencoded')) {
|
|
93
|
+
const generator = new UrlEncodedDataNodeGenerator();
|
|
94
|
+
return generator.generate(structuredValue, shapeName);
|
|
95
|
+
}
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Wraps the generated XML example into an element according to the `shape` properties.
|
|
101
|
+
*/
|
|
102
|
+
wrapXmlValue(value?: string, shape?: IShapeUnion): string | undefined {
|
|
103
|
+
if (!value || !shape) {
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
const { name } = shape;
|
|
107
|
+
const parts = [`<${name}>`];
|
|
108
|
+
parts.push(formatXmlValue(' ', value.trim()));
|
|
109
|
+
parts.push(`</${name}>`);
|
|
110
|
+
return parts.join('\n');
|
|
111
|
+
}
|
|
112
|
+
}
|