feat: add GitHub site initialization
This commit is contained in:
+41
-4
@@ -1,6 +1,15 @@
|
||||
import type { DocPage, GitHubConnection, SiteSettings } from "../types";
|
||||
import { parseFrontmatter, parseSiteSettings, stringifyMarkdownPage, stringifySiteConfig } from "../utils/siteContent";
|
||||
|
||||
interface InitSiteResponse {
|
||||
files: Array<{
|
||||
path: string;
|
||||
status: "created" | "updated" | "skipped" | "failed";
|
||||
sha?: string;
|
||||
error?: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
interface GitTreeResponse {
|
||||
tree: Array<{
|
||||
path: string;
|
||||
@@ -71,6 +80,11 @@ function encodeBase64Content(content: string) {
|
||||
return btoa(binary);
|
||||
}
|
||||
|
||||
async function readApiError(response: Response, fallback: string) {
|
||||
const data = await response.json().catch(() => null);
|
||||
return data?.error || `${fallback}:${response.status}`;
|
||||
}
|
||||
|
||||
async function githubFetch<T>(connection: GitHubConnection, path: string, init?: RequestInit) {
|
||||
const response = await fetch(`https://api.github.com${path}`, {
|
||||
...init,
|
||||
@@ -101,7 +115,7 @@ export async function loadGitHubPages(connection: GitHubConnection) {
|
||||
const response = await fetch(`/api/github/site/pages?${siteParams(connection).toString()}`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`读取 GitHub 页面失败:${response.status}`);
|
||||
throw new Error(await readApiError(response, "读取 GitHub 页面失败"));
|
||||
}
|
||||
|
||||
const data = (await response.json()) as { pages: DocPage[] };
|
||||
@@ -159,7 +173,7 @@ export async function saveGitHubPage(connection: GitHubConnection, page: DocPage
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`保存 GitHub 页面失败:${response.status}`);
|
||||
throw new Error(await readApiError(response, "保存 GitHub 页面失败"));
|
||||
}
|
||||
|
||||
const data = (await response.json()) as { sha: string };
|
||||
@@ -193,7 +207,7 @@ export async function loadGitHubSettings(connection: GitHubConnection) {
|
||||
const response = await fetch(`/api/github/site/settings?${siteParams(connection).toString()}`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`读取 GitHub 配置失败:${response.status}`);
|
||||
throw new Error(await readApiError(response, "读取 GitHub 配置失败"));
|
||||
}
|
||||
|
||||
return (await response.json()) as {
|
||||
@@ -212,6 +226,29 @@ export async function loadGitHubSettings(connection: GitHubConnection) {
|
||||
};
|
||||
}
|
||||
|
||||
export async function initializeGitHubSite(connection: GitHubConnection) {
|
||||
const response = await fetch("/api/github/site/init", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
site: {
|
||||
owner: connection.owner,
|
||||
repo: connection.repo,
|
||||
branch: connection.branch,
|
||||
siteRoot: connection.siteRoot,
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(await readApiError(response, "初始化 VitePress 模板失败"));
|
||||
}
|
||||
|
||||
return (await response.json()) as InitSiteResponse;
|
||||
}
|
||||
|
||||
export async function saveGitHubSettings(
|
||||
connection: GitHubConnection,
|
||||
settings: SiteSettings,
|
||||
@@ -236,7 +273,7 @@ export async function saveGitHubSettings(
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`保存 GitHub 配置失败:${response.status}`);
|
||||
throw new Error(await readApiError(response, "保存 GitHub 配置失败"));
|
||||
}
|
||||
|
||||
const data = (await response.json()) as { sha: string };
|
||||
|
||||
Reference in New Issue
Block a user