@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.
Files changed (151) 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/data/Bindings.d.ts +161 -0
  74. package/build/src/models/data/Bindings.js +2 -0
  75. package/build/src/models/data/Bindings.js.map +1 -0
  76. package/build/src/models/data/DataAssociation.d.ts +135 -14
  77. package/build/src/models/data/DataAssociation.js +154 -21
  78. package/build/src/models/data/DataAssociation.js.map +1 -1
  79. package/build/src/models/data/DataAssociationSchema.d.ts +36 -0
  80. package/build/src/models/data/DataEntity.d.ts +71 -15
  81. package/build/src/models/data/DataEntity.js +133 -53
  82. package/build/src/models/data/DataEntity.js.map +1 -1
  83. package/build/src/models/data/DataFile.d.ts +3 -0
  84. package/build/src/models/data/DataFile.js +3 -0
  85. package/build/src/models/data/DataFile.js.map +1 -1
  86. package/build/src/models/data/DataModel.d.ts +1 -1
  87. package/build/src/models/data/DataModel.js.map +1 -1
  88. package/build/src/models/data/DataNamespace.d.ts +14 -0
  89. package/build/src/models/data/DataNamespace.js +50 -0
  90. package/build/src/models/data/DataNamespace.js.map +1 -1
  91. package/build/src/models/data/DataProperty.d.ts +107 -36
  92. package/build/src/models/data/DataProperty.js +147 -17
  93. package/build/src/models/data/DataProperty.js.map +1 -1
  94. package/data/apis/oas-date/oas-date.yaml +28 -0
  95. package/data/apis/oas-types/oas-types.yaml +159 -0
  96. package/data/apis/oas-unions/oas-unions.yaml +75 -0
  97. package/data/apis/raml-date/raml-date.raml +28 -0
  98. package/data/apis/recursive/recursive.raml +14 -0
  99. package/data/apis/schema-api/examples/person.json +14 -0
  100. package/data/apis/schema-api/examples/person.raml +14 -0
  101. package/data/apis/schema-api/examples/person.url.encoded +1 -0
  102. package/data/apis/schema-api/examples/person.xml +14 -0
  103. package/data/apis/schema-api/library/demo-types.raml +43 -0
  104. package/data/apis/schema-api/schema-api.raml +644 -0
  105. package/data/apis/schema-api/schemas/person.json +104 -0
  106. package/data/apis/schema-api/schemas/person.xsd +26 -0
  107. package/data/apis/schema-api/types/DemoPerson.raml +67 -0
  108. package/data/model.js +106 -0
  109. package/data/models/oas-date.json +637 -0
  110. package/data/models/oas-types.json +5352 -0
  111. package/data/models/oas-unions.json +1881 -0
  112. package/data/models/raml-date.json +1096 -0
  113. package/data/models/recursive.json +610 -0
  114. package/data/models/schema-api.json +37319 -0
  115. package/package.json +9 -6
  116. package/src/amf/AmfMixin.ts +1623 -0
  117. package/src/amf/AmfSerializer.ts +2028 -0
  118. package/src/amf/AmfShapeGenerator.ts +400 -0
  119. package/src/amf/AmfTypes.ts +126 -0
  120. package/src/amf/ApiExampleGenerator.ts +112 -0
  121. package/src/amf/ApiMonacoSchemaGenerator.ts +296 -0
  122. package/src/amf/ApiSchemaGenerator.ts +108 -0
  123. package/src/amf/ApiSchemaValues.ts +411 -0
  124. package/src/amf/Utils.ts +182 -0
  125. package/src/amf/data-node/DataNodeBase.ts +81 -0
  126. package/src/amf/data-node/JsonDataNodeGenerator.ts +26 -0
  127. package/src/amf/data-node/README.md +3 -0
  128. package/src/amf/data-node/UrlEncodedDataNodeGenerator.ts +43 -0
  129. package/src/amf/data-node/XmlDataNodeGenerator.ts +38 -0
  130. package/src/amf/definitions/Amf.ts +443 -0
  131. package/src/amf/definitions/Api.ts +427 -0
  132. package/src/amf/definitions/Base.ts +13 -0
  133. package/src/amf/definitions/Namespace.ts +341 -0
  134. package/src/amf/definitions/Shapes.ts +414 -0
  135. package/src/amf/models/AmfDataNode.ts +200 -0
  136. package/src/amf/shape/README.md +4 -0
  137. package/src/amf/shape/ShapeBase.ts +160 -0
  138. package/src/amf/shape/ShapeJsonSchemaGenerator.ts +422 -0
  139. package/src/amf/shape/ShapeXmlSchemaGenerator.ts +876 -0
  140. package/src/models/data/Bindings.ts +186 -0
  141. package/src/models/data/DataAssociation.ts +226 -29
  142. package/src/models/data/DataAssociationSchema.ts +38 -0
  143. package/src/models/data/DataEntity.ts +162 -60
  144. package/src/models/data/DataFile.ts +3 -0
  145. package/src/models/data/DataModel.ts +1 -1
  146. package/src/models/data/DataNamespace.ts +54 -0
  147. package/src/models/data/DataProperty.ts +191 -47
  148. package/build/src/models/data/DataPropertySchema.d.ts +0 -125
  149. package/build/src/models/data/DataPropertySchema.js +0 -33
  150. package/build/src/models/data/DataPropertySchema.js.map +0 -1
  151. package/src/models/data/DataPropertySchema.ts +0 -156
@@ -0,0 +1,1146 @@
1
+ /**
2
+ @license
3
+ Copyright 2018 The Advanced REST client authors <arc@mulesoft.com>
4
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
+ use this file except in compliance with the License. You may obtain a copy of
6
+ the License at
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+ Unless required by applicable law or agreed to in writing, software
9
+ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10
+ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11
+ License for the specific language governing permissions and limitations under
12
+ the License.
13
+ */
14
+ // @ts-ignore
15
+ import { AmfModelExpander, JsonLdOptions, JsonLd } from 'amf-json-ld-lib';
16
+ import { AmfNamespace as ns } from './definitions/Namespace.js';
17
+ export const expandKey = Symbol('expandKey');
18
+ export const findAmfType = Symbol('findAmfType');
19
+ export const findReferenceObject = Symbol('findReferenceObject');
20
+ export const getArrayItems = Symbol('getArrayItems');
21
+ export const computeReferenceSecurity = Symbol('computeReferenceSecurity');
22
+ /**
23
+ * Common functions used by AMF components to compute AMF values.
24
+ */
25
+ export const AmfMixin = (superClass) => {
26
+ class AmfMixin extends superClass {
27
+ _amf;
28
+ _flattenedAmf;
29
+ __cachedKeys;
30
+ get amf() {
31
+ return this._amf;
32
+ }
33
+ set amf(value) {
34
+ const old = this._amf;
35
+ if (old === value) {
36
+ return;
37
+ }
38
+ let expanded;
39
+ if (!value || AmfModelExpander.isInExpandedForm(value)) {
40
+ this._flattenedAmf = undefined;
41
+ expanded = value;
42
+ }
43
+ else {
44
+ const oldFlattened = this._flattenedAmf;
45
+ if (oldFlattened === value) {
46
+ return;
47
+ }
48
+ this._flattenedAmf = value;
49
+ expanded = this._expand(value);
50
+ }
51
+ // Cached keys cannot be static as this element can be using in the sane
52
+ // document with different AMF models
53
+ this.__cachedKeys = {};
54
+ this._amf = expanded;
55
+ this._amfChanged(expanded);
56
+ }
57
+ /**
58
+ * This is an abstract method to be implemented by the components.
59
+ * If, instead, the component uses `amf` setter you must use `super.amf` to
60
+ * set the value.
61
+ * @param amf Current AMF model. Can be undefined.
62
+ */
63
+ /* eslint-disable-next-line no-unused-vars */
64
+ _amfChanged(amf) { }
65
+ /**
66
+ * Expands flattened AMF model
67
+ */
68
+ _expand(amf) {
69
+ AmfModelExpander.preprocessLegacyRootNodeId(amf);
70
+ const linkEmbeddingFilter = (key) => !key.endsWith("fixPoint");
71
+ const rootNode = amf['@context'] ? '' : "amf://id";
72
+ const options = JsonLdOptions.apply()
73
+ .withEmbeddedLinks(linkEmbeddingFilter)
74
+ .withCompactedIris()
75
+ .withExpandedStructure()
76
+ .withRootNode(rootNode);
77
+ return JsonLd.process(amf, options);
78
+ }
79
+ /**
80
+ * Returns compact model key for given value.
81
+ * @param property AMF original property
82
+ * @param context A context to use. If not set, it looks for the context of the passed model
83
+ * @returns Compact model property name or the same value if value not found in the context.
84
+ */
85
+ _getAmfKey(property, context) {
86
+ if (!property) {
87
+ return undefined;
88
+ }
89
+ let { amf } = this;
90
+ if (!amf && !context) {
91
+ return property;
92
+ }
93
+ if (Array.isArray(amf)) {
94
+ [amf] = amf;
95
+ }
96
+ if (!this.__cachedKeys) {
97
+ this.__cachedKeys = {};
98
+ }
99
+ const ctx = (context || amf && amf['@context']);
100
+ if (!ctx || !property) {
101
+ return property;
102
+ }
103
+ const cache = this.__cachedKeys;
104
+ if (property in cache) {
105
+ return cache[property];
106
+ }
107
+ property = String(property);
108
+ const hashIndex = property.indexOf('#');
109
+ const hashProperty = property.substring(0, hashIndex + 1);
110
+ const keys = Object.keys(ctx);
111
+ for (let i = 0, len = keys.length; i < len; i++) {
112
+ const k = keys[i];
113
+ if (ctx[k] === property) {
114
+ cache[property] = k;
115
+ return k;
116
+ }
117
+ if (hashIndex === -1 && property.indexOf(ctx[k]) === 0) {
118
+ const result = property.replace(ctx[k], `${k}:`);
119
+ cache[property] = result;
120
+ return result;
121
+ }
122
+ if (ctx[k] === hashProperty) {
123
+ const result = `${k}:${property.substring(hashIndex + 1)}`;
124
+ cache[property] = result;
125
+ return result;
126
+ }
127
+ }
128
+ return property;
129
+ }
130
+ /**
131
+ * Ensures that the model is AMF object.
132
+ *
133
+ * @param amf AMF json/ld model
134
+ * @returns The API spec
135
+ */
136
+ _ensureAmfModel(amf) {
137
+ if (!amf) {
138
+ return undefined;
139
+ }
140
+ if (Array.isArray(amf)) {
141
+ [amf] = amf;
142
+ }
143
+ if (this._hasType(amf, ns.aml.vocabularies.document.Document)) {
144
+ return amf;
145
+ }
146
+ return undefined;
147
+ }
148
+ /**
149
+ * Ensures that the value is an array.
150
+ * It returns undefined when there's no value.
151
+ * It returns the same array if the value is already an array.
152
+ * It returns new array of the item is not an array.
153
+ *
154
+ * @param value An item to test
155
+ */
156
+ _ensureArray(value) {
157
+ if (!value) {
158
+ return undefined;
159
+ }
160
+ if (value instanceof Array) {
161
+ return value;
162
+ }
163
+ return [value];
164
+ }
165
+ /**
166
+ * Gets a single scalar value from a model.
167
+ * @param model Amf model to extract the value from.
168
+ * @param untrustedKey Model key to search for the value
169
+ * @param context A context to use. If not set, it looks for the context of the passed model
170
+ * @returns Value for key
171
+ */
172
+ _getValue(model, untrustedKey, context) {
173
+ const key = this._getAmfKey(untrustedKey, context);
174
+ if (!key) {
175
+ return undefined;
176
+ }
177
+ let data = model && model[key];
178
+ if (!data) {
179
+ // This includes "undefined", "false", "null" and "0"
180
+ return data;
181
+ }
182
+ if (Array.isArray(data)) {
183
+ [data] = data;
184
+ }
185
+ if (!data) {
186
+ return undefined;
187
+ }
188
+ const type = typeof data;
189
+ if (['string', 'number', 'boolean', 'undefined'].includes(type)) {
190
+ return data;
191
+ }
192
+ return data['@value'];
193
+ }
194
+ /**
195
+ * Gets values from a model as an array of `@value` properties.
196
+ * @param model Amf model to extract the value from.
197
+ * @param untrustedKey Model key to search for the value
198
+ * @param context A context to use. If not set, it looks for the context of the passed model
199
+ * @returns The value for key
200
+ */
201
+ _getValueArray(model, untrustedKey, context) {
202
+ const key = this._getAmfKey(untrustedKey, context);
203
+ if (!key) {
204
+ return undefined;
205
+ }
206
+ const data = model && this._ensureArray(model[key]);
207
+ if (!Array.isArray(data)) {
208
+ return undefined;
209
+ }
210
+ return data.map((item) => item['@value'] || item);
211
+ }
212
+ /**
213
+ * Reads an array from the model.
214
+ *
215
+ * @param model Amf model to extract the value from.
216
+ * @param untrustedKey Model key to search for the value
217
+ * @param context A context to use. If not set, it looks for the context of the passed model
218
+ * @returns Value for the key
219
+ */
220
+ [getArrayItems](model, untrustedKey, context) {
221
+ const k = this._getAmfKey(untrustedKey, context);
222
+ if (!k) {
223
+ return undefined;
224
+ }
225
+ const data = model && this._ensureArray(model[k]);
226
+ if (!Array.isArray(data)) {
227
+ return undefined;
228
+ }
229
+ return data;
230
+ }
231
+ /**
232
+ * Reads the value of the `@id` property.
233
+ * @param model Amf model to extract the value from.
234
+ * @param untrustedKey Model key to search for the @id
235
+ * @param context A context to use. If not set, it looks for the context of the passed model
236
+ */
237
+ _getLinkValue(model, untrustedKey, context) {
238
+ const k = this._getAmfKey(untrustedKey, context);
239
+ if (!k) {
240
+ return undefined;
241
+ }
242
+ let data = model && model[k];
243
+ if (!data) {
244
+ return undefined;
245
+ }
246
+ if (Array.isArray(data)) {
247
+ [data] = data;
248
+ }
249
+ if (!data) {
250
+ return undefined;
251
+ }
252
+ return data['@id'];
253
+ }
254
+ /**
255
+ * Reads the list of value for the `@id` property.
256
+ * @param model Amf model to extract the value from.
257
+ * @param untrustedKey Model key to search for the @id
258
+ * @param context A context to use. If not set, it looks for the context of the passed model
259
+ */
260
+ _getLinkValues(model, untrustedKey, context) {
261
+ const k = this._getAmfKey(untrustedKey, context);
262
+ if (!k) {
263
+ return undefined;
264
+ }
265
+ let data = (model && model[k]);
266
+ if (!data) {
267
+ return undefined;
268
+ }
269
+ if (!Array.isArray(data)) {
270
+ data = [data];
271
+ }
272
+ return data.map(i => i['@id']);
273
+ }
274
+ /**
275
+ * Checks if a model has a type.
276
+ * @param model Model to test
277
+ * @param type Type name
278
+ * @param context A context to use. If not set, it looks for the context of the passed model
279
+ * @returns True if model has a type.
280
+ */
281
+ _hasType(model, type, context) {
282
+ const types = this._ensureArray(model && model['@type']);
283
+ if (!types || !types.length) {
284
+ return false;
285
+ }
286
+ const key = this._getAmfKey(type, context);
287
+ for (let i = 0; i < types.length; i++) {
288
+ if (types[i] === key) {
289
+ return true;
290
+ }
291
+ }
292
+ return false;
293
+ }
294
+ /**
295
+ * Checks if a shape has a property.
296
+ * @param shape The shape to test
297
+ * @param untrustedKey Property name to test
298
+ * @param context A context to use. If not set, it looks for the context of the passed model
299
+ */
300
+ _hasProperty(shape, untrustedKey, context) {
301
+ const key = this._getAmfKey(untrustedKey, context);
302
+ return !!(shape && key && key in shape);
303
+ }
304
+ /**
305
+ * Computes array value of a property in a model (shape).
306
+ *
307
+ * @param shape AMF shape object
308
+ * @param untrustedKey Property name
309
+ * @param context A context to use. If not set, it looks for the context of the passed model
310
+ */
311
+ _computePropertyArray(shape, untrustedKey, context) {
312
+ if (!shape) {
313
+ return undefined;
314
+ }
315
+ /* eslint-disable-next-line no-param-reassign */
316
+ const key = this._getAmfKey(untrustedKey, context);
317
+ if (!key) {
318
+ return undefined;
319
+ }
320
+ const data = this._ensureArray(shape && shape[key]);
321
+ if (!data || !Array.isArray(data)) {
322
+ return undefined;
323
+ }
324
+ return data;
325
+ }
326
+ /**
327
+ * Computes API version from the AMF model.
328
+ *
329
+ * @param amf
330
+ * @param context A context to use. If not set, it looks for the context of the passed model
331
+ */
332
+ _computeApiVersion(amf, context) {
333
+ const api = this._computeApi(amf);
334
+ if (!api) {
335
+ return undefined;
336
+ }
337
+ return this._getValue(api, ns.aml.vocabularies.core.version, context);
338
+ }
339
+ /**
340
+ * Computes model's `encodes` property.
341
+ *
342
+ * @param model AMF data model
343
+ * @param context A context to use. If not set, it looks for the context of the passed model
344
+ * @returns List of encodes
345
+ */
346
+ _computeEncodes(model, context) {
347
+ if (!model) {
348
+ return undefined;
349
+ }
350
+ if (Array.isArray(model)) {
351
+ [model] = model;
352
+ }
353
+ const key = this._getAmfKey(ns.aml.vocabularies.document.encodes, context);
354
+ if (!key) {
355
+ return undefined;
356
+ }
357
+ const data = model[key];
358
+ if (data) {
359
+ return Array.isArray(data) ? data[0] : data;
360
+ }
361
+ return undefined;
362
+ }
363
+ /**
364
+ * Computes list of declarations in the AMF api model.
365
+ *
366
+ * @param model AMF json/ld model for an API
367
+ * @param context A context to use. If not set, it looks for the context of the passed model
368
+ * @returns List of declarations
369
+ */
370
+ _computeDeclares(model, context) {
371
+ if (!model) {
372
+ return undefined;
373
+ }
374
+ if (Array.isArray(model)) {
375
+ [model] = model;
376
+ }
377
+ if (!model) {
378
+ return undefined;
379
+ }
380
+ const key = this._getAmfKey(ns.aml.vocabularies.document.declares, context);
381
+ if (!key) {
382
+ return undefined;
383
+ }
384
+ const data = this._ensureArray(model[key]);
385
+ return Array.isArray(data) ? data : undefined;
386
+ }
387
+ /**
388
+ * Computes list of references in the AMF api model.
389
+ *
390
+ * @param model AMF json/ld model for an API
391
+ * @param context A context to use. If not set, it looks for the context of the passed model
392
+ * @returns List of declarations
393
+ */
394
+ _computeReferences(model, context) {
395
+ if (!model) {
396
+ return undefined;
397
+ }
398
+ if (Array.isArray(model)) {
399
+ [model] = model;
400
+ }
401
+ if (!model) {
402
+ return undefined;
403
+ }
404
+ const key = this._getAmfKey(ns.aml.vocabularies.document.references, context);
405
+ if (!key) {
406
+ return undefined;
407
+ }
408
+ const data = this._ensureArray(model[key]);
409
+ return data instanceof Array ? data : undefined;
410
+ }
411
+ /**
412
+ * Computes AMF's `http://schema.org/WebAPI` model
413
+ *
414
+ * @param model AMF json/ld model for an API
415
+ * @param context A context to use. If not set, it looks for the context of the passed model
416
+ * @returns Web API declaration.
417
+ */
418
+ _computeWebApi(model, context) {
419
+ const enc = this._computeEncodes(model, context);
420
+ if (!enc) {
421
+ return undefined;
422
+ }
423
+ if (this._hasType(enc, ns.aml.vocabularies.apiContract.WebAPI, context)) {
424
+ return enc;
425
+ }
426
+ return undefined;
427
+ }
428
+ /**
429
+ * Computes AMF's `http://schema.org/API` model
430
+ *
431
+ * @param model AMF json/ld model for an API
432
+ * @param context A context to use. If not set, it looks for the context of the passed model
433
+ * @returns API declaration.
434
+ */
435
+ _computeApi(model, context) {
436
+ const enc = this._computeEncodes(model, context);
437
+ if (!enc) {
438
+ return undefined;
439
+ }
440
+ if (this._isAPI(model, context) || this._isWebAPI(model, context) || this._isAsyncAPI(model, context)) {
441
+ return enc;
442
+ }
443
+ return undefined;
444
+ }
445
+ /**
446
+ * Returns whether an AMF node is a WebAPI node
447
+ *
448
+ * @param model AMF json/ld model for an API
449
+ * @param context A context to use. If not set, it looks for the context of the passed model
450
+ */
451
+ _isWebAPI(model, context) {
452
+ const enc = this._computeEncodes(model, context);
453
+ if (!enc) {
454
+ return false;
455
+ }
456
+ return this._hasType(enc, ns.aml.vocabularies.apiContract.WebAPI, context);
457
+ }
458
+ /**
459
+ * Returns whether an AMF node is an AsyncAPI node
460
+ *
461
+ * @param model AMF json/ld model for an API
462
+ * @param context A context to use. If not set, it looks for the context of the passed model
463
+ */
464
+ _isAsyncAPI(model, context) {
465
+ const enc = this._computeEncodes(model, context);
466
+ if (!enc) {
467
+ return false;
468
+ }
469
+ return this._hasType(enc, ns.aml.vocabularies.apiContract.AsyncAPI, context);
470
+ }
471
+ /**
472
+ * Returns whether an AMF node is an API node
473
+ *
474
+ * @param model AMF json/ld model for an API
475
+ * @param context A context to use. If not set, it looks for the context of the passed model
476
+ */
477
+ _isAPI(model, context) {
478
+ const enc = this._computeEncodes(model, context);
479
+ if (!enc) {
480
+ return false;
481
+ }
482
+ return this._hasType(enc, ns.aml.vocabularies.apiContract.API, context);
483
+ }
484
+ /**
485
+ * Determines whether a partial model is valid for reading servers from
486
+ * Current valid values:
487
+ * - Operation
488
+ * - Endpoint
489
+ * @param model The partial model to evaluate
490
+ * @param context A context to use. If not set, it looks for the context of the passed model
491
+ * @returns Whether the model's type is part of the array of valid node types from which
492
+ * to read servers
493
+ * @private
494
+ */
495
+ _isValidServerPartial(model, context) {
496
+ if (Array.isArray(model)) {
497
+ [model] = model;
498
+ }
499
+ if (!model) {
500
+ return false;
501
+ }
502
+ const oKey = ns.aml.vocabularies.apiContract.Operation;
503
+ const eKey = ns.aml.vocabularies.apiContract.EndPoint;
504
+ const allowedPartialModelTypes = [this._getAmfKey(oKey, context), this._getAmfKey(eKey, context)];
505
+ const types = model['@type'];
506
+ for (const type of types) {
507
+ if (allowedPartialModelTypes.indexOf(type) !== -1) {
508
+ return true;
509
+ }
510
+ }
511
+ return false;
512
+ }
513
+ /**
514
+ * @param options Server query options
515
+ * @param context A context to use. If not set, it looks for the context of the passed model
516
+ * @returns List of servers for method, if defined, or endpoint, if defined, or root level
517
+ */
518
+ _getServers(options = {}, context) {
519
+ const { endpointId, methodId } = options;
520
+ const { amf } = this;
521
+ if (!amf) {
522
+ return undefined;
523
+ }
524
+ let api = this._computeApi(amf, context);
525
+ if (Array.isArray(api)) {
526
+ [api] = api;
527
+ }
528
+ if (!api) {
529
+ if (this._isValidServerPartial(amf, context)) {
530
+ api = amf;
531
+ }
532
+ else {
533
+ return undefined;
534
+ }
535
+ }
536
+ const serverKey = this._getAmfKey(ns.aml.vocabularies.apiContract.server, context);
537
+ const getRootServers = () => this[getArrayItems](api, serverKey, context);
538
+ const getEndpointServers = () => {
539
+ const endpoint = this._computeEndpointModel(api, endpointId, context);
540
+ const servers = this[getArrayItems](endpoint, serverKey, context);
541
+ if (servers) {
542
+ return servers;
543
+ }
544
+ return getRootServers();
545
+ };
546
+ const getMethodServers = () => {
547
+ const method = this._computeMethodModel(api, methodId, context);
548
+ const servers = (this[getArrayItems](method, serverKey, context));
549
+ if (servers) {
550
+ return servers;
551
+ }
552
+ return getEndpointServers();
553
+ };
554
+ if (methodId) {
555
+ return getMethodServers();
556
+ }
557
+ if (endpointId) {
558
+ return getEndpointServers();
559
+ }
560
+ return getRootServers();
561
+ }
562
+ /**
563
+ * Computes value for the `expects` property.
564
+ *
565
+ * @param method AMF `supportedOperation` model
566
+ * @param context A context to use. If not set, it looks for the context of the passed model
567
+ */
568
+ _computeExpects(method, context) {
569
+ const operationKey = ns.aml.vocabularies.apiContract.Operation;
570
+ const expectsKey = ns.aml.vocabularies.apiContract.expects;
571
+ if (this._hasType(method, operationKey, context)) {
572
+ const key = this._getAmfKey(expectsKey, context);
573
+ const expects = this._ensureArray(method[key]);
574
+ if (expects) {
575
+ return Array.isArray(expects) ? expects[0] : expects;
576
+ }
577
+ }
578
+ return undefined;
579
+ }
580
+ /**
581
+ * Computes list of endpoints from a WebApi model.
582
+ * @param webApi
583
+ * @param context A context to use. If not set, it looks for the context of the passed model
584
+ * @returns An array of endpoints.
585
+ */
586
+ _computeEndpoints(webApi, context) {
587
+ if (!webApi) {
588
+ return [];
589
+ }
590
+ const endpointKey = ns.aml.vocabularies.apiContract.endpoint;
591
+ const key = this._getAmfKey(endpointKey, context);
592
+ return this._ensureArray(webApi[key]);
593
+ }
594
+ /**
595
+ * Computes model for an endpoint documentation.
596
+ *
597
+ * @param webApi Current value of `webApi` property
598
+ * @param id Selected shape ID
599
+ * @param context A context to use. If not set, it looks for the context of the passed model
600
+ * @returns An endpoint definition
601
+ */
602
+ _computeEndpointModel(webApi, id, context) {
603
+ if (this._hasType(webApi, ns.aml.vocabularies.apiContract.EndPoint, context)) {
604
+ return webApi;
605
+ }
606
+ const endpoints = this._computeEndpoints(webApi, context);
607
+ if (!endpoints) {
608
+ return undefined;
609
+ }
610
+ return endpoints.find((item) => item['@id'] === id);
611
+ }
612
+ /**
613
+ * Computes method for the method documentation.
614
+ *
615
+ * @param webApi Current value of `webApi` property
616
+ * @param selected Selected shape
617
+ * @param context A context to use. If not set, it looks for the context of the passed model
618
+ * @returns A method definition
619
+ */
620
+ _computeMethodModel(webApi, selected, context) {
621
+ const methods = this.__computeMethodsListForMethod(webApi, selected, context);
622
+ if (!methods) {
623
+ return undefined;
624
+ }
625
+ return methods.find((item) => item['@id'] === selected);
626
+ }
627
+ /**
628
+ * Computes an endpoint for a method.
629
+ * @param webApi The WebApi AMF model
630
+ * @param methodId Method id
631
+ * @param context A context to use. If not set, it looks for the context of the passed model
632
+ * @returns An endpoint model of undefined.
633
+ */
634
+ _computeMethodEndpoint(webApi, methodId, context) {
635
+ if (!webApi || !methodId) {
636
+ return undefined;
637
+ }
638
+ if (this._hasType(webApi, ns.aml.vocabularies.apiContract.EndPoint, context)) {
639
+ return webApi;
640
+ }
641
+ const endpoints = this._computeEndpoints(webApi, context);
642
+ if (!endpoints) {
643
+ return undefined;
644
+ }
645
+ const opKey = this._getAmfKey(ns.aml.vocabularies.apiContract.supportedOperation, context);
646
+ for (let i = 0, len = endpoints.length; i < len; i++) {
647
+ const endpoint = endpoints[i];
648
+ let methods = endpoint[opKey];
649
+ if (!methods) {
650
+ continue;
651
+ }
652
+ if (!Array.isArray(methods)) {
653
+ methods = [methods];
654
+ }
655
+ for (let j = 0, jLen = methods.length; j < jLen; j++) {
656
+ if (methods[j]['@id'] === methodId) {
657
+ return endpoint;
658
+ }
659
+ }
660
+ }
661
+ return undefined;
662
+ }
663
+ /**
664
+ * Computes a list of methods for an endpoint that contains a method with
665
+ * given id.
666
+ *
667
+ * @param webApi WebApi model
668
+ * @param methodId Method id.
669
+ * @param context A context to use. If not set, it looks for the context of the passed model
670
+ * @returns A list of sibling methods or undefined.
671
+ */
672
+ __computeMethodsListForMethod(webApi, methodId, context) {
673
+ const endpoint = this._computeMethodEndpoint(webApi, methodId, context);
674
+ if (!endpoint) {
675
+ return undefined;
676
+ }
677
+ const opKey = this._getAmfKey(ns.aml.vocabularies.apiContract.supportedOperation, context);
678
+ return this._ensureArray(endpoint[opKey]);
679
+ }
680
+ /**
681
+ * Computes a type documentation model.
682
+ *
683
+ * @param declares Current value of `declares` property
684
+ * @param references Current value of `references` property
685
+ * @param selected Selected shape
686
+ * @param context A context to use. If not set, it looks for the context of the passed model
687
+ * @returns A type definition
688
+ */
689
+ _computeType(declares, references, selected, context) {
690
+ if ((!declares && !references) || !selected) {
691
+ return undefined;
692
+ }
693
+ // In compact model some IDs are presented in long version (in source maps for examples)
694
+ // This must test for this case as well.
695
+ const compactId = selected.replace('amf://id', '');
696
+ let type = declares && declares.find((item) => item['@id'] === selected || item['@id'] === compactId);
697
+ if (!type && references && references.length) {
698
+ for (let i = 0, len = references.length; i < len; i++) {
699
+ if (!this._hasType(references[i], ns.aml.vocabularies.document.Module)) {
700
+ continue;
701
+ }
702
+ type = this._computeReferenceType(references[i], selected, context);
703
+ if (type) {
704
+ break;
705
+ }
706
+ }
707
+ }
708
+ return type;
709
+ }
710
+ /**
711
+ * Finds a type in the model declares and references.
712
+ * @param domainId The domain id of the type (AMF's shape).
713
+ * @param context A context to use. If not set, it looks for the context of the passed model
714
+ * @returns The AMF shape or undefined when not found.
715
+ */
716
+ [findAmfType](domainId, context) {
717
+ let { amf } = this;
718
+ if (!amf || !domainId) {
719
+ return undefined;
720
+ }
721
+ if (Array.isArray(amf)) {
722
+ [amf] = amf;
723
+ }
724
+ const declares = this._computeDeclares(amf, context);
725
+ const compactId = domainId.replace('amf://id', '');
726
+ if (Array.isArray(declares)) {
727
+ const result = declares.find((item) => item['@id'] === domainId || item['@id'] === compactId);
728
+ if (result) {
729
+ return result;
730
+ }
731
+ }
732
+ return this[findReferenceObject](domainId);
733
+ }
734
+ /**
735
+ * Searches for an object in model's references list.
736
+ * It does not resolve the object (useful for handling links correctly).
737
+ *
738
+ * @param domainId The domain of the object to find in the references.
739
+ * @param context A context to use. If not set, it looks for the context of the passed model
740
+ * @returns The domain object or undefined.
741
+ */
742
+ [findReferenceObject](domainId, context) {
743
+ let { amf } = this;
744
+ if (Array.isArray(amf)) {
745
+ [amf] = amf;
746
+ }
747
+ if (!amf) {
748
+ return undefined;
749
+ }
750
+ const references = this._computeReferences(amf, context);
751
+ if (!Array.isArray(references) || !references.length) {
752
+ return undefined;
753
+ }
754
+ const compactId = domainId.replace('amf://id', '');
755
+ for (let i = 0, len = references.length; i < len; i++) {
756
+ const ref = /** @type AmfDocument */ (references[i]);
757
+ const declares = this._computeDeclares(ref, context);
758
+ if (!Array.isArray(declares)) {
759
+ continue;
760
+ }
761
+ for (let j = 0, lenDecl = declares.length; j < lenDecl; j++) {
762
+ let declared = declares[j];
763
+ if (Array.isArray(declared)) {
764
+ [declared] = declared;
765
+ }
766
+ if (declared['@id'] === domainId || declared['@id'] === compactId) {
767
+ return declared;
768
+ }
769
+ }
770
+ }
771
+ return undefined;
772
+ }
773
+ /**
774
+ * Computes a type model from a reference (library for example).
775
+ * @param reference AMF model for a reference to extract the data from
776
+ * @param selected Node ID to look for
777
+ * @param context A context to use. If not set, it looks for the context of the passed model
778
+ * @returns Type definition or undefined if not found.
779
+ */
780
+ _computeReferenceType(reference, selected, context) {
781
+ const declare = this._computeDeclares(reference, context);
782
+ if (!declare || !selected) {
783
+ return undefined;
784
+ }
785
+ // In compact model some IDs are presented in long version (in source maps for examples)
786
+ // This must test for this case as well.
787
+ const compactId = selected.replace('amf://id', '');
788
+ let result = declare.find((item) => {
789
+ if (Array.isArray(item)) {
790
+ /* eslint-disable-next-line no-param-reassign */
791
+ [item] = item;
792
+ }
793
+ return item['@id'] === selected || item['@id'] === compactId;
794
+ });
795
+ if (Array.isArray(result)) {
796
+ [result] = result;
797
+ }
798
+ return this._resolve(result);
799
+ }
800
+ /**
801
+ * Computes a documentation model.
802
+ *
803
+ * @param webApi Current value of `webApi` property
804
+ * @param selected Selected shape
805
+ * @param context A context to use. If not set, it looks for the context of the passed model
806
+ */
807
+ _computeDocument(webApi, selected, context) {
808
+ if (!webApi || !selected) {
809
+ return undefined;
810
+ }
811
+ const key = this._getAmfKey(ns.aml.vocabularies.core.documentation, context);
812
+ const docs = this._ensureArray(webApi[key]);
813
+ return docs && docs.find((item) => item['@id'] === selected);
814
+ }
815
+ /**
816
+ * Resolves a reference to an external fragment.
817
+ *
818
+ * @param shape A shape to resolve
819
+ * @param context A context to use. If not set, it looks for the context of the passed model
820
+ * @returns Resolved shape.
821
+ */
822
+ _resolve(shape, context) {
823
+ const { amf } = this;
824
+ if (typeof shape !== 'object' || Array.isArray(shape) || !amf || shape.__apicResolved) {
825
+ return shape;
826
+ }
827
+ let refKey = this._getAmfKey(ns.aml.vocabularies.document.linkTarget, context);
828
+ let refValue = this._ensureArray(shape[refKey]);
829
+ let refData;
830
+ if (refValue) {
831
+ const rk = refValue[0]['@id'];
832
+ if (rk === shape['@id']) {
833
+ // recursive shape.
834
+ shape.__apicResolved = true;
835
+ return shape;
836
+ }
837
+ refData = this._getLinkTarget(amf, rk, context);
838
+ }
839
+ else {
840
+ refKey = this._getAmfKey(ns.aml.vocabularies.document.referenceId, context);
841
+ refValue = this._ensureArray(shape[refKey]);
842
+ if (refValue) {
843
+ const rk = refValue[0]['@id'];
844
+ if (rk === shape['@id']) {
845
+ // recursive shape.
846
+ shape.__apicResolved = true;
847
+ return shape;
848
+ }
849
+ refData = this._getReferenceId(amf, rk, context);
850
+ }
851
+ }
852
+ if (!refData) {
853
+ this._resolveRecursive(shape);
854
+ shape.__apicResolved = true;
855
+ return shape;
856
+ }
857
+ const copy = { ...refData };
858
+ delete copy['@id'];
859
+ const types = copy['@type'];
860
+ if (types) {
861
+ if (shape['@type']) {
862
+ shape['@type'] = shape['@type'].concat(types);
863
+ }
864
+ else {
865
+ shape['@type'] = types;
866
+ }
867
+ delete copy['@type'];
868
+ }
869
+ this._mergeShapes(shape, copy, context);
870
+ shape.__apicResolved = true;
871
+ this._resolveRecursive(shape);
872
+ return shape;
873
+ }
874
+ /**
875
+ * @param amf References object to search in
876
+ * @param id Id of the shape to resolve
877
+ * @param context A context to use. If not set, it looks for the context of the passed model
878
+ * @returns Resolved shape for given reference, undefined otherwise
879
+ */
880
+ _getLinkTarget(amf, id, context) {
881
+ if (!amf || !id) {
882
+ return undefined;
883
+ }
884
+ let target;
885
+ const declares = this._computeDeclares(amf, context);
886
+ if (declares) {
887
+ target = this._findById(declares, id);
888
+ }
889
+ if (!target) {
890
+ const references = this._computeReferences(amf, context);
891
+ target = this._obtainShapeFromReferences(references, id, context);
892
+ }
893
+ if (!target) {
894
+ return undefined;
895
+ }
896
+ // Declaration may contain references
897
+ target = this._resolve(target);
898
+ return target;
899
+ }
900
+ /**
901
+ * Resolves the shape of a given reference.
902
+ *
903
+ * @param references References object to search in
904
+ * @param id Id of the shape to resolve
905
+ * @param context A context to use. If not set, it looks for the context of the passed model
906
+ * @returns Resolved shape for given reference, undefined otherwise
907
+ */
908
+ _obtainShapeFromReferences(references, id, context) {
909
+ if (!Array.isArray(references) || !references.length) {
910
+ return undefined;
911
+ }
912
+ let target;
913
+ for (let i = 0; i < references.length; i++) {
914
+ const _ref = references[i];
915
+ // case of fragment that encodes the shape
916
+ const encoded = this._computeEncodes(_ref, context);
917
+ if (encoded && encoded['@id'] === id) {
918
+ target = encoded;
919
+ break;
920
+ }
921
+ // case of a library which declares types
922
+ if (!encoded) {
923
+ target = this._findById(this._computeDeclares(_ref, context), id);
924
+ if (target)
925
+ break;
926
+ }
927
+ }
928
+ return target;
929
+ }
930
+ /**
931
+ * Searches a node with a given ID in an array
932
+ *
933
+ * @param array Array to search for a given ID
934
+ * @param id Id to search for
935
+ * @returns Node with the given ID when found, undefined otherwise
936
+ */
937
+ _findById(array, id) {
938
+ if (!array)
939
+ return undefined;
940
+ let target;
941
+ for (let i = 0; i < array.length; i++) {
942
+ const _current = array[i];
943
+ if (_current && _current['@id'] === id) {
944
+ target = _current;
945
+ break;
946
+ }
947
+ }
948
+ return target;
949
+ }
950
+ _getReferenceId(amf, id, context) {
951
+ if (!amf || !id) {
952
+ return undefined;
953
+ }
954
+ const refs = this._computeReferences(amf, context);
955
+ if (!refs) {
956
+ return undefined;
957
+ }
958
+ for (let i = 0; i < refs.length; i++) {
959
+ const _ref = refs[i];
960
+ const enc = this._computeEncodes(_ref, context);
961
+ if (enc) {
962
+ if (enc['@id'] === id) {
963
+ return enc;
964
+ }
965
+ }
966
+ }
967
+ return undefined;
968
+ }
969
+ _resolveRecursive(shape, context) {
970
+ Object.keys(shape).forEach((key) => {
971
+ const currentShape = shape[key];
972
+ if (Array.isArray(currentShape)) {
973
+ for (let i = 0, len = currentShape.length; i < len; i++) {
974
+ currentShape[i] = this._resolve(currentShape[i]);
975
+ }
976
+ }
977
+ else if (typeof currentShape === 'object') {
978
+ /* eslint-disable-next-line no-param-reassign */
979
+ shape[key] = this._resolve(currentShape, context);
980
+ }
981
+ });
982
+ }
983
+ /**
984
+ * Merge two shapes together. If the resulting shape has one of the "special merge" keys,
985
+ * then the special merge function for that key will be used to match that property
986
+ * @param shapeA AMF node
987
+ * @param shapeB AMF node
988
+ * @param context A context to use. If not set, it looks for the context of the passed model
989
+ * @returns Merged AMF node
990
+ */
991
+ _mergeShapes(shapeA, shapeB, context) {
992
+ const merged = { ...shapeA, ...shapeB };
993
+ const specialMerges = [
994
+ {
995
+ key: this._getAmfKey(ns.aml.vocabularies.docSourceMaps.sources, context),
996
+ merger: this._mergeSourceMapsSources.bind(this)
997
+ },
998
+ ];
999
+ specialMerges.forEach(({ key, merger }) => {
1000
+ if (this._hasProperty(merged, key, context)) {
1001
+ merged[key] = merger(shapeA, shapeB, context);
1002
+ }
1003
+ });
1004
+ return Object.assign(shapeA, merged);
1005
+ }
1006
+ /**
1007
+ * Obtains source map sources value from two shapes and returns the merged result
1008
+ * If neither shape has a sources node, then an empty object will be returned.
1009
+ * Result is wrapped in an array as per AMF model standard
1010
+ * @param shapeA AMF node
1011
+ * @param shapeB AMF node
1012
+ * @param context A context to use. If not set, it looks for the context of the passed model
1013
+ * @returns Empty object or resulting merge, wrapped in an array
1014
+ * @private
1015
+ */
1016
+ _mergeSourceMapsSources(shapeA, shapeB, context) {
1017
+ const sourcesKey = this._getAmfKey(ns.aml.vocabularies.docSourceMaps.sources, context);
1018
+ let aSources = shapeA[sourcesKey] || {};
1019
+ if (Array.isArray(aSources)) {
1020
+ aSources = aSources[0];
1021
+ }
1022
+ let bSources = shapeB[sourcesKey] || {};
1023
+ if (Array.isArray(bSources)) {
1024
+ bSources = bSources[0];
1025
+ }
1026
+ return [Object.assign(aSources, bSources)];
1027
+ }
1028
+ /**
1029
+ * Expands the key property from compacted mode to full mode.
1030
+ * @param value The value to process
1031
+ * @returns The expanded value.
1032
+ */
1033
+ [expandKey](value, context) {
1034
+ let { amf } = this;
1035
+ if (!value || typeof value !== 'string' || (!amf && !context)) {
1036
+ return value;
1037
+ }
1038
+ if (Array.isArray(amf)) {
1039
+ [amf] = amf;
1040
+ }
1041
+ const ctx = context || amf && amf['@context'];
1042
+ if (!ctx) {
1043
+ return value;
1044
+ }
1045
+ const [root, key] = value.split(':');
1046
+ if (!root || !key) {
1047
+ return value;
1048
+ }
1049
+ const prefix = ctx[root];
1050
+ if (!prefix) {
1051
+ return value;
1052
+ }
1053
+ return `${prefix}${key}`;
1054
+ }
1055
+ /**
1056
+ * Computes a security model from a reference (library for example).
1057
+ * @param domainId Domain id of the security requirement to find.
1058
+ * @param context A context to use. If not set, it looks for the context of the passed model
1059
+ * @returns Type definition or undefined if not found.
1060
+ */
1061
+ findSecurityScheme(domainId, context) {
1062
+ const { amf } = this;
1063
+ const declares = this._computeDeclares(amf, context);
1064
+ let result;
1065
+ if (declares) {
1066
+ result = declares.find((item) => item['@id'] === domainId);
1067
+ }
1068
+ if (result) {
1069
+ result = this._resolve(result);
1070
+ return result;
1071
+ }
1072
+ const references = this._computeReferences(amf, context);
1073
+ if (Array.isArray(references) && references.length) {
1074
+ for (const ref of references) {
1075
+ if (this._hasType(ref, ns.aml.vocabularies.document.Module, context)) {
1076
+ result = this[computeReferenceSecurity](ref, domainId, context);
1077
+ if (result) {
1078
+ result = this._resolve(result);
1079
+ return result;
1080
+ }
1081
+ }
1082
+ }
1083
+ }
1084
+ return undefined;
1085
+ }
1086
+ /**
1087
+ * Computes a security model from a reference (library for example).
1088
+ * @param reference AMF model for a reference to extract the data from
1089
+ * @param selected Node ID to look for
1090
+ * @param context A context to use. If not set, it looks for the context of the passed model
1091
+ * @returns Type definition or undefined if not found.
1092
+ */
1093
+ [computeReferenceSecurity](reference, selected, context) {
1094
+ const declare = this._computeDeclares(reference, context);
1095
+ if (!declare) {
1096
+ return undefined;
1097
+ }
1098
+ let result = declare.find((item) => {
1099
+ let declared = item;
1100
+ if (Array.isArray(declared)) {
1101
+ [declared] = declared;
1102
+ }
1103
+ return declared['@id'] === selected;
1104
+ });
1105
+ if (Array.isArray(result)) {
1106
+ [result] = result;
1107
+ }
1108
+ return this._resolve(result);
1109
+ }
1110
+ /**
1111
+ * Collects domain objects by a domain type.
1112
+ * @param source The element to search for declare/encoded objects.
1113
+ * @param type The domain type
1114
+ * @param context A context to use. If not set, it looks for the context of the passed model
1115
+ */
1116
+ getByType(source, type, context) {
1117
+ if (!source) {
1118
+ return [];
1119
+ }
1120
+ let result = [];
1121
+ const declares = this._computeDeclares(source);
1122
+ const key = this._getAmfKey(type, context);
1123
+ if (declares && declares.length) {
1124
+ declares.forEach((declared) => {
1125
+ if (this._hasType(declared, key)) {
1126
+ result.push(declared);
1127
+ }
1128
+ });
1129
+ }
1130
+ const references = this._computeReferences(source);
1131
+ if (Array.isArray(references) && references.length) {
1132
+ for (const ref of references) {
1133
+ if (this._hasType(ref, ns.aml.vocabularies.document.Module)) {
1134
+ const items = this.getByType(ref, type, context);
1135
+ if (items.length) {
1136
+ result = result.concat(items);
1137
+ }
1138
+ }
1139
+ }
1140
+ }
1141
+ return result;
1142
+ }
1143
+ }
1144
+ return AmfMixin;
1145
+ };
1146
+ //# sourceMappingURL=AmfMixin.js.map