JavaScript SDK
内置模块
文章、页面、媒体、商品、购物车、订单、支付、钱包、SSE 事件和健康检查。
SDK 为内置功能提供了专用模块。直接通过 client 实例访问。
文章
client.posts.list(query?)
client.posts.get(slug)
client.posts.create(body)
client.posts.update(slug, body)
client.posts.delete(slug)// 列出已发布的文章
const result = await client.posts.list({
page: 1,
page_size: 20,
status: "published",
category_id: "cat_id",
tag_id: "tag_id",
});
// 按 slug 获取
const post = await client.posts.get("hello-world");
// 创建
const post = await client.posts.create({
title: "Hello World",
content: "<p>第一篇文章</p>",
excerpt: "我的第一篇文章",
status: "draft",
category_id: "cat_id",
tag_ids: ["tag1", "tag2"],
});页面
client.pages.list(page?, pageSize?)
client.pages.get(slug)
client.pages.sitemap()const pages = await client.pages.list(1, 20);
const page = await client.pages.get("about");
const sitemap = await client.pages.sitemap();评论
client.comments.list(postSlug, page?, pageSize?)
client.comments.create(postSlug, body)
client.comments.listAll(page?, pageSize?)
client.comments.updateStatus(id, status)
client.comments.delete(id)// 某篇文章的评论
const comments = await client.comments.list("hello-world", 1, 20);
// 添加评论
await client.comments.create("hello-world", {
content: "好文章!",
nickname: "访客",
email: "visitor@example.com",
parent_id: null,
});分类
client.categories.list(page?, pageSize?)
client.categories.get(id)
client.categories.create(body)
client.categories.update(id, body)
client.categories.delete(id)标签
client.tags.list(page?, pageSize?)
client.tags.get(id)
client.tags.create(body)
client.tags.update(id, body)
client.tags.delete(id)媒体
client.media.upload(file, options?)
client.media.list(page?, pageSize?)
client.media.stats()
client.media.delete(id)
client.media.getFileURL(record, field, options?)// 上传文件
const fileInput = document.querySelector<HTMLInputElement>("#file")!;
const media = await client.media.upload(fileInput.files![0]);
// 获取文件 URL
const url = client.media.getFileURL(record, "cover");
// 带缩略图
const thumbUrl = client.media.getFileURL(record, "cover", {
thumb: "300x200",
});
// 存储统计
const stats = await client.media.stats();
// { total_files: 42, total_size: 10485760 }商品
client.products.list(page?, pageSize?)
client.products.get(id)const products = await client.products.list(1, 20);
const product = await client.products.get("product_id");购物车
client.cart.list()
client.cart.add(body)
client.cart.updateItem(id, body)
client.cart.removeItem(id)
client.cart.clear()
client.cart.checkout()// 查看购物车
const cart = await client.cart.list();
// 加入购物车
await client.cart.add({
product_id: "product_id",
quantity: 2,
});
// 更新数量
await client.cart.updateItem("item_id", { quantity: 3 });
// 移除商品
await client.cart.removeItem("item_id");
// 结算(创建订单)
const result = await client.cart.checkout();订单
client.orders.list(page?, pageSize?)
client.orders.get(id)
client.orders.create(body)
client.orders.cancel(id)
client.orders.confirmReceipt(id)// 直接创建订单
const order = await client.orders.create({
items: [
{ product_id: "product_id", quantity: 1 },
],
currency: "USD",
buyer_name: "Alice",
buyer_email: "alice@example.com",
shipping_address: "123 Main St",
remark: "请礼盒包装",
});
// 列出我的订单
const orders = await client.orders.list(1, 20);
// 取消
await client.orders.cancel("order_id");
// 确认收货
await client.orders.confirmReceipt("order_id");支付
client.payment.listAvailableChannels(query)
client.payment.createOrder(body)
client.payment.getOrder(id)
client.payment.cancelOrder(id)
client.payment.listTransactions(id)
client.payment.listRefunds(id)
client.payment.listOrders(page?, pageSize?)// 获取可用支付渠道
const channels = await client.payment.listAvailableChannels({
order_id: "order_id",
country: "US",
});
// 创建支付订单
const paymentOrder = await client.payment.createOrder({
order_id: "order_id",
channel: "stripe",
});钱包
client.wallets.list()
client.wallets.get(currency)
client.wallets.listTransactions(currency, query?)
client.wallets.listAllTransactions(query?)// 我的钱包
const wallets = await client.wallets.list();
// 某币种交易记录
const txs = await client.wallets.listTransactions("USD", {
page: 1,
page_size: 20,
});实时事件(SSE)
服务器推送实时事件:
const es = client.events.subscribe();
// 监听特定事件
es.addEventListener("post.created", (event) => {
console.log("新文章:", JSON.parse(event.data));
});
es.addEventListener("order.paid", (event) => {
console.log("订单已支付:", JSON.parse(event.data));
});
// 过滤事件
const es = client.events.subscribe("order.*");
// 清理
es.close();返回原生浏览器 EventSource。不支持 WebSocket — 实时功能仅通过 SSE。
健康检查
client.health.check()
client.health.liveness()
client.health.readiness()const status = await client.health.check();
// { status: "ok", version: "0.1.0" }