40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { cacheLife, cacheTag } from "next/cache";
|
|
|
|
import { apiRequest } from "./client";
|
|
import { serverFetchJson } from "./server-fetch";
|
|
import type { AppListResp } from "./types";
|
|
|
|
/** GET /api/app/applist/list。categoryId=0 返回默认分类及全部分类列表。浏览器端可用。 */
|
|
export function fetchAppList(categoryId = 0, token?: string | null) {
|
|
return apiRequest<AppListResp>(`/api/app/applist/list?categoryId=${categoryId}`, {
|
|
method: "GET",
|
|
token,
|
|
});
|
|
}
|
|
|
|
export type AppListLoadResult = {
|
|
data: AppListResp | null;
|
|
error: string;
|
|
};
|
|
|
|
/**
|
|
* 服务端拉取应用列表。
|
|
* 网络失败在缓存函数内消化,避免 "use cache" 抛错把整页打成 500。
|
|
*/
|
|
export async function getAppListCached(categoryId = 0): Promise<AppListLoadResult> {
|
|
"use cache";
|
|
cacheLife("hours");
|
|
cacheTag("applist");
|
|
|
|
try {
|
|
const data = await serverFetchJson<AppListResp>(`/api/app/applist/list?categoryId=${categoryId}`);
|
|
return { data, error: "" };
|
|
} catch (reason: unknown) {
|
|
const error = reason instanceof Error ? reason.message : "应用列表加载失败";
|
|
if (process.env.NODE_ENV === "development") {
|
|
console.warn("[applist]", reason);
|
|
}
|
|
return { data: null, error };
|
|
}
|
|
}
|