import {z} from 'zod' export type CreateApiThreeDSecureSetupsArgs = {body: {account_id: string, type: 'MERCHANT_PROVIDED', browser_info: {user_agent: string, accept_header: string, color_depth: string, screen_height: string, screen_width: string, language: string, javascript_enabled?: boolean | undefined, java_enabled?: boolean | undefined, browser_time_difference?: string | undefined, platform?: ('WEB' | 'IOS' | 'ANDROID') | undefined, accept_content?: string | undefined, accept_browser?: string | undefined}, device_fingerprints?: Array<{provider_id: string, id: string}> | undefined}}; export type CreateApiThreeDSecureSetupsOptions = {baseUrl?: string | undefined, publicApiKey?: string | undefined, privateSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiThreeDSecureSetupsResponse = z.object({three_d_secure_setup_id: z.string().optional(), account_id: z.string().optional(), type: z.string().optional(), browser_info: z.object({user_agent: z.string().optional(), accept_header: z.string().optional(), color_depth: z.string().optional(), screen_height: z.string().optional(), screen_width: z.string().optional(), language: z.string().optional(), javascript_enabled: z.boolean().optional(), java_enabled: z.boolean().optional(), browser_time_difference: z.string().optional(), platform: z.string().optional()}).optional(), device_fingerprints: z.array(z.object({provider_id: z.string().optional(), id: z.string().optional()})).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 }; /** * Create 3DS2 Setup * * POST /three-d-secure/setups */ export const createApiThreeDSecureSetups = async (args: CreateApiThreeDSecureSetupsArgs, options?: CreateApiThreeDSecureSetupsOptions) => { const {baseUrl = "https://api-sandbox.y.uno/v1", fetch: fetchFn = fetch, publicApiKey, privateSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (publicApiKey !== undefined) headers.set("PUBLIC-API-KEY", publicApiKey) if (privateSecretKey !== undefined) headers.set("PRIVATE-SECRET-KEY", privateSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/three-d-secure/setups${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiThreeDSecureSetupsResponse, await res.json()) };