Files
Genarrative/src/services/puzzle-works/puzzleWorksClient.ts
2026-05-14 14:21:17 +08:00

124 lines
3.0 KiB
TypeScript

import type { PuzzleDraftLevel } from '../../../packages/shared/src/contracts/puzzleAgentDraft';
import type {
PuzzleWorkDetailResponse,
PuzzleWorkMutationResponse,
PuzzleWorksResponse,
} from '../../../packages/shared/src/contracts/puzzleWorkSummary';
import { type ApiRetryOptions, requestJson } from '../apiClient';
const PUZZLE_WORKS_API_BASE = '/api/runtime/puzzle/works';
const PUZZLE_WORKS_READ_RETRY: ApiRetryOptions = {
maxRetries: 1,
baseDelayMs: 120,
maxDelayMs: 360,
};
const PUZZLE_WORKS_WRITE_RETRY: ApiRetryOptions = {
maxRetries: 1,
baseDelayMs: 120,
maxDelayMs: 360,
retryUnsafeMethods: true,
};
/**
* 读取当前用户的拼图作品列表。
*/
export async function listPuzzleWorks() {
return requestJson<PuzzleWorksResponse>(
PUZZLE_WORKS_API_BASE,
{
method: 'GET',
},
'读取拼图作品列表失败',
{
retry: PUZZLE_WORKS_READ_RETRY,
},
);
}
/**
* 读取拼图作品详情。
*/
export async function getPuzzleWorkDetail(profileId: string) {
return requestJson<PuzzleWorkDetailResponse>(
`${PUZZLE_WORKS_API_BASE}/${encodeURIComponent(profileId)}`,
{
method: 'GET',
},
'读取拼图作品详情失败',
{
retry: PUZZLE_WORKS_READ_RETRY,
},
);
}
/**
* 更新已发布或草稿态拼图作品的轻量字段。
* 只覆盖结果页约定的作品信息、首关摘要、标签、正式图与关卡列表。
*/
export async function updatePuzzleWork(
profileId: string,
payload: {
workTitle?: string;
workDescription?: string;
levelName: string;
summary: string;
themeTags: string[];
coverImageSrc?: string | null;
coverAssetId?: string | null;
levels: PuzzleDraftLevel[];
},
) {
return requestJson<PuzzleWorkMutationResponse>(
`${PUZZLE_WORKS_API_BASE}/${encodeURIComponent(profileId)}`,
{
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
},
'更新拼图作品失败',
{
retry: PUZZLE_WORKS_WRITE_RETRY,
},
);
}
/**
* 删除当前用户的拼图作品,并返回删除后的作品列表。
*/
export async function deletePuzzleWork(profileId: string) {
return requestJson<PuzzleWorksResponse>(
`${PUZZLE_WORKS_API_BASE}/${encodeURIComponent(profileId)}`,
{
method: 'DELETE',
},
'删除拼图作品失败',
{
retry: PUZZLE_WORKS_WRITE_RETRY,
},
);
}
/**
* 领取当前用户名下拼图作品的整数泥点激励。
*/
export async function claimPuzzleWorkPointIncentive(profileId: string) {
return requestJson<PuzzleWorkMutationResponse>(
`${PUZZLE_WORKS_API_BASE}/${encodeURIComponent(profileId)}/point-incentive/claim`,
{
method: 'POST',
},
'领取拼图积分激励失败',
{
retry: PUZZLE_WORKS_WRITE_RETRY,
},
);
}
export const puzzleWorksClient = {
claimPointIncentive: claimPuzzleWorkPointIncentive,
delete: deletePuzzleWork,
getDetail: getPuzzleWorkDetail,
list: listPuzzleWorks,
update: updatePuzzleWork,
};