@api-client/core 0.13.6 → 0.14.1

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 (174) hide show
  1. package/build/src/amf/AmfTypes.d.ts +1 -1
  2. package/build/src/amf/AmfTypes.js +1 -1
  3. package/build/src/amf/AmfTypes.js.map +1 -1
  4. package/build/src/amf/Utils.d.ts +0 -6
  5. package/build/src/amf/Utils.d.ts.map +1 -1
  6. package/build/src/amf/Utils.js +0 -14
  7. package/build/src/amf/Utils.js.map +1 -1
  8. package/build/src/browser.d.ts +1 -0
  9. package/build/src/browser.d.ts.map +1 -1
  10. package/build/src/browser.js +1 -0
  11. package/build/src/browser.js.map +1 -1
  12. package/build/src/index.d.ts +1 -0
  13. package/build/src/index.d.ts.map +1 -1
  14. package/build/src/index.js +1 -0
  15. package/build/src/index.js.map +1 -1
  16. package/build/src/legacy.d.ts +0 -8
  17. package/build/src/legacy.d.ts.map +1 -1
  18. package/build/src/legacy.js +0 -9
  19. package/build/src/legacy.js.map +1 -1
  20. package/build/src/modeling/Bindings.d.ts +1 -1
  21. package/build/src/modeling/Bindings.js.map +1 -1
  22. package/build/src/modeling/DataDomain.js +2 -2
  23. package/build/src/modeling/DataDomain.js.map +1 -1
  24. package/build/src/modeling/DataFormat.d.ts +0 -40
  25. package/build/src/modeling/DataFormat.d.ts.map +1 -1
  26. package/build/src/modeling/DataFormat.js +0 -27
  27. package/build/src/modeling/DataFormat.js.map +1 -1
  28. package/build/src/modeling/DomainAssociation.d.ts +28 -0
  29. package/build/src/modeling/DomainAssociation.d.ts.map +1 -1
  30. package/build/src/modeling/DomainAssociation.js +73 -4
  31. package/build/src/modeling/DomainAssociation.js.map +1 -1
  32. package/build/src/modeling/DomainEntity.d.ts +25 -9
  33. package/build/src/modeling/DomainEntity.d.ts.map +1 -1
  34. package/build/src/modeling/DomainEntity.js +65 -21
  35. package/build/src/modeling/DomainEntity.js.map +1 -1
  36. package/build/src/modeling/DomainFile.d.ts +3 -4
  37. package/build/src/modeling/DomainFile.d.ts.map +1 -1
  38. package/build/src/modeling/DomainFile.js +5 -9
  39. package/build/src/modeling/DomainFile.js.map +1 -1
  40. package/build/src/modeling/DomainImpactAnalysis.d.ts +1 -1
  41. package/build/src/modeling/DomainImpactAnalysis.d.ts.map +1 -1
  42. package/build/src/modeling/DomainImpactAnalysis.js +3 -3
  43. package/build/src/modeling/DomainImpactAnalysis.js.map +1 -1
  44. package/build/src/modeling/DomainModel.d.ts +2 -2
  45. package/build/src/modeling/DomainModel.js +2 -2
  46. package/build/src/modeling/DomainModel.js.map +1 -1
  47. package/build/src/modeling/DomainProperty.d.ts +28 -12
  48. package/build/src/modeling/DomainProperty.d.ts.map +1 -1
  49. package/build/src/modeling/DomainProperty.js +61 -26
  50. package/build/src/modeling/DomainProperty.js.map +1 -1
  51. package/build/src/modeling/Semantics.d.ts +109 -0
  52. package/build/src/modeling/Semantics.d.ts.map +1 -0
  53. package/build/src/modeling/Semantics.js +97 -0
  54. package/build/src/modeling/Semantics.js.map +1 -0
  55. package/build/src/models/CertificateFile.d.ts +2 -3
  56. package/build/src/models/CertificateFile.d.ts.map +1 -1
  57. package/build/src/models/CertificateFile.js +4 -8
  58. package/build/src/models/CertificateFile.js.map +1 -1
  59. package/build/src/models/Folder.d.ts +1 -2
  60. package/build/src/models/Folder.d.ts.map +1 -1
  61. package/build/src/models/Folder.js +2 -4
  62. package/build/src/models/Folder.js.map +1 -1
  63. package/build/src/models/Project.d.ts +2 -3
  64. package/build/src/models/Project.d.ts.map +1 -1
  65. package/build/src/models/Project.js +4 -8
  66. package/build/src/models/Project.js.map +1 -1
  67. package/build/src/models/kinds.d.ts +0 -24
  68. package/build/src/models/kinds.d.ts.map +1 -1
  69. package/build/src/models/kinds.js +0 -24
  70. package/build/src/models/kinds.js.map +1 -1
  71. package/build/src/models/store/File.d.ts +1 -21
  72. package/build/src/models/store/File.d.ts.map +1 -1
  73. package/build/src/models/store/File.js +3 -23
  74. package/build/src/models/store/File.js.map +1 -1
  75. package/build/src/models/store/data_catalog.d.ts +1 -1
  76. package/build/src/models/store/data_catalog.js.map +1 -1
  77. package/build/src/sdk/FilesSdk.js +1 -1
  78. package/build/src/sdk/FilesSdk.js.map +1 -1
  79. package/build/tsconfig.tsbuildinfo +1 -1
  80. package/data/models/example-generator-api.json +22 -22
  81. package/eslint.config.js +1 -0
  82. package/package.json +5 -5
  83. package/src/amf/AmfTypes.ts +1 -1
  84. package/src/amf/Utils.ts +0 -15
  85. package/src/modeling/Bindings.ts +1 -1
  86. package/src/modeling/DataDomain.ts +2 -2
  87. package/src/modeling/DataFormat.ts +0 -48
  88. package/src/modeling/DomainAssociation.ts +66 -3
  89. package/src/modeling/DomainEntity.ts +56 -17
  90. package/src/modeling/DomainFile.ts +5 -9
  91. package/src/modeling/DomainImpactAnalysis.ts +3 -3
  92. package/src/modeling/DomainModel.ts +2 -2
  93. package/src/modeling/DomainProperty.ts +60 -21
  94. package/src/modeling/Semantics.ts +178 -0
  95. package/src/modeling/graph.md +14 -14
  96. package/src/modeling/readme.md +29 -29
  97. package/src/models/CertificateFile.ts +4 -12
  98. package/src/models/Folder.ts +2 -4
  99. package/src/models/Project.ts +4 -8
  100. package/src/models/kinds.ts +0 -25
  101. package/src/models/store/File.ts +4 -35
  102. package/src/models/store/data_catalog.ts +1 -1
  103. package/src/sdk/FilesSdk.ts +1 -1
  104. package/tests/unit/modeling/data_domain_change_observers.spec.ts +11 -10
  105. package/tests/unit/modeling/data_domain_entities.spec.ts +129 -1
  106. package/tests/unit/modeling/data_domain_property.spec.ts +1 -1
  107. package/tests/unit/modeling/domain_asociation.spec.ts +177 -0
  108. package/tests/unit/modeling/domain_entity.spec.ts +27 -26
  109. package/tests/unit/modeling/domain_entity_example_generator_json.spec.ts +11 -11
  110. package/tests/unit/modeling/domain_entity_example_generator_xml.spec.ts +10 -10
  111. package/tests/unit/modeling/domain_file.spec.ts +4 -27
  112. package/tests/unit/modeling/{domain.property.spec.ts → domain_property.spec.ts} +139 -23
  113. package/tests/unit/models/Certificate/from_name.spec.ts +3 -15
  114. package/tests/unit/models/File/constructor.spec.ts +0 -1
  115. package/tests/unit/models/File/new.spec.ts +0 -13
  116. package/tests/unit/models/File/shortcutTo.spec.ts +1 -2
  117. package/tests/unit/models/File/toJSON.spec.ts +0 -13
  118. package/tests/unit/models/File/updateByMeMeta.spec.ts +4 -6
  119. package/tests/unit/models/Folder/create.spec.ts +2 -23
  120. package/tests/unit/models/Project/create.spec.ts +6 -32
  121. package/build/src/amf/AmfShapeGenerator.d.ts +0 -103
  122. package/build/src/amf/AmfShapeGenerator.d.ts.map +0 -1
  123. package/build/src/amf/AmfShapeGenerator.js +0 -416
  124. package/build/src/amf/AmfShapeGenerator.js.map +0 -1
  125. package/build/src/modeling/legacy/DataAssociation.d.ts +0 -284
  126. package/build/src/modeling/legacy/DataAssociation.d.ts.map +0 -1
  127. package/build/src/modeling/legacy/DataAssociation.js +0 -443
  128. package/build/src/modeling/legacy/DataAssociation.js.map +0 -1
  129. package/build/src/modeling/legacy/DataEntity.d.ts +0 -358
  130. package/build/src/modeling/legacy/DataEntity.d.ts.map +0 -1
  131. package/build/src/modeling/legacy/DataEntity.js +0 -855
  132. package/build/src/modeling/legacy/DataEntity.js.map +0 -1
  133. package/build/src/modeling/legacy/DataEntityBuilder.d.ts +0 -162
  134. package/build/src/modeling/legacy/DataEntityBuilder.d.ts.map +0 -1
  135. package/build/src/modeling/legacy/DataEntityBuilder.js +0 -221
  136. package/build/src/modeling/legacy/DataEntityBuilder.js.map +0 -1
  137. package/build/src/modeling/legacy/DataImpactAnalysis.d.ts +0 -298
  138. package/build/src/modeling/legacy/DataImpactAnalysis.d.ts.map +0 -1
  139. package/build/src/modeling/legacy/DataImpactAnalysis.js +0 -441
  140. package/build/src/modeling/legacy/DataImpactAnalysis.js.map +0 -1
  141. package/build/src/modeling/legacy/DataModel.d.ts +0 -99
  142. package/build/src/modeling/legacy/DataModel.d.ts.map +0 -1
  143. package/build/src/modeling/legacy/DataModel.js +0 -237
  144. package/build/src/modeling/legacy/DataModel.js.map +0 -1
  145. package/build/src/modeling/legacy/DataNamespace.d.ts +0 -340
  146. package/build/src/modeling/legacy/DataNamespace.d.ts.map +0 -1
  147. package/build/src/modeling/legacy/DataNamespace.js +0 -784
  148. package/build/src/modeling/legacy/DataNamespace.js.map +0 -1
  149. package/build/src/modeling/legacy/DataProperty.d.ts +0 -332
  150. package/build/src/modeling/legacy/DataProperty.d.ts.map +0 -1
  151. package/build/src/modeling/legacy/DataProperty.js +0 -415
  152. package/build/src/modeling/legacy/DataProperty.js.map +0 -1
  153. package/build/src/models/store/DataFile.d.ts +0 -31
  154. package/build/src/models/store/DataFile.d.ts.map +0 -1
  155. package/build/src/models/store/DataFile.js +0 -92
  156. package/build/src/models/store/DataFile.js.map +0 -1
  157. package/src/amf/AmfShapeGenerator.ts +0 -477
  158. package/src/modeling/legacy/DataAssociation.ts +0 -554
  159. package/src/modeling/legacy/DataEntity.ts +0 -1019
  160. package/src/modeling/legacy/DataEntityBuilder.ts +0 -236
  161. package/src/modeling/legacy/DataImpactAnalysis.ts +0 -530
  162. package/src/modeling/legacy/DataModel.ts +0 -276
  163. package/src/modeling/legacy/DataNamespace.ts +0 -929
  164. package/src/modeling/legacy/DataProperty.ts +0 -630
  165. package/src/models/store/DataFile.ts +0 -100
  166. package/tests/unit/modeling/legacy/amf_shape_generator.spec.ts +0 -1041
  167. package/tests/unit/modeling/legacy/data_association.spec.ts +0 -710
  168. package/tests/unit/modeling/legacy/data_entity.spec.ts +0 -2061
  169. package/tests/unit/modeling/legacy/data_entity_generator_json.spec.ts +0 -987
  170. package/tests/unit/modeling/legacy/data_entity_generator_xml.spec.ts +0 -1451
  171. package/tests/unit/modeling/legacy/data_model.spec.ts +0 -395
  172. package/tests/unit/modeling/legacy/data_namespace.spec.ts +0 -1312
  173. package/tests/unit/modeling/legacy/data_property.spec.ts +0 -887
  174. package/tests/unit/modeling/legacy/impact_analysis.spec.ts +0 -373
@@ -1,554 +0,0 @@
1
- import { AmfShapeGenerator } from '../../amf/AmfShapeGenerator.js'
2
- import { IApiAssociationShape, IApiPropertyShape } from '../../amf/definitions/Shapes.js'
3
- import { nanoid } from '../../nanoid.js'
4
- import { IThing, Thing } from '../../models/Thing.js'
5
- import { DataEntity, IDataEntity } from './DataEntity.js'
6
- import { DataNamespace } from './DataNamespace.js'
7
- import { type FieldValidationMessage, ValidationError } from '../../exceptions/validation_error.js'
8
- import { DataAssociationKind } from '../../models/kinds.js'
9
- import type { AssociationBinding, AssociationBindings, AssociationWebBindings } from '../Bindings.js'
10
- import { DataAttributeAttributes, type DataAttributeAttribute } from '../DataFormat.js'
11
-
12
- /**
13
- * Describes association target of an entity
14
- * @deprecated
15
- */
16
- export interface AssociationTarget {
17
- /**
18
- * The key of the associated entity.
19
- */
20
- key: string
21
- /**
22
- * The key of the namespace to look for entities.
23
- * By default it is the current (root) namespace. When this is set the program
24
- * will look into `foreign` of the root namespace for the definition
25
- * of the entity.
26
- */
27
- namespace?: string
28
- }
29
-
30
- /**
31
- * Describes an association between entities. An association is another property of an entity.
32
- * The `name` is the name of the property and the value is the associated target or targets.
33
- *
34
- * An association can have multiple targets to allow describing the model as `allOf`, `anyOf` and `oneOf` schemas.
35
- * When the association has only one target, then generated schema cannot have union types.
36
- *
37
- * Depending on the schema translation an association can be embedded as a sub-object in the generated schema
38
- * or can be references via a primary key.
39
- * @deprecated
40
- */
41
- export interface IDataAssociation {
42
- kind: typeof DataAssociationKind
43
- /**
44
- * The key of the association.
45
- */
46
- key: string
47
- /**
48
- * The data association description.
49
- */
50
- info: IThing
51
- /**
52
- * Wether the data association is required.
53
- */
54
- required?: boolean
55
- /**
56
- * Whether the data association allows multiple items.
57
- */
58
- multiple?: boolean
59
- /**
60
- * The list of keys associated with the entity through this association.
61
- * An association without a target is considered invalid and discarded when processing the values.
62
- * When multiple associations are set then we are dealing with an union.
63
- */
64
- targets?: AssociationTarget[]
65
- /**
66
- * The schema allowing to translate the model into a specific format (like JSON, RAML, XML, etc.)
67
- *
68
- * When this is defined then it is used as the schema. When this is not defined it uses
69
- * referenced entities schemas. Note, changes in the referenced entities may not be propagated
70
- * to schemas altered by the user.
71
- */
72
- schema?: IApiAssociationShape
73
- /**
74
- * The list of bindings for this property.
75
- *
76
- * A binding defines a translation from a data model to a specific format.
77
- * For example allows to define properties required to generate AMF shape and therefore RAML/OAS shapes for web APIs
78
- * or a protocol buffer schema.
79
- */
80
- bindings?: AssociationBinding[]
81
- }
82
-
83
- /**
84
- * Describes an association between entities. An association is a relationship defined on an entity
85
- * that links it to one or more other entities. The association has a `name` which is used as the
86
- * property name in the context of the parent entity, and it connects to a set of `targets`
87
- * representing the associated entities.
88
- *
89
- * An association can have multiple targets to allow describing the model as `allOf`, `anyOf`, or `oneOf`
90
- * schemas. When the association has only one target, then the generated schema cannot have union types.
91
- *
92
- * Depending on the schema translation, an association can be embedded as a sub-object in the generated
93
- * schema, or it can be referenced via a primary key.
94
- *
95
- * ## Key Concepts
96
- *
97
- * - **Targets**: An association's `targets` specify the entities that this association points to.
98
- * Each target is defined by:
99
- * - `key`: The key of the target entity.
100
- * - `namespace` (optional): The key of the namespace where the target entity is defined.
101
- * If not specified, it defaults to the current namespace.
102
- *
103
- * - **Cardinality**:
104
- * - `multiple` (boolean): Indicates whether the association allows multiple target entities.
105
- * If `true`, it's a to-many relationship; otherwise, it's a to-one relationship.
106
- *
107
- * - **Schema**:
108
- * - `schema` (optional): A custom schema for the association, allowing manual definition of the
109
- * association's structure in specific formats (e.g., JSON, RAML, XML).
110
- * When provided, this schema overrides the automatically generated schema based on the
111
- * target entities.
112
- * - **Important**: If a custom schema is defined and changes are made to the referenced
113
- * entities, those changes **will not** be automatically propagated to the custom schema.
114
- * This means the custom schema may become outdated and no longer accurately reflect the
115
- * structure of the target entities.
116
- *
117
- * - **Hidden**:
118
- * - `hidden`: (boolean) Defines if this association is a part of the schema or not.
119
- * @deprecated
120
- */
121
- export class DataAssociation {
122
- kind = DataAssociationKind
123
-
124
- key = ''
125
-
126
- /**
127
- * The description of the data association.
128
- */
129
- info: Thing = Thing.fromName('')
130
-
131
- /**
132
- * Wether the data association is required.
133
- */
134
- required?: boolean
135
-
136
- /**
137
- * Whether the data association allows multiple items.
138
- */
139
- multiple?: boolean
140
-
141
- /**
142
- * The list of keys associated with the entity through this association.
143
- * An association without a target is considered invalid and discarded when processing the values.
144
- * When multiple associations are set then we are dealing with an union.
145
- */
146
- targets: AssociationTarget[] = []
147
-
148
- /**
149
- * The schema allowing to translate the model into a specific format (like JSON, RAML, XML, etc.)
150
- *
151
- * When this is defined then it is used as the schema. When this is not defined it uses
152
- * referenced entities schemas. Note, changes in the referenced entities may not be propagated
153
- * to schemas altered by the user.
154
- */
155
- schema?: IApiAssociationShape
156
-
157
- /**
158
- * The list of bindings for this property.
159
- *
160
- * A binding defines a translation from a data model to a specific format.
161
- * For example allows to define properties required to generate AMF shape and therefore RAML/OAS shapes for web APIs
162
- * or a protocol buffer schema.
163
- */
164
- bindings: AssociationBinding[] = []
165
-
166
- /**
167
- * Returns true when the association has 0 or 1 targets.
168
- */
169
- get isSingle(): boolean {
170
- return this.targets.length < 2
171
- }
172
-
173
- /**
174
- * Creates an instance of DataAssociation from a target entity key.
175
- * Note, this entity will have no name. The default name is used instead.
176
- *
177
- * @param root The namespace root.
178
- * @param target The target entity key.
179
- * @param namespace The optional key of the target association when different to the current one.
180
- * @returns A new `DataAssociation` instance.
181
- */
182
- static fromTarget(root: DataNamespace, target: string, namespace?: string): DataAssociation {
183
- const assoc = new DataAssociation(root)
184
- const info: AssociationTarget = {
185
- key: target,
186
- }
187
- if (namespace) {
188
- info.namespace = namespace
189
- }
190
- assoc.targets = [info]
191
- return assoc
192
- }
193
-
194
- /**
195
- * Creates an instance of DataAssociation from a name, without defining a target.
196
- *
197
- * @param root The namespace root.
198
- * @param name The name of the association.
199
- * @returns A new `DataAssociation` instance.
200
- */
201
- static fromName(root: DataNamespace, name: string): DataAssociation {
202
- const assoc = new DataAssociation(root)
203
- assoc.info = Thing.fromName(name)
204
- return assoc
205
- }
206
-
207
- /**
208
- * @param input The data association definition to restore.
209
- */
210
- constructor(
211
- public root: DataNamespace,
212
- input?: string | IDataAssociation
213
- ) {
214
- let init: IDataAssociation
215
- if (typeof input === 'string') {
216
- try {
217
- init = JSON.parse(input)
218
- } catch {
219
- throw new ValidationError([
220
- {
221
- field: '',
222
- message: 'Unable to parse the data association input as JSON.',
223
- rule: 'object_required',
224
- },
225
- ])
226
- }
227
- } else if (typeof input === 'object') {
228
- init = input
229
- } else {
230
- init = {
231
- kind: DataAssociationKind,
232
- key: nanoid(),
233
- info: Thing.fromName('Unnamed association').toJSON(),
234
- }
235
- }
236
- this.new(init)
237
- }
238
-
239
- /**
240
- * Initializes the association using provided data.
241
- *
242
- * @param init - The data association definition to restore.
243
- */
244
- new(init: IDataAssociation): this {
245
- DataAssociation.validate(init)
246
- const { info, key = nanoid(), kind = DataAssociationKind, schema, multiple, required, targets, bindings } = init
247
- this.kind = kind
248
- this.key = key
249
- if (info) {
250
- this.info = new Thing(info)
251
- } else {
252
- this.info = Thing.fromName('')
253
- }
254
- if (schema) {
255
- this.schema = structuredClone(schema)
256
- } else {
257
- this.schema = undefined
258
- }
259
- if (typeof multiple === 'boolean') {
260
- this.multiple = multiple
261
- } else {
262
- this.multiple = undefined
263
- }
264
- if (typeof required === 'boolean') {
265
- this.required = required
266
- } else {
267
- this.required = undefined
268
- }
269
- if (Array.isArray(targets)) {
270
- this.targets = targets.map((i) => ({ ...i }))
271
- } else {
272
- this.targets = []
273
- }
274
- if (Array.isArray(bindings)) {
275
- this.bindings = bindings.map((i) => structuredClone(i))
276
- } else {
277
- this.bindings = []
278
- }
279
- return this
280
- }
281
-
282
- /**
283
- * Validates if the input object is a valid `DataAssociation` definition.
284
- * Throws a `ValidationError` if the input is invalid.
285
- *
286
- * @param input The object to validate.
287
- * @throws {ValidationError} When the validation fails.
288
- * @deprecated
289
- */
290
- static validate(input: unknown): void {
291
- const typed = input as IDataAssociation
292
- const messages: FieldValidationMessage[] = []
293
- if (!typed) {
294
- messages.push({
295
- field: '',
296
- message: 'The input is required. None given',
297
- rule: 'required',
298
- })
299
- throw new ValidationError(messages)
300
- }
301
- if (typed.kind !== DataAssociationKind) {
302
- messages.push({
303
- field: 'kind',
304
- message: `The kind property must be ${DataAssociationKind}`,
305
- rule: 'invalid',
306
- })
307
- }
308
- if (typed.info && typeof typed.info !== 'object') {
309
- messages.push({
310
- field: 'info',
311
- message: 'The info property must be an object',
312
- rule: 'invalid',
313
- })
314
- }
315
- if (messages.length) {
316
- throw new ValidationError(messages)
317
- }
318
- }
319
-
320
- /**
321
- * Generates an object representing the association, suitable for serialization.
322
- *
323
- * @returns A plain object representation of the `DataAssociation`.
324
- */
325
- toJSON(): IDataAssociation {
326
- const result: IDataAssociation = {
327
- kind: DataAssociationKind,
328
- key: this.key,
329
- info: this.info.toJSON(),
330
- }
331
- if (this.schema) {
332
- result.schema = structuredClone(this.schema)
333
- }
334
- if (typeof this.multiple === 'boolean') {
335
- result.multiple = this.multiple
336
- }
337
- if (typeof this.required === 'boolean') {
338
- result.required = this.required
339
- }
340
- if (Array.isArray(this.targets) && this.targets.length) {
341
- result.targets = this.targets.map((i) => ({ ...i }))
342
- }
343
- if (Array.isArray(this.bindings) && this.bindings.length) {
344
- result.bindings = this.bindings.map((i) => structuredClone(i))
345
- }
346
- return result
347
- }
348
-
349
- /**
350
- * Retrieves the list of target entities.
351
- *
352
- * @returns An array of `DataEntity` instances representing the targets of this association.
353
- */
354
- getTargets(): DataEntity[] {
355
- const { targets } = this
356
- return this.root.findAssociatedEntities(targets).filter((i) => !!i) as DataEntity[]
357
- }
358
-
359
- /**
360
- * Removes self from the parent entity and the namespace definition.
361
- */
362
- remove(): void {
363
- const { root } = this
364
- const entity = root.definitions.entities.find((i) => i.associations.some((j) => j.key === this.key))
365
- if (entity) {
366
- const assocIndex = entity.associations.findIndex((i) => i.key === this.key)
367
- entity.associations.splice(assocIndex, 1)
368
- }
369
- const defIndex = this.root.definitions.associations.findIndex((i) => i.key === this.key)
370
- if (defIndex >= 0) {
371
- this.root.definitions.associations.splice(defIndex, 1)
372
- }
373
- }
374
-
375
- /**
376
- * Adds a target entity to the association.
377
- * If the entity comes from another namespace then it is set as a foreign entity.
378
- *
379
- * @param entity - The `DataEntity` instance to add.
380
- */
381
- addTarget(entity: DataEntity): void
382
-
383
- /**
384
- * Adds a target entity to the association.
385
- *
386
- * @param entity - The `IDataEntity` definition or just its key.
387
- * @param namespace - The key of the foreign namespace this entity belongs to.
388
- * Do not set this value for local entities.
389
- */
390
- addTarget(entity: IDataEntity | string, namespace?: string): void
391
-
392
- /**
393
- * @param init The key of an entity, its instance, or schema.
394
- * @param namespace - The key of the foreign namespace this entity belongs to.
395
- * Do not set this value for local entities.
396
- */
397
- addTarget(init: string | DataEntity | IDataEntity, namespace?: string): void {
398
- let key: string
399
- if (typeof init === 'string') {
400
- key = init
401
- } else {
402
- key = init.key
403
- const typed = init as IDataEntity & { root?: DataNamespace }
404
- if (!namespace && typed.root && typed.root !== this.root) {
405
- namespace = typed.root.key
406
- }
407
- }
408
- if (this.targets.some((i) => i.key === key)) {
409
- const message = `Target ${key} already exists.`
410
- throw new ValidationError(
411
- [
412
- {
413
- field: 'targets',
414
- message,
415
- rule: 'unique',
416
- },
417
- ],
418
- { message }
419
- )
420
- }
421
- if (namespace && namespace !== this.root.key) {
422
- const foreignNamespace = this.root.foreign.find((ns) => ns.key === namespace)
423
- if (!foreignNamespace) {
424
- throw new Error(`Foreign namespace "${namespace}" does not exist.`)
425
- }
426
- }
427
- const info: AssociationTarget = {
428
- key,
429
- }
430
- if (namespace) {
431
- info.namespace = namespace
432
- }
433
- this.targets.push(info)
434
- }
435
-
436
- /**
437
- * Removes a target entity from the targets list.
438
- *
439
- * @param init The key of an entity, its instance, or schema.
440
- */
441
- removeTarget(init: string | DataEntity | IDataEntity): void {
442
- let key: string
443
- if (typeof init === 'string') {
444
- key = init
445
- } else {
446
- key = init.key
447
- }
448
- const index = this.targets.findIndex((i) => i.key === key)
449
- if (index >= 0) {
450
- this.targets.splice(index, 1)
451
- }
452
- }
453
-
454
- /**
455
- * Creates a Property Shape of AMF.
456
- * The property itself is auto-generated. If the `schema` is defined then it is used
457
- * as the `range` of the property. Otherwise basic shape is generated for the range.
458
- *
459
- * This is a preferred way of reading the AMF shape as this synchronizes changed
460
- * data properties with the shape definition.
461
- *
462
- * @returns AMF property shape definition.
463
- */
464
- toApiShape(): IApiPropertyShape {
465
- const serializer = new AmfShapeGenerator()
466
- return serializer.associationProperty(this)
467
- }
468
-
469
- /**
470
- * @deprecated Use the `getParentInstance()` method instead.
471
- */
472
- getParent(): DataEntity {
473
- return this.getParentInstance()
474
- }
475
-
476
- /**
477
- * Retrieves the parent entity of this association.
478
- *
479
- * @returns The `DataEntity` instance that contains this association.
480
- */
481
- getParentInstance(): DataEntity {
482
- // this is forced as an association is only created when an entity is created
483
- // so it has to be defined.
484
- return this.root.definitions.entities.find((i) => i.associations.some((a) => a.key === this.key)) as DataEntity
485
- }
486
-
487
- /**
488
- * Creates the `schema` object if missing and returns it.
489
- * @returns The schema object.
490
- */
491
- ensureSchema(): IApiAssociationShape {
492
- if (!this.schema) {
493
- this.schema = {}
494
- }
495
- return this.schema
496
- }
497
-
498
- /**
499
- * Checks whether the passed value is one of the supported data property attributes.
500
- * @param value The value to test
501
- * @returns True when the passed value is one of the supported data property attributes.
502
- * @deprecated
503
- */
504
- static isValidAttribute(value: unknown): value is DataAttributeAttribute {
505
- if (typeof value !== 'string') {
506
- return false
507
- }
508
- return DataAttributeAttributes.includes(value as DataAttributeAttribute)
509
- }
510
-
511
- /**
512
- * Creates if not existing and returns web bindings definition.
513
- * @returns The web binding definition
514
- */
515
- getWebBinding(): AssociationWebBindings {
516
- let object = this.bindings.find((i) => i.type === 'web') as AssociationBinding | undefined
517
- if (!object) {
518
- object = { type: 'web', schema: {} } as AssociationBinding
519
- this.bindings.push(object)
520
- }
521
- return object.schema as AssociationWebBindings
522
- }
523
-
524
- /**
525
- * Returns the schema value of the binding, if any was created.
526
- * @param type The type of the binding to read.
527
- * @returns The binding schema, if any
528
- */
529
- readBinding(type: 'web' | 'protobuf'): AssociationBindings | undefined {
530
- const item = this.bindings.find((i) => i.type === type) as AssociationBinding
531
- return item?.schema
532
- }
533
-
534
- /**
535
- * Checks whether the association is a child of the given namespace, data model, or an entity.
536
- * The relationship doesn't have to be direct, as long as the association is in the hierarchy it will return true.
537
- *
538
- * @param key The key of the parent to check.
539
- * @returns True if this data association is a child of the given namespace, data model, or an entity
540
- */
541
- isChildOf(key: string): boolean {
542
- if (this.key === key) {
543
- return false
544
- }
545
- const parent = this.getParentInstance()
546
- if (!parent) {
547
- return false
548
- }
549
- if (parent.key === key) {
550
- return true
551
- }
552
- return parent.isChildOf(key)
553
- }
554
- }