import {z} from 'zod' export type DeviceData = {acceptHeader: string, userAgentHeader: string, browserLanguage?: string | undefined, ipAddress?: string | undefined, browserJavaEnabled?: boolean | undefined, browserColorDepth?: number | undefined, browserScreenHeight?: number | undefined, browserScreenWidth?: number | undefined, timeZone?: string | undefined, browserJavascriptEnabled?: boolean | undefined, channel?: ('browser' | 'native' | 'ucpBrowser') | undefined}; export type DeviceDataRequest = {collectionReference?: string | undefined, deviceData?: DeviceData | undefined}; export type CreateApiDeviceDataKeyCompleteArgs = {'WP-Api-Version': '2026-10-01', key: string, body: DeviceDataRequest}; export type CreateApiDeviceDataKeyCompleteOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const schema3DSDevice200Response = z.object({outcome: 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 }; /** * Device Data Complete * * POST /deviceData/{key}/complete */ export const createApiDeviceDataKeyComplete = async (args: CreateApiDeviceDataKeyCompleteArgs, options?: CreateApiDeviceDataKeyCompleteOptions) => { const {baseUrl = "https://try.access.worldpay.com/3ds", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['WP-Api-Version'] != null) headers.set("WP-Api-Version", String(args['WP-Api-Version'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/deviceData/${encodeURIComponent(String(args.key))}/complete${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(schema3DSDevice200Response, await res.json()) };