kontent-ai / model-generator-js

MIT License
13 stars 9 forks source link
hacktoberfest kontent-ai kontent-ai-tool kontent-model-generator typescript

npm version Build GitHub license

Kontent.ai Model Generator

The purpose of this project is to help you generate Typescript models based on Kontent.ai item types. These models can be used with the Delivery SDK and enhances your experience by providing strongly typed models.

Installation

Install package globally so you can use it anywhere:

npm i @kontent-ai/model-generator -g

Generate models with CLI

Go to folder where you want to create models and run:

kontent-generate --environmentId=xxx --apiKey=yyy

You may specify other options like:

kontent-generate --environmentId=xxx --apiKey=yyy --addTimestamp=false --elementResolver=camelCase

Generate models in code

Apart from generating models via CLI, you may also generate models in code which also gives you some additional configuration options (such as using custom name resolver).

import { generateModelsAsync } from '@kontent-ai/model-generator';

await generateModelsAsync({
    sdkType: 'delivery',
    environmentId: 'da5abe9f-fdad-4168-97cd-b3464be2ccb9',
    isEnterpriseSubscription: true,
    apiKey: 'yyy',
    addTimestamp: true,
    moduleResolution: 'nodeNext',
    addEnvironmentInfo: true,
    elementResolver: 'camelCase',
    sortConfig: {
        sortTaxonomyTerms: true
    }
});

Customizing generated file names

You may customize the way filenames are stored on file system using the contentTypeFileResolver and / or taxonomyTypeFileResolver configuration option:

await generateModelsAsync({
    sdkType: 'delivery',
    environmentId: 'da5abe9f-fdad-4168-97cd-b3464be2ccb9',
    isEnterpriseSubscription: true,
    addEnvironmentInfo: true,
    apiKey: 'yyy',
    moduleResolution: 'nodeNext',
    addTimestamp: true,
    elementResolver: 'camelCase',
    contentTypeFileResolver: (type) => `content_type_${type.codename}`,
    taxonomyTypeFileResolver: (taxonomy) => `taxonomy_${taxonomy.codename}`
});

Customizing generated content type names

You may customize name of content types using the contentTypeResolver configuration option and taxonomy types with the taxonomyTypeResolver option:

await generateModelsAsync({
    sdkType: 'delivery',
    environmentId: 'da5abe9f-fdad-4168-97cd-b3464be2ccb9',
    isEnterpriseSubscription: true,
    apiKey: 'yyy',
    moduleResolution: 'nodeNext',
    addTimestamp: true,
    addEnvironmentInfo: true,
    elementResolver: 'camelCase',
    contentTypeResolver: (type) => `${textHelper.toPascalCase(type.codename)}Model`,
    taxonomyTypeResolver: (taxonomy) => `${textHelper.toPascalCase(taxonomy.codename)}Taxonomy`
});

FAQ

CLI Configuration

Example models

Generator creates file for each content type in your project. For example:

movie.ts

import { IContentItem, Elements } from '@kontent-ai/delivery-sdk';
import { Actor } from './actor';
import { ReleaseCategory } from '../taxonomies/releasecategory';

/**
 * Generated by '@kontent-ai/model-generator@5.0.0-3' at 'Thu, 14 Jul 2022 13:58:53 GMT'
 *
 * Movie
 * Id: b0c0f9c2-ffb6-4e62-bac9-34e14172dd8c
 * Codename: movie
 */
export type Movie = IContentItem<{
    /**
     * Title (text)
     * Required: true
     * Id: 3473187e-dc78-eff2-7099-f690f7042d4a
     * Codename: title
     */
    title: Elements.TextElement;

    /**
     * Plot (rich_text)
     * Required: false
     * Id: f7ee4f27-27fd-a19b-3c5c-102aae1c50ce
     * Codename: plot
     */
    plot: Elements.RichTextElement;

    /**
     * Released (date_time)
     * Required: false
     * Id: 5ccf4644-0d65-5d96-9a32-f4ea21974d51
     * Codename: released
     */
    released: Elements.DateTimeElement;

    /**
     * Length (number)
     * Required: false
     * Id: 7e8ecfab-a419-27ee-d8ec-8adb76fd007c
     * Codename: length
     */
    length: Elements.NumberElement;

    /**
     * Poster (asset)
     * Required: false
     * Id: a39a7237-9503-a1ae-8431-5b6cdb85ae9d
     * Codename: poster
     */
    poster: Elements.AssetsElement;

    /**
     * Category (multiple_choice)
     * Required: false
     * Id: 9821c252-6414-f549-c17f-cc171dd87713
     * Codename: category
     */
    category: Elements.MultipleChoiceElement;

    /**
     * Stars (modular_content)
     * Required: false
     * Id: aa26a55d-19f8-7501-fea3-b0d9b1eeac71
     * Codename: stars
     */
    stars: Elements.LinkedItemsElement<Actor | Movie>;

    /**
     * SeoName (url_slug)
     * Required: false
     * Id: 756cc91a-a090-60f9-a7f0-f505bfbe046c
     * Codename: seoname
     */
    seoname: Elements.UrlSlugElement;

    /**
     * ReleaseCategory (taxonomy)
     * Required: false
     * Id: 65f2fd44-1856-bc2b-17c2-decb0635e3d2
     * Codename: releasecategory
     */
    releasecategory: Elements.TaxonomyElement<ReleaseCategory>;
}>;

movietype.ts

/**
 * Generated by '@kontent-ai/model-generator@5.0.0' at 'Mon, 28 Mar 2022 14:36:32 GMT'
 *
 * MovieType
 * Id: 365a17e6-1929-27ab-9f67-a9273c846717
 * Codename: movietype
 */
export type MovieType = 'student' | 'film' | 'tv' | 'blockbuster' | 'cinema_only';

To learn the complete generator output, see the following folder: https://github.com/kontent-ai/model-generator-js/tree/master/sample

Contribution & Feedback

Contributions are welcomed. Simply make a pull request.