import {z} from 'zod' export type GetApiProtectionEventsFormatArgs = {order?: string | undefined, since_token?: string | undefined, count?: string | undefined, state?: string | undefined}; export type GetApiProtectionEventsFormatOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const protectionEventProperties = z.object({updated_at: z.string().optional(), created_at: z.string().optional(), fraud_control_vendor_key: z.string().optional(), vendor_transaction_id: z.string().optional(), order_id: z.string().optional(), protection_provider_key: z.string().optional(), amount: z.number().optional(), currency_code: z.string().optional(), ip: z.string().optional(), email: z.string().optional(), callback_url: z.string().optional(), test_scenario: z.string().optional(), acquiring_bank_fraud_rate: z.string().optional(), exemption_type: z.string().optional(), warning: z.string().optional(), managed_order_token: z.string().optional(), fraud_decision: z.string().optional(), fraud_recommendation: z.string().optional(), fraud_decision_reason: z.string().optional(), vendor_response: z.object({forterDecision: z.string().optional(), recommendation: z.string().optional(), verificationMethod: z.object({}).optional(), decisionReason: z.string().optional(), merchantPolicyId: z.string().optional(), orderId: z.string().optional(), linkToEventInDashboard: z.string().optional()}).optional(), token: z.string().optional(), succeeded: z.boolean().optional(), state: z.string().optional(), message: z.string().optional(), transaction_type: z.string().optional(), gateway_transaction_key: z.string().optional(), sca_provider_key: z.string().optional(), acs_transaction_id: z.string().optional(), three_ds_server_trans_id: z.string().optional(), directory_server_transaction_id: z.string().optional(), xid: z.string().optional(), three_ds_version: z.string().optional(), flow_performed: z.string().optional(), ecommerce_indicator: z.string().optional(), directory_response_status: z.string().optional(), authentication_response_status: z.string().optional(), authentication_value: z.string().optional(), trans_status_reason: z.string().optional(), daf: z.boolean().optional(), payment_method_key: z.string().optional()}); export const listProtectionEventsResponse = z.object({events: z.array(protectionEventProperties).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 }; /** * List protection events * * GET /protection/events.{format} */ export const getApiProtectionEventsFormat = async (args: GetApiProtectionEventsFormatArgs, options?: GetApiProtectionEventsFormatOptions) => { const {baseUrl = "https://core.spreedly.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.order != null) query.append("order", String(args.order)) if (args.since_token != null) query.append("since_token", String(args.since_token)) if (args.count != null) query.append("count", String(args.count)) if (args.state != null) query.append("state", String(args.state)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/protection/events.${encodeURIComponent(String(args.format))}${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(listProtectionEventsResponse, await res.json()) };