27 lines
855 B
TypeScript
27 lines
855 B
TypeScript
import { fetchAPI } from "./api-client";
|
|
import type {
|
|
ProductPageDetail,
|
|
ProductPageSummary,
|
|
WagtailListResponse,
|
|
} from "@/types/wagtail";
|
|
|
|
/** 获取产品列表。 */
|
|
export async function getProductList(): Promise<ProductPageSummary[]> {
|
|
const res = await fetchAPI<WagtailListResponse<ProductPageSummary>>(
|
|
"/api/v2/pages/?type=products.ProductPage&fields=summary,cover_image",
|
|
{ tags: ["product-list"] },
|
|
);
|
|
return res.items;
|
|
}
|
|
|
|
/** 根据 slug 获取产品详情。 */
|
|
export async function getProductBySlug(
|
|
slug: string,
|
|
): Promise<ProductPageDetail | null> {
|
|
const res = await fetchAPI<WagtailListResponse<ProductPageDetail>>(
|
|
`/api/v2/pages/?type=products.ProductPage&slug=${encodeURIComponent(slug)}&fields=body,summary,cover_image&limit=1`,
|
|
{ tags: [`product:${slug}`] },
|
|
);
|
|
return res.items[0] ?? null;
|
|
}
|