Tsgen Plugin
The tsgen plugin is a Contentstack CLI plugin that generates TypeScript typings from content types. The type file containing the TypeScript typings assists developers in working with content types 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.
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 -d
Note: Skip this step if you already have a delivery token.
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".
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 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
Supported Contentstack Fields
- Number
- Text
- IsoDate
- Boolean
- Single Select w/ String and Number Types
- Multiple Select w/ String and Number Types
- Modular Block
- Global Field
- Group
- Link
- File
- References
Supported Field Options
- Mandatory
- Multiple
- Multiple Max Limit
- Description (used in JSDoc comment)