Files
app-261004/lib/posts/index.ts
T
2026-10-08 11:09:37 +08:00

49 lines
1.5 KiB
TypeScript

import "server-only";
import {
listAllPublishedPosts,
getPostBySlug as getPostBySlugFromDb,
getAdjacentPosts as getAdjacentPostsFromDb,
listTags,
getPostStats,
} from "./repository";
import type { Post, PostMeta } from "./types";
/**
* 内容读取门面(服务端)。
*
* 页面层只依赖本模块,不直接接触仓储实现。这样做的价值:
* 数据来源从「文件系统」切换到「PostgreSQL」时,**页面代码一行都不用改**。
*
* 所有函数默认只返回已发布文章(草稿在仓储层被过滤),
* 因此草稿既不出现在列表里,也无法通过直接访问 URL 读到。
*/
export type { Post, PostMeta } from "./types";
/** 全部已发布文章,按发布日期倒序 */
export async function getAllPosts(): Promise<PostMeta[]> {
return listAllPublishedPosts();
}
/** 按 slug 读取单篇文章(含正文);草稿与不存在的 slug 均返回 null */
export async function getPostBySlug(slug: string): Promise<Post | null> {
const post = await getPostBySlugFromDb(slug);
return post as Post | null;
}
/** 相邻文章(详情页上下篇导航) */
export async function getAdjacentPosts(slug: string) {
return getAdjacentPostsFromDb(slug);
}
/** 全部标签及其文章数 */
export async function getAllTags(): Promise<{ tag: string; count: number }[]> {
return listTags();
}
/** 站点统计(已发布数 / 草稿数 / 标签数) */
export async function getStats() {
return getPostStats();
}