Files
goodbuddy-shareserver/web/src/lib/sharing-api.ts
T

16 lines
670 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export async function sharingRequest<T>(
path: string,
signal?: AbortSignal,
method = 'GET'
): Promise<T> {
const init: RequestInit = { signal }
if (method !== 'GET') init.method = method
const response = await fetch(`/api/v1/sharing${path}`, init)
const payload = await response.json().catch(() => null) as { code?: string; message?: string; requestId?: string } | null
if (!response.ok) {
const details = [payload?.code, payload?.requestId ? `请求 ID:${payload.requestId}` : ''].filter(Boolean).join(' · ')
throw new Error(`${payload?.message ?? `HTTP ${response.status}`}${details ? `(${details})` : ''}`)
}
return payload as T
}