29 lines
859 B
TypeScript
29 lines
859 B
TypeScript
import { fetchAPI } from "./api-client";
|
|
import type {
|
|
BlogPageDetail,
|
|
BlogPageSummary,
|
|
WagtailListResponse,
|
|
} from "@/types/wagtail";
|
|
|
|
/**
|
|
* 获取博客列表(按发布时间倒序)。
|
|
*/
|
|
export async function getBlogList(): Promise<BlogPageSummary[]> {
|
|
const res = await fetchAPI<WagtailListResponse<BlogPageSummary>>(
|
|
"/api/v2/pages/?type=blog.BlogPage&fields=intro,published_at&order=-published_at",
|
|
{ tags: ["blog-list"] },
|
|
);
|
|
return res.items;
|
|
}
|
|
|
|
/**
|
|
* 根据 slug 获取博客详情。
|
|
*/
|
|
export async function getBlogBySlug(slug: string): Promise<BlogPageDetail | null> {
|
|
const res = await fetchAPI<WagtailListResponse<BlogPageDetail>>(
|
|
`/api/v2/pages/?type=blog.BlogPage&slug=${encodeURIComponent(slug)}&fields=body,intro,published_at&limit=1`,
|
|
{ tags: [`blog:${slug}`] },
|
|
);
|
|
return res.items[0] ?? null;
|
|
}
|