npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kontent-ai/model-generator

v7.0.3

Published

This utility generates strongly-typed models based on Content Types in a Kontent.ai project. These models can be used with SDKs

Downloads

8,052

Readme

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.