@contentstack/types-generator

中文文档
3.10.6发布于 2026/10/06MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1.2万

内容tack 类型定义生成库

中文使用文档
文档目录6 节
  1. 安装
  2. 移徙
  3. 使用量
  4. 在节点J
  5. 在 Web 应用程序中
  6. 使用指南

内容ack - TypeScript 生成库

这个库帮助生成 Stack 中可用的内容类型 TypeScript 类型定义 .

安装

$ npm install @contentstack/types-generator

移徙

参见 移徙指南 3版,如果您正在从2版或2版以上迁移.

使用量

在节点J

require("@contentstack/types-generator") 联合来文委员会

或

import {<< required method >>} from "@contentstack/types-generator" eCMAScript模块(ESM)

在 Web 应用程序中

import {<< required method >>} from "@contentstack/types-generator/dist/web"

使用指南

1. 联合国. generateTS() (可用于节点JS和Web应用程序)

这是一个同步方法,它利用给定输入生成 Stack 中可用的内容类型类型定义。 使用此方法用于 REST API。 。 。 .

输入 :

属性名称说明数据类型接受数值强制性默认值
符号用于授权的独特标识符字符串对
符号类型正在提供的令牌类型(目前我们只支持送出令牌)字符串交付对
键堆叠 API 密钥字符串对
环境环境名称(例如:开发、中转、生产)字符串对
区域目录 API 区域字符串美国(AWS NA)、欧盟(AWS 欧盟)、非盟(AWS AU)、AZURE NA、AZURE EU、GCP NA对
分支堆栈分支名称字符串没有
前缀为每个接口添加的可选前缀字符串没有
包括文档在生成文件中添加内容类型文档布尔真实,虚假没有真实
系统字段布尔旗表示是否在响应中包含系统生成的字段布尔真实,虚假没有虚假

输出 :

返回以数据解决或以错误拒绝的保证.

如果解决:

类型:字符串

数据: 生成的 Typescript 类型定义

如果被拒绝:

类型: 出错对象

数据 : 对象为 error_message

示例用法: generateTS()

import { generateTS } from "@contentstack/types-generator"; // Import statement for NodeJS
import { generateTS } from "@contentstack/types-generator/dist/web"; // Import statement for Web application

async function getTypeDef() {
  try {
    const typeDef = await generateTS({
      token: "<< your_delivery_token >>",
      tokenType: "delivery", // Currently we are supporting only delivery token
      apiKey: "<< your_stack_api_key >>",
      environment: "development",
      region: "US",
      branch: "main",
      prefix: "CS",
      includeDocumentation: true,
      systemFields: false,
    });

    // Handle the resolved promise, e.g., process the typeDef
  } catch (error) {
    // Handle the rejected promise
    // error: { error_message: "Unauthorized! Please check the given token and api key" }
  }
}

getTypeDef();

示例输出 : generateTS()

/** This is a description. */
interface BuiltinExample {
  /** Title */
  title: string;
  /** URL */
  url: string;
  /** Group1 */
  group1?: {
    /** Group2 */
    group2?: {
      /** Group3 */
      group3?: {
        /** Number */
        number?: number;
      };
    };
  };
  /** SEO */
  seo?: Seo;
  /** Single line textbox */
  single_line?: string;
  /** Multi line textbox */
  multi_line?: string;
  /** Rich text editor */
  rich_text_editor?: string;
  /** Multiple Single Line Textbox */
  multiple_single_line_textbox?: string[];
  /** Markdown */
  markdown?: string;
  /** Multiple Choice */
  multiple_choice?: ("Choice 1" | "Choice 2" | "Choice 3")[];
  /** Single Choice */
  single_choice: "Choice 1" | "Choice 2" | "Choice 3";
  /** Modular Blocks */
  modular_blocks?: ModularBlocks[];
  /** Number */
  number?: number;
  /** Link */
  link?: Link;
  /** File */
  file?: File;
  /** Boolean */
  boolean?: boolean;
  /** Date */
  date?: string;
}

interface ModularBlocks {
  block_1: {
    /** Number */
    number?: number;
    /** Single line textbox */
    single_line?: string;
  };
  block_2: {
    /** Boolean */
    boolean?: boolean;
    /** Date */
    date?: string;
  };
  seo_gf: {
    /** Keywords */
    keywords?: string;
    /** Description */
    description?: string;
  };
}

2. 联合国. graphqlTS() (仅供NodeJS使用)

这是一个同步方法,它利用给定的输入为GraphQL生成 Stack 中可用的内容类型类型定义。 使用此方法用于 GraphQL。 。 。 .

输入 :

属性名称说明数据类型接受数值强制性
符号用于授权的独特标识符 。 这应该是堆的送货符.字符串对
键堆叠 API 密钥字符串对
环境环境名称(例如:开发、中转、生产)字符串对
区域目录 API 区域字符串美国(AWS NA)、欧盟(AWS 欧盟)、非盟(AWS AU)、AZURE NA、AZURE EU、GCP NA对
分支堆栈分支名称字符串没有
命名空间识别计划内的特定命名空间字符串没有

输出 :

返回以数据解决或以错误拒绝的保证.

如果解决:

类型:字符串

数据: 生成的 Typescript 类型定义

如果被拒绝:

类型: 出错对象

数据 : 对象为 error_message

示例用法: graphqlTS()

import { graphqlTS } from "@contentstack/types-generator"; // Import statement for NodeJS

async function getTypeDef() {
  try {
    const typeDef = await graphqlTS({
      token: "<< your_delivery_token >>", // Currently we are supporting only delivery token
      apiKey: "<< your_stack_api_key >>",
      environment: "development",
      region: "US",
      branch: "main",
      namespace: "<< your_name_space >>",
    });

    // Handle the resolved promise, e.g., process the typeDef
  } catch (error) {
    // Handle the rejected promise
    // error: { error_message: "Unauthorized! Please check the given token and api key" }
  }
}

getTypeDef();

版本与发布记录

@contentstack/types-generator 共发布 35 个版本,首次发布于 2024/08/27;过去 12 个月发布了 13 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.10.6latest2026/10/061 个月前
3.10.52026/09/151 个月前
3.10.42026/08/132 个月前
3.10.32026/08/132 个月前
3.10.22026/06/194 个月前
3.10.12026/05/255 个月前

关于 @contentstack/types-generator 的常见问题

@contentstack/types-generator 是什么?

内容tack 类型定义生成库

如何安装 @contentstack/types-generator?

在项目目录运行 npm install @contentstack/types-generator,或使用 pnpm add / yarn add / bun add。

@contentstack/types-generator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@contentstack/types-generator 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@contentstack/types-generator 可以商用吗?

@contentstack/types-generator 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@contentstack/types-generator 的下载量有多少?

根据 npm 官方下载统计,@contentstack/types-generator 在 2026/10/11 – 2026/11/09 这 30 天内被下载 11,593 次,日均约 386 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@contentstack/types-generator 还在维护吗?

最新版本 3.10.6 发布于 2026/10/06(1 个月前);至今共发布 35 个版本,其中过去 12 个月发布了 13 个;首次发布于 2024/08/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。