import {z} from 'zod' export type GetApiV1OauthAuthorizeArgs = {'App-ID': string, 'Auth-Token': string, client_id: string, redirect_uri: string, scope: string, response_type: string, code_challenge: string, code_challenge_method?: string | undefined}; export type GetApiV1OauthAuthorizeOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const oAuth2AuthorizeClientSchema = z.object({name: z.string()}); export const oAuth2AuthorizeSystemDomain = z.object({id: z.string(), name: z.string()}); export const oAuth2AuthorizeUser = z.object({email: z.string(), first_name: z.string().nullable().optional(), id: z.string(), last_name: z.string().nullable().optional(), photo: z.string().nullable().optional()}); export const oAuth2AuthorizeDescribeSchema = z.object({client: oAuth2AuthorizeClientSchema, redirect_uri: z.string(), requires_admin_approval: z.boolean().optional(), scopes: z.array(z.string()), state: z.string().nullable().optional(), system_domain: oAuth2AuthorizeSystemDomain, user: oAuth2AuthorizeUser}); 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 }; /** * Validate the authorization request and return consent metadata * * GET /v1/oauth/authorize/ */ export const getApiV1OauthAuthorize = async (args: GetApiV1OauthAuthorizeArgs, options: GetApiV1OauthAuthorizeOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['App-ID'] != null) headers.set("App-ID", String(args['App-ID'])) if (args['Auth-Token'] != null) headers.set("Auth-Token", String(args['Auth-Token'])) if (args.client_id != null) query.append("client_id", String(args.client_id)) if (args.redirect_uri != null) query.append("redirect_uri", String(args.redirect_uri)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.response_type != null) query.append("response_type", String(args.response_type)) if (args.code_challenge != null) query.append("code_challenge", String(args.code_challenge)) if (args.code_challenge_method != null) query.append("code_challenge_method", String(args.code_challenge_method)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/oauth/authorize/${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(oAuth2AuthorizeDescribeSchema, await res.json()) };