export type GetApiOauthCallbackArgs = {code: string, state: string}; export type GetApiOauthCallbackOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; /** * Handle receiving a code from an OAuth flow. * * GET /oauth/callback */ export const getApiOauthCallback = async (args: GetApiOauthCallbackArgs, options: GetApiOauthCallbackOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.code != null) query.append("code", String(args.code)) if (args.state != null) query.append("state", String(args.state)) const url = `${baseUrl.replace(/\/+$/, '')}/oauth/callback${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 (await res.json()) as unknown };