import {z} from 'zod' export type GetApiSsoGoogleLoginArgs = {access_token: string, contact_consent?: boolean | undefined, rewardful_referral?: string | undefined, remember_me?: boolean | undefined}; export type GetApiSsoGoogleLoginOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const appRoutersGoogleTokenResponse = z.object({token_type: z.string(), access_token: z.string(), email: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Google Login * * GET /sso/google/login */ export const getApiSsoGoogleLogin = async (args: GetApiSsoGoogleLoginArgs, options: GetApiSsoGoogleLoginOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.access_token != null) query.append("access_token", String(args.access_token)) if (args.contact_consent != null) query.append("contact_consent", String(args.contact_consent)) if (args.rewardful_referral != null) query.append("rewardful_referral", String(args.rewardful_referral)) if (args.remember_me != null) query.append("remember_me", String(args.remember_me)) const url = `${baseUrl.replace(/\/+$/, '')}/sso/google/login${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 validateResponse(appRoutersGoogleTokenResponse, await res.json()) };