import { YamlFields, YamlRelationships } from '../../../types'; import { isValidARI } from '../../../helpers/parseAri'; /** * Converts any slugs present in the DEPENDS_ON array into component ARIs, ignoring any entries that did * not have a corresponding ARI returned from the lookup. * * If the feature flag is NOT enabled, dependenciesBySlugs will be empty and all slugs will be removed. * * @param relationships - The relationships from the YAML config. * @param dependenciesBySlugs - Map of slugs to ARI based on lookup. * @returns Merged relationships DEPENDS_ON containing ARIs instead of slugs. */ export const convertRelationshipSlugs = ( relationships: YamlRelationships, dependenciesBySlugs: Record, ): YamlRelationships => { const convertedRelationships = { ...relationships }; // If any relationships were provided, replace any that are slugs with the corresponding ARI. convertedRelationships.DEPENDS_ON?.forEach((ariOrSlug, index) => { if (ariOrSlug in dependenciesBySlugs) { convertedRelationships.DEPENDS_ON[index] = dependenciesBySlugs[ariOrSlug]; } }); // Filter out any remaining slugs that did not have a corresponding ARI. convertedRelationships.DEPENDS_ON = convertedRelationships.DEPENDS_ON?.filter((ari) => isValidARI(ari)) || []; return convertedRelationships; }; /** * Transforms relationships from a YAML config to the format expected by the API. * * @param relationships - The relationships from the YAML config. * @param dependenciesBySlugs - Map of slugs to ARI based on lookup. * @returns The transformed relationships used to update a component. */ export function transformRelationshipsFromYamlConfig( relationships: YamlRelationships, dependenciesBySlugs: Record, ): any[] { const transformedRelationships: any[] = []; if (!relationships) { return transformedRelationships; } const convertedRelationships = convertRelationshipSlugs( relationships, dependenciesBySlugs, ); for (const relationshipType of Object.keys(convertedRelationships)) { transformedRelationships.push( (convertedRelationships as any)[relationshipType].map((nodeId: any) => ({ nodeId, type: relationshipType, })), ); } return transformedRelationships.flat(); } export function transformFieldsFromYamlConfig( fields: YamlFields, ): Record> { if (!fields || Object.keys(fields).length === 0) { return null; } const outputFields: Record> = {}; for (const [k, v] of Object.entries(fields)) { if (v != null) { outputFields[k] = Array.isArray(v) ? v.map(toString) : [v.toString()]; } } return outputFields; }