import {z} from 'zod' export type GetApiV1AccountSsoCallbackSlugArgs = {slug: string, code?: string | undefined, samlResponse?: string | undefined, state?: string | undefined, error?: string | undefined, errorDesc?: string | undefined}; export type GetApiV1AccountSsoCallbackSlugOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const accountV1HandleSSOCallbackReply = z.object({redirectUrl: z.string().optional()}); 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 }; /** * HandleSSOCallback 处理 SSO 回调 * * GET /v1/account/sso/callback/{slug} */ export const getApiV1AccountSsoCallbackSlug = async (args: GetApiV1AccountSsoCallbackSlugArgs, options: GetApiV1AccountSsoCallbackSlugOptions) => { 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.samlResponse != null) query.append("samlResponse", String(args.samlResponse)) if (args.state != null) query.append("state", String(args.state)) if (args.error != null) query.append("error", String(args.error)) if (args.errorDesc != null) query.append("errorDesc", String(args.errorDesc)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/account/sso/callback/${encodeURIComponent(String(args.slug))}${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(accountV1HandleSSOCallbackReply, await res.json()) };