@api-client/core 0.11.11 → 0.12.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.
- package/Testing.md +1 -1
- package/bin/plugins/events/EventPlugin.ts +61 -0
- package/bin/plugins/events/assert.ts +193 -0
- package/bin/plugins/events/types.ts +6 -0
- package/bin/test.ts +8 -1
- package/build/src/amf/AmfShapeGenerator.d.ts +6 -3
- package/build/src/amf/AmfShapeGenerator.d.ts.map +1 -1
- package/build/src/amf/AmfShapeGenerator.js +7 -4
- package/build/src/amf/AmfShapeGenerator.js.map +1 -1
- package/build/src/amf/AmfTypes.d.ts +2 -2
- package/build/src/amf/AmfTypes.d.ts.map +1 -1
- package/build/src/amf/AmfTypes.js.map +1 -1
- package/build/src/amf/DataValueGenerator.d.ts +15 -15
- package/build/src/amf/DataValueGenerator.d.ts.map +1 -1
- package/build/src/amf/DataValueGenerator.js +17 -16
- package/build/src/amf/DataValueGenerator.js.map +1 -1
- package/build/src/amf/models/AmfDataNode.d.ts.map +1 -1
- package/build/src/amf/models/AmfDataNode.js +2 -2
- package/build/src/amf/models/AmfDataNode.js.map +1 -1
- package/build/src/browser.d.ts +12 -9
- package/build/src/browser.d.ts.map +1 -1
- package/build/src/browser.js +11 -8
- package/build/src/browser.js.map +1 -1
- package/build/src/exceptions/attach_exception.d.ts +11 -0
- package/build/src/exceptions/attach_exception.d.ts.map +1 -0
- package/build/src/exceptions/attach_exception.js +11 -0
- package/build/src/exceptions/attach_exception.js.map +1 -0
- package/build/src/exceptions/detach_exception.d.ts +11 -0
- package/build/src/exceptions/detach_exception.d.ts.map +1 -0
- package/build/src/exceptions/detach_exception.js +11 -0
- package/build/src/exceptions/detach_exception.js.map +1 -0
- package/build/src/exceptions/remove_model_exception.d.ts +8 -0
- package/build/src/exceptions/remove_model_exception.d.ts.map +1 -0
- package/build/src/exceptions/remove_model_exception.js +8 -0
- package/build/src/exceptions/remove_model_exception.js.map +1 -0
- package/build/src/exceptions/remove_namespace_exception.d.ts +8 -0
- package/build/src/exceptions/remove_namespace_exception.d.ts.map +1 -0
- package/build/src/exceptions/remove_namespace_exception.js +8 -0
- package/build/src/exceptions/remove_namespace_exception.js.map +1 -0
- package/build/src/index.d.ts +12 -9
- package/build/src/index.d.ts.map +1 -1
- package/build/src/index.js +11 -8
- package/build/src/index.js.map +1 -1
- package/build/src/legacy.d.ts +8 -0
- package/build/src/legacy.d.ts.map +1 -1
- package/build/src/legacy.js +9 -0
- package/build/src/legacy.js.map +1 -1
- package/build/src/lib/uuid.d.ts +1 -1
- package/build/src/lib/uuid.js +1 -1
- package/build/src/lib/uuid.js.map +1 -1
- package/build/src/mocking/lib/History.js +8 -8
- package/build/src/mocking/lib/History.js.map +1 -1
- package/build/src/mocking/lib/HostRules.js +1 -1
- package/build/src/mocking/lib/HostRules.js.map +1 -1
- package/build/src/mocking/lib/User.js +2 -2
- package/build/src/mocking/lib/User.js.map +1 -1
- package/build/src/modeling/Bindings.d.ts +2 -2
- package/build/src/modeling/Bindings.d.ts.map +1 -1
- package/build/src/modeling/Bindings.js.map +1 -1
- package/build/src/modeling/DataDomain.d.ts +601 -0
- package/build/src/modeling/DataDomain.d.ts.map +1 -0
- package/build/src/modeling/DataDomain.js +1141 -0
- package/build/src/modeling/DataDomain.js.map +1 -0
- package/build/src/modeling/DataFormat.d.ts +42 -41
- package/build/src/modeling/DataFormat.d.ts.map +1 -1
- package/build/src/modeling/DataFormat.js +30 -131
- package/build/src/modeling/DataFormat.js.map +1 -1
- package/build/src/modeling/DomainAssociation.d.ts +281 -0
- package/build/src/modeling/DomainAssociation.d.ts.map +1 -0
- package/build/src/modeling/DomainAssociation.js +440 -0
- package/build/src/modeling/DomainAssociation.js.map +1 -0
- package/build/src/modeling/DomainElement.d.ts +33 -0
- package/build/src/modeling/DomainElement.d.ts.map +1 -0
- package/build/src/modeling/DomainElement.js +32 -0
- package/build/src/modeling/DomainElement.js.map +1 -0
- package/build/src/modeling/DomainEntity.d.ts +383 -0
- package/build/src/modeling/DomainEntity.d.ts.map +1 -0
- package/build/src/modeling/DomainEntity.js +718 -0
- package/build/src/modeling/DomainEntity.js.map +1 -0
- package/build/src/modeling/DomainFile.d.ts +25 -0
- package/build/src/modeling/DomainFile.d.ts.map +1 -0
- package/build/src/modeling/DomainFile.js +86 -0
- package/build/src/modeling/DomainFile.js.map +1 -0
- package/build/src/modeling/DomainImpactAnalysis.d.ts +60 -47
- package/build/src/modeling/DomainImpactAnalysis.d.ts.map +1 -1
- package/build/src/modeling/DomainImpactAnalysis.js +201 -132
- package/build/src/modeling/DomainImpactAnalysis.js.map +1 -1
- package/build/src/modeling/DomainModel.d.ts +226 -0
- package/build/src/modeling/DomainModel.d.ts.map +1 -0
- package/build/src/modeling/DomainModel.js +401 -0
- package/build/src/modeling/DomainModel.js.map +1 -0
- package/build/src/modeling/DomainNamespace.d.ts +268 -0
- package/build/src/modeling/DomainNamespace.d.ts.map +1 -0
- package/build/src/modeling/DomainNamespace.js +512 -0
- package/build/src/modeling/DomainNamespace.js.map +1 -0
- package/build/src/modeling/DomainProperty.d.ts +281 -0
- package/build/src/modeling/DomainProperty.d.ts.map +1 -0
- package/build/src/modeling/DomainProperty.js +560 -0
- package/build/src/modeling/DomainProperty.js.map +1 -0
- package/build/src/modeling/DomainSerialization.d.ts +40 -0
- package/build/src/modeling/DomainSerialization.d.ts.map +1 -0
- package/build/src/modeling/DomainSerialization.js +288 -0
- package/build/src/modeling/DomainSerialization.js.map +1 -0
- package/build/src/modeling/DomainVersioning.d.ts +17 -0
- package/build/src/modeling/DomainVersioning.d.ts.map +1 -0
- package/build/src/modeling/DomainVersioning.js +124 -0
- package/build/src/modeling/DomainVersioning.js.map +1 -0
- package/build/src/modeling/GraphUtils.d.ts +8 -0
- package/build/src/modeling/GraphUtils.d.ts.map +1 -0
- package/build/src/modeling/GraphUtils.js +26 -0
- package/build/src/modeling/GraphUtils.js.map +1 -0
- package/build/src/modeling/amf/ShapeGenerator.d.ts +164 -0
- package/build/src/modeling/amf/ShapeGenerator.d.ts.map +1 -0
- package/build/src/modeling/amf/ShapeGenerator.js +492 -0
- package/build/src/modeling/amf/ShapeGenerator.js.map +1 -0
- package/build/src/modeling/{DataAssociation.d.ts → legacy/DataAssociation.d.ts} +10 -5
- package/build/src/modeling/legacy/DataAssociation.d.ts.map +1 -0
- package/build/src/modeling/{DataAssociation.js → legacy/DataAssociation.js} +11 -8
- package/build/src/modeling/legacy/DataAssociation.js.map +1 -0
- package/build/src/modeling/{DataEntity.d.ts → legacy/DataEntity.d.ts} +12 -7
- package/build/src/modeling/legacy/DataEntity.d.ts.map +1 -0
- package/build/src/modeling/{DataEntity.js → legacy/DataEntity.js} +21 -20
- package/build/src/modeling/legacy/DataEntity.js.map +1 -0
- package/build/src/modeling/{DataEntityBuilder.d.ts → legacy/DataEntityBuilder.d.ts} +3 -2
- package/build/src/modeling/legacy/DataEntityBuilder.d.ts.map +1 -0
- package/build/src/modeling/{DataEntityBuilder.js → legacy/DataEntityBuilder.js} +4 -3
- package/build/src/modeling/legacy/DataEntityBuilder.js.map +1 -0
- package/build/src/modeling/legacy/DataImpactAnalysis.d.ts +298 -0
- package/build/src/modeling/legacy/DataImpactAnalysis.d.ts.map +1 -0
- package/build/src/modeling/legacy/DataImpactAnalysis.js +441 -0
- package/build/src/modeling/legacy/DataImpactAnalysis.js.map +1 -0
- package/build/src/modeling/{DataModel.d.ts → legacy/DataModel.d.ts} +6 -4
- package/build/src/modeling/legacy/DataModel.d.ts.map +1 -0
- package/build/src/modeling/{DataModel.js → legacy/DataModel.js} +9 -8
- package/build/src/modeling/legacy/DataModel.js.map +1 -0
- package/build/src/modeling/{DataNamespace.d.ts → legacy/DataNamespace.d.ts} +22 -3
- package/build/src/modeling/legacy/DataNamespace.d.ts.map +1 -0
- package/build/src/modeling/{DataNamespace.js → legacy/DataNamespace.js} +9 -5
- package/build/src/modeling/legacy/DataNamespace.js.map +1 -0
- package/build/src/modeling/{DataProperty.d.ts → legacy/DataProperty.d.ts} +13 -5
- package/build/src/modeling/legacy/DataProperty.d.ts.map +1 -0
- package/build/src/modeling/{DataProperty.js → legacy/DataProperty.js} +10 -7
- package/build/src/modeling/legacy/DataProperty.js.map +1 -0
- package/build/src/modeling/observed.d.ts +67 -0
- package/build/src/modeling/observed.d.ts.map +1 -0
- package/build/src/modeling/observed.js +230 -0
- package/build/src/modeling/observed.js.map +1 -0
- package/build/src/modeling/types.d.ts +165 -1
- package/build/src/modeling/types.d.ts.map +1 -1
- package/build/src/modeling/types.js.map +1 -1
- package/build/src/models/AuthorizationData.js +3 -3
- package/build/src/models/AuthorizationData.js.map +1 -1
- package/build/src/models/CertificateFile.js +2 -2
- package/build/src/models/CertificateFile.js.map +1 -1
- package/build/src/models/ClientCertificate.js +5 -5
- package/build/src/models/ClientCertificate.js.map +1 -1
- package/build/src/models/Environment.js +6 -6
- package/build/src/models/Environment.js.map +1 -1
- package/build/src/models/Folder.js +2 -2
- package/build/src/models/Folder.js.map +1 -1
- package/build/src/models/HostRule.js +4 -4
- package/build/src/models/HostRule.js.map +1 -1
- package/build/src/models/HttpProject.js +12 -12
- package/build/src/models/HttpProject.js.map +1 -1
- package/build/src/models/Project.d.ts.map +1 -1
- package/build/src/models/Project.js +2 -2
- package/build/src/models/Project.js.map +1 -1
- package/build/src/models/ProjectFolder.d.ts.map +1 -1
- package/build/src/models/ProjectFolder.js +6 -6
- package/build/src/models/ProjectFolder.js.map +1 -1
- package/build/src/models/ProjectRequest.d.ts.map +1 -1
- package/build/src/models/ProjectRequest.js +8 -8
- package/build/src/models/ProjectRequest.js.map +1 -1
- package/build/src/models/ProjectSchema.js +6 -6
- package/build/src/models/ProjectSchema.js.map +1 -1
- package/build/src/models/Thing.d.ts +26 -5
- package/build/src/models/Thing.d.ts.map +1 -1
- package/build/src/models/Thing.js +193 -91
- package/build/src/models/Thing.js.map +1 -1
- package/build/src/models/kinds.d.ts +31 -6
- package/build/src/models/kinds.d.ts.map +1 -1
- package/build/src/models/kinds.js +31 -6
- package/build/src/models/kinds.js.map +1 -1
- package/build/src/models/store/DataFile.d.ts +3 -1
- package/build/src/models/store/DataFile.d.ts.map +1 -1
- package/build/src/models/store/DataFile.js +4 -2
- package/build/src/models/store/DataFile.js.map +1 -1
- package/build/src/models/store/File.d.ts.map +1 -1
- package/build/src/models/store/File.js +3 -3
- package/build/src/models/store/File.js.map +1 -1
- package/build/src/models/store/Organization.js +3 -3
- package/build/src/models/store/Organization.js.map +1 -1
- package/build/src/models/store/Permission.js +7 -7
- package/build/src/models/store/Permission.js.map +1 -1
- package/build/src/models/store/UserIdentity.js +3 -3
- package/build/src/models/store/UserIdentity.js.map +1 -1
- package/build/src/models/transformers/ArcDexieTransformer.js +4 -4
- package/build/src/models/transformers/ArcDexieTransformer.js.map +1 -1
- package/build/src/models/transformers/ArcLegacyTransformer.js +3 -3
- package/build/src/models/transformers/ArcLegacyTransformer.js.map +1 -1
- package/build/src/models/transformers/ArcPouchTransformer.js +2 -2
- package/build/src/models/transformers/ArcPouchTransformer.js.map +1 -1
- package/build/src/models/transformers/PostmanV21Transformer.js +2 -2
- package/build/src/models/transformers/PostmanV21Transformer.js.map +1 -1
- package/build/src/models/transformers/PostmanV2Transformer.js +2 -2
- package/build/src/models/transformers/PostmanV2Transformer.js.map +1 -1
- package/build/src/models/types.d.ts +12 -0
- package/build/src/models/types.d.ts.map +1 -0
- package/build/src/models/types.js +2 -0
- package/build/src/models/types.js.map +1 -0
- package/build/src/patch/PatchClient.js +2 -2
- package/build/src/patch/PatchClient.js.map +1 -1
- package/build/src/runtime/store/FilesSdk.d.ts +2 -2
- package/build/src/runtime/store/FilesSdk.d.ts.map +1 -1
- package/build/src/runtime/store/FilesSdk.js +3 -3
- package/build/src/runtime/store/FilesSdk.js.map +1 -1
- package/data/models/example-generator-api.json +8 -8
- package/package.json +20 -6
- package/readme.md +1 -1
- package/src/amf/AmfShapeGenerator.ts +10 -7
- package/src/amf/AmfTypes.ts +2 -2
- package/src/amf/DataValueGenerator.ts +24 -23
- package/src/amf/models/AmfDataNode.ts +2 -2
- package/src/exceptions/attach_exception.ts +11 -0
- package/src/exceptions/detach_exception.ts +11 -0
- package/src/exceptions/remove_model_exception.ts +8 -0
- package/src/exceptions/remove_namespace_exception.ts +8 -0
- package/src/lib/uuid.ts +1 -1
- package/src/mocking/lib/History.ts +8 -8
- package/src/mocking/lib/HostRules.ts +1 -1
- package/src/mocking/lib/User.ts +2 -2
- package/src/modeling/Bindings.ts +2 -2
- package/src/modeling/DataDomain.ts +1220 -0
- package/src/modeling/DataFormat.ts +54 -163
- package/src/modeling/DomainAssociation.ts +476 -0
- package/src/modeling/DomainElement.ts +50 -0
- package/src/modeling/DomainEntity.ts +769 -0
- package/src/modeling/DomainFile.ts +94 -0
- package/src/modeling/DomainImpactAnalysis.ts +218 -144
- package/src/modeling/DomainModel.ts +421 -0
- package/src/modeling/DomainNamespace.ts +537 -0
- package/src/modeling/DomainProperty.ts +548 -0
- package/src/modeling/DomainSerialization.ts +312 -0
- package/src/modeling/DomainVersioning.ts +144 -0
- package/src/modeling/GraphUtils.ts +28 -0
- package/src/modeling/amf/ShapeGenerator.ts +552 -0
- package/src/modeling/graph.md +115 -0
- package/src/modeling/{DataAssociation.ts → legacy/DataAssociation.ts} +15 -10
- package/src/modeling/{DataEntity.ts → legacy/DataEntity.ts} +30 -25
- package/src/modeling/{DataEntityBuilder.ts → legacy/DataEntityBuilder.ts} +5 -4
- package/src/modeling/legacy/DataImpactAnalysis.ts +530 -0
- package/src/modeling/{DataModel.ts → legacy/DataModel.ts} +12 -10
- package/src/modeling/{DataNamespace.ts → legacy/DataNamespace.ts} +25 -7
- package/src/modeling/{DataProperty.ts → legacy/DataProperty.ts} +17 -9
- package/src/modeling/observed.ts +267 -0
- package/src/modeling/types.ts +174 -1
- package/src/models/AuthorizationData.ts +3 -3
- package/src/models/CertificateFile.ts +2 -2
- package/src/models/ClientCertificate.ts +5 -5
- package/src/models/Environment.ts +6 -6
- package/src/models/Folder.ts +2 -2
- package/src/models/HostRule.ts +4 -4
- package/src/models/HttpProject.ts +12 -12
- package/src/models/Project.ts +2 -2
- package/src/models/ProjectFolder.ts +6 -6
- package/src/models/ProjectRequest.ts +8 -8
- package/src/models/ProjectSchema.ts +6 -6
- package/src/models/Thing.ts +70 -5
- package/src/models/kinds.ts +32 -6
- package/src/models/store/DataFile.ts +5 -3
- package/src/models/store/File.ts +3 -3
- package/src/models/store/Organization.ts +3 -3
- package/src/models/store/Permission.ts +7 -7
- package/src/models/store/UserIdentity.ts +3 -3
- package/src/models/transformers/ArcDexieTransformer.ts +4 -4
- package/src/models/transformers/ArcLegacyTransformer.ts +3 -3
- package/src/models/transformers/ArcPouchTransformer.ts +2 -2
- package/src/models/transformers/PostmanV21Transformer.ts +2 -2
- package/src/models/transformers/PostmanV2Transformer.ts +2 -2
- package/src/models/types.ts +11 -0
- package/src/patch/PatchClient.ts +2 -2
- package/src/runtime/store/FilesSdk.ts +5 -5
- package/tests/unit/amf/data_value_generator.spec.ts +15 -15
- package/tests/unit/legacy-transformers/ARC-legacy-import.spec.ts +1 -1
- package/tests/unit/modeling/amf/shape_generator.spec.ts +1174 -0
- package/tests/unit/modeling/data_domain.spec.ts +444 -0
- package/tests/unit/modeling/data_domain_associations.spec.ts +279 -0
- package/tests/unit/modeling/data_domain_change_observers.spec.ts +681 -0
- package/tests/unit/modeling/data_domain_entities.spec.ts +449 -0
- package/tests/unit/modeling/data_domain_foreign.spec.ts +355 -0
- package/tests/unit/modeling/data_domain_models.spec.ts +658 -0
- package/tests/unit/modeling/data_domain_namespaces.spec.ts +668 -0
- package/tests/unit/modeling/data_domain_property.spec.ts +264 -0
- package/tests/unit/modeling/data_domain_serialization.spec.ts +294 -0
- package/tests/unit/modeling/domain.property.spec.ts +822 -0
- package/tests/unit/modeling/domain_asociation.spec.ts +643 -0
- package/tests/unit/modeling/domain_asociation_targets.spec.ts +350 -0
- package/tests/unit/modeling/domain_entity.spec.ts +730 -0
- package/tests/unit/modeling/domain_entity_associations.spec.ts +330 -0
- package/tests/unit/modeling/domain_entity_example_generator_json.spec.ts +988 -0
- package/tests/unit/modeling/domain_entity_example_generator_xml.spec.ts +1451 -0
- package/tests/unit/modeling/domain_entity_fields.spec.ts +113 -0
- package/tests/unit/modeling/domain_entity_generators.spec.ts +20 -0
- package/tests/unit/modeling/domain_entity_parents.spec.ts +291 -0
- package/tests/unit/modeling/domain_entity_properties.spec.ts +305 -0
- package/tests/unit/modeling/{data_file.spec.ts → domain_file.spec.ts} +29 -85
- package/tests/unit/modeling/domain_impact_analysis.spec.ts +452 -0
- package/tests/unit/modeling/domain_model.spec.ts +568 -0
- package/tests/unit/modeling/domain_model_entities.spec.ts +408 -0
- package/tests/unit/modeling/domain_namespace.spec.ts +514 -0
- package/tests/unit/modeling/domain_namespace_models.spec.ts +324 -0
- package/tests/unit/modeling/domain_namespace_namespaces.spec.ts +404 -0
- package/tests/unit/modeling/domain_versioning.spec.ts +140 -0
- package/tests/unit/{amf → modeling/legacy}/amf_shape_generator.spec.ts +11 -11
- package/tests/unit/modeling/{data_association.spec.ts → legacy/data_association.spec.ts} +3 -11
- package/tests/unit/modeling/{data_entity.spec.ts → legacy/data_entity.spec.ts} +10 -8
- package/tests/unit/modeling/{data_entity_generator_json.spec.ts → legacy/data_entity_generator_json.spec.ts} +1 -1
- package/tests/unit/modeling/{data_entity_generator_xml.spec.ts → legacy/data_entity_generator_xml.spec.ts} +1 -1
- package/tests/unit/modeling/{data_model.spec.ts → legacy/data_model.spec.ts} +3 -3
- package/tests/unit/modeling/{data_namespace.spec.ts → legacy/data_namespace.spec.ts} +3 -10
- package/tests/unit/modeling/{data_property.spec.ts → legacy/data_property.spec.ts} +3 -6
- package/tests/unit/modeling/{impact_analysis.spec.ts → legacy/impact_analysis.spec.ts} +9 -9
- package/tests/unit/models/File/new.spec.ts +1 -1
- package/tests/unit/models/HttpProject.spec.ts +3 -3
- package/tests/unit/runtime/proxy/HttpProjectProxy.spec.ts +8 -8
- package/tsconfig.node.json +35 -0
- package/build/src/modeling/DataAssociation.d.ts.map +0 -1
- package/build/src/modeling/DataAssociation.js.map +0 -1
- package/build/src/modeling/DataEntity.d.ts.map +0 -1
- package/build/src/modeling/DataEntity.js.map +0 -1
- package/build/src/modeling/DataEntityBuilder.d.ts.map +0 -1
- package/build/src/modeling/DataEntityBuilder.js.map +0 -1
- package/build/src/modeling/DataModel.d.ts.map +0 -1
- package/build/src/modeling/DataModel.js.map +0 -1
- package/build/src/modeling/DataNamespace.d.ts.map +0 -1
- package/build/src/modeling/DataNamespace.js.map +0 -1
- package/build/src/modeling/DataProperty.d.ts.map +0 -1
- package/build/src/modeling/DataProperty.js.map +0 -1
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
const reactiveSymbol = Symbol('reactive');
|
|
3
|
+
const proxySymbol = Symbol('proxy');
|
|
4
|
+
/**
|
|
5
|
+
* Reads the raw value of the object, that is, not proxied object.
|
|
6
|
+
* @param source The source object that contains the object
|
|
7
|
+
* @param target The target object set on the source object
|
|
8
|
+
* @returns The not proxied object or undefined
|
|
9
|
+
*/
|
|
10
|
+
export function toRaw(source, target) {
|
|
11
|
+
const proxies = Reflect.get(source, proxySymbol);
|
|
12
|
+
if (!proxies) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
for (const [key, value] of proxies.entries()) {
|
|
16
|
+
if (value === target) {
|
|
17
|
+
return key;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Turns a class property or a setter into an observed property
|
|
23
|
+
* that notifies the root domain when changed.
|
|
24
|
+
*
|
|
25
|
+
* A property has to be declared with the `accessor` type to be
|
|
26
|
+
* observed.
|
|
27
|
+
*
|
|
28
|
+
* ```typescript
|
|
29
|
+
* @observed() accessor name: string | undefined
|
|
30
|
+
*
|
|
31
|
+
* @observed()
|
|
32
|
+
* set height(value: number) {
|
|
33
|
+
* this.#height = value
|
|
34
|
+
* }
|
|
35
|
+
*
|
|
36
|
+
* get height(): number {
|
|
37
|
+
* return this.#height
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* The decorator can be used in the following ways:
|
|
42
|
+
* - As a class property decorator
|
|
43
|
+
* - As a class setter decorator
|
|
44
|
+
*
|
|
45
|
+
* The property class either has to have a `root` property
|
|
46
|
+
* or a `notifyChange` method. The decorator will call the
|
|
47
|
+
* `notifyChange` method if it exists. Otherwise, it will
|
|
48
|
+
* call the `notifyChange` method of the root domain.
|
|
49
|
+
*/
|
|
50
|
+
export function observed(config = {}) {
|
|
51
|
+
return (target, context) => {
|
|
52
|
+
const { kind } = context;
|
|
53
|
+
const { deep } = config;
|
|
54
|
+
function createDeepProxy(obj, notifyChange) {
|
|
55
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
56
|
+
return obj;
|
|
57
|
+
}
|
|
58
|
+
// instead of using the target object to store the proxy info, we
|
|
59
|
+
// reflect the proxy info to the object itself. This way we can
|
|
60
|
+
// keep the stored values clean, which helps with testing (deep equal would include symbols)
|
|
61
|
+
// Keys are original objects, values are proxies
|
|
62
|
+
let proxies = Reflect.get(this, proxySymbol);
|
|
63
|
+
if (!proxies) {
|
|
64
|
+
proxies = new Map();
|
|
65
|
+
Reflect.set(this, proxySymbol, proxies);
|
|
66
|
+
}
|
|
67
|
+
if (proxies.has(obj)) {
|
|
68
|
+
return obj;
|
|
69
|
+
}
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
71
|
+
const targetObject = this;
|
|
72
|
+
const proxy = new Proxy(obj, {
|
|
73
|
+
get(target, prop) {
|
|
74
|
+
const value = Reflect.get(target, prop);
|
|
75
|
+
return createDeepProxy.bind(targetObject)(value, notifyChange);
|
|
76
|
+
},
|
|
77
|
+
set(target, prop, value) {
|
|
78
|
+
const oldValue = Reflect.get(target, prop);
|
|
79
|
+
if (oldValue !== value) {
|
|
80
|
+
Reflect.set(target, prop, value);
|
|
81
|
+
notifyChange();
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
},
|
|
85
|
+
deleteProperty(target, prop) {
|
|
86
|
+
Reflect.deleteProperty(target, prop);
|
|
87
|
+
notifyChange();
|
|
88
|
+
return true;
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
proxies.set(obj, proxy);
|
|
92
|
+
return proxy;
|
|
93
|
+
}
|
|
94
|
+
if (kind === 'accessor') {
|
|
95
|
+
return {
|
|
96
|
+
set(value) {
|
|
97
|
+
let map = Reflect.get(this, reactiveSymbol);
|
|
98
|
+
if (!map) {
|
|
99
|
+
map = {};
|
|
100
|
+
Reflect.set(this, reactiveSymbol, map);
|
|
101
|
+
}
|
|
102
|
+
if (map[context.name] === context.name) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const notify = () => {
|
|
106
|
+
if (this.domain) {
|
|
107
|
+
this.domain.notifyChange();
|
|
108
|
+
}
|
|
109
|
+
else if (this.notifyChange) {
|
|
110
|
+
this.notifyChange();
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
if (deep) {
|
|
114
|
+
value = createDeepProxy.bind(this)(value, notify);
|
|
115
|
+
}
|
|
116
|
+
if (value === undefined) {
|
|
117
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
118
|
+
delete map[context.name];
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
map[context.name] = value;
|
|
122
|
+
}
|
|
123
|
+
notify();
|
|
124
|
+
},
|
|
125
|
+
get() {
|
|
126
|
+
const map = Reflect.get(this, reactiveSymbol) || {};
|
|
127
|
+
return map[context.name];
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
else if (kind === 'setter') {
|
|
132
|
+
const { name } = context;
|
|
133
|
+
return function (value) {
|
|
134
|
+
const oldValue = this[name];
|
|
135
|
+
if (value === oldValue) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const notify = () => {
|
|
139
|
+
if (this.domain) {
|
|
140
|
+
this.domain.notifyChange();
|
|
141
|
+
}
|
|
142
|
+
else if (this.notifyChange) {
|
|
143
|
+
this.notifyChange();
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
target.call(this, value);
|
|
147
|
+
notify();
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
throw new Error(`Unsupported decorator location: ${kind}`);
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* A decorator that can be used to retarget the change event
|
|
155
|
+
* of a property to the root domain.
|
|
156
|
+
*
|
|
157
|
+
* This decorator should not be used with the `observed` decorator.
|
|
158
|
+
* It does the same thing. It will notify about a change to the
|
|
159
|
+
* property itself.
|
|
160
|
+
*/
|
|
161
|
+
export function retargetChange() {
|
|
162
|
+
return (target, context) => {
|
|
163
|
+
const { kind } = context;
|
|
164
|
+
function observer() {
|
|
165
|
+
if (this.domain) {
|
|
166
|
+
this.domain.notifyChange();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
let binding;
|
|
170
|
+
if (kind === 'accessor') {
|
|
171
|
+
return {
|
|
172
|
+
set(value) {
|
|
173
|
+
let map = Reflect.get(this, reactiveSymbol);
|
|
174
|
+
if (!map) {
|
|
175
|
+
map = {};
|
|
176
|
+
Reflect.set(this, reactiveSymbol, map);
|
|
177
|
+
}
|
|
178
|
+
if (map[context.name] === context.name) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const oldValue = map[context.name];
|
|
182
|
+
if (oldValue === value) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (value === undefined) {
|
|
186
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
187
|
+
delete map[context.name];
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
map[context.name] = value;
|
|
191
|
+
}
|
|
192
|
+
if (oldValue && binding) {
|
|
193
|
+
oldValue.removeEventListener('change', binding);
|
|
194
|
+
}
|
|
195
|
+
if (value) {
|
|
196
|
+
binding = observer.bind(this);
|
|
197
|
+
value.addEventListener('change', binding);
|
|
198
|
+
}
|
|
199
|
+
if (this.domain) {
|
|
200
|
+
this.domain.notifyChange();
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
get() {
|
|
204
|
+
const map = Reflect.get(this, reactiveSymbol) || {};
|
|
205
|
+
return map[context.name];
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
else if (kind === 'setter') {
|
|
210
|
+
const { name } = context;
|
|
211
|
+
return function (value) {
|
|
212
|
+
const oldValue = this[name];
|
|
213
|
+
if (value === oldValue) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
;
|
|
217
|
+
target.call(this, value);
|
|
218
|
+
if (oldValue && binding) {
|
|
219
|
+
oldValue.removeEventListener('change', binding);
|
|
220
|
+
}
|
|
221
|
+
if (value) {
|
|
222
|
+
binding = observer.bind(this);
|
|
223
|
+
value.addEventListener('change', binding);
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
throw new Error(`Unsupported decorator location: ${kind}`);
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
//# sourceMappingURL=observed.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observed.js","sourceRoot":"","sources":["../../../src/modeling/observed.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,MAAM,cAAc,GAAG,MAAM,CAAC,UAAU,CAAC,CAAA;AACzC,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,CAAC,CAAA;AA+BnC;;;;;GAKG;AACH,MAAM,UAAU,KAAK,CAA4B,MAAc,EAAE,MAAS;IACxE,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,WAAW,CAAwB,CAAA;IACvE,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,SAAS,CAAA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C3C,CAAC;YACH,CAAC,CAAA;QACH,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,mCAAmC,IAAI,EAAE,CAAC,CAAA;IAC5D,CAAC,CAAA;AACH,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nconst reactiveSymbol = Symbol('reactive')\nconst proxySymbol = Symbol('proxy')\n\ninterface DomainInstance {\n domain?: { notifyChange(): void }\n notifyChange?: () => void\n}\n\nexport interface PropertyDecorator {\n // accessor decorator signature\n <C extends DomainInstance, V>(\n target: ClassAccessorDecoratorTarget<C, V>,\n context: ClassAccessorDecoratorContext<C, V>\n ): ClassAccessorDecoratorResult<C, V>\n // setter decorator signature\n <C extends DomainInstance, V>(\n target: (value: V) => void,\n context: ClassSetterDecoratorContext<C, V>\n ): (this: C, value: V) => void\n}\n\ntype StandardPropertyContext<C, V> = ClassAccessorDecoratorContext<C, V> | ClassSetterDecoratorContext<C, V>\ntype StandardPropertyTarget<C, V> = ClassAccessorDecoratorTarget<C, V> | ((value: V) => void)\n\nexport interface ObserveConfig {\n /**\n * To be used when observing an object and not a primitive.\n * It creates a proxy that observes changes in the object.\n */\n deep?: boolean\n}\n\n/**\n * Reads the raw value of the object, that is, not proxied object.\n * @param source The source object that contains the object\n * @param target The target object set on the source object\n * @returns The not proxied object or undefined\n */\nexport function toRaw<T extends object = object>(source: object, target: T): T | undefined {\n const proxies = Reflect.get(source, proxySymbol) as Map<object, object>\n if (!proxies) {\n return undefined\n }\n for (const [key, value] of proxies.entries()) {\n if (value === target) {\n return key as T\n }\n }\n}\n\n/**\n * Turns a class property or a setter into an observed property\n * that notifies the root domain when changed.\n *\n * A property has to be declared with the `accessor` type to be\n * observed.\n *\n * ```typescript\n * @observed() accessor name: string | undefined\n *\n * @observed()\n * set height(value: number) {\n * this.#height = value\n * }\n *\n * get height(): number {\n * return this.#height\n * }\n * ```\n *\n * The decorator can be used in the following ways:\n * - As a class property decorator\n * - As a class setter decorator\n *\n * The property class either has to have a `root` property\n * or a `notifyChange` method. The decorator will call the\n * `notifyChange` method if it exists. Otherwise, it will\n * call the `notifyChange` method of the root domain.\n */\nexport function observed(config: ObserveConfig = {}): PropertyDecorator {\n return <C extends DomainInstance, V>(\n target: StandardPropertyTarget<C, V>,\n context: StandardPropertyContext<C, V>\n ): any => {\n const { kind } = context\n const { deep } = config\n\n function createDeepProxy(this: DomainInstance, obj: unknown, notifyChange: () => void): any {\n if (typeof obj !== 'object' || obj === null) {\n return obj\n }\n // instead of using the target object to store the proxy info, we\n // reflect the proxy info to the object itself. This way we can\n // keep the stored values clean, which helps with testing (deep equal would include symbols)\n // Keys are original objects, values are proxies\n let proxies = Reflect.get(this, proxySymbol) as Map<object, object>\n if (!proxies) {\n proxies = new Map()\n Reflect.set(this, proxySymbol, proxies)\n }\n\n if (proxies.has(obj)) {\n return obj\n }\n\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const targetObject = this\n\n const proxy = new Proxy(obj, {\n get(target, prop) {\n const value = Reflect.get(target, prop)\n return createDeepProxy.bind(targetObject)(value, notifyChange)\n },\n set(target, prop, value) {\n const oldValue = Reflect.get(target, prop)\n if (oldValue !== value) {\n Reflect.set(target, prop, value)\n notifyChange()\n }\n return true\n },\n deleteProperty(target, prop) {\n Reflect.deleteProperty(target, prop)\n notifyChange()\n return true\n },\n })\n proxies.set(obj, proxy)\n return proxy\n }\n\n if (kind === 'accessor') {\n return {\n set(this: DomainInstance, value: V): void {\n let map = Reflect.get(this, reactiveSymbol)\n if (!map) {\n map = {}\n Reflect.set(this, reactiveSymbol, map)\n }\n if (map[context.name] === context.name) {\n return\n }\n const notify = () => {\n if (this.domain) {\n this.domain.notifyChange()\n } else if (this.notifyChange) {\n this.notifyChange()\n }\n }\n if (deep) {\n value = createDeepProxy.bind(this)(value, notify)\n }\n if (value === undefined) {\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete map[context.name]\n } else {\n map[context.name] = value\n }\n notify()\n },\n\n get(): V {\n const map = Reflect.get(this, reactiveSymbol) || {}\n return map[context.name]\n },\n }\n } else if (kind === 'setter') {\n const { name } = context\n return function (this: DomainInstance, value: V): void {\n const oldValue = this[name as keyof DomainInstance]\n if (value === oldValue) {\n return\n }\n const notify = () => {\n if (this.domain) {\n this.domain.notifyChange()\n } else if (this.notifyChange) {\n this.notifyChange()\n }\n }\n ;(target as (value: V) => void).call(this, value)\n notify()\n }\n }\n throw new Error(`Unsupported decorator location: ${kind}`)\n }\n}\n\n/**\n * A decorator that can be used to retarget the change event\n * of a property to the root domain.\n *\n * This decorator should not be used with the `observed` decorator.\n * It does the same thing. It will notify about a change to the\n * property itself.\n */\nexport function retargetChange() {\n return <C extends DomainInstance, V extends EventTarget>(\n target: StandardPropertyTarget<C, V>,\n context: StandardPropertyContext<C, V>\n ): any => {\n const { kind } = context\n function observer(this: DomainInstance) {\n if (this.domain) {\n this.domain.notifyChange()\n }\n }\n let binding: ((this: DomainInstance) => void) | undefined\n if (kind === 'accessor') {\n return {\n set(this: DomainInstance, value: V): void {\n let map = Reflect.get(this, reactiveSymbol)\n if (!map) {\n map = {}\n Reflect.set(this, reactiveSymbol, map)\n }\n if (map[context.name] === context.name) {\n return\n }\n const oldValue = map[context.name]\n if (oldValue === value) {\n return\n }\n if (value === undefined) {\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete map[context.name]\n } else {\n map[context.name] = value\n }\n if (oldValue && binding) {\n oldValue.removeEventListener('change', binding)\n }\n if (value) {\n binding = observer.bind(this)\n value.addEventListener('change', binding)\n }\n if (this.domain) {\n this.domain.notifyChange()\n }\n },\n\n get(): V {\n const map = Reflect.get(this, reactiveSymbol) || {}\n return map[context.name]\n },\n }\n } else if (kind === 'setter') {\n const { name } = context\n return function (this: DomainInstance, value: V): void {\n const oldValue = this[name as keyof DomainInstance] as unknown as V\n if (value === oldValue) {\n return\n }\n ;(target as (value: V) => void).call(this, value)\n if (oldValue && binding) {\n oldValue.removeEventListener('change', binding)\n }\n if (value) {\n binding = observer.bind(this)\n value.addEventListener('change', binding)\n }\n }\n }\n throw new Error(`Unsupported decorator location: ${kind}`)\n }\n}\n"]}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
import type { Graph, GraphJson } from '@api-client/graph';
|
|
2
|
+
import type { DomainAssociation } from './DomainAssociation.js';
|
|
3
|
+
import type { DomainEntity } from './DomainEntity.js';
|
|
4
|
+
import type { DomainModel } from './DomainModel.js';
|
|
5
|
+
import type { DomainNamespace } from './DomainNamespace.js';
|
|
6
|
+
import type { DomainProperty } from './DomainProperty.js';
|
|
1
7
|
export interface DataDomainRemoveOptions {
|
|
2
8
|
/**
|
|
3
|
-
* When true, the object will be
|
|
9
|
+
* When true, the object will be forcibly removed.
|
|
4
10
|
* The resolution defined in the `ImpactResolution` class will be applied.
|
|
5
11
|
*
|
|
6
12
|
* For example, when removing an entity that is a parent to another entity, it will
|
|
@@ -11,4 +17,162 @@ export interface DataDomainRemoveOptions {
|
|
|
11
17
|
*/
|
|
12
18
|
force?: boolean;
|
|
13
19
|
}
|
|
20
|
+
export interface AssociationAddOptions {
|
|
21
|
+
/**
|
|
22
|
+
* When set, it is the the association target's origin data domain.
|
|
23
|
+
* The key of the target data domain where this target entity is defined.
|
|
24
|
+
* Only used when `key` is specified.
|
|
25
|
+
*/
|
|
26
|
+
domain?: string;
|
|
27
|
+
/**
|
|
28
|
+
* The key of the association target, if known.
|
|
29
|
+
* If the target of the association is not specified, and the association has no other targets,
|
|
30
|
+
* the association is ignored in the namespace while processing.
|
|
31
|
+
*/
|
|
32
|
+
key?: string;
|
|
33
|
+
}
|
|
34
|
+
export interface DomainGraphEdge {
|
|
35
|
+
/**
|
|
36
|
+
* Indicates that the edge is to or from a foreign domain.
|
|
37
|
+
*/
|
|
38
|
+
foreign?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* The key of the foreign domain. Always set with the `foreign` property.
|
|
41
|
+
*/
|
|
42
|
+
domain?: string;
|
|
43
|
+
/**
|
|
44
|
+
* The type of the edge.
|
|
45
|
+
* - `association` The edge is to an association object.
|
|
46
|
+
* - When coming **from** an entiry (the `v` property), that entity owns the association.
|
|
47
|
+
* - When coming **to** an entity (the `w` property), that entity is the target of the association.
|
|
48
|
+
* An association can have multiple targets.
|
|
49
|
+
* - `property` The edge is to a property object. Can only be created between an entity and a property.
|
|
50
|
+
* - The **from** (`v`) is the entity.
|
|
51
|
+
* - The **to** (`w`) is the property.
|
|
52
|
+
* - `parent` The edge is to a parent object.
|
|
53
|
+
* - The **from** (`v`) is the child entity.
|
|
54
|
+
* - The **to** (`w`) is the parent entity.
|
|
55
|
+
*/
|
|
56
|
+
type: 'association' | 'property' | 'parent';
|
|
57
|
+
}
|
|
58
|
+
export type DomainGraphNodeType = DomainNamespace | DomainModel | DomainEntity | DomainProperty | DomainAssociation;
|
|
59
|
+
/**
|
|
60
|
+
* An interface that describes the data domain dependency.
|
|
61
|
+
*/
|
|
62
|
+
export interface ForeignDomainDependency {
|
|
63
|
+
/**
|
|
64
|
+
* The system registered key of the foreign domain.
|
|
65
|
+
*/
|
|
66
|
+
key: string;
|
|
67
|
+
/**
|
|
68
|
+
* The version of the foreign domain used in this domain.
|
|
69
|
+
*/
|
|
70
|
+
version: string;
|
|
71
|
+
}
|
|
72
|
+
export interface AssociationTarget {
|
|
73
|
+
/**
|
|
74
|
+
* The key of the target entity.
|
|
75
|
+
*/
|
|
76
|
+
key: string;
|
|
77
|
+
/**
|
|
78
|
+
* The key of the target data domain.
|
|
79
|
+
* This is only set when the target is in a different data domain.
|
|
80
|
+
*/
|
|
81
|
+
domain?: string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Describes the default value set on a property schema.
|
|
85
|
+
*/
|
|
86
|
+
export interface SchemaDefaultValue {
|
|
87
|
+
/**
|
|
88
|
+
* The type of the default value.
|
|
89
|
+
*
|
|
90
|
+
* - `literal` The `value` should be used as-is.
|
|
91
|
+
* - `function` The value represents the name of the function to be used.
|
|
92
|
+
*/
|
|
93
|
+
type: 'literal' | 'function';
|
|
94
|
+
/**
|
|
95
|
+
* The actual value type depends on the `type`.
|
|
96
|
+
* It is always a string and it has to be casted to the property's data type.
|
|
97
|
+
*/
|
|
98
|
+
value: string;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* A general schema definition for a property.
|
|
102
|
+
* This is propagated to all bindings (when they support these properties).
|
|
103
|
+
*/
|
|
104
|
+
export interface PropertySchema {
|
|
105
|
+
/**
|
|
106
|
+
* The minimum value of the property.
|
|
107
|
+
*
|
|
108
|
+
* By default it is inclusive value. Use the `exclusiveMinimum` property to make it exclusive.
|
|
109
|
+
*
|
|
110
|
+
* Used with:
|
|
111
|
+
* - string
|
|
112
|
+
* - number
|
|
113
|
+
* - date
|
|
114
|
+
* - time
|
|
115
|
+
* - datetime
|
|
116
|
+
*/
|
|
117
|
+
minimum?: number;
|
|
118
|
+
/**
|
|
119
|
+
* The maximum value of the property.
|
|
120
|
+
*
|
|
121
|
+
* By default it is inclusive value. Use the `exclusiveMaximum` property to make it exclusive.
|
|
122
|
+
*
|
|
123
|
+
* Used with:
|
|
124
|
+
* - string
|
|
125
|
+
* - number
|
|
126
|
+
* - date
|
|
127
|
+
* - time
|
|
128
|
+
* - datetime
|
|
129
|
+
*/
|
|
130
|
+
maximum?: number;
|
|
131
|
+
/**
|
|
132
|
+
* When set, the minimum value is exclusive.
|
|
133
|
+
*
|
|
134
|
+
* - When `false`: value ≥ minimum.
|
|
135
|
+
* - When `true`: value > minimum.
|
|
136
|
+
*/
|
|
137
|
+
exclusiveMinimum?: boolean;
|
|
138
|
+
/**
|
|
139
|
+
* When set, the exclusiveMaximum value is exclusive.
|
|
140
|
+
*
|
|
141
|
+
* - When `false`: value ≤ maximum.
|
|
142
|
+
* - When `true`: value < minimum.
|
|
143
|
+
*/
|
|
144
|
+
exclusiveMaximum?: boolean;
|
|
145
|
+
/**
|
|
146
|
+
* The multiplier value for a number scalar.
|
|
147
|
+
*
|
|
148
|
+
* Examples:
|
|
149
|
+
* - if `1` then every integer is allowed
|
|
150
|
+
* - if `2` than every even number is allowed
|
|
151
|
+
* - if `0.5` than every number dividable by `0.5` is allowed
|
|
152
|
+
*/
|
|
153
|
+
multipleOf?: number;
|
|
154
|
+
/**
|
|
155
|
+
* The enum values for the property.
|
|
156
|
+
* They are always encoded as strings. The actual type is defined in the `dataType` property.
|
|
157
|
+
*/
|
|
158
|
+
enum?: string[];
|
|
159
|
+
/**
|
|
160
|
+
* The default value for the property.
|
|
161
|
+
* This is always encoded as a string. The actual type is defined in the `dataType` property.
|
|
162
|
+
*/
|
|
163
|
+
defaultValue?: SchemaDefaultValue;
|
|
164
|
+
/**
|
|
165
|
+
* The example values for the property.
|
|
166
|
+
* They are always encoded as strings. The actual type is defined in the `dataType` property.
|
|
167
|
+
*/
|
|
168
|
+
examples?: string[];
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The shortcut type for the data domain graph.
|
|
172
|
+
*/
|
|
173
|
+
export type DataDomainGraph = Graph<unknown, DomainGraphNodeType, DomainGraphEdge>;
|
|
174
|
+
/**
|
|
175
|
+
* The serialized version of the data domain graph.
|
|
176
|
+
*/
|
|
177
|
+
export type SerializedGraph = GraphJson<unknown, object, DomainGraphEdge>;
|
|
14
178
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/modeling/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,uBAAuB;IACtC;;;;;;;;;OASG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB"}
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/modeling/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AACzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAE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AAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B;;;;;;;OAOG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;IACf;;;OAGG;IACH,YAAY,CAAC,EAAE,kBAAkB,CAAA;IACjC;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;CACpB;AAED;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,KAAK,CAAC,OAAO,EAAE,mBAAmB,EAAE,eAAe,CAAC,CAAA;AAClF;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,eAAe,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/modeling/types.ts"],"names":[],"mappings":"","sourcesContent":["
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/modeling/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Graph, GraphJson } from '@api-client/graph'\nimport type { DomainAssociation } from './DomainAssociation.js'\nimport type { DomainEntity } from './DomainEntity.js'\nimport type { DomainModel } from './DomainModel.js'\nimport type { DomainNamespace } from './DomainNamespace.js'\nimport type { DomainProperty } from './DomainProperty.js'\n\nexport interface DataDomainRemoveOptions {\n /**\n * When true, the object will be forcibly removed.\n * The resolution defined in the `ImpactResolution` class will be applied.\n *\n * For example, when removing an entity that is a parent to another entity, it will\n * removed itself as a parent from the child entity.\n *\n * Note, this option should only be used when the DomainImpactAnalysis has been performed\n * and the user was informed of the impact.\n */\n force?: boolean\n}\n\nexport interface AssociationAddOptions {\n /**\n * When set, it is the the association target's origin data domain.\n * The key of the target data domain where this target entity is defined.\n * Only used when `key` is specified.\n */\n domain?: string\n /**\n * The key of the association target, if known.\n * If the target of the association is not specified, and the association has no other targets,\n * the association is ignored in the namespace while processing.\n */\n key?: string\n}\n\nexport interface DomainGraphEdge {\n /**\n * Indicates that the edge is to or from a foreign domain.\n */\n foreign?: boolean\n /**\n * The key of the foreign domain. Always set with the `foreign` property.\n */\n domain?: string\n /**\n * The type of the edge.\n * - `association` The edge is to an association object.\n * - When coming **from** an entiry (the `v` property), that entity owns the association.\n * - When coming **to** an entity (the `w` property), that entity is the target of the association.\n * An association can have multiple targets.\n * - `property` The edge is to a property object. Can only be created between an entity and a property.\n * - The **from** (`v`) is the entity.\n * - The **to** (`w`) is the property.\n * - `parent` The edge is to a parent object.\n * - The **from** (`v`) is the child entity.\n * - The **to** (`w`) is the parent entity.\n */\n type: 'association' | 'property' | 'parent'\n}\n\nexport type DomainGraphNodeType = DomainNamespace | DomainModel | DomainEntity | DomainProperty | DomainAssociation\n\n/**\n * An interface that describes the data domain dependency.\n */\nexport interface ForeignDomainDependency {\n /**\n * The system registered key of the foreign domain.\n */\n key: string\n /**\n * The version of the foreign domain used in this domain.\n */\n version: string\n}\n\nexport interface AssociationTarget {\n /**\n * The key of the target entity.\n */\n key: string\n /**\n * The key of the target data domain.\n * This is only set when the target is in a different data domain.\n */\n domain?: string\n}\n\n/**\n * Describes the default value set on a property schema.\n */\nexport interface SchemaDefaultValue {\n /**\n * The type of the default value.\n *\n * - `literal` The `value` should be used as-is.\n * - `function` The value represents the name of the function to be used.\n */\n type: 'literal' | 'function'\n /**\n * The actual value type depends on the `type`.\n * It is always a string and it has to be casted to the property's data type.\n */\n value: string\n}\n\n/**\n * A general schema definition for a property.\n * This is propagated to all bindings (when they support these properties).\n */\nexport interface PropertySchema {\n /**\n * The minimum value of the property.\n *\n * By default it is inclusive value. Use the `exclusiveMinimum` property to make it exclusive.\n *\n * Used with:\n * - string\n * - number\n * - date\n * - time\n * - datetime\n */\n minimum?: number\n /**\n * The maximum value of the property.\n *\n * By default it is inclusive value. Use the `exclusiveMaximum` property to make it exclusive.\n *\n * Used with:\n * - string\n * - number\n * - date\n * - time\n * - datetime\n */\n maximum?: number\n /**\n * When set, the minimum value is exclusive.\n *\n * - When `false`: value ≥ minimum.\n * - When `true`: value > minimum.\n */\n exclusiveMinimum?: boolean\n /**\n * When set, the exclusiveMaximum value is exclusive.\n *\n * - When `false`: value ≤ maximum.\n * - When `true`: value < minimum.\n */\n exclusiveMaximum?: boolean\n /**\n * The multiplier value for a number scalar.\n *\n * Examples:\n * - if `1` then every integer is allowed\n * - if `2` than every even number is allowed\n * - if `0.5` than every number dividable by `0.5` is allowed\n */\n multipleOf?: number\n /**\n * The enum values for the property.\n * They are always encoded as strings. The actual type is defined in the `dataType` property.\n */\n enum?: string[]\n /**\n * The default value for the property.\n * This is always encoded as a string. The actual type is defined in the `dataType` property.\n */\n defaultValue?: SchemaDefaultValue\n /**\n * The example values for the property.\n * They are always encoded as strings. The actual type is defined in the `dataType` property.\n */\n examples?: string[]\n}\n\n/**\n * The shortcut type for the data domain graph.\n */\nexport type DataDomainGraph = Graph<unknown, DomainGraphNodeType, DomainGraphEdge>\n/**\n * The serialized version of the data domain graph.\n */\nexport type SerializedGraph = GraphJson<unknown, object, DomainGraphEdge>\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { nanoid } from 'nanoid';
|
|
2
2
|
/**
|
|
3
3
|
* Represents an auth data stored in API Client's data store to be retrieved
|
|
4
4
|
* when the response status is 401.
|
|
@@ -23,7 +23,7 @@ export class AuthorizationData {
|
|
|
23
23
|
}
|
|
24
24
|
else {
|
|
25
25
|
init = {
|
|
26
|
-
key:
|
|
26
|
+
key: nanoid(),
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
this.new(init);
|
|
@@ -34,7 +34,7 @@ export class AuthorizationData {
|
|
|
34
34
|
* Note, this throws an error when the provider is not an API Client provider object.
|
|
35
35
|
*/
|
|
36
36
|
new(init) {
|
|
37
|
-
const { username, password, domain, key =
|
|
37
|
+
const { username, password, domain, key = nanoid() } = init;
|
|
38
38
|
this.username = username;
|
|
39
39
|
this.password = password;
|
|
40
40
|
this.domain = domain;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthorizationData.js","sourceRoot":"","sources":["../../../src/models/AuthorizationData.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"AuthorizationData.js","sourceRoot":"","sources":["../../../src/models/AuthorizationData.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAS/B;;;;;GAKG;AACH,MAAM,OAAO,iBAAiB;IAC5B,GAAG,GAAG,EAAE,CAAA;IACR,QAAQ,CAAS;IACjB,QAAQ,CAAS;IACjB,MAAM,CAAS;IAEf;;OAEG;IACH,YAAY,KAAmC;QAC7C,IAAI,IAAwB,CAAA;QAC5B,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;QAC1B,CAAC;aAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YACrC,IAAI,GAAG,KAAK,CAAA;QACd,CAAC;aAAM,CAAC;YACN,IAAI,GAAG;gBACL,GAAG,EAAE,MAAM,EAAE;aACd,CAAA;QACH,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IAChB,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,IAAwB;QAC1B,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,GAAG,MAAM,EAAE,EAAE,GAAG,IAAI,CAAA;QAC3D,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;IAChB,CAAC;IAED,MAAM;QACJ,MAAM,MAAM,GAAuB;YACjC,GAAG,EAAE,IAAI,CAAC,GAAG;SACd,CAAA;QACD,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAA;QACjC,CAAC;QACD,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAA;QACjC,CAAC;QACD,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,MAAM,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAA;QAC7B,CAAC;QACD,OAAO,MAAM,CAAA;IACf,CAAC;CACF","sourcesContent":["import { nanoid } from 'nanoid'\n\nexport interface IAuthorizationData {\n key: string\n username?: string\n password?: string\n domain?: string\n}\n\n/**\n * Represents an auth data stored in API Client's data store to be retrieved\n * when the response status is 401.\n *\n * Each entry represent a Basic or NTLM authorization.\n */\nexport class AuthorizationData {\n key = ''\n username?: string\n password?: string\n domain?: string\n\n /**\n * @param input The provider definition used to restore the state.\n */\n constructor(input?: string | IAuthorizationData) {\n let init: IAuthorizationData\n if (typeof input === 'string') {\n init = JSON.parse(input)\n } else if (typeof input === 'object') {\n init = input\n } else {\n init = {\n key: nanoid(),\n }\n }\n this.new(init)\n }\n\n /**\n * Creates a new provider clearing anything that is so far defined.\n *\n * Note, this throws an error when the provider is not an API Client provider object.\n */\n new(init: IAuthorizationData): void {\n const { username, password, domain, key = nanoid() } = init\n this.username = username\n this.password = password\n this.domain = domain\n this.key = key\n }\n\n toJSON(): IAuthorizationData {\n const result: IAuthorizationData = {\n key: this.key,\n }\n if (this.username) {\n result.username = this.username\n }\n if (this.password) {\n result.password = this.password\n }\n if (this.domain) {\n result.domain = this.domain\n }\n return result\n }\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { nanoid } from 'nanoid';
|
|
2
2
|
import { CertificateFileKind } from './kinds.js';
|
|
3
3
|
import { File } from './store/File.js';
|
|
4
4
|
import { Thing } from './Thing.js';
|
|
@@ -37,7 +37,7 @@ export class CertificateFile extends File {
|
|
|
37
37
|
else {
|
|
38
38
|
init = {
|
|
39
39
|
kind: CertificateFileKind,
|
|
40
|
-
key:
|
|
40
|
+
key: nanoid(),
|
|
41
41
|
organization: '',
|
|
42
42
|
info: Thing.fromName('').toJSON(),
|
|
43
43
|
parents: [],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CertificateFile.js","sourceRoot":"","sources":["../../../src/models/CertificateFile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"CertificateFile.js","sourceRoot":"","sources":["../../../src/models/CertificateFile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE/B,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAChD,OAAO,EAAS,IAAI,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAMlC,MAAM,OAAO,eAAgB,SAAQ,IAAI;IAC9B,IAAI,GAAG,mBAAmB,CAAA;IAEnC,MAAM,CAAC,eAAe,CACpB,WAAuC,EACvC,YAAoB,EACpB,KAAc;QAEd,MAAM,IAAI,GAAqB;YAC7B,IAAI,EAAE,mBAAmB;YACzB,GAAG,EAAE,WAAW,CAAC,GAAG;YACpB,YAAY;YACZ,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;YAC/C,YAAY,EAAE,EAAE,IAAI,EAAE,KAAK,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE;YACzD,KAAK;YACL,OAAO,EAAE,EAAE;YACX,aAAa,EAAE,EAAE;YACjB,WA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AAA;QAC/B,OAAO,IAAI,CAAA;IACb,CAAC;IAED,MAAM,CAAC,aAAa,CAAC,KAAc;QACjC,MAAM,KAAK,GAAG,KAAyB,CAAA;QACvC,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,mBAAmB,EAAE,CAAC;YACjD,OAAO,KAAK,CAAA;QACd,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;IAEQ,MAAM;QACb,MAAM,MAAM,GAAqB;YAC/B,GAAG,KAAK,CAAC,MAAM,EAAE;YACjB,IAAI,EAAE,mBAAmB;SAC1B,CAAA;QACD,OAAO,MAAM,CAAA;IACf,CAAC;CACF","sourcesContent":["import { nanoid } from 'nanoid'\nimport { Certificate, ICertificate } from './ClientCertificate.js'\nimport { CertificateFileKind } from './kinds.js'\nimport { IFile, File } from './store/File.js'\nimport { Thing } from './Thing.js'\n\nexport interface ICertificateFile extends IFile {\n kind: typeof CertificateFileKind\n}\n\nexport class CertificateFile extends File {\n override kind = CertificateFileKind\n\n static fromCertificate(\n certificate: ICertificate | Certificate,\n organization: string,\n owner?: string\n ): CertificateFile {\n const init: ICertificateFile = {\n kind: CertificateFileKind,\n key: certificate.key,\n organization,\n info: Thing.fromName(certificate.name).toJSON(),\n lastModified: { user: owner || '', time: 0, byMe: false },\n owner,\n parents: [],\n permissionIds: [],\n permissions: [],\n }\n return new CertificateFile(init)\n }\n\n /**\n * @param name The name to set.\n * @param owner The user id that is the owner of the file.\n */\n static override fromName(name: string, organization: string, owner?: string): CertificateFile {\n return super.fromName(name, organization, owner, CertificateFileKind) as CertificateFile\n }\n\n constructor(input?: string | ICertificateFile) {\n super()\n let init: ICertificateFile\n if (typeof input === 'string') {\n init = JSON.parse(input)\n } else if (typeof input === 'object') {\n init = input\n } else {\n init = {\n kind: CertificateFileKind,\n key: nanoid(),\n organization: '',\n info: Thing.fromName('').toJSON(),\n parents: [],\n permissionIds: [],\n permissions: [],\n lastModified: { user: '', time: 0, byMe: false },\n }\n }\n this.new(init)\n }\n\n override new(init: ICertificateFile): this {\n if (!CertificateFile.isCertificate(init)) {\n throw new Error(`Not a certificate file.`)\n }\n super.new(init)\n this.kind = CertificateFileKind\n return this\n }\n\n static isCertificate(input: unknown): boolean {\n const typed = input as ICertificateFile\n if (!input || typed.kind !== CertificateFileKind) {\n return false\n }\n return true\n }\n\n override toJSON(): ICertificateFile {\n const result: ICertificateFile = {\n ...super.toJSON(),\n kind: CertificateFileKind,\n }\n return result\n }\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { base64ToBuffer, bufferToBase64 } from '../lib/Buffer.js';
|
|
2
|
-
import
|
|
2
|
+
import { nanoid } from 'nanoid';
|
|
3
3
|
export const Kind = 'Core#Certificate';
|
|
4
4
|
/**
|
|
5
5
|
* A class that represents a certificate in the system
|
|
@@ -48,7 +48,7 @@ export class Certificate {
|
|
|
48
48
|
certKey: {
|
|
49
49
|
data: key,
|
|
50
50
|
},
|
|
51
|
-
key:
|
|
51
|
+
key: nanoid(),
|
|
52
52
|
name,
|
|
53
53
|
type: 'pem',
|
|
54
54
|
created: Date.now(),
|
|
@@ -71,7 +71,7 @@ export class Certificate {
|
|
|
71
71
|
cert: {
|
|
72
72
|
data: cert,
|
|
73
73
|
},
|
|
74
|
-
key:
|
|
74
|
+
key: nanoid(),
|
|
75
75
|
name,
|
|
76
76
|
type: 'p12',
|
|
77
77
|
created: Date.now(),
|
|
@@ -89,7 +89,7 @@ export class Certificate {
|
|
|
89
89
|
* to its original format.
|
|
90
90
|
*/
|
|
91
91
|
static fromLegacy(index, cert) {
|
|
92
|
-
const { name = '', type, created = Date.now(), _id =
|
|
92
|
+
const { name = '', type, created = Date.now(), _id = nanoid() } = index;
|
|
93
93
|
if (type === 'pem') {
|
|
94
94
|
const data = Array.isArray(cert.cert) ? cert.cert[0] : cert.cert;
|
|
95
95
|
const key = Array.isArray(cert.key) ? cert.key[0] : cert.key;
|
|
@@ -122,7 +122,7 @@ export class Certificate {
|
|
|
122
122
|
throw new Error(`Unable to create a certificate. Unknown type: ${type}.`);
|
|
123
123
|
}
|
|
124
124
|
constructor(certificate) {
|
|
125
|
-
const { type, cert, key =
|
|
125
|
+
const { type, cert, key = nanoid(), name = '', created = Date.now() } = certificate;
|
|
126
126
|
this.key = key;
|
|
127
127
|
this.name = name;
|
|
128
128
|
this.type = type;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClientCertificate.js","sourceRoot":"","sources":["../../../src/models/ClientCertificate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAS/B,MAAM,CAAC,MAAM,IAAI,GAAG,kBAAkB,CAAA;AAoHtC;;GAEG;AACH,MAAM,OAAO,WAAW;IACtB,IAAI,GAAG,IAAI,CAAA;IACX;;OAEG;IACH,GAAG,CAAQ;IACX;;OAEG;IACH,IAAI,CAAQ;IACZ;;;OAGG;IACH,OAAO,GAAG,CAAC,CAAA;IACX;;OAEG;IACH,IAAI,CAAiB;IACrB;;OAEG;IACH,IAAI,CAAkB;IACtB;;OAEG;IACH,OAAO,CAAmB;IAE1B;;;;;;;OAOG;IACH,MAAM,CAAC,OAAO,CACZ,GAA0B,EAC1B,GAA0B,EAC1B,IAAI,GAAG,qBAAqB,EAC5B,aAAsB;QAEtB,MAAM,IAAI,GAAoB;YAC5B,IAAI,EAAE,IAAI;YACV,IAAI,EAAE;gBACJ,IAAI,EAAE,GAAG;aACV;YACD,OAAO,EAAE;gBACP,IAAI,EAAE,GAAG;aACV;YACD,GAAG,EAAE,EAAE,EAAE;YACT,IAAI;YACJ,IAAI,EAAE,KAAK;YACX,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE;SACpB,CAAA;QACD,IAAI,aAAa,EAAE,CAAC;YAClB,IAAI,CAAC,OAAO,CAAC,UAAU,GAAG,aAAa,CAAA;QACzC,CAAC;QACD,OAAO,IAA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AAI,KAAK,KAAK,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACxC,MAAM,KAAK,GAAG,MAAoC,CAAA;YAClD,KAAK,CAAC,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QACnD,CAAC;QAED,OAAO,MAAM,CAAA;IACf,CAAC;CACF","sourcesContent":["import { base64ToBuffer, bufferToBase64 } from '../lib/Buffer.js'\nimport v4 from '../lib/uuid.js'\nimport {\n Certificate as LegacyCertificate,\n ARCCertificateIndex,\n RequestCertificate,\n} from './legacy/models/ClientCertificate.js'\n\nexport type CertificateType = 'p12' | 'pem'\n\nexport const Kind = 'Core#Certificate'\n\nexport type CertificateDataFormat = string | ArrayBuffer | Uint8Array\n\n/**\n * Represents a single certificate object (cert/key)\n */\nexport interface ICertificateData {\n /**\n * The certificate to use.\n * The `p12` type certificate must be a Buffer.\n */\n data: CertificateDataFormat\n /**\n * A passphrase to use to unlock the certificate.\n */\n passphrase?: string\n /**\n * The original data type of the certificate. This is only used internally by the data store\n * to move between buffers and string values stored in the store.\n * Outside the internal procedure of the data store this\n * is always `undefined` and the `data` contains the original data format.\n */\n type?: 'buffer'\n}\n\nexport interface ICertificate {\n kind: typeof Kind\n /**\n * The data store key to refer.\n */\n key: string\n /**\n * The custom name of the certificate.\n */\n name: string\n /**\n * The timestamp when the certificate was inserted into the data store.\n * Required when returning a result. Auto-generated when inserting, if missing.\n */\n created?: number\n /**\n * Certificate type. Either `p12` or `pem`.\n */\n type: CertificateType\n /**\n * Certificate or list of certificates to use.\n */\n cert: ICertificateData\n}\n\nexport interface IP12Certificate extends ICertificate {\n type: 'p12'\n}\n\n/**\n * Represents a complete certificate configuration required to make\n * an HTTP request.\n */\nexport interface IPemCertificate extends ICertificate {\n type: 'pem'\n /**\n * The key for the `pem` type certificate.\n */\n certKey: ICertificateData\n}\n\nexport type HttpCertificate = IP12Certificate | IPemCertificate | ICertificate\n\n/**\n * @deprecated Do not use this type anymore. Create a cert instance instead.\n */\nexport interface IPemCreateOptions {\n type: 'p12'\n /**\n * The certificate contents.\n */\n cert: CertificateDataFormat\n /**\n * The key contents.\n */\n key: CertificateDataFormat\n /**\n * Optional name for the certificate.\n */\n name?: string\n /**\n * Optional passphrase for the key.\n */\n passphrase?: string\n}\n\n/**\n * @deprecated Do not use this type anymore. Create a cert instance instead.\n */\nexport interface IP12CreateOptions {\n type: 'pem'\n /**\n * The certificate contents.\n */\n cert: CertificateDataFormat\n /**\n * Optional name for the certificate.\n */\n name?: string\n /**\n * Optional passphrase for the certificate.\n */\n passphrase?: string\n}\n\n/**\n * @deprecated Do not use this type anymore. Create a cert instance instead.\n */\nexport type ICertificateCreateOptions = IPemCreateOptions | IP12CreateOptions\n\n/**\n * A class that represents a certificate in the system\n */\nexport class Certificate {\n kind = Kind\n /**\n * The data store key to refer.\n */\n key: string\n /**\n * Custom name of the certificate.\n */\n name: string\n /**\n * Timestamp when the certificate was inserted into the data store.\n * Required when returning a result. Auto-generated when inserting, if missing.\n */\n created = 0\n /**\n * Certificate type. Either `p12` or `pem`.\n */\n type: CertificateType\n /**\n * Certificate or list of certificates to use.\n */\n cert: ICertificateData\n /**\n * The key for the `pem` type certificate.\n */\n certKey?: ICertificateData\n\n /**\n * Creates a new certificate instance for a PEM key\n *\n * @param pem The certificate contents\n * @param key The key contents\n * @param name The certificate name\n * @param keyPassphrase The key passphrase\n */\n static fromPem(\n pem: CertificateDataFormat,\n key: CertificateDataFormat,\n name = 'New PEM certificate',\n keyPassphrase?: string\n ): Certificate {\n const init: IPemCertificate = {\n kind: Kind,\n cert: {\n data: pem,\n },\n certKey: {\n data: key,\n },\n key: v4(),\n name,\n type: 'pem',\n created: Date.now(),\n }\n if (keyPassphrase) {\n init.certKey.passphrase = keyPassphrase\n }\n return new Certificate(init)\n }\n\n /**\n * Creates a new certificate instance for a P12 key\n *\n * @param cert The certificate contents\n * @param name The certificate name\n * @param passphrase The key passphrase\n */\n static fromP12(cert: CertificateDataFormat, name = 'New P12 certificate', passphrase?: string): Certificate {\n const init: IP12Certificate = {\n kind: Kind,\n cert: {\n data: cert,\n },\n key: v4(),\n name,\n type: 'p12',\n created: Date.now(),\n }\n if (passphrase) {\n init.cert.passphrase = passphrase\n }\n return new Certificate(init)\n }\n\n /**\n * Creates a certificate object from the ARC's legacy certificate definition.\n *\n * @param index The legacy certificate index object. If it has set `_id` it will be used as the `key`.\n * @param index The certificate data object as read from the data store. Do not restore the data\n * to its original format.\n */\n static fromLegacy(index: ARCCertificateIndex, cert: RequestCertificate): Certificate {\n const { name = '', type, created = Date.now(), _id = v4() } = index\n if (type === 'pem') {\n const data = Array.isArray(cert.cert) ? cert.cert[0] : cert.cert\n const key = Array.isArray(cert.key) ? cert.key[0] : (cert.key as LegacyCertificate)\n if (!key) {\n throw new Error(`Unable to create a PEM certificate without the key.`)\n }\n const init: IPemCertificate = {\n kind: Kind,\n cert: data as ICertificateData, // these are compatible.\n certKey: key as ICertificateData,\n key: _id,\n name,\n type: 'pem',\n created,\n }\n return new Certificate(init)\n } else if (type === 'p12') {\n const data = Array.isArray(cert.cert) ? cert.cert[0] : cert.cert\n const init: IP12Certificate = {\n kind: Kind,\n cert: data as ICertificateData, // these are compatible.\n key: _id,\n name,\n type: 'p12',\n created,\n }\n return new Certificate(init)\n }\n throw new Error(`Unable to create a certificate. Unknown type: ${type}.`)\n }\n\n constructor(certificate: HttpCertificate) {\n const { type, cert, key = v4(), name = '', created = Date.now() } = certificate\n this.key = key\n this.name = name\n this.type = type\n this.cert = Certificate.fromStore(cert)\n this.created = created\n if (type === 'pem') {\n const typed = certificate as IPemCertificate\n this.certKey = Certificate.fromStore(typed.certKey)\n }\n }\n\n /**\n * When needed it reads the certificate's original format.\n * @param data The certificate data.\n * @returns The restored certificate.\n */\n static fromStore(data: ICertificateData): ICertificateData {\n if (data.type) {\n delete data.type\n const content = data.data as string\n data.data = base64ToBuffer(content)\n }\n return data\n }\n\n /**\n * Prepares certificate object to be stored in the data store.\n * If the `data` property is not string then it assumes buffer (either\n * Node's or ArrayBuffer). In this case it converts buffer to base64 string.\n * It also adds `type` property set to `buffer` for the `fromStore()`\n * function to recognize what to do with the data.\n *\n * Note, for optimization, PEM keys should be strings as the content of the\n * certificate is already a base62 string. To spare double base64 conversion\n * use string data.\n *\n * @param data The certificate data object.\n * @throws When data is not set\n */\n static toStore(data: ICertificateData): ICertificateData {\n if (!data) {\n throw new Error('Certificate data is missing.')\n }\n if (!data.data) {\n throw new Error('Certificate content not set.')\n }\n if (typeof data.data !== 'string') {\n data.type = 'buffer'\n const buff = data.data as ArrayBuffer | Uint8Array\n data.data = bufferToBase64(buff)\n }\n return data\n }\n\n toJSON(): HttpCertificate {\n const result: HttpCertificate = {\n kind: Kind,\n key: this.key,\n cert: Certificate.toStore(this.cert),\n name: this.name,\n type: this.type,\n created: this.created,\n }\n\n if (this.type === 'pem' && this.certKey) {\n const typed = result as unknown as IPemCertificate\n typed.certKey = Certificate.toStore(this.certKey)\n }\n\n return result\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ClientCertificate.js","sourceRoot":"","sources":["../../../src/models/ClientCertificate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAS/B,MAAM,CAAC,MAAM,IAAI,GAAG,kBAAkB,CAAA;AAoHtC;;GAEG;AACH,MAAM,OAAO,WAAW;IACtB,IAAI,GAAG,IAAI,CAAA;IACX;;OAEG;IACH,GAAG,CAAQ;IACX;;OAEG;IACH,IAAI,CAAQ;IACZ;;;OAGG;IACH,OAAO,GAAG,CAAC,CAAA;IACX;;OAEG;IACH,IAAI,CAAiB;IACrB;;OAEG;IACH,IAAI,CAAkB;IACtB;;OAEG;IACH,OAAO,CAAmB;IAE1B;;;;;;;OAOG;IACH,MAAM,CAAC,OAAO,CACZ,GAA0B,EAC1B,GAA0B,EAC1B,IAAI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I,CAAC,IAAI,GAAG,cAAc,CAAC,IAAI,CAAC,CAAA;QAClC,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;IAED,MAAM;QACJ,MAAM,MAAM,GAAoB;YAC9B,IAAI,EAAE,IAAI;YACV,GAAG,EAAE,IAAI,CAAC,GAAG;YACb,IAAI,EAAE,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;YACpC,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAA;QAED,IAAI,IAAI,CAAC,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACxC,MAAM,KAAK,GAAG,MAAoC,CAAA;YAClD,KAAK,CAAC,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QACnD,CAAC;QAED,OAAO,MAAM,CAAA;IACf,CAAC;CACF","sourcesContent":["import { base64ToBuffer, bufferToBase64 } from '../lib/Buffer.js'\nimport { nanoid } from 'nanoid'\nimport {\n Certificate as LegacyCertificate,\n ARCCertificateIndex,\n RequestCertificate,\n} from './legacy/models/ClientCertificate.js'\n\nexport type CertificateType = 'p12' | 'pem'\n\nexport const Kind = 'Core#Certificate'\n\nexport type CertificateDataFormat = string | ArrayBuffer | Uint8Array\n\n/**\n * Represents a single certificate object (cert/key)\n */\nexport interface ICertificateData {\n /**\n * The certificate to use.\n * The `p12` type certificate must be a Buffer.\n */\n data: CertificateDataFormat\n /**\n * A passphrase to use to unlock the certificate.\n */\n passphrase?: string\n /**\n * The original data type of the certificate. This is only used internally by the data store\n * to move between buffers and string values stored in the store.\n * Outside the internal procedure of the data store this\n * is always `undefined` and the `data` contains the original data format.\n */\n type?: 'buffer'\n}\n\nexport interface ICertificate {\n kind: typeof Kind\n /**\n * The data store key to refer.\n */\n key: string\n /**\n * The custom name of the certificate.\n */\n name: string\n /**\n * The timestamp when the certificate was inserted into the data store.\n * Required when returning a result. Auto-generated when inserting, if missing.\n */\n created?: number\n /**\n * Certificate type. Either `p12` or `pem`.\n */\n type: CertificateType\n /**\n * Certificate or list of certificates to use.\n */\n cert: ICertificateData\n}\n\nexport interface IP12Certificate extends ICertificate {\n type: 'p12'\n}\n\n/**\n * Represents a complete certificate configuration required to make\n * an HTTP request.\n */\nexport interface IPemCertificate extends ICertificate {\n type: 'pem'\n /**\n * The key for the `pem` type certificate.\n */\n certKey: ICertificateData\n}\n\nexport type HttpCertificate = IP12Certificate | IPemCertificate | ICertificate\n\n/**\n * @deprecated Do not use this type anymore. Create a cert instance instead.\n */\nexport interface IPemCreateOptions {\n type: 'p12'\n /**\n * The certificate contents.\n */\n cert: CertificateDataFormat\n /**\n * The key contents.\n */\n key: CertificateDataFormat\n /**\n * Optional name for the certificate.\n */\n name?: string\n /**\n * Optional passphrase for the key.\n */\n passphrase?: string\n}\n\n/**\n * @deprecated Do not use this type anymore. Create a cert instance instead.\n */\nexport interface IP12CreateOptions {\n type: 'pem'\n /**\n * The certificate contents.\n */\n cert: CertificateDataFormat\n /**\n * Optional name for the certificate.\n */\n name?: string\n /**\n * Optional passphrase for the certificate.\n */\n passphrase?: string\n}\n\n/**\n * @deprecated Do not use this type anymore. Create a cert instance instead.\n */\nexport type ICertificateCreateOptions = IPemCreateOptions | IP12CreateOptions\n\n/**\n * A class that represents a certificate in the system\n */\nexport class Certificate {\n kind = Kind\n /**\n * The data store key to refer.\n */\n key: string\n /**\n * Custom name of the certificate.\n */\n name: string\n /**\n * Timestamp when the certificate was inserted into the data store.\n * Required when returning a result. Auto-generated when inserting, if missing.\n */\n created = 0\n /**\n * Certificate type. Either `p12` or `pem`.\n */\n type: CertificateType\n /**\n * Certificate or list of certificates to use.\n */\n cert: ICertificateData\n /**\n * The key for the `pem` type certificate.\n */\n certKey?: ICertificateData\n\n /**\n * Creates a new certificate instance for a PEM key\n *\n * @param pem The certificate contents\n * @param key The key contents\n * @param name The certificate name\n * @param keyPassphrase The key passphrase\n */\n static fromPem(\n pem: CertificateDataFormat,\n key: CertificateDataFormat,\n name = 'New PEM certificate',\n keyPassphrase?: string\n ): Certificate {\n const init: IPemCertificate = {\n kind: Kind,\n cert: {\n data: pem,\n },\n certKey: {\n data: key,\n },\n key: nanoid(),\n name,\n type: 'pem',\n created: Date.now(),\n }\n if (keyPassphrase) {\n init.certKey.passphrase = keyPassphrase\n }\n return new Certificate(init)\n }\n\n /**\n * Creates a new certificate instance for a P12 key\n *\n * @param cert The certificate contents\n * @param name The certificate name\n * @param passphrase The key passphrase\n */\n static fromP12(cert: CertificateDataFormat, name = 'New P12 certificate', passphrase?: string): Certificate {\n const init: IP12Certificate = {\n kind: Kind,\n cert: {\n data: cert,\n },\n key: nanoid(),\n name,\n type: 'p12',\n created: Date.now(),\n }\n if (passphrase) {\n init.cert.passphrase = passphrase\n }\n return new Certificate(init)\n }\n\n /**\n * Creates a certificate object from the ARC's legacy certificate definition.\n *\n * @param index The legacy certificate index object. If it has set `_id` it will be used as the `key`.\n * @param index The certificate data object as read from the data store. Do not restore the data\n * to its original format.\n */\n static fromLegacy(index: ARCCertificateIndex, cert: RequestCertificate): Certificate {\n const { name = '', type, created = Date.now(), _id = nanoid() } = index\n if (type === 'pem') {\n const data = Array.isArray(cert.cert) ? cert.cert[0] : cert.cert\n const key = Array.isArray(cert.key) ? cert.key[0] : (cert.key as LegacyCertificate)\n if (!key) {\n throw new Error(`Unable to create a PEM certificate without the key.`)\n }\n const init: IPemCertificate = {\n kind: Kind,\n cert: data as ICertificateData, // these are compatible.\n certKey: key as ICertificateData,\n key: _id,\n name,\n type: 'pem',\n created,\n }\n return new Certificate(init)\n } else if (type === 'p12') {\n const data = Array.isArray(cert.cert) ? cert.cert[0] : cert.cert\n const init: IP12Certificate = {\n kind: Kind,\n cert: data as ICertificateData, // these are compatible.\n key: _id,\n name,\n type: 'p12',\n created,\n }\n return new Certificate(init)\n }\n throw new Error(`Unable to create a certificate. Unknown type: ${type}.`)\n }\n\n constructor(certificate: HttpCertificate) {\n const { type, cert, key = nanoid(), name = '', created = Date.now() } = certificate\n this.key = key\n this.name = name\n this.type = type\n this.cert = Certificate.fromStore(cert)\n this.created = created\n if (type === 'pem') {\n const typed = certificate as IPemCertificate\n this.certKey = Certificate.fromStore(typed.certKey)\n }\n }\n\n /**\n * When needed it reads the certificate's original format.\n * @param data The certificate data.\n * @returns The restored certificate.\n */\n static fromStore(data: ICertificateData): ICertificateData {\n if (data.type) {\n delete data.type\n const content = data.data as string\n data.data = base64ToBuffer(content)\n }\n return data\n }\n\n /**\n * Prepares certificate object to be stored in the data store.\n * If the `data` property is not string then it assumes buffer (either\n * Node's or ArrayBuffer). In this case it converts buffer to base64 string.\n * It also adds `type` property set to `buffer` for the `fromStore()`\n * function to recognize what to do with the data.\n *\n * Note, for optimization, PEM keys should be strings as the content of the\n * certificate is already a base62 string. To spare double base64 conversion\n * use string data.\n *\n * @param data The certificate data object.\n * @throws When data is not set\n */\n static toStore(data: ICertificateData): ICertificateData {\n if (!data) {\n throw new Error('Certificate data is missing.')\n }\n if (!data.data) {\n throw new Error('Certificate content not set.')\n }\n if (typeof data.data !== 'string') {\n data.type = 'buffer'\n const buff = data.data as ArrayBuffer | Uint8Array\n data.data = bufferToBase64(buff)\n }\n return data\n }\n\n toJSON(): HttpCertificate {\n const result: HttpCertificate = {\n kind: Kind,\n key: this.key,\n cert: Certificate.toStore(this.cert),\n name: this.name,\n type: this.type,\n created: this.created,\n }\n\n if (this.type === 'pem' && this.certKey) {\n const typed = result as unknown as IPemCertificate\n typed.certKey = Certificate.toStore(this.certKey)\n }\n\n return result\n }\n}\n"]}
|