UNPKG

@kontent-ai/model-generator

Version:

This utility generates strongly-typed models for Delivery JS SDK, Migration toolkit or just general scripting to improve the experience when referencing Kontent.ai related objects.

97 lines (95 loc) 4.53 kB
import { match } from "ts-pattern"; import { deliveryConfig } from "../../../config.js"; import { wrapComment } from "../../../core/comment.utils.js"; import { getImporter } from "../../../core/importer.js"; import { resolveCase } from "../../../core/resolvers.js"; export function deliveryEntityUtils() { return { getPluralName, getTypeMapping, getTypeMappingItem, getCodeOfDeliveryEntity, getTaxonomyTermCodenames, getCodenameTypeguardFunctionCode, getCoreContentTypeEntityImports, getCoreContentTypeCode, }; } function getCoreContentTypeCode(types, entityNames) { return `export type ${deliveryConfig.coreContentTypeName} = ${getCoreContentTypeValue(types, entityNames)}`; } function getCoreContentTypeEntityNames(types, entityNames) { return types.map((m) => entityNames.getEntityName(m)); } function getCoreContentTypeValue(types, entityNames) { const typeNames = getCoreContentTypeEntityNames(types, entityNames); if (!typeNames.length) { return "never"; } return typeNames.join(" | "); } function getCoreContentTypeEntityImports(types, entityNames, moduleFileExtension) { const importer = getImporter(moduleFileExtension); return types.map((m) => importer.importType({ filePathOrPackage: `../${entityNames.folderName}/${entityNames.getEntityFilename(m, true)}`, importValue: entityNames.getEntityName(m), })); } function getTypeMappingTypeName(entityType) { return `Codename${resolveCase(entityType, "pascalCase")}Mapping`; } function getTypeMapping(entityType, items) { return `export type ${getTypeMappingTypeName(entityType)} = { ${items.map((item) => `readonly ${item.codename}: ${item.typeName},`).join("\n")} };`; } function getTypeMappingItem({ codenamesTypeName, defaultTypeName, entityType, }) { const codenameGenericName = `T${resolveCase(entityType, "pascalCase")}Codename`; const typedItemTypeName = `Codename${resolveCase(entityType, "pascalCase")}Mapper`; return `export type ${typedItemTypeName}<${codenameGenericName} extends ${codenamesTypeName}> = ${codenameGenericName} extends keyof ${getTypeMappingTypeName(entityType)} ? ${getTypeMappingTypeName(entityType)}[${codenameGenericName}] : ${defaultTypeName};`; } function getCodenameTypeguardFunctionCode({ codenameTypeName, entity, typeGuardName, }) { return `export function ${typeGuardName}(value: string | undefined | null): value is ${codenameTypeName} { return typeof value === 'string' && value === ('${entity.codename}' satisfies ${codenameTypeName}); }`; } function getTaxonomyTermCodenames(taxonomyTerms) { const termCodenames = taxonomyTerms.reduce((codenames, taxonomyTerm) => { return codenames.concat(getTaxonomyTermCodenames(taxonomyTerm.terms), taxonomyTerm.codename); }, []); return [...new Set(termCodenames)]; } function getPluralName(entityType) { return match(entityType) .returnType() .with("Taxonomy", () => "Taxonomies") .otherwise(() => `${entityType}s`); } function getCodeOfDeliveryEntity({ extendedType, codenames, names, }) { const uniqueCodenames = [...new Set(codenames)]; const getCodenameTypeguardFunctionCode = () => { return `export function ${names.typeGuardFunctionName}(value: string | undefined | null): value is ${names.codenamesTypeName} { return typeof value === 'string' && (${names.valuesPropertyName} as readonly string[]).includes(value); }`; }; const getValuesCode = () => { return `export const ${names.valuesPropertyName} = [${uniqueCodenames.map((m) => `'${m}'`).join(", ")}] as const;`; }; const getCodenamesTypeCode = () => { return `export type ${names.codenamesTypeName} = typeof ${names.valuesPropertyName}[number];`; }; const getComment = (title) => { return wrapComment(title, { lines: [], }); }; const getEntityTypeNameForComments = () => `${extendedType.toLowerCase()}`; return ` ${getComment(`Array of all ${getEntityTypeNameForComments()} codenames`)} ${getValuesCode()}; ${getComment(`Type representing all ${getEntityTypeNameForComments()} codenames`)} ${getCodenamesTypeCode()}; ${getComment(`Typeguard for ${getEntityTypeNameForComments()} codename`)} ${getCodenameTypeguardFunctionCode()};`; } //# sourceMappingURL=delivery-entity.utils.js.map