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.

138 lines (136 loc) 6.77 kB
import { match } from "ts-pattern"; import { deliveryConfig } from "../../config.js"; import { wrapComment } from "../../core/comment.utils.js"; import { isNotUndefined, uniqueFilter } from "../../core/core.utils.js"; import { getFlattenedElements } from "../../core/element.utils.js"; import { getImporter } from "../../core/importer.js"; import { getDeliveryEntityGenerator } from "./delivery-entity.generator.js"; export function deliveryGenerator(config) { const importer = getImporter(config.moduleFileExtension); const getUniqueDeliveryElements = () => { const flattenedElements = getFlattenedElements({ elements: [ ...config.environmentData.types.flatMap((type) => type.elements), ...config.environmentData.snippets.flatMap((snippet) => snippet.elements), ], snippets: config.environmentData.snippets, taxonomies: config.environmentData.taxonomies, types: config.environmentData.types, }); const uniqueElementCodenames = flattenedElements .map((element) => element.codename) .filter(isNotUndefined) .filter(uniqueFilter); return flattenedElements .filter((element) => uniqueElementCodenames.includes(element.codename)) .map((m) => ({ codename: m.codename, name: m.title, externalId: m.externalId, })); }; const entityGenerators = { collections: getDeliveryEntityGenerator({ entities: config.environmentData.collections, entityType: "Collection", moduleFileExtension: config.moduleFileExtension, generateOnlyOverviewFile: false, deliveryGeneratorConfig: config, }), languages: getDeliveryEntityGenerator({ entities: config.environmentData.languages, entityType: "Language", moduleFileExtension: config.moduleFileExtension, generateOnlyOverviewFile: false, deliveryGeneratorConfig: config, }), workflows: getDeliveryEntityGenerator({ entities: config.environmentData.workflows, entityType: "Workflow", moduleFileExtension: config.moduleFileExtension, generateOnlyOverviewFile: false, deliveryGeneratorConfig: config, }), taxonomies: getDeliveryEntityGenerator({ entities: config.environmentData.taxonomies, entityType: "Taxonomy", moduleFileExtension: config.moduleFileExtension, generateOnlyOverviewFile: false, deliveryGeneratorConfig: config, }), contentTypes: getDeliveryEntityGenerator({ entities: config.environmentData.types, entityType: "Type", moduleFileExtension: config.moduleFileExtension, generateOnlyOverviewFile: false, deliveryGeneratorConfig: config, }), snippets: getDeliveryEntityGenerator({ entities: config.environmentData.snippets, entityType: "Snippet", moduleFileExtension: config.moduleFileExtension, generateOnlyOverviewFile: false, deliveryGeneratorConfig: config, }), elements: getDeliveryEntityGenerator({ entities: getUniqueDeliveryElements(), entityType: "Element", moduleFileExtension: config.moduleFileExtension, generateOnlyOverviewFile: true, deliveryGeneratorConfig: config, }), }; const getDeliverySystemFile = () => { const sdkImports = [deliveryConfig.sdkTypes.deliveryClient]; return { filename: `${deliveryConfig.mainSystemFilename}.ts`, text: ` ${importer.importType({ filePathOrPackage: deliveryConfig.npmPackageName, importValue: `${sdkImports.join(", ")}`, })} ${Object.values(entityGenerators) .filter((generator) => generator.entityType !== "Snippet") .map((generator) => { const importValues = match(generator.entityType) .with("Workflow", () => [ generator.entityNames.codenamesTypeName, entityGenerators.workflows.entityNames.allStepsNames.codenamesTypeName, ]) .with("Type", () => [generator.entityNames.codenamesTypeName, deliveryConfig.coreContentTypeName]) .otherwise(() => [generator.entityNames.codenamesTypeName]); return importer.importType({ filePathOrPackage: `./${generator.entityNames.overviewFilename}`, importValue: `${importValues.join(", ")}`, }); }) .join("\n")} ${wrapComment(`Core types for '${deliveryConfig.sdkTypes.deliveryClient}'`)} export type ${deliveryConfig.coreDeliveryClientTypesTypeName} = { readonly collectionCodenames: ${entityGenerators.collections.entityNames.codenamesTypeName}; readonly contentItemType: ${deliveryConfig.coreContentTypeName}; readonly contentTypeCodenames: ${entityGenerators.contentTypes.entityNames.codenamesTypeName}; readonly elementCodenames: ${entityGenerators.elements.entityNames.codenamesTypeName}; readonly languageCodenames: ${entityGenerators.languages.entityNames.codenamesTypeName}; readonly taxonomyCodenames: ${entityGenerators.taxonomies.entityNames.codenamesTypeName}; readonly workflowCodenames: ${entityGenerators.workflows.entityNames.codenamesTypeName}; readonly workflowStepCodenames: ${entityGenerators.workflows.entityNames.allStepsNames.codenamesTypeName}; }; ${wrapComment(`Typed delivery client. Use this instead of '${deliveryConfig.sdkTypes.deliveryClient}'`)} export type ${deliveryConfig.coreDeliveryClientTypeName} = IDeliveryClient<${deliveryConfig.coreDeliveryClientTypesTypeName}>; `, }; }; return { getTypeFiles: () => { return Object.values(entityGenerators).map((generator) => generator.generateEntityTypes()); }, getSystemFiles() { return { folderName: deliveryConfig.systemTypesFolderName, files: [getDeliverySystemFile(), ...Object.values(entityGenerators).map((generator) => generator.generateOverviewFile())], }; }, }; } //# sourceMappingURL=delivery.generator.js.map