export type GetApiSyncStreamArgs = {token: string, since?: string | undefined}; export type GetApiSyncStreamOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; /** * SSE 实时同步流 * * GET /sync/stream */ export const getApiSyncStream = async (args: GetApiSyncStreamArgs, options?: GetApiSyncStreamOptions) => { const {baseUrl = "http://localhost:3200/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "text/event-stream") if (args.token != null) query.append("token", String(args.token)) if (args.since != null) query.append("since", String(args.since)) const url = `${baseUrl.replace(/\/+$/, '')}/sync/stream${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return res.text() };