Code Monkey home page Code Monkey logo

model-generator-js's Introduction

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,
    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',
  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',
  addTimestamp: true,
  addEnvironmentInfo: true,
  elementResolver: 'camelCase',
  contentTypeResolver: (type) => `${textHelper.toPascalCase(type.codename)}Model`,
  taxonomyTypeResolver: (taxonomy) => `${textHelper.toPascalCase(taxonomy.codename)}Taxonomy`,
});

FAQ

  • If you are getting the The Subscription API is not supported in your plan error, set the isEnterpriseSubscription option to false

CLI Configuration

  • environmentId - Id of the Kontent.ai environment
  • apiKey- Management API Key
  • outputDir- Directory where files will be created. Defaults to current directory - --outputDir=./. Some other examples: --outputDir=./sample
  • isEnterpriseSubscription - Indicates if enterprise subscription endpoint can be used to export data.
  • addTimestamp- Indicates if timestamp is added to generated models
  • addEnvironmentInfo- Indicates if environment info stamp is added to generated models
  • elementResolver- Name resolver for elements. Available options are: camelCase, pascalCase, snakeCase
  • contentTypeFileResolver- Name resolver for content type filenames. Available options are: camelCase, pascalCase, snakeCase
  • contentTypeSnippetFileResolver- Name resolver for content type snippet filenames. Available options are: camelCase, pascalCase, snakeCase
  • taxonomyTypeFileResolver- Name resolver for taxonomy filenames. Available options are: camelCase, pascalCase, snakeCase
  • contentTypeResolver- Name resolver for content type names. Available options are: camelCase, pascalCase, snakeCase
  • contentTypeSnippetResolver- Name resolver for content type snippet names. Available options are: camelCase, pascalCase, snakeCase
  • taxonomyTypeResolver- Name resolver for taxonomy type names. Available options are: camelCase, pascalCase, snakeCase
  • sdkType- Type of sdk for which models are generated. Available options are: delivery
  • exportWebhooks - Indicates if webhooks are exported
  • exportWorkflows - Indicates if workflows are exported
  • exportAssetFolders - Indicates if asset folders are exported
  • exportCollections - Indicates if collections are exported
  • exportLanguages - Indicates if languages are exported
  • exportRoles - Indicates if roles are exported. * Only available for Enterprise subscription plans
  • managementApiUrl - Sets the url of Management API.

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/[email protected]' 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/[email protected]' 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.

model-generator-js's People

Contributors

enngage avatar renovate[bot] avatar simply007 avatar ivankiral avatar jirilojda avatar petrsvihlik avatar x-oss-byte avatar kontent-ai-bot avatar

Watchers

 avatar

model-generator-js's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): replace dependency standard-version with commit-and-tag-version 9.5.0
  • chore(deps): update dependency eslint to v8.57.0
  • chore(deps): update actions/setup-node action to v4
  • chore(deps): update dependency eslint to v9
  • chore(deps): update typescript-eslint monorepo to v7 (major) (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)
  • fix(deps): update dependency @kontent-ai/management-sdk to v6
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/main.yml
  • actions/checkout v2
  • actions/setup-node v1
npm
package.json
  • yargs 17.7.2
  • prettier 3.0.1
  • colors 1.4.0
  • @kontent-ai/delivery-sdk 14.3.1
  • @kontent-ai/management-sdk 5.2.0
  • @types/yargs 17.0.24
  • standard-version 9.5.0
  • typescript 5.1.6
  • @types/node 20.5.0
  • @types/prettier 3.0.0
  • @typescript-eslint/eslint-plugin 6.7.3
  • @typescript-eslint/parser 6.7.3
  • eslint 8.50.0
  • ts-node 10.9.1
  • dotenv-cli 7.2.1
  • node >= 8

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.