RequestDto & ResponseDto
POST/PUT 用 RequestDto 包装。GET fetchFn 必须返回 payload。
- Frontend ResponseDto is the simplified client shape (payload + status)
- Always return payload from list/detail fetchFn — call sites must not read data?.payload
TSXfeature.api.ts
1import { RequestDto, ResponseDto } from "@quan-erp/shared-frontend-core";
2import { withApiMetadataFetchFn } from "@quan-erp/shared-types";
3import { getAxiosClient } from "../../lib/axios";
4import metadata from "../../../module.metadata.json" with { type: "json" };
5
6const PLUGIN_PREFIX = `/${metadata.name}`;
7
8export const getItemsApi = withApiMetadataFetchFn({
9 api: { method: "GET", url: `${PLUGIN_PREFIX}/items` },
10 fetchFn: async (skip: number, limit: number): Promise<ItemDto[]> => {
11 const response = await getAxiosClient().get(`${PLUGIN_PREFIX}/items`, {
12 params: { skip, limit },
13 });
14 const data: ResponseDto<ItemDto[]> = response.data;
15 if (data.status === "error") throw new Error(data.message);
16 return data.payload;
17 },
18});
19
20export const createItemApi = withApiMetadataFetchFn({
21 api: { method: "POST", url: `${PLUGIN_PREFIX}/items` },
22 fetchFn: async (payload: CreateItemDto): Promise<ItemDto> => {
23 const response = await getAxiosClient().post(
24 `${PLUGIN_PREFIX}/items`,
25 new RequestDto(payload),
26 );
27 const data: ResponseDto<ItemDto> = response.data;
28 if (data.status === "error") throw new Error(data.message);
29 return data.payload;
30 },
31});