You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.9 KiB
49 lines
1.9 KiB
|
7 months ago
|
import type { Paginated, ReviewerPayload, ReviewerRow } from './types'
|
||
|
|
import { adminHttp } from './http'
|
||
|
|
|
||
|
|
function unwrapPaginated<T>(raw: unknown): Paginated<T> {
|
||
|
|
if (!raw || typeof raw !== 'object') throw new Error('列表响应格式无效')
|
||
|
|
const o = raw as Record<string, unknown>
|
||
|
|
if (Array.isArray(o.data) && typeof o.current_page === 'number') {
|
||
|
|
const rows = o.data as T[]
|
||
|
|
return {
|
||
|
|
data: rows,
|
||
|
|
meta: {
|
||
|
|
current_page: o.current_page as number,
|
||
|
|
last_page: typeof o.last_page === 'number' ? o.last_page : 1,
|
||
|
|
per_page: typeof o.per_page === 'number' ? o.per_page : rows.length || 15,
|
||
|
|
total: typeof o.total === 'number' ? o.total : rows.length,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
throw new Error('列表响应格式无效')
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function listReviewers(params: {
|
||
|
|
page?: number
|
||
|
|
per_page?: number
|
||
|
|
q?: string
|
||
|
|
status?: 'active' | 'disabled' | ''
|
||
|
|
}): Promise<Paginated<ReviewerRow>> {
|
||
|
|
const { data } = await adminHttp.get<unknown>('/reviewers', { params })
|
||
|
|
return unwrapPaginated<ReviewerRow>(data)
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function createReviewer(payload: ReviewerPayload): Promise<ReviewerRow> {
|
||
|
|
const { data } = await adminHttp.post<unknown>('/reviewers', payload)
|
||
|
|
const body = (data as { data?: ReviewerRow })?.data ?? (data as ReviewerRow)
|
||
|
|
if (!body || typeof body !== 'object') throw new Error('创建响应无效')
|
||
|
|
return body as ReviewerRow
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function updateReviewer(id: number, payload: Partial<ReviewerPayload>): Promise<ReviewerRow> {
|
||
|
|
const { data } = await adminHttp.put<unknown>(`/reviewers/${id}`, payload)
|
||
|
|
const body = (data as { data?: ReviewerRow })?.data ?? (data as ReviewerRow)
|
||
|
|
if (!body || typeof body !== 'object') throw new Error('更新响应无效')
|
||
|
|
return body as ReviewerRow
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function deleteReviewer(id: number): Promise<void> {
|
||
|
|
await adminHttp.delete(`/reviewers/${id}`)
|
||
|
|
}
|