内容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();