import {z} from 'zod' export type GetApiFraudAlertArgs = {status?: ('PENDING' | 'CONFIRMED' | 'DECLINED') | undefined, transactionId?: string | undefined, page?: number | undefined, limit?: number | undefined, fromDate?: string | undefined, toDate?: string | undefined, 'Accept-Version': string}; export type GetApiFraudAlertOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiFraudAlertResponse = z.object({items: z.array(z.object({fraudAlertId: z.string(), transactionId: z.string(), status: z.enum(["PENDING", "CONFIRMED", "DECLINED"]), cardId: z.string(), merchantInfo: z.object({name: z.string().optional(), location: z.string().optional()}), transactionInfo: z.object({amount: z.string(), currency: z.string(), timestamp: z.string()})})).optional(), meta: z.object({itemCount: z.number(), totalItems: z.number(), itemsPerPage: z.number(), totalPages: z.number(), currentPage: z.number()}).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 }; /** * Get fraud alerts * * GET /fraud-alert */ export const getApiFraudAlert = async (args: GetApiFraudAlertArgs, options?: GetApiFraudAlertOptions) => { const {baseUrl = "https://sandbox.api.caas.reap.global", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.transactionId != null) query.append("transactionId", String(args.transactionId)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args['Accept-Version'] != null) headers.set("Accept-Version", String(args['Accept-Version'])) if (ApiKeyAuth !== undefined) headers.set("x-reap-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/fraud-alert${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(getApiFraudAlertResponse, await res.json()) };