import {z} from 'zod' export type GetApiOauthAuthorizeArgs = {response_type?: (string | null) | undefined, client_id?: (string | null) | undefined, redirect_uri?: (string | null) | undefined, scope?: (string | null) | undefined, state?: (string | null) | undefined, code_challenge?: (string | null) | undefined, code_challenge_method?: (string | null) | undefined, resource?: (string | null) | undefined, request_uri?: (string | null) | undefined, logfire_token?: (string | null) | undefined}; export type GetApiOauthAuthorizeOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiOauthAuthorizeResponse = z.unknown(); 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 }; /** * Oauth Authorize Get * * GET /oauth/authorize */ export const getApiOauthAuthorize = async (args: GetApiOauthAuthorizeArgs, options: GetApiOauthAuthorizeOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() const cookies: string[] = [] headers.set('accept', "application/json") if (args.response_type != null) query.append("response_type", String(args.response_type)) 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.state != null) query.append("state", String(args.state)) 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)) if (args.resource != null) query.append("resource", String(args.resource)) if (args.request_uri != null) query.append("request_uri", String(args.request_uri)) if (args.logfire_token != null) cookies.push("logfire_token=" + encodeURIComponent(String(args.logfire_token))) if (cookies.length > 0) headers.set('cookie', cookies.join('; ')) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiOauthAuthorizeResponse, await res.json()) };