@sphereon/ssi-sdk.data-store
Version:
458 lines • 22.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDidAuthConfig = exports.isOpenIdConfig = exports.didAuthConfigFrom = exports.openIdConfigFrom = exports.configFrom = exports.partyTypeFrom = exports.partyTypeEntityFrom = exports.partyRelationshipFrom = exports.partyRelationshipEntityFrom = exports.organizationFrom = exports.organizationEntityFrom = exports.openIdConfigEntityFrom = exports.naturalPersonFrom = exports.naturalPersonEntityFrom = exports.metadataItemFrom = exports.contactMetadataItemEntityFrom = exports.identityMetadataItemEntityFrom = exports.identityFrom = exports.identityEntityFrom = exports.physicalAddressFrom = exports.physicalAddressEntityFrom = exports.electronicAddressFrom = exports.electronicAddressEntityFrom = exports.didAuthConfigEntityFrom = exports.correlationIdentifierFrom = exports.correlationIdentifierEntityFrom = exports.connectionFrom = exports.connectionEntityFrom = exports.isOrganization = exports.isNaturalPerson = exports.contactFrom = exports.contactEntityFrom = exports.partyFrom = exports.partyEntityFrom = void 0;
const PartyEntity_1 = require("../../entities/contact/PartyEntity");
const IdentityEntity_1 = require("../../entities/contact/IdentityEntity");
const ElectronicAddressEntity_1 = require("../../entities/contact/ElectronicAddressEntity");
const PartyRelationshipEntity_1 = require("../../entities/contact/PartyRelationshipEntity");
const NaturalPersonEntity_1 = require("../../entities/contact/NaturalPersonEntity");
const OrganizationEntity_1 = require("../../entities/contact/OrganizationEntity");
const ConnectionEntity_1 = require("../../entities/contact/ConnectionEntity");
const CorrelationIdentifierEntity_1 = require("../../entities/contact/CorrelationIdentifierEntity");
const DidAuthConfigEntity_1 = require("../../entities/contact/DidAuthConfigEntity");
const IdentityMetadataItemEntity_1 = require("../../entities/contact/IdentityMetadataItemEntity");
const OpenIdConfigEntity_1 = require("../../entities/contact/OpenIdConfigEntity");
const PartyTypeEntity_1 = require("../../entities/contact/PartyTypeEntity");
const PhysicalAddressEntity_1 = require("../../entities/contact/PhysicalAddressEntity");
const ContactMetadataItemEntity_1 = require("../../entities/contact/ContactMetadataItemEntity");
const FormattingUtils_1 = require("../FormattingUtils");
const partyEntityFrom = (party) => {
const partyEntity = new PartyEntity_1.PartyEntity();
partyEntity.uri = party.uri;
partyEntity.identities = party.identities ? party.identities.map((identity) => (0, exports.identityEntityFrom)(identity)) : [];
partyEntity.electronicAddresses = party.electronicAddresses
? party.electronicAddresses.map((electronicAddress) => (0, exports.electronicAddressEntityFrom)(electronicAddress))
: [];
partyEntity.physicalAddresses = party.physicalAddresses
? party.physicalAddresses.map((physicalAddress) => (0, exports.physicalAddressEntityFrom)(physicalAddress))
: [];
partyEntity.partyType = (0, exports.partyTypeEntityFrom)(party.partyType);
partyEntity.contact = (0, exports.contactEntityFrom)(party.contact);
partyEntity.ownerId = party.ownerId;
partyEntity.tenantId = party.tenantId;
return partyEntity;
};
exports.partyEntityFrom = partyEntityFrom;
const partyFrom = (party) => {
var _a;
const result = {
id: party.id,
uri: party.uri,
roles: [...new Set((_a = party.identities) === null || _a === void 0 ? void 0 : _a.flatMap((identity) => identity.roles))],
identities: party.identities ? party.identities.map((identity) => (0, exports.identityFrom)(identity)) : [],
electronicAddresses: party.electronicAddresses
? party.electronicAddresses.map((electronicAddress) => (0, exports.electronicAddressFrom)(electronicAddress))
: [],
physicalAddresses: party.physicalAddresses
? party.physicalAddresses.map((physicalAddress) => (0, exports.physicalAddressFrom)(physicalAddress))
: [],
relationships: party.relationships ? party.relationships.map((relationship) => (0, exports.partyRelationshipFrom)(relationship)) : [],
partyType: (0, exports.partyTypeFrom)(party.partyType),
contact: (0, exports.contactFrom)(party.contact),
ownerId: party.ownerId,
tenantId: party.tenantId,
createdAt: party.createdAt,
lastUpdatedAt: party.lastUpdatedAt,
};
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.partyFrom = partyFrom;
const contactEntityFrom = (contact) => {
if ((0, exports.isNaturalPerson)(contact)) {
return (0, exports.naturalPersonEntityFrom)(contact);
}
else if ((0, exports.isOrganization)(contact)) {
return (0, exports.organizationEntityFrom)(contact);
}
throw new Error('Contact not supported');
};
exports.contactEntityFrom = contactEntityFrom;
const contactFrom = (contact) => {
if ((0, exports.isNaturalPerson)(contact)) {
return (0, exports.naturalPersonFrom)(contact);
}
else if ((0, exports.isOrganization)(contact)) {
return (0, exports.organizationFrom)(contact);
}
throw new Error(`Contact type not supported`);
};
exports.contactFrom = contactFrom;
const isNaturalPerson = (contact) => 'firstName' in contact && 'lastName' in contact;
exports.isNaturalPerson = isNaturalPerson;
const isOrganization = (contact) => 'legalName' in contact;
exports.isOrganization = isOrganization;
const connectionEntityFrom = (connection) => {
const connectionEntity = new ConnectionEntity_1.ConnectionEntity();
connectionEntity.type = connection.type;
connectionEntity.config = configEntityFrom(connection.config);
connectionEntity.ownerId = connection.ownerId;
connectionEntity.tenantId = connection.tenantId;
return connectionEntity;
};
exports.connectionEntityFrom = connectionEntityFrom;
const connectionFrom = (connection) => {
const result = {
id: connection.id,
type: connection.type,
ownerId: connection.ownerId,
tenantId: connection.tenantId,
config: (0, exports.configFrom)(connection.config),
};
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.connectionFrom = connectionFrom;
const configEntityFrom = (config) => {
if ((0, exports.isOpenIdConfig)(config)) {
return (0, exports.openIdConfigEntityFrom)(config);
}
else if ((0, exports.isDidAuthConfig)(config)) {
return (0, exports.didAuthConfigEntityFrom)(config);
}
throw new Error('config type not supported');
};
const correlationIdentifierEntityFrom = (identifier) => {
const identifierEntity = new CorrelationIdentifierEntity_1.CorrelationIdentifierEntity();
identifierEntity.type = identifier.type;
identifierEntity.correlationId = identifier.correlationId;
identifierEntity.ownerId = identifier.ownerId;
identifierEntity.tenantId = identifier.tenantId;
return identifierEntity;
};
exports.correlationIdentifierEntityFrom = correlationIdentifierEntityFrom;
const correlationIdentifierFrom = (identifier) => {
const result = {
id: identifier.id,
type: identifier.type,
correlationId: identifier.correlationId,
ownerId: identifier.ownerId,
tenantId: identifier.tenantId,
};
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.correlationIdentifierFrom = correlationIdentifierFrom;
const didAuthConfigEntityFrom = (config) => {
let identifier = undefined;
if (typeof config.idOpts.identifier === 'string') {
identifier = config.idOpts.identifier;
}
else if (typeof config.idOpts.identifier === 'object') {
if ('did' in config.idOpts.identifier && typeof config.idOpts.identifier.did === 'string') {
identifier = config.idOpts.identifier.did;
}
}
if (!identifier) {
throw Error(`Identifier method ${config.idOpts.method} not yet supported`);
}
const didAuthConfig = new DidAuthConfigEntity_1.DidAuthConfigEntity();
didAuthConfig.identifier = identifier;
didAuthConfig.redirectUrl = config.redirectUrl;
didAuthConfig.sessionId = config.sessionId;
didAuthConfig.ownerId = config.ownerId;
didAuthConfig.tenantId = config.tenantId;
return didAuthConfig;
};
exports.didAuthConfigEntityFrom = didAuthConfigEntityFrom;
const electronicAddressEntityFrom = (electronicAddress) => {
const electronicAddressEntity = new ElectronicAddressEntity_1.ElectronicAddressEntity();
electronicAddressEntity.type = electronicAddress.type;
electronicAddressEntity.electronicAddress = electronicAddress.electronicAddress;
electronicAddressEntity.ownerId = electronicAddress.ownerId;
electronicAddressEntity.tenantId = electronicAddress.tenantId;
return electronicAddressEntity;
};
exports.electronicAddressEntityFrom = electronicAddressEntityFrom;
const electronicAddressFrom = (electronicAddress) => {
const result = {
id: electronicAddress.id,
type: electronicAddress.type,
electronicAddress: electronicAddress.electronicAddress,
ownerId: electronicAddress.ownerId,
tenantId: electronicAddress.tenantId,
createdAt: electronicAddress.createdAt,
lastUpdatedAt: electronicAddress.lastUpdatedAt,
};
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.electronicAddressFrom = electronicAddressFrom;
const physicalAddressEntityFrom = (physicalAddress) => {
const physicalAddressEntity = new PhysicalAddressEntity_1.PhysicalAddressEntity();
physicalAddressEntity.type = physicalAddress.type;
physicalAddressEntity.streetName = physicalAddress.streetName;
physicalAddressEntity.streetNumber = physicalAddress.streetNumber;
physicalAddressEntity.postalCode = physicalAddress.postalCode;
physicalAddressEntity.cityName = physicalAddress.cityName;
physicalAddressEntity.provinceName = physicalAddress.provinceName;
physicalAddressEntity.countryCode = physicalAddress.countryCode;
physicalAddressEntity.buildingName = physicalAddress.buildingName;
physicalAddressEntity.ownerId = physicalAddress.ownerId;
physicalAddressEntity.tenantId = physicalAddress.tenantId;
return physicalAddressEntity;
};
exports.physicalAddressEntityFrom = physicalAddressEntityFrom;
const physicalAddressFrom = (physicalAddress) => {
const result = {
id: physicalAddress.id,
type: physicalAddress.type,
streetName: physicalAddress.streetName,
streetNumber: physicalAddress.streetNumber,
postalCode: physicalAddress.postalCode,
cityName: physicalAddress.cityName,
provinceName: physicalAddress.provinceName,
countryCode: physicalAddress.countryCode,
buildingName: physicalAddress.buildingName,
ownerId: physicalAddress.ownerId,
tenantId: physicalAddress.tenantId,
createdAt: physicalAddress.createdAt,
lastUpdatedAt: physicalAddress.lastUpdatedAt,
};
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.physicalAddressFrom = physicalAddressFrom;
const identityEntityFrom = (entity) => {
const identityEntity = new IdentityEntity_1.IdentityEntity();
identityEntity.alias = entity.alias;
identityEntity.origin = entity.origin;
identityEntity.ownerId = entity.ownerId;
identityEntity.tenantId = entity.tenantId;
identityEntity.roles = entity.roles;
identityEntity.identifier = (0, exports.correlationIdentifierEntityFrom)(entity.identifier);
identityEntity.connection = entity.connection ? (0, exports.connectionEntityFrom)(entity.connection) : undefined;
identityEntity.metadata = entity.metadata
? entity.metadata.map(exports.identityMetadataItemEntityFrom).filter((entity) => entity !== undefined)
: [];
return identityEntity;
};
exports.identityEntityFrom = identityEntityFrom;
const identityFrom = (identity) => {
const result = Object.assign(Object.assign({ id: identity.id, alias: identity.alias, origin: identity.origin, roles: identity.roles, tenantId: identity.tenantId, ownerId: identity.ownerId, identifier: (0, exports.correlationIdentifierFrom)(identity.identifier) }, (identity.connection && { connection: (0, exports.connectionFrom)(identity.connection) })), { metadata: identity.metadata ? identity.metadata.map((item) => (0, exports.metadataItemFrom)(item)) : [], createdAt: identity.createdAt, lastUpdatedAt: identity.createdAt });
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.identityFrom = identityFrom;
const metadataItemEntityFrom = (item, EntityClass) => {
const { label, value } = item;
if (value === null || value === undefined) {
return undefined;
}
const metadataItemEntity = new EntityClass();
metadataItemEntity.label = label;
metadataItemEntity.valueType = typeof value;
switch (typeof value) {
case 'undefined':
return undefined;
case 'string':
metadataItemEntity.stringValue = value;
break;
case 'number':
metadataItemEntity.numberValue = value;
break;
case 'boolean':
metadataItemEntity.boolValue = value;
break;
case 'object':
metadataItemEntity.valueType = Object.prototype.toString.call(value).slice(8, -1);
if (value instanceof Date) {
metadataItemEntity.dateValue = value;
}
else {
// For now, we only support / implement not-primitive type Date in the entity
throw new Error(`Unsupported object type: ${metadataItemEntity.valueType} for value ${value}`);
}
break;
}
return metadataItemEntity;
};
const identityMetadataItemEntityFrom = (item) => {
return metadataItemEntityFrom(item, IdentityMetadataItemEntity_1.IdentityMetadataItemEntity);
};
exports.identityMetadataItemEntityFrom = identityMetadataItemEntityFrom;
const contactMetadataItemEntityFrom = (item) => {
return metadataItemEntityFrom(item, ContactMetadataItemEntity_1.ContactMetadataItemEntity);
};
exports.contactMetadataItemEntityFrom = contactMetadataItemEntityFrom;
const metadataItemFrom = (entity) => {
const item = {
id: entity.id,
label: entity.label,
};
switch (entity.valueType) {
case 'string':
item.value = entity.stringValue;
break;
case 'number':
item.value = entity.numberValue;
break;
case 'Date':
item.value = entity.dateValue;
break;
case 'boolean':
item.value = entity.boolValue;
break;
default:
throw new Error(`Unsupported valueType ${entity.valueType}`);
}
return item;
};
exports.metadataItemFrom = metadataItemFrom;
const naturalPersonEntityFrom = (naturalPerson) => {
const naturalPersonEntity = new NaturalPersonEntity_1.NaturalPersonEntity();
naturalPersonEntity.firstName = naturalPerson.firstName;
naturalPersonEntity.middleName = naturalPerson.middleName;
naturalPersonEntity.lastName = naturalPerson.lastName;
naturalPersonEntity.displayName = naturalPerson.displayName;
naturalPersonEntity.ownerId = naturalPerson.ownerId;
naturalPersonEntity.tenantId = naturalPerson.tenantId;
naturalPersonEntity.metadata = naturalPerson.metadata
? naturalPerson.metadata.map(exports.contactMetadataItemEntityFrom).filter((entity) => entity !== undefined)
: [];
return naturalPersonEntity;
};
exports.naturalPersonEntityFrom = naturalPersonEntityFrom;
const naturalPersonFrom = (naturalPerson) => {
const result = {
id: naturalPerson.id,
firstName: naturalPerson.firstName,
middleName: naturalPerson.middleName,
lastName: naturalPerson.lastName,
displayName: naturalPerson.displayName,
metadata: naturalPerson.metadata ? naturalPerson.metadata.map((item) => (0, exports.metadataItemFrom)(item)) : [],
ownerId: naturalPerson.ownerId,
tenantId: naturalPerson.tenantId,
createdAt: naturalPerson.createdAt,
lastUpdatedAt: naturalPerson.lastUpdatedAt,
};
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.naturalPersonFrom = naturalPersonFrom;
const openIdConfigEntityFrom = (config) => {
const openIdConfig = new OpenIdConfigEntity_1.OpenIdConfigEntity();
openIdConfig.clientId = config.clientId;
openIdConfig.clientSecret = config.clientSecret;
openIdConfig.scopes = config.scopes;
openIdConfig.issuer = config.issuer;
openIdConfig.redirectUrl = config.redirectUrl;
openIdConfig.dangerouslyAllowInsecureHttpRequests = config.dangerouslyAllowInsecureHttpRequests;
openIdConfig.clientAuthMethod = config.clientAuthMethod;
openIdConfig.ownerId = config.ownerId;
openIdConfig.tenantId = config.tenantId;
return openIdConfig;
};
exports.openIdConfigEntityFrom = openIdConfigEntityFrom;
const organizationEntityFrom = (organization) => {
const organizationEntity = new OrganizationEntity_1.OrganizationEntity();
organizationEntity.legalName = organization.legalName;
organizationEntity.displayName = organization.displayName;
organizationEntity.ownerId = organization.ownerId;
organizationEntity.tenantId = organization.tenantId;
organizationEntity.metadata = organization.metadata
? organization.metadata.map(exports.contactMetadataItemEntityFrom).filter((entity) => entity !== undefined)
: [];
return organizationEntity;
};
exports.organizationEntityFrom = organizationEntityFrom;
const organizationFrom = (organization) => {
const result = {
id: organization.id,
legalName: organization.legalName,
displayName: organization.displayName,
metadata: organization.metadata ? organization.metadata.map((item) => (0, exports.metadataItemFrom)(item)) : [],
ownerId: organization.ownerId,
tenantId: organization.tenantId,
createdAt: organization.createdAt,
lastUpdatedAt: organization.lastUpdatedAt,
};
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.organizationFrom = organizationFrom;
const partyRelationshipEntityFrom = (relationship) => {
const partyRelationshipEntity = new PartyRelationshipEntity_1.PartyRelationshipEntity();
partyRelationshipEntity.leftId = relationship.leftId;
partyRelationshipEntity.rightId = relationship.rightId;
partyRelationshipEntity.ownerId = relationship.ownerId;
partyRelationshipEntity.tenantId = relationship.tenantId;
return partyRelationshipEntity;
};
exports.partyRelationshipEntityFrom = partyRelationshipEntityFrom;
const partyRelationshipFrom = (relationship) => {
const result = {
id: relationship.id,
leftId: relationship.leftId,
rightId: relationship.rightId,
ownerId: relationship.ownerId,
tenantId: relationship.tenantId,
createdAt: relationship.createdAt,
lastUpdatedAt: relationship.lastUpdatedAt,
};
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.partyRelationshipFrom = partyRelationshipFrom;
const partyTypeEntityFrom = (args) => {
const partyTypeEntity = new PartyTypeEntity_1.PartyTypeEntity();
if (args.id) {
partyTypeEntity.id = args.id;
}
partyTypeEntity.type = args.type;
partyTypeEntity.origin = args.origin;
partyTypeEntity.name = args.name;
partyTypeEntity.description = args.description;
partyTypeEntity.tenantId = args.tenantId;
return partyTypeEntity;
};
exports.partyTypeEntityFrom = partyTypeEntityFrom;
const partyTypeFrom = (partyType) => {
const result = {
id: partyType.id,
type: partyType.type,
origin: partyType.origin,
name: partyType.name,
tenantId: partyType.tenantId,
description: partyType.description,
createdAt: partyType.createdAt,
lastUpdatedAt: partyType.lastUpdatedAt,
};
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.partyTypeFrom = partyTypeFrom;
const configFrom = (config) => {
if ((0, exports.isOpenIdConfig)(config)) {
return (0, exports.openIdConfigFrom)(config);
}
else if ((0, exports.isDidAuthConfig)(config)) {
return (0, exports.didAuthConfigFrom)(config);
}
throw new Error('config type not supported');
};
exports.configFrom = configFrom;
const openIdConfigFrom = (config) => {
const result = {
id: config.id,
clientId: config.clientId,
clientSecret: config.clientSecret,
scopes: config.scopes,
issuer: config.issuer,
redirectUrl: config.redirectUrl,
dangerouslyAllowInsecureHttpRequests: config.dangerouslyAllowInsecureHttpRequests,
clientAuthMethod: config.clientAuthMethod,
ownerId: config.ownerId,
tenantId: config.tenantId,
};
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.openIdConfigFrom = openIdConfigFrom;
const didAuthConfigFrom = (config) => {
const result = {
id: config.id,
idOpts: { identifier: config.identifier },
stateId: '', // FIXME
redirectUrl: config.redirectUrl,
sessionId: config.sessionId,
ownerId: config.ownerId,
tenantId: config.tenantId,
};
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.didAuthConfigFrom = didAuthConfigFrom;
const isOpenIdConfig = (config) => 'clientSecret' in config && 'issuer' in config && 'redirectUrl' in config;
exports.isOpenIdConfig = isOpenIdConfig;
const isDidAuthConfig = (config) => ('identifier' in config || ('idOpts' in config && 'identifier' in config.idOpts)) && 'redirectUrl' in config && 'sessionId' in config;
exports.isDidAuthConfig = isDidAuthConfig;
//# sourceMappingURL=MappingUtils.js.map