feat: Enhance HaloClient with retry logic and improve error handling

- Added retry options to HaloClient for handling transient errors.
- Refactored request methods in HaloClient to utilize retry logic.
- Updated HaloService to include logging for error handling.
- Introduced ApiPaths utility for managing API endpoints.
- Implemented logger utility for consistent logging across services.
- Added tests for ContentService, Error handling, and TaxonomyService.
- Created retry utility for managing retry logic with exponential backoff.
- Updated types to include additional properties for better API response handling.
This commit is contained in:
2026-04-28 18:01:26 +08:00
parent b7f6288492
commit 12a7aebeff
18 changed files with 1573 additions and 815 deletions
+5 -2
View File
@@ -8,7 +8,9 @@
"dev": "rslib build --watch",
"build": "rslib build",
"version": "node version-bump.mjs && git add manifest.json versions.json",
"check": "biome check --write src/"
"check": "biome check --write src/",
"test": "vitest",
"test:run": "vitest run"
},
"author": "LHY",
"maintainers": [
@@ -28,7 +30,8 @@
"builtin-modules": "3.3.0",
"obsidian": "latest",
"tslib": "2.6.0",
"typescript": "5.1.6"
"typescript": "5.1.6",
"vitest": "^4.1.5"
},
"dependencies": {
"@halo-dev/api-client": "^2.20.0",
+734
View File
File diff suppressed because it is too large Load Diff
+4 -4
View File
@@ -2,7 +2,7 @@ import i18next from "i18next";
import { Notice, requestUrl } from "obsidian";
import { openSiteSelectionModal } from "../site-selection-modal";
import type HaloPlugin from "../main";
import type { HaloSite } from "../settings";
import type { Post, Snapshot } from "../services/types";
export async function exportPostAsMarkdown(plugin: HaloPlugin, postName: string, title: string): Promise<void> {
try {
@@ -18,7 +18,7 @@ export async function exportPostAsMarkdown(plugin: HaloPlugin, postName: string,
const snapshot = (await requestUrl({
url: `${site.url}/apis/uc.api.content.halo.run/v1alpha1/posts/${postName}/draft?patched=true`,
headers,
}).json) as any;
}).json) as Snapshot;
const { "content.halo.run/patched-raw": raw } = snapshot?.metadata?.annotations || {};
@@ -59,12 +59,12 @@ export async function exportPostAsJson(plugin: HaloPlugin, postName: string, tit
const post = (await requestUrl({
url: `${site.url}/apis/uc.api.content.halo.run/v1alpha1/posts/${postName}`,
headers,
}).json) as any;
}).json) as Post;
const snapshot = (await requestUrl({
url: `${site.url}/apis/uc.api.content.halo.run/v1alpha1/posts/${postName}/draft?patched=true`,
headers,
}).json) as any;
}).json) as Snapshot;
const exportData = {
post: post,
@@ -1,5 +1,5 @@
import i18next from "i18next";
import { Modal, Notice, Setting } from "obsidian";
import { Modal, Notice, Setting, type TFile } from "obsidian";
import type HaloPlugin from "../main";
import type { HaloSite } from "../settings";
@@ -72,9 +72,10 @@ class DeleteConfirmModal extends Modal {
});
});
const hasLocalFile = this.plugin.app.metadataCache.getFileCache(
Array.from(this.plugin.app.vault.getFiles()).find(f => f.basename === this.title) || this.plugin.app.vault.getAbstractFileByPath(`/${this.title}.md`) as any
);
const foundFile = Array.from(this.plugin.app.vault.getFiles()).find((f: TFile) => f.basename === this.title);
const abstractFile = this.plugin.app.vault.getAbstractFileByPath(`/${this.title}.md`);
const localFile = (foundFile || (abstractFile instanceof TFile ? abstractFile : null));
const hasLocalFile = localFile ? this.plugin.app.metadataCache.getFileCache(localFile) : null;
if (!hasLocalFile) {
const localFile = this.plugin.app.vault.getAbstractFileByPath(`/${this.title}.md`);
+5 -3
View File
@@ -1,5 +1,7 @@
import type { TFile, Vault } from "obsidian";
import { requestUrl } from "obsidian";
import { logger } from "../utils/logger";
import { ApiPaths } from "../utils/api-paths";
export default class ImageUploader {
private readonly siteUrl: string;
@@ -37,7 +39,7 @@ export default class ImageUploader {
const file = vault.getAbstractFileByPath(normalizedPath);
if (!file || !(file instanceof TFile)) {
console.error(`[ImageUploader] 文件不存在或不是有效文件: ${normalizedPath}`);
logger.error("ImageUploader", `文件不存在或不是有效文件: ${normalizedPath}`);
return null;
}
@@ -54,7 +56,7 @@ export default class ImageUploader {
body += `\r\n--${boundary}--\r\n`;
const response = await requestUrl({
url: `${this.siteUrl}/apis/api.console.halo.run/v1alpha1/attachments/upload`,
url: `${this.siteUrl}${ApiPaths.attachments.upload()}`,
method: "POST",
headers: {
...this.headers,
@@ -72,7 +74,7 @@ export default class ImageUploader {
return null;
} catch (error) {
console.error(`[ImageUploader] 上传图片失败: ${filePath}`, error);
logger.error("ImageUploader", `上传图片失败: ${filePath}`, error);
return null;
}
}
+6 -758
View File
@@ -1,758 +1,6 @@
import ImageUploader from "./image-uploader";
import i18next from "i18next";
import { type App, Notice, requestUrl, type TFile } from "obsidian";
import { randomUUID } from "src/utils/id";
import markdownIt from "src/utils/markdown";
import { slugify } from "transliteration";
import type { HaloSetting, HaloSite } from "../settings";
import { extractImageReferences, getAbsolutePath, replaceImagePaths } from "../utils/image";
class HaloService {
private readonly site: HaloSite;
private readonly app: App;
private readonly settings: HaloSetting;
private readonly headers: Record<string, string> = {};
constructor(app: App, settings: HaloSetting, site: HaloSite) {
this.app = app;
this.settings = settings;
this.site = site;
this.headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${site.token}`,
};
}
public async getPost(name: string): Promise<{ post: Post; content: Content } | undefined> {
try {
const post = (await requestUrl({
url: `${this.site.url}/apis/uc.api.content.halo.run/v1alpha1/posts/${name}`,
headers: this.headers,
}).json) as Post;
const snapshot = (await requestUrl({
url: `${this.site.url}/apis/uc.api.content.halo.run/v1alpha1/posts/${name}/draft?patched=true`,
headers: this.headers,
}).json) as Snapshot;
const { "content.halo.run/patched-content": patchedContent, "content.halo.run/patched-raw": patchedRaw } =
snapshot.metadata.annotations || {};
const { rawType } = snapshot.spec || {};
const content: Content = {
content: patchedContent,
raw: patchedRaw,
rawType,
};
return Promise.resolve({
post,
content,
});
} catch (error) {
return Promise.resolve(undefined);
}
}
public async publishPost(): Promise<void> {
const { activeEditor } = this.app.workspace;
if (!activeEditor || !activeEditor.file) {
return;
}
const imageUploader = new ImageUploader(this.site.url, this.site.token);
let params: Post = {
apiVersion: "content.halo.run/v1alpha1",
kind: "Post",
metadata: {
annotations: {},
name: "",
},
spec: {
allowComment: true,
baseSnapshot: "",
categories: [],
cover: "",
deleted: false,
excerpt: {
autoGenerate: true,
raw: "",
},
headSnapshot: "",
htmlMetas: [],
owner: "",
pinned: false,
priority: 0,
publish: false,
publishTime: "",
releaseSnapshot: "",
slug: "",
tags: [],
template: "",
title: "",
visible: "PUBLIC",
},
};
let content: Content = {
rawType: "markdown",
raw: "",
content: "",
};
const md = await this.app.vault.read(activeEditor.file);
const matterData = this.app.metadataCache.getFileCache(activeEditor.file)?.frontmatter;
const frontmatterPosition = this.app.metadataCache.getFileCache(activeEditor.file)?.frontmatterPosition;
const raw = frontmatterPosition ? md.slice(frontmatterPosition?.end.offset) : md;
// 检测并上传本地图片
let processedRaw = raw;
if (this.settings.imageUpload?.enabled) {
console.log("[HaloService] 图片上传功能已启用");
const imageReferences = extractImageReferences(raw);
console.log(`[HaloService] 检测到 ${imageReferences.length} 个图片引用`);
if (imageReferences.length > 0) {
const localImages = imageReferences.filter((ref) => !ref.path.startsWith("http://") && !ref.path.startsWith("https://") && !ref.path.startsWith("data:"));
console.log(`[HaloService] 其中 ${localImages.length} 个是本地图片`);
if (localImages.length > 0) {
new Notice(`检测到 ${localImages.length} 个本地图片,正在上传...`);
const absolutePaths = localImages
.map((ref) => ({
original: ref.path,
absolute: getAbsolutePath(this.app.vault, ref.path, activeEditor.file.path),
}))
.filter((item) => item.absolute !== null) as { original: string; absolute: string }[];
console.log(`[HaloService] 其中 ${absolutePaths.length} 个图片可以解析为绝对路径`);
if (absolutePaths.length > 0) {
console.log(`[HaloService] 开始上传 ${absolutePaths.length} 个图片到 ${this.site.url}`);
const pathMapping = await imageUploader.uploadImages(absolutePaths.map((item) => item.absolute), this.app.vault);
console.log(`[HaloService] 上传完成,成功 ${pathMapping.size}`);
if (pathMapping.size > 0) {
const mapping = new Map<string, string>();
absolutePaths.forEach((item) => {
const remoteUrl = pathMapping.get(item.absolute);
if (remoteUrl) {
mapping.set(item.original, remoteUrl);
}
});
processedRaw = replaceImagePaths(processedRaw, mapping);
const successCount = mapping.size;
const failCount = absolutePaths.length - successCount;
if (failCount === 0) {
new Notice(`✓ 图片上传成功 (${successCount}/${absolutePaths.length})`);
} else {
new Notice(`⚠ 部分图片上传失败 (成功: ${successCount}, 失败: ${failCount})`);
}
} else {
new Notice("⚠ 图片上传失败,保留原始路径");
}
}
}
}
} else {
console.log("[HaloService] 图片上传功能未启用");
}
// 检查站点 URL
if (matterData?.halo?.site && matterData.halo.site !== this.site.url) {
new Notice(i18next.t("service.error_site_not_match"));
return;
}
// 如果已发布,获取现有文章信息
if (matterData?.halo?.name) {
const post = await this.getPost(matterData.halo.name);
if (post) {
params = post.post;
content = post.content;
}
}
content.raw = processedRaw;
content.content = markdownIt.render(processedRaw);
// 恢复元数据
if (matterData?.title) {
params.spec.title = matterData.title;
}
if (matterData?.slug) {
params.spec.slug = matterData.slug;
}
if (matterData?.excerpt) {
params.spec.excerpt.raw = matterData.excerpt;
params.spec.excerpt.autoGenerate = false;
}
if (matterData?.cover) {
params.spec.cover = matterData.cover;
}
if (matterData?.categories) {
const categoryNames = await this.getCategoryNames(matterData.categories);
params.spec.categories = categoryNames;
}
if (matterData?.tags) {
const tagNames = await this.getTagNames(matterData.tags);
params.spec.tags = tagNames;
}
try {
// 设置标题和 slug
params.spec.title = matterData?.title || activeEditor.file.basename;
params.spec.slug = matterData?.slug || slugify(params.spec.title, { trim: true });
// 设置内容注解
params.metadata.annotations = {
...params.metadata.annotations,
"content.halo.run/content-json": JSON.stringify(content),
};
// 设置 metadata.name(如果还没有的话)
if (!params.metadata.name) {
params.metadata.name = randomUUID();
}
console.log(`[HaloService] 开始发布文章,站点: ${this.site.url}`);
console.log(`[HaloService] 文章标题: ${params.spec.title}`);
console.log(`[HaloService] 文章 slug: ${params.spec.slug}`);
console.log(`[HaloService] 文章 name: ${params.metadata.name}`);
// 发送创建/更新请求
const isUpdate = !!matterData?.halo?.name;
if (isUpdate) {
console.log(`[HaloService] 更新现有文章: ${params.metadata.name}`);
await requestUrl({
url: `${this.site.url}/apis/uc.api.content.halo.run/v1alpha1/posts/${params.metadata.name}`,
method: "PUT",
contentType: "application/json",
headers: this.headers,
body: JSON.stringify(params),
});
console.log(`[HaloService] 文章基本信息更新成功`);
} else {
console.log(`[HaloService] 创建新文章`);
const post = await requestUrl({
url: `${this.site.url}/apis/uc.api.content.halo.run/v1alpha1/posts`,
method: "POST",
contentType: "application/json",
headers: this.headers,
body: JSON.stringify(params),
}).json;
console.log(`[HaloService] 文章创建响应:`, JSON.stringify(post));
if (!post || !post.metadata) {
console.error(`[HaloService] 创建文章响应格式错误:`, post);
throw new Error("创建文章响应格式错误");
}
console.log(`[HaloService] 文章创建成功: ${post.metadata.name}`);
params = post;
}
// 处理发布状态
// biome-ignore lint: no
if (matterData?.halo?.hasOwnProperty("publish")) {
if (matterData?.halo?.publish) {
await this.changePostPublish(params.metadata.name, true);
} else {
await this.changePostPublish(params.metadata.name, false);
}
} else {
if (this.settings.publishByDefault) {
await this.changePostPublish(params.metadata.name, true);
}
}
const postResult = await this.getPost(params.metadata.name);
if (!postResult || !postResult.post.metadata) {
console.error("[HaloService] 获取文章详情失败");
new Notice(i18next.t("service.error_publish_failed"));
return;
}
params = postResult.post;
const postCategories = await this.getCategoryDisplayNames(params.spec.categories);
const postTags = await this.getTagDisplayNames(params.spec.tags);
this.app.fileManager.processFrontMatter(activeEditor.file, (frontmatter) => {
frontmatter.title = params.spec.title;
frontmatter.slug = params.spec.slug;
frontmatter.cover = params.spec.cover;
frontmatter.excerpt = params.spec.excerpt.autoGenerate ? undefined : params.spec.excerpt.raw;
frontmatter.categories = postCategories;
frontmatter.tags = postTags;
frontmatter.halo = {
site: this.site.url,
name: params.metadata.name,
publish: params.spec.publish,
};
});
new Notice(i18next.t("service.notice_publish_success"));
} catch (error) {
console.error(`[HaloService] 发布失败,错误:`, error);
new Notice(i18next.t("service.error_publish_failed"));
return;
}
}
public async changePostPublish(name: string, publish: boolean): Promise<void> {
await requestUrl({
url: `${this.site.url}/apis/uc.api.content.halo.run/v1alpha1/posts/${name}/${publish ? "publish" : "unpublish"}`,
method: "PUT",
contentType: "application/json",
headers: this.headers,
});
}
public async getCategories(): Promise<Category[]> {
const data = await requestUrl({
url: `${this.site.url}/apis/content.halo.run/v1alpha1/categories`,
headers: this.headers,
});
return Promise.resolve(data.json.items);
}
public async getTags(): Promise<Tag[]> {
const data = await requestUrl({
url: `${this.site.url}/apis/content.halo.run/v1alpha1/tags`,
headers: this.headers,
});
return Promise.resolve(data.json.items);
}
public async updatePost(): Promise<void> {
const { activeEditor } = this.app.workspace;
if (!activeEditor || !activeEditor.file) {
return;
}
const matterData = this.app.metadataCache.getFileCache(activeEditor.file)?.frontmatter;
if (!matterData?.halo?.name) {
new Notice(i18next.t("service.error_not_published"));
return;
}
const post = await this.getPost(matterData.halo.name);
if (!post) {
new Notice(i18next.t("service.error_post_not_found"));
return;
}
const postCategories = await this.getCategoryDisplayNames(post.post.spec.categories);
const postTags = await this.getTagDisplayNames(post.post.spec.tags);
await this.app.vault.modify(activeEditor.file, `${post.content.raw}`);
this.app.fileManager.processFrontMatter(activeEditor.file, (frontmatter) => {
frontmatter.title = post.post.spec.title;
frontmatter.slug = post.post.spec.slug;
frontmatter.cover = post.post.spec.cover;
frontmatter.excerpt = post.post.spec.excerpt.autoGenerate ? undefined : post.post.spec.excerpt.raw;
frontmatter.categories = postCategories;
frontmatter.tags = postTags;
frontmatter.halo = {
site: this.site.url,
name: post.post.metadata.name,
publish: post.post.spec.publish,
};
});
}
public async pullPost(name: string): Promise<void> {
const post = await this.getPost(name);
if (!post) {
new Notice(i18next.t("service.error_post_not_found"));
return;
}
const postCategories = await this.getCategoryDisplayNames(post.post.spec.categories);
const postTags = await this.getTagDisplayNames(post.post.spec.tags);
const file = await this.app.vault.create(`${post.post.spec.title}.md`, `${post.content.raw}`);
this.app.workspace.getLeaf().openFile(file);
this.app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter.title = post.post.spec.title;
frontmatter.slug = post.post.spec.slug;
frontmatter.cover = post.post.spec.cover;
frontmatter.excerpt = post.post.spec.excerpt.autoGenerate ? undefined : post.post.spec.excerpt.raw;
frontmatter.categories = postCategories;
frontmatter.tags = postTags;
frontmatter.halo = {
site: this.site.url,
name: name,
publish: post.post.spec.publish,
};
});
}
public async getCategoryNames(displayNames: string[]): Promise<string[]> {
const allCategories = await this.getCategories();
const notExistDisplayNames = displayNames.filter(
(name) => !allCategories.find((item) => item.spec.displayName === name),
);
const promises = notExistDisplayNames.map((name, index) =>
requestUrl({
url: `${this.site.url}/apis/content.halo.run/v1alpha1/categories`,
method: "POST",
contentType: "application/json",
headers: this.headers,
body: JSON.stringify({
spec: {
displayName: name,
slug: slugify(name, { trim: true }),
description: "",
cover: "",
template: "",
priority: allCategories.length + index,
children: [],
},
apiVersion: "content.halo.run/v1alpha1",
kind: "Category",
metadata: { name: "", generateName: "category-" },
}),
}),
);
const newCategories = await Promise.all(promises);
const existNames = displayNames
.map((name) => {
const found = allCategories.find((item) => item.spec.displayName === name);
return found ? found.metadata.name : undefined;
})
.filter(Boolean) as string[];
return [...existNames, ...newCategories.map((item) => item.json.metadata.name)];
}
public async getCategoryDisplayNames(names?: string[]): Promise<string[]> {
const categories = await this.getCategories();
return names
?.map((name) => {
const found = categories.find((item) => item.metadata.name === name);
return found ? found.spec.displayName : undefined;
})
.filter(Boolean) as string[];
}
public async getTagNames(displayNames: string[]): Promise<string[]> {
const allTags = await this.getTags();
const notExistDisplayNames = displayNames.filter((name) => !allTags.find((item) => item.spec.displayName === name));
const promises = notExistDisplayNames.map((name) =>
requestUrl({
url: `${this.site.url}/apis/content.halo.run/v1alpha1/tags`,
method: "POST",
contentType: "application/json",
headers: this.headers,
body: JSON.stringify({
spec: {
displayName: name,
slug: slugify(name, { trim: true }),
color: "#ffffff",
cover: "",
},
apiVersion: "content.halo.run/v1alpha1",
kind: "Tag",
metadata: { name: "", generateName: "tag-" },
}),
}),
);
const newTags = await Promise.all(promises);
const existNames = displayNames
.map((name) => {
const found = allTags.find((item) => item.spec.displayName === name);
return found ? found.metadata.name : undefined;
})
.filter(Boolean) as string[];
return [...existNames, ...newTags.map((item) => item.json.metadata.name)];
}
public async getTagDisplayNames(names?: string[]): Promise<string[]> {
const tags = await this.getTags();
return names
?.map((name) => {
const found = tags.find((item) => item.metadata.name === name);
return found ? found.spec.displayName : undefined;
})
.filter(Boolean) as string[];
}
public async importPost(file: TFile, publishImmediately: boolean = false): Promise<boolean> {
try {
const imageUploader = new ImageUploader(this.site.url, this.site.token);
const md = await this.app.vault.read(file);
const matterData = this.app.metadataCache.getFileCache(file)?.frontmatter;
const frontmatterPosition = this.app.metadataCache.getFileCache(file)?.frontmatterPosition;
let raw = frontmatterPosition ? md.slice(frontmatterPosition.end.offset) : md;
// 检测并上传本地图片
if (this.settings.imageUpload?.enabled) {
const imageReferences = extractImageReferences(raw);
if (imageReferences.length > 0) {
const localImages = imageReferences.filter((ref) =>
!ref.path.startsWith("http://") &&
!ref.path.startsWith("https://") &&
!ref.path.startsWith("data:")
);
if (localImages.length > 0) {
new Notice(`检测到 ${localImages.length} 个本地图片,正在上传...`);
const absolutePaths = localImages
.map((ref) => ({
original: ref.path,
absolute: getAbsolutePath(this.app.vault, ref.path, file.path),
}))
.filter((item) => item.absolute !== null) as { original: string; absolute: string }[];
if (absolutePaths.length > 0) {
const pathMapping = await imageUploader.uploadImages(absolutePaths.map((item) => item.absolute), this.app.vault);
if (pathMapping.size > 0) {
const mapping = new Map<string, string>();
absolutePaths.forEach((item) => {
const remoteUrl = pathMapping.get(item.absolute);
if (remoteUrl) {
mapping.set(item.original, remoteUrl);
}
});
raw = replaceImagePaths(raw, mapping);
const successCount = mapping.size;
const failCount = absolutePaths.length - successCount;
if (failCount === 0) {
new Notice(`✓ 图片上传成功 (${successCount}/${absolutePaths.length})`);
} else {
new Notice(`⚠ 部分图片上传失败 (成功: ${successCount}, 失败: ${failCount})`);
}
}
}
}
}
}
const content: Content = {
rawType: "markdown",
raw: raw,
content: markdownIt.render(raw),
};
let params: Post = {
apiVersion: "content.halo.run/v1alpha1",
kind: "Post",
metadata: {
annotations: {
"content.halo.run/content-json": JSON.stringify(content),
},
name: randomUUID(),
},
spec: {
allowComment: true,
baseSnapshot: "",
categories: [],
cover: matterData?.cover || "",
deleted: false,
excerpt: {
autoGenerate: matterData?.excerpt ? false : true,
raw: matterData?.excerpt || "",
},
headSnapshot: "",
htmlMetas: [],
owner: "",
pinned: false,
priority: 0,
publish: publishImmediately,
publishTime: "",
releaseSnapshot: "",
slug: matterData?.slug || slugify(matterData?.title || file.basename, { trim: true }),
tags: [],
template: "",
title: matterData?.title || file.basename,
visible: "PUBLIC",
},
};
// 处理分类
if (matterData?.categories) {
const categoryNames = await this.getCategoryNames(matterData.categories);
params.spec.categories = categoryNames;
}
// 处理标签
if (matterData?.tags) {
const tagNames = await this.getTagNames(matterData.tags);
params.spec.tags = tagNames;
}
// 创建文章
const post = await requestUrl({
url: `${this.site.url}/apis/uc.api.content.halo.run/v1alpha1/posts`,
method: "POST",
contentType: "application/json",
headers: this.headers,
body: JSON.stringify(params),
}).json;
return !!post && !!post.metadata;
} catch (error) {
console.error("[HaloService] 导入文章失败:", error);
return false;
}
}
public async deletePost(name: string): Promise<boolean> {
try {
await requestUrl({
url: `${this.site.url}/apis/uc.api.content.halo.run/v1alpha1/posts/${name}`,
method: "DELETE",
headers: this.headers,
});
return true;
} catch (error) {
console.error("[HaloService] 删除文章失败:", error);
return false;
}
}
public async createTag(displayName: string, slug: string, color: string): Promise<void> {
await requestUrl({
url: `${this.site.url}/apis/content.halo.run/v1alpha1/tags`,
method: "POST",
contentType: "application/json",
headers: this.headers,
body: JSON.stringify({
spec: {
displayName,
slug,
color: color || "#ffffff",
cover: "",
},
apiVersion: "content.halo.run/v1alpha1",
kind: "Tag",
metadata: { name: "", generateName: "tag-" },
}),
});
}
public async updateTag(name: string, displayName: string, slug: string, color: string): Promise<void> {
const tag = (await requestUrl({
url: `${this.site.url}/apis/content.halo.run/v1alpha1/tags/${name}`,
headers: this.headers,
}).json) as any;
tag.spec.displayName = displayName;
tag.spec.slug = slug;
tag.spec.color = color || "#ffffff";
await requestUrl({
url: `${this.site.url}/apis/content.halo.run/v1alpha1/tags/${name}`,
method: "PUT",
contentType: "application/json",
headers: this.headers,
body: JSON.stringify(tag),
});
}
public async deleteTag(name: string): Promise<void> {
await requestUrl({
url: `${this.site.url}/apis/content.halo.run/v1alpha1/tags/${name}`,
method: "DELETE",
headers: this.headers,
});
}
public async createCategory(displayName: string, slug: string, priority: number): Promise<void> {
await requestUrl({
url: `${this.site.url}/apis/content.halo.run/v1alpha1/categories`,
method: "POST",
contentType: "application/json",
headers: this.headers,
body: JSON.stringify({
spec: {
displayName,
slug,
description: "",
cover: "",
template: "",
priority,
children: [],
},
apiVersion: "content.halo.run/v1alpha1",
kind: "Category",
metadata: { name: "", generateName: "category-" },
}),
});
}
public async updateCategory(name: string, displayName: string, slug: string, priority: number): Promise<void> {
const category = (await requestUrl({
url: `${this.site.url}/apis/content.halo.run/v1alpha1/categories/${name}`,
headers: this.headers,
}).json) as any;
category.spec.displayName = displayName;
category.spec.slug = slug;
category.spec.priority = priority;
await requestUrl({
url: `${this.site.url}/apis/content.halo.run/v1alpha1/categories/${name}`,
method: "PUT",
contentType: "application/json",
headers: this.headers,
body: JSON.stringify(category),
});
}
public async deleteCategory(name: string): Promise<void> {
await requestUrl({
url: `${this.site.url}/apis/content.halo.run/v1alpha1/categories/${name}`,
method: "DELETE",
headers: this.headers,
});
}
}
export default HaloService;
// 向后兼容:重新导出新的模块化 HaloService
// 新实现位于 src/services/halo-service.ts
export {
HaloService,
default,
} from "../services/halo-service";
@@ -0,0 +1,179 @@
import { describe, it, expect } from "vitest";
import { ContentService } from "../content-service";
describe("ContentService", () => {
const service = new ContentService();
describe("renderMarkdown", () => {
it("should render markdown to HTML", () => {
const result = service.renderMarkdown("# Hello World");
expect(result).toContain("<h1");
expect(result).toContain("Hello World");
});
it("should render bold text", () => {
const result = service.renderMarkdown("**bold**");
expect(result).toContain("<strong>bold</strong>");
});
it("should render links", () => {
const result = service.renderMarkdown("[link](https://example.com)");
expect(result).toContain('<a href="https://example.com">link</a>');
});
it("should render code blocks", () => {
const result = service.renderMarkdown("```js\nconst x = 1;\n```");
expect(result).toContain("<pre><code");
});
});
describe("extractFrontmatter", () => {
it("should extract frontmatter from markdown", () => {
const md = `---
title: Test Title
slug: test-slug
tags:
- tag1
- tag2
---
# Content`;
const { frontmatter, rawContent } = service.extractFrontmatter(md);
expect(frontmatter.title).toBe("Test Title");
expect(frontmatter.slug).toBe("test-slug");
expect(frontmatter.tags).toEqual(["tag1", "tag2"]);
expect(rawContent.trim()).toBe("# Content");
});
it("should handle markdown without frontmatter", () => {
const md = "# Just Content";
const { frontmatter, rawContent } = service.extractFrontmatter(md);
expect(frontmatter).toEqual({});
expect(rawContent).toBe("# Just Content");
});
it("should handle frontmatter with categories", () => {
const md = `---
title: Categories Test
categories:
- 技术
- 编程
---
Content here`;
const { frontmatter, rawContent } = service.extractFrontmatter(md);
expect(frontmatter.categories).toEqual(["技术", "编程"]);
expect(rawContent.trim()).toBe("Content here");
});
it("should handle frontmatter with halo metadata", () => {
const md = `---
title: Halo Post
halo:
site: https://example.com
name: abc-123
publish: true
---
Content`;
const { frontmatter } = service.extractFrontmatter(md);
expect(frontmatter.halo?.site).toBe("https://example.com");
expect(frontmatter.halo?.name).toBe("abc-123");
expect(frontmatter.halo?.publish).toBe(true);
});
it("should handle empty frontmatter", () => {
const md = `---
---
Content`;
const { frontmatter, rawContent } = service.extractFrontmatter(md);
expect(frontmatter).toEqual({});
expect(rawContent.trim()).toBe("Content");
});
});
describe("buildPostSpec", () => {
it("should build post spec with default values", () => {
const spec = service.buildPostSpec(
"Test Title",
"test-slug",
"# Content",
{},
{ rawType: "markdown", raw: "# Content", content: "<h1>Content</h1>" }
);
expect(spec.title).toBe("Test Title");
expect(spec.slug).toBe("test-slug");
expect(spec.allowComment).toBe(true);
expect(spec.visible).toBe("PUBLIC");
expect(spec.publish).toBe(false);
expect(spec.excerpt.autoGenerate).toBe(true);
});
it("should use provided slug", () => {
const spec = service.buildPostSpec(
"Title",
"custom-slug",
"# Content",
{},
{ rawType: "markdown", raw: "# Content", content: "<h1>Content</h1>" }
);
expect(spec.slug).toBe("custom-slug");
});
it("should auto-generate slug from title if not provided", () => {
const spec = service.buildPostSpec(
"Test Title",
"",
"# Content",
{},
{ rawType: "markdown", raw: "# Content", content: "<h1>Content</h1>" }
);
// slugify should convert "Test Title" to "test-title"
expect(spec.slug).toBeTruthy();
expect(spec.slug).toContain("test");
});
it("should use excerpt from frontmatter", () => {
const spec = service.buildPostSpec(
"Title",
"slug",
"# Content",
{ excerpt: "Custom excerpt" },
{ rawType: "markdown", raw: "# Content", content: "<h1>Content</h1>" }
);
expect(spec.excerpt.autoGenerate).toBe(false);
expect(spec.excerpt.raw).toBe("Custom excerpt");
});
it("should use cover from frontmatter", () => {
const spec = service.buildPostSpec(
"Title",
"slug",
"# Content",
{ cover: "https://example.com/cover.jpg" },
{ rawType: "markdown", raw: "# Content", content: "<h1>Content</h1>" }
);
expect(spec.cover).toBe("https://example.com/cover.jpg");
});
});
});
@@ -0,0 +1,115 @@
import { describe, it, expect } from "vitest";
import { HaloError, HttpError } from "../error";
describe("HaloError", () => {
it("should create error with all properties", () => {
const error = new HaloError("Test message", "TEST_CODE", 400, new Error("original"));
expect(error.message).toBe("Test message");
expect(error.code).toBe("TEST_CODE");
expect(error.statusCode).toBe(400);
expect(error.originalError).toBeInstanceOf(Error);
expect(error.name).toBe("HaloError");
});
it("should create network error", () => {
const error = HaloError.network(new Error("fetch failed"));
expect(error.code).toBe("NETWORK_ERROR");
expect(error.message).toBe("网络请求失败,请检查网络连接");
expect(error.isNetworkError()).toBe(true);
expect(error.isAuthError()).toBe(false);
});
it("should create unauthorized error", () => {
const error = HaloError.unauthorized();
expect(error.code).toBe("UNAUTHORIZED");
expect(error.statusCode).toBe(401);
expect(error.isAuthError()).toBe(true);
});
it("should create forbidden error", () => {
const error = HaloError.forbidden();
expect(error.code).toBe("FORBIDDEN");
expect(error.statusCode).toBe(403);
expect(error.isAuthError()).toBe(true);
});
it("should create notFound error", () => {
const error = HaloError.notFound("Post");
expect(error.code).toBe("NOT_FOUND");
expect(error.statusCode).toBe(404);
expect(error.message).toBe("Post 未找到");
expect(error.isNotFound()).toBe(true);
});
it("should create server error", () => {
const error = HaloError.serverError();
expect(error.code).toBe("SERVER_ERROR");
expect(error.statusCode).toBe(500);
});
it("should create validation error", () => {
const error = HaloError.validationError("Invalid slug");
expect(error.code).toBe("VALIDATION_ERROR");
expect(error.message).toBe("Invalid slug");
});
it("should create unknown error from Error", () => {
const original = new Error("Something went wrong");
const error = HaloError.unknown(original);
expect(error.code).toBe("UNKNOWN");
expect(error.message).toBe("Something went wrong");
});
it("should create unknown error from non-Error", () => {
const error = HaloError.unknown("string error");
expect(error.code).toBe("UNKNOWN");
expect(error.message).toBe("未知错误");
});
});
describe("HttpError", () => {
it("should create from status code 400", () => {
const error = HttpError.fromStatus(400, { message: "bad request" });
expect(error.statusCode).toBe(400);
expect(error.message).toBe("请求参数错误");
expect(error.name).toBe("HttpError");
});
it("should create from status code 401", () => {
const error = HttpError.fromStatus(401);
expect(error.statusCode).toBe(401);
expect(error.message).toBe("认证失败");
});
it("should create from status code 404", () => {
const error = HttpError.fromStatus(404);
expect(error.statusCode).toBe(404);
expect(error.message).toBe("资源不存在");
});
it("should create from status code 500", () => {
const error = HttpError.fromStatus(500);
expect(error.statusCode).toBe(500);
expect(error.message).toBe("服务器内部错误");
});
it("should create from unknown status code", () => {
const error = HttpError.fromStatus(418);
expect(error.statusCode).toBe(418);
expect(error.message).toBe("HTTP 错误 (418)");
});
});
@@ -0,0 +1,242 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { TaxonomyService } from "../taxonomy-service";
import type { HaloClient } from "../client";
import type { Category, Tag } from "../types";
describe("TaxonomyService", () => {
let service: TaxonomyService;
let mockClient: HaloClient;
beforeEach(() => {
mockClient = {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
} as unknown as HaloClient;
service = new TaxonomyService(mockClient);
});
describe("getCategories", () => {
it("should return categories from API", async () => {
const mockCategories: Category[] = [
{
metadata: { name: "cat-1" },
spec: { displayName: "技术", slug: "tech", description: "", cover: "", template: "", priority: 0, children: [] },
},
{
metadata: { name: "cat-2" },
spec: { displayName: "生活", slug: "life", description: "", cover: "", template: "", priority: 1, children: [] },
},
];
vi.mocked(mockClient.get).mockResolvedValue({ items: mockCategories });
const result = await service.getCategories();
expect(result).toEqual(mockCategories);
expect(mockClient.get).toHaveBeenCalledWith("/apis/content.halo.run/v1alpha1/categories");
});
});
describe("getTags", () => {
it("should return tags from API", async () => {
const mockTags: Tag[] = [
{
metadata: { name: "tag-1" },
spec: { displayName: "Python", slug: "python", color: "#3776AB", cover: "" },
},
];
vi.mocked(mockClient.get).mockResolvedValue({ items: mockTags });
const result = await service.getTags();
expect(result).toEqual(mockTags);
expect(mockClient.get).toHaveBeenCalledWith("/apis/content.halo.run/v1alpha1/tags");
});
});
describe("getCategoryNames", () => {
it("should return existing category names", async () => {
const mockCategories: Category[] = [
{
metadata: { name: "cat-1" },
spec: { displayName: "技术", slug: "tech", description: "", cover: "", template: "", priority: 0, children: [] },
},
];
vi.mocked(mockClient.get).mockResolvedValue({ items: mockCategories });
const result = await service.getCategoryNames(["技术"]);
expect(result).toContain("cat-1");
});
it("should create new categories if not exist", async () => {
const mockCategories: Category[] = [];
const newCategory: Category = {
metadata: { name: "new-cat" },
spec: { displayName: "新分类", slug: "xin-fen-lei", description: "", cover: "", template: "", priority: 0, children: [] },
};
vi.mocked(mockClient.get).mockResolvedValue({ items: mockCategories });
vi.mocked(mockClient.post).mockResolvedValue(newCategory);
const result = await service.getCategoryNames(["新分类"]);
expect(result).toContain("new-cat");
expect(mockClient.post).toHaveBeenCalled();
});
});
describe("getTagNames", () => {
it("should return existing tag names", async () => {
const mockTags: Tag[] = [
{
metadata: { name: "tag-1" },
spec: { displayName: "Python", slug: "python", color: "#3776AB", cover: "" },
},
];
vi.mocked(mockClient.get).mockResolvedValue({ items: mockTags });
const result = await service.getTagNames(["Python"]);
expect(result).toContain("tag-1");
});
it("should create new tags if not exist", async () => {
const mockTags: Tag[] = [];
const newTag: Tag = {
metadata: { name: "new-tag" },
spec: { displayName: "新标签", slug: "xin-biao-qian", color: "#ffffff", cover: "" },
};
vi.mocked(mockClient.get).mockResolvedValue({ items: mockTags });
vi.mocked(mockClient.post).mockResolvedValue(newTag);
const result = await service.getTagNames(["新标签"]);
expect(result).toContain("new-tag");
expect(mockClient.post).toHaveBeenCalled();
});
});
describe("getCategoryDisplayNames", () => {
it("should convert category names to display names", async () => {
const mockCategories: Category[] = [
{
metadata: { name: "cat-1" },
spec: { displayName: "技术", slug: "tech", description: "", cover: "", template: "", priority: 0, children: [] },
},
];
vi.mocked(mockClient.get).mockResolvedValue({ items: mockCategories });
const result = await service.getCategoryDisplayNames(["cat-1"]);
expect(result).toEqual(["技术"]);
});
it("should return empty array for empty input", async () => {
const result = await service.getCategoryDisplayNames([]);
expect(result).toEqual([]);
});
it("should filter out unknown category names", async () => {
const mockCategories: Category[] = [
{
metadata: { name: "cat-1" },
spec: { displayName: "技术", slug: "tech", description: "", cover: "", template: "", priority: 0, children: [] },
},
];
vi.mocked(mockClient.get).mockResolvedValue({ items: mockCategories });
const result = await service.getCategoryDisplayNames(["cat-1", "unknown-cat"]);
expect(result).toEqual(["技术"]);
});
});
describe("getTagDisplayNames", () => {
it("should convert tag names to display names", async () => {
const mockTags: Tag[] = [
{
metadata: { name: "tag-1" },
spec: { displayName: "Python", slug: "python", color: "#3776AB", cover: "" },
},
];
vi.mocked(mockClient.get).mockResolvedValue({ items: mockTags });
const result = await service.getTagDisplayNames(["tag-1"]);
expect(result).toEqual(["Python"]);
});
});
describe("createCategory", () => {
it("should create a new category", async () => {
const newCategory: Category = {
metadata: { name: "cat-created" },
spec: { displayName: "新分类", slug: "xin-fen-lei", description: "", cover: "", template: "", priority: 0, children: [] },
};
vi.mocked(mockClient.post).mockResolvedValue(newCategory);
const result = await service.createCategory("新分类", "xin-fen-lei", 0);
expect(result).toEqual(newCategory);
expect(mockClient.post).toHaveBeenCalledWith(
"/apis/content.halo.run/v1alpha1/categories",
expect.objectContaining({
spec: expect.objectContaining({ displayName: "新分类", slug: "xin-fen-lei" }),
}),
);
});
});
describe("createTag", () => {
it("should create a new tag", async () => {
const newTag: Tag = {
metadata: { name: "tag-created" },
spec: { displayName: "新标签", slug: "xin-biao-qian", color: "#ffffff", cover: "" },
};
vi.mocked(mockClient.post).mockResolvedValue(newTag);
const result = await service.createTag("新标签", "xin-biao-qian", "#ffffff");
expect(result).toEqual(newTag);
expect(mockClient.post).toHaveBeenCalledWith(
"/apis/content.halo.run/v1alpha1/tags",
expect.objectContaining({
spec: expect.objectContaining({ displayName: "新标签", slug: "xin-biao-qian", color: "#ffffff" }),
}),
);
});
});
describe("deleteCategory", () => {
it("should delete a category", async () => {
vi.mocked(mockClient.delete).mockResolvedValue();
await service.deleteCategory("cat-1");
expect(mockClient.delete).toHaveBeenCalledWith("/apis/content.halo.run/v1alpha1/categories/cat-1");
});
});
describe("deleteTag", () => {
it("should delete a tag", async () => {
vi.mocked(mockClient.delete).mockResolvedValue();
await service.deleteTag("tag-1");
expect(mockClient.delete).toHaveBeenCalledWith("/apis/content.halo.run/v1alpha1/tags/tag-1");
});
});
});
+23 -5
View File
@@ -1,17 +1,24 @@
import { requestUrl } from "obsidian";
import type { HaloSite } from "../settings";
import { HaloError, HttpError } from "./error";
import { withRetry, type RetryOptions } from "../utils/retry";
export interface HaloClientOptions {
retry?: RetryOptions;
}
export class HaloClient {
private readonly baseUrl: string;
private readonly headers: Record<string, string>;
private readonly retryOptions: RetryOptions;
constructor(site: HaloSite) {
constructor(site: HaloSite, options: HaloClientOptions = {}) {
this.baseUrl = site.url;
this.headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${site.token}`,
};
this.retryOptions = options.retry || {};
}
private handleResponse<T>(response: { json: unknown; status: number }): T {
@@ -25,7 +32,7 @@ export class HaloClient {
}
private async request<T>(path: string, options: { method: string; body?: object }): Promise<T> {
try {
const doRequest = async () => {
const response = await requestUrl({
url: `${this.baseUrl}${path}`,
method: options.method,
@@ -35,12 +42,15 @@ export class HaloClient {
});
return this.handleResponse<T>({ json: response.json, status: response.status });
};
try {
return await withRetry(doRequest, this.retryOptions);
} catch (error) {
if (error instanceof HttpError) {
throw this.mapHttpError(error);
}
if (error instanceof TypeError) {
// Network error
throw HaloError.network(error);
}
throw HaloError.unknown(error);
@@ -77,7 +87,7 @@ export class HaloClient {
}
async delete(path: string): Promise<void> {
try {
const doDelete = async () => {
const response = await requestUrl({
url: `${this.baseUrl}${path}`,
method: "DELETE",
@@ -87,6 +97,10 @@ export class HaloClient {
if (response.status >= 400) {
throw HttpError.fromStatus(response.status, response.json);
}
};
try {
await withRetry(doDelete, this.retryOptions);
} catch (error) {
if (error instanceof HttpError) {
throw this.mapHttpError(error);
@@ -99,7 +113,7 @@ export class HaloClient {
}
async putVoid(path: string, body?: object): Promise<void> {
try {
const doPut = async () => {
const response = await requestUrl({
url: `${this.baseUrl}${path}`,
method: "PUT",
@@ -111,6 +125,10 @@ export class HaloClient {
if (response.status >= 400) {
throw HttpError.fromStatus(response.status, response.json);
}
};
try {
await withRetry(doPut, this.retryOptions);
} catch (error) {
if (error instanceof HttpError) {
throw this.mapHttpError(error);
+18 -15
View File
@@ -1,19 +1,21 @@
import { type App, Notice, type TFile } from "obsidian";
import { randomUUID } from "./utils/id";
import markdownIt from "./utils/markdown";
import { randomUUID } from "../utils/id";
import markdownIt from "../utils/markdown";
import { slugify } from "transliteration";
import type { HaloSetting, HaloSite } from "./settings";
import type { HaloClient } from "./services/client";
import { PostService } from "./services/post-service";
import { ImageService } from "./services/image-service";
import { TaxonomyService } from "./services/taxonomy-service";
import { ContentService } from "./services/content-service";
import type { Content, Post } from "./services/types";
import type { HaloSetting, HaloSite } from "../settings";
import { HaloClient } from "./client";
import { PostService } from "./post-service";
import { ImageService } from "./image-service";
import { TaxonomyService } from "./taxonomy-service";
import { ContentService } from "./content-service";
import type { Content, Post } from "./types";
import { HaloError } from "./error";
import { logger } from "../utils/logger";
export class HaloService {
private readonly app: App;
private readonly siteUrl: string;
private readonly settings: HaloSetting;
private readonly client: HaloClient;
private readonly postService: PostService;
private readonly imageService: ImageService;
@@ -23,6 +25,7 @@ export class HaloService {
constructor(app: App, settings: HaloSetting, site: HaloSite) {
this.app = app;
this.siteUrl = site.url;
this.settings = settings;
this.client = new HaloClient(site);
this.postService = new PostService(app, this.client);
this.imageService = new ImageService(site);
@@ -162,10 +165,10 @@ export class HaloService {
new Notice("发布成功");
} catch (error) {
if (error instanceof HaloError) {
console.error(`[HaloService] 发布失败 [${error.code}]:`, error.message);
logger.error("HaloService", `发布失败 [${error.code}]`, error.message);
new Notice(error.message);
} else {
console.error("[HaloService] 发布失败:", error);
logger.error("HaloService", "发布失败", error);
new Notice("发布失败");
}
}
@@ -317,9 +320,9 @@ export class HaloService {
return !!post && !!post.metadata;
} catch (error) {
if (error instanceof HaloError) {
console.error(`[HaloService] 导入文章失败 [${error.code}]:`, error.message);
logger.error("HaloService", `导入文章失败 [${error.code}]`, error.message);
} else {
console.error("[HaloService] 导入文章失败:", error);
logger.error("HaloService", "导入文章失败", error);
}
return false;
}
@@ -331,9 +334,9 @@ export class HaloService {
return true;
} catch (error) {
if (error instanceof HaloError) {
console.error(`[HaloService] 删除文章失败 [${error.code}]:`, error.message);
logger.error("HaloService", `删除文章失败 [${error.code}]`, error.message);
} else {
console.error("[HaloService] 删除文章失败:", error);
logger.error("HaloService", "删除文章失败", error);
}
return false;
}
+7 -10
View File
@@ -3,6 +3,7 @@ import { randomUUID } from "../utils/id";
import type { HaloClient } from "./client";
import type { Post, Snapshot, Content } from "./types";
import { HaloError } from "./error";
import { ApiPaths } from "../utils/api-paths";
export class PostService {
constructor(
@@ -12,11 +13,9 @@ export class PostService {
async getPost(name: string): Promise<{ post: Post; content: Content } | undefined> {
try {
const post = await this.client.get<Post>(`/apis/uc.api.content.halo.run/v1alpha1/posts/${name}`);
const post = await this.client.get<Post>(ApiPaths.posts.get(name));
const snapshot = await this.client.get<Snapshot>(
`/apis/uc.api.content.halo.run/v1alpha1/posts/${name}/draft?patched=true`,
);
const snapshot = await this.client.get<Snapshot>(ApiPaths.posts.getDraft(name));
const { "content.halo.run/patched-content": patchedContent, "content.halo.run/patched-raw": patchedRaw } =
snapshot.metadata.annotations || {};
@@ -39,21 +38,19 @@ export class PostService {
}
async createPost(params: Post): Promise<Post> {
return await this.client.post<Post>("/apis/uc.api.content.halo.run/v1alpha1/posts", params);
return await this.client.post<Post>(ApiPaths.posts.create(), params);
}
async updatePost(name: string, params: Post): Promise<void> {
await this.client.put(`/apis/uc.api.content.halo.run/v1alpha1/posts/${name}`, params);
await this.client.put(ApiPaths.posts.update(name), params);
}
async deletePost(name: string): Promise<void> {
await this.client.delete(`/apis/uc.api.content.halo.run/v1alpha1/posts/${name}`);
await this.client.delete(ApiPaths.posts.delete(name));
}
async changePublishStatus(name: string, publish: boolean): Promise<void> {
await this.client.putVoid(
`/apis/uc.api.content.halo.run/v1alpha1/posts/${name}/${publish ? "publish" : "unpublish"}`,
);
await this.client.putVoid(publish ? ApiPaths.posts.publish(name) : ApiPaths.posts.unpublish(name));
}
async createPostFromFile(
+11 -10
View File
@@ -1,17 +1,18 @@
import { slugify } from "transliteration";
import type { HaloClient } from "./client";
import type { Category, Tag } from "./types";
import { ApiPaths } from "../utils/api-paths";
export class TaxonomyService {
constructor(private client: HaloClient) {}
async getCategories(): Promise<Category[]> {
const data = await this.client.get<{ items: Category[] }>("/apis/content.halo.run/v1alpha1/categories");
const data = await this.client.get<{ items: Category[] }>(ApiPaths.categories.list());
return data.items;
}
async getTags(): Promise<Tag[]> {
const data = await this.client.get<{ items: Tag[] }>("/apis/content.halo.run/v1alpha1/tags");
const data = await this.client.get<{ items: Tag[] }>(ApiPaths.tags.list());
return data.items;
}
@@ -50,7 +51,7 @@ export class TaxonomyService {
}
async createCategory(displayName: string, slug: string, priority: number): Promise<Category> {
return await this.client.post<Category>("/apis/content.halo.run/v1alpha1/categories", {
return await this.client.post<Category>(ApiPaths.categories.create(), {
spec: {
displayName,
slug,
@@ -67,15 +68,15 @@ export class TaxonomyService {
}
async updateCategory(name: string, displayName: string, slug: string, priority: number): Promise<void> {
const category = await this.client.get<Category>(`/apis/content.halo.run/v1alpha1/categories/${name}`);
const category = await this.client.get<Category>(ApiPaths.categories.get(name));
category.spec.displayName = displayName;
category.spec.slug = slug;
category.spec.priority = priority;
await this.client.put(`/apis/content.halo.run/v1alpha1/categories/${name}`, category);
await this.client.put(ApiPaths.categories.update(name), category);
}
async deleteCategory(name: string): Promise<void> {
await this.client.delete(`/apis/content.halo.run/v1alpha1/categories/${name}`);
await this.client.delete(ApiPaths.categories.delete(name));
}
async getTagNames(displayNames: string[]): Promise<string[]> {
@@ -111,7 +112,7 @@ export class TaxonomyService {
}
async createTag(displayName: string, slug: string, color: string): Promise<Tag> {
return await this.client.post<Tag>("/apis/content.halo.run/v1alpha1/tags", {
return await this.client.post<Tag>(ApiPaths.tags.create(), {
spec: {
displayName,
slug,
@@ -125,14 +126,14 @@ export class TaxonomyService {
}
async updateTag(name: string, displayName: string, slug: string, color: string): Promise<void> {
const tag = await this.client.get<Tag>(`/apis/content.halo.run/v1alpha1/tags/${name}`);
const tag = await this.client.get<Tag>(ApiPaths.tags.get(name));
tag.spec.displayName = displayName;
tag.spec.slug = slug;
tag.spec.color = color || "#ffffff";
await this.client.put(`/apis/content.halo.run/v1alpha1/tags/${name}`, tag);
await this.client.put(ApiPaths.tags.update(name), tag);
}
async deleteTag(name: string): Promise<void> {
await this.client.delete(`/apis/content.halo.run/v1alpha1/tags/${name}`);
await this.client.delete(ApiPaths.tags.delete(name));
}
}
+3
View File
@@ -34,9 +34,12 @@ export interface Post {
export interface Snapshot {
metadata: {
annotations?: Record<string, string>;
name?: string;
};
spec?: {
rawType?: string;
displayName?: string;
slug?: string;
};
}
+66
View File
@@ -0,0 +1,66 @@
// Halo API 路径常量
// 便于统一管理和 Halo API 升级时批量修改
export const ApiPaths = {
// 文章相关 (UC API)
posts: {
list: (page: number, size: number, labelSelector?: string) =>
`/apis/uc.api.content.halo.run/v1alpha1/posts?page=${page}&size=${size}${labelSelector ? `&labelSelector=${encodeURIComponent(labelSelector)}` : ""}`,
get: (name: string) =>
`/apis/uc.api.content.halo.run/v1alpha1/posts/${name}`,
getDraft: (name: string) =>
`/apis/uc.api.content.halo.run/v1alpha1/posts/${name}/draft?patched=true`,
create: () =>
`/apis/uc.api.content.halo.run/v1alpha1/posts`,
update: (name: string) =>
`/apis/uc.api.content.halo.run/v1alpha1/posts/${name}`,
delete: (name: string) =>
`/apis/uc.api.content.halo.run/v1alpha1/posts/${name}`,
publish: (name: string) =>
`/apis/uc.api.content.halo.run/v1alpha1/posts/${name}/publish`,
unpublish: (name: string) =>
`/apis/uc.api.content.halo.run/v1alpha1/posts/${name}/unpublish`,
},
// 分类相关 (Content API)
categories: {
list: () =>
`/apis/content.halo.run/v1alpha1/categories`,
get: (name: string) =>
`/apis/content.halo.run/v1alpha1/categories/${name}`,
create: () =>
`/apis/content.halo.run/v1alpha1/categories`,
update: (name: string) =>
`/apis/content.halo.run/v1alpha1/categories/${name}`,
delete: (name: string) =>
`/apis/content.halo.run/v1alpha1/categories/${name}`,
},
// 标签相关 (Content API)
tags: {
list: () =>
`/apis/content.halo.run/v1alpha1/tags`,
get: (name: string) =>
`/apis/content.halo.run/v1alpha1/tags/${name}`,
create: () =>
`/apis/content.halo.run/v1alpha1/tags`,
update: (name: string) =>
`/apis/content.halo.run/v1alpha1/tags/${name}`,
delete: (name: string) =>
`/apis/content.halo.run/v1alpha1/tags/${name}`,
},
// 附件/图片上传 (Console API)
attachments: {
upload: () =>
`/apis/api.console.halo.run/v1alpha1/attachments/upload`,
},
// 用户权限 (Console API)
users: {
permissions: () =>
`/apis/api.console.halo.run/v1alpha1/users/-/permissions`,
},
} as const;
export type ApiPaths = typeof ApiPaths;
+60
View File
@@ -0,0 +1,60 @@
export enum LogLevel {
DEBUG = 0,
INFO = 1,
WARN = 2,
ERROR = 3,
NONE = 4,
}
const currentLevel = LogLevel.INFO;
const PREFIX = {
HaloService: "HaloService",
PostService: "PostService",
ImageService: "ImageService",
TaxonomyService: "TaxonomyService",
ContentService: "ContentService",
HaloClient: "HaloClient",
Plugin: "HaloPlugin",
ImageUploader: "ImageUploader",
};
function formatMessage(prefix: string, message: string): string {
return `[${prefix}] ${message}`;
}
function shouldLog(level: LogLevel): boolean {
return level >= currentLevel;
}
export const logger = {
debug(prefix: keyof typeof PREFIX, message: string, ...args: unknown[]): void {
if (shouldLog(LogLevel.DEBUG)) {
console.debug(formatMessage(PREFIX[prefix], message), ...args);
}
},
info(prefix: keyof typeof PREFIX, message: string, ...args: unknown[]): void {
if (shouldLog(LogLevel.INFO)) {
console.log(formatMessage(PREFIX[prefix], message), ...args);
}
},
warn(prefix: keyof typeof PREFIX, message: string, ...args: unknown[]): void {
if (shouldLog(LogLevel.WARN)) {
console.warn(formatMessage(PREFIX[prefix], message), ...args);
}
},
error(prefix: keyof typeof PREFIX, message: string, error?: unknown): void {
if (shouldLog(LogLevel.ERROR)) {
if (error) {
console.error(formatMessage(PREFIX[prefix], message), error);
} else {
console.error(formatMessage(PREFIX[prefix], message));
}
}
},
};
export default logger;
+73
View File
@@ -0,0 +1,73 @@
import { HaloError } from "../services/error";
export interface RetryOptions {
maxRetries?: number;
delayMs?: number;
backoffMultiplier?: number;
retryableStatuses?: number[];
}
const DEFAULT_OPTIONS: Required<RetryOptions> = {
maxRetries: 3,
delayMs: 1000,
backoffMultiplier: 2,
retryableStatuses: [408, 429, 500, 502, 503, 504],
};
export async function withRetry<T>(
fn: () => Promise<T>,
options: RetryOptions = {},
): Promise<T> {
const opts = { ...DEFAULT_OPTIONS, ...options };
let lastError: unknown;
let delay = opts.delayMs;
for (let attempt = 0; attempt <= opts.maxRetries; attempt++) {
try {
return await fn();
} catch (error) {
lastError = error;
if (attempt === opts.maxRetries) {
break;
}
if (error instanceof HaloError && !error.isNetworkError()) {
if (!error.isAuthError() && !error.isNotFound()) {
const statusCode = error.statusCode;
if (statusCode && opts.retryableStatuses.includes(statusCode)) {
await sleep(delay);
delay *= opts.backoffMultiplier;
continue;
}
}
throw error;
}
if (error instanceof TypeError) {
await sleep(delay);
delay *= opts.backoffMultiplier;
continue;
}
throw error;
}
}
throw lastError;
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export class RetryError extends Error {
constructor(
message: string,
public readonly attempts: number,
public readonly lastError?: unknown,
) {
super(message);
this.name = "RetryError";
}
}
+13
View File
@@ -0,0 +1,13 @@
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true,
environment: 'node',
include: ['src/**/*.test.ts'],
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],
},
},
});