@ bettercms-ai/ sdk (英语)
为BetterCMS交付API所打入的JavaScript/TypeScript客户端.
安装
npm install @bettercms-ai/sdk
# or
pnpm add @bettercms-ai/sdk
# or
yarn add @bettercms-ai/sdk
快开始
import { BetterCMS } from "@bettercms-ai/sdk";
const client = BetterCMS.site({ workspace: "my-workspace" });
// Fetch a single page
const page = await client.getContent("home");
console.log(page.entry.title);
// List pages (paginated)
const result = await client.listContent({ page: 1, perPage: 20 });
console.log(result.items);
console.log(`Next page? ${result.hasNextPage}`);
// Iterate all pages automatically
for await (const item of client.listContentAll()) {
console.log(item.entry.title);
}
// Collect with a limit
const top = await client.listContentAll().all({ limit: 500 });
带有 API 密钥
const client = BetterCMS.site({
workspace: "my-workspace",
apiKey: process.env.BETTERCMS_API_KEY,
});
类型脚本
这个包被用TypeScript来写出,并用自有的种类声明来写出. 无单独的 @types/ 需要软件包。 所有类型来自 @bettercms-ai/types 被再出口.
处理错误
SDK抛出 BetterCMSError 关于失败。 每个错误都有语义 code数字 :
import { BetterCMSError, ErrorCodes } from "@bettercms-ai/sdk";
try {
const page = await client.getContent("does-not-exist");
} catch (err) {
if (err instanceof BetterCMSError) {
switch (err.code) {
case ErrorCodes.CONTENT_NOT_FOUND:
// 404
break;
case ErrorCodes.UNAUTHORIZED:
// 401
break;
case ErrorCodes.FORBIDDEN:
// 403
break;
case ErrorCodes.RATE_LIMITED:
// 429
break;
case ErrorCodes.INTERNAL_ERROR:
// 5xx
break;
case ErrorCodes.NETWORK_ERROR:
// Network failure
break;
}
}
}
预览和视觉编辑草稿
一个客户端创建于 perspective: "drafts" 读取未公布的内容——通过
content:read:draft API 密钥, 或者通过 a previewToken 为一份文件做了薄荷.
草案还载有 stega 出处: 附加到每个字的无形零宽字符
出道值,命名出道的入道和出道场. 这就是让仪表板的画布
(和 <VisualEditing> 覆盖) 映射一个已渲染的字符串返回到它的编辑器,而不猜测
它的文本。 没错 默认值 perspective: "drafts" 从1.15.0开始 在此之前,它不得不
请求,并预览一个未知要询问已变无名字符串的构建.
const draft = createClient({
apiUrl,
workspace: "my-workspace",
apiKey: process.env.BETTERCMS_DRAFT_KEY,
perspective: "drafts", // stega is on
});
// Opt out — e.g. when diffing or checksumming draft content, where the invisible
// characters would show up as differences.
const plain = createClient({
apiUrl,
workspace: "my-workspace",
apiKey: process.env.BETTERCMS_DRAFT_KEY,
perspective: "drafts",
stega: false,
});
出版的书从不携带出处,不管什么 stega 设定为:有效载荷不能达到
生产建设。 自己脱去 stripStega(value) 如果你需要干净的绳子,和
解码为 decodeStega(value)。 。 。 .
丰富文本
一个丰富的字段到达 { format, value, html } (或作赤弦相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相相接相接相相相接相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相
从文本切换). 渲染器 html 与 rich(),或使结构(value)你自己:
import { rich, plain, portableText, embeds, resolved } from "@bettercms-ai/sdk";
const entry = await client.getEntry<{ body: RichTextValue }>("launch");
rich(entry.data.body); // HTML for set:html / dangerouslySetInnerHTML: everything drawn
plain(entry.data.body); // text for <title>, meta description, alt
// Rendering the structure with @bettercms-ai/ui (or your own components):
<PortableText
value={portableText(entry.data.body) ?? entry.data.body}
embeds={embeds(entry.data.body) ?? undefined}
resolved={resolved(entry.data.body, entry) ?? undefined}
/>;
相关帖子和作者的生平由服务器从公布的回复中解决
条目,并且从未存储。 信封价值包含其决心的内容(value.resolved页:1
以空块阵列存储的正体不能, 因此条目附带合并的地图 data
(单位:千美元)entry.resolved) (中文(简体) ). resolved(value, entry) 返回该值自己的地图,否则则返回条目的地图.
API 密码
BetterCMS.site(options)
| 选项 | 类型 | 需求 | 默认 |
|---|---|---|---|
workspace | string | 对 | —— 说 |
apiKey | string | 没有 | —— 说 |
baseUrl | string | 没有 | https://api.bettercms.ai/api/v1/delivery |
回返a BetterCMSDeliveryClient 举个例子.
client.getContent(slug)
以 sattle 获取单个已出版的页面 .
const content = await client.getContent("about-us");
client.listContent(options?)
返回一个相加结果对象.
const result = await client.listContent({ page: 1, perPage: 20 });
// result.items, result.hasNextPage, result.totalPages, ...
client.listContentAll(options?)
返回一个透明地遵循平面标记的同步指示器.
for await (const item of client.listContentAll()) { ... }
// With a limit:
const items = await client.listContentAll().all({ limit: 100 });