cs-icon.svg

Tsgen Plugin

The Contentstack CLI tsgen plugin generates TypeScript typings from REST API and GraphQL query. The type file containing the TypeScript typings assists developers in working with content types, global field, and GraphQL query in TypeScript.

With the tsgen plugin, you can annotate interfaces and fields with comments from the JSDoc library. Additionally, the tsgen plugin allows you to add prefixes to interfaces.

Note: The Contentstack CLI uses the introspection query to generate a GraphQL schema.

This step-by-step guide lets you install and use the tsgen plugin in CLI.

Prerequisites

Steps for execution

  1. Install the tsgen plugin using the following command:
  2. csdx plugins:install contentstack-cli-tsgen
    
  3. Add the delivery token as an alias to the CLI using the following command:
  4. csdx auth:tokens:add --delivery
    

    Note: Skip this step if you already have a delivery token as an alias.

Usage

csdx tsgen

Options

  • -a--token-alias=token-alias: (mandatory) Delivery token alias.
  • -d--[no-]doc: (optional) Includes documentation comments. By default, this flag is enabled.
  • -o--output=output: (mandatory) The full path to output.
  • -p--prefix=prefix: (optional) Interface prefix, e.g., "I".
  • --api-type=api-type: [default: rest] (optional) The API type to generate type definitions. <options: rest|graphql>.
  • --branch=branch: (optional) The name of the branch to be used.
  • --include-system-fields: Includes system fields in generated types.
  • --namespace=namespace: (optional) The namespace for the GraphQL API type to organize the generated types.

Examples

  1. To generate a type file with a delivery token alias and output file path:
    csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts"
    
  2. To generate a type file with a delivery token alias and output file path for a branch named develop:
    csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --branch "develop"
    
  3. To generate a type file with a prefix:
    csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" -p "I"
    
  4. To generate a type file excluding documentation comments:
    csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --no-doc
    
  5. To generate a type file with the graphql API type:
    csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --api-type graphql
    
  6. To generate a type file with the graphql API type and GraphQL as the namespace:
    csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --api-type graphql --namespace "GraphQL"
    

Supported Contentstack Fields

Supported Field Options

  • Mandatory
  • Multiple
  • Multiple Max Limit
  • Description (used in JSDoc comment)
Was this article helpful?
^