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
- Contentstack account
- CLI installed (version 1.1.0 and above)
- Delivery token
Steps for execution
- Install the tsgen plugin using the following command:
- Add the delivery token as an alias to the CLI using the following command:
csdx plugins:install contentstack-cli-tsgen
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
- 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"
- 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"
- To generate a type file with a prefix:
csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" -p "I"
- To generate a type file excluding documentation comments:
csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --no-doc
- To generate a type file with the graphql API type:
csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --api-type graphql
- 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
- Number
- Title
- Date
- Boolean
- Single Select w/ String and Number Types
- Multiple Select w/ String and Number Types
- Modular Block
- Global
- Group
- Link
- File
- Reference
- Taxonomy
Supported Field Options
- Mandatory
- Multiple
- Multiple Max Limit
- Description (used in JSDoc comment)