XBlog API 文档
此页面提供XBlog内容API的使用说明和示例代码,方便开发者集成和使用。
内容API
内容API允许你获取博客文章、项目和作品集的数据。所有API返回JSON格式数据。
获取内容列表
GET
/api/content/:type参数
| 名称 | 类型 | 描述 |
|---|---|---|
| type | string | 内容类型: blog, projects, work |
响应示例
{
"success": true,
"data": [
{
"slug": "minimal-ui-design",
"frontmatter": {
"title": "极简UI设计的原则",
"description": "探索极简UI设计的关键原则...",
"date": "2023-05-12",
"category": "UI设计",
"image": "/svg/subtrack.svg"
}
},
// ...更多内容
],
"count": 5
}使用示例
// 获取博客文章列表
fetch('/api/content/blog')
.then(res => res.json())
.then(data => {
if (data.success) {
console.log(data.data);
}
});获取单个内容
GET
/api/content/:type/:slug参数
| 名称 | 类型 | 描述 |
|---|---|---|
| type | string | 内容类型: blog, projects, work |
| slug | string | 内容的唯一标识符 |
响应示例
{
"success": true,
"data": {
"slug": "minimal-ui-design",
"frontmatter": {
"title": "极简UI设计的原则",
"description": "探索极简UI设计的关键原则...",
"date": "2023-05-12",
"category": "UI设计",
"image": "/svg/subtrack.svg"
},
"content": "# 极简UI设计的原则\n\n极简主义设计不仅仅是一种美学选择..."
}
}使用示例
// 获取单个博客文章
fetch('/api/content/blog/minimal-ui-design')
.then(res => res.json())
.then(data => {
if (data.success) {
console.log(data.data);
}
});缓存管理API
缓存管理API允许你查看和清除内容缓存。这些API需要API密钥进行认证。
获取缓存信息
GET
/api/cache/info请求头
| 名称 | 描述 |
|---|---|
| Authorization | Bearer xblog-secret-key |
响应示例
{
"success": true,
"data": {
"stats": {
"size": 6,
"types": {
"blog": 3,
"projects": 2,
"work": 1
},
"lists": {
"blog": true,
"projects": true,
"work": false
},
"items": {
"blog": 3,
"projects": 2,
"work": 1
},
"oldestItem": "blog:list",
"newestItem": "blog:minimal-ui-design",
"oldestTimestampISO": "2023-07-18T14:25:33.012Z",
"newestTimestampISO": "2023-07-18T14:27:45.872Z"
},
"content": {
"blog": {
"count": 2,
"titles": ["极简UI设计的原则", "创建无障碍色彩系统"],
"slugs": ["minimal-ui-design", "accessibility-color-system"]
},
"projects": {
"count": 1,
"titles": ["移动银行应用"],
"slugs": ["mobile-banking-app"]
},
"work": null
},
"timestamp": "2023-07-18T14:28:15.123Z"
}
}清除缓存
POST
/api/cache/clear请求头
| 名称 | 描述 |
|---|---|
| Authorization | Bearer xblog-secret-key |
| Content-Type | application/json |
请求体
{
"type": "blog" // 可选,如果不提供则清除所有缓存
}响应示例
{
"success": true,
"message": "已清除 blog 类型的缓存"
}错误处理
所有API都使用标准的HTTP状态码和一致的错误响应格式。
错误响应格式
{
"success": false,
"error": "错误类型标识",
"message": "用户友好的错误说明"
}常见错误状态码
| 状态码 | 描述 |
|---|---|
| 400 | 请求参数错误或格式不正确 |
| 401 | 未提供授权或授权无效 |
| 404 | 请求的资源不存在 |
| 500 | 服务器内部错误 |