export type GetApiOauthIdpCallbackArgs = {code: string, state?: string | undefined}; export type GetApiOauthIdpCallbackOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; /** * Oauth2 callback from IDP to intent gateway * * GET /oauth/idp_callback */ export const getApiOauthIdpCallback = async (args: GetApiOauthIdpCallbackArgs, options: GetApiOauthIdpCallbackOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() if (args.code != null) query.append("code", String(args.code)) if (args.state != null) query.append("state", String(args.state)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/oauth/idp_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 undefined };