import {z} from 'zod' export type Criteria = {field?: number | undefined, operator?: ('%3D' | '%3C' | '%3E' | '%3C%3D' | '%3E%3D' | 'START%20WITH') | undefined, value?: string | undefined}; export type SearchQuery = {name: 'Search' | 'TransHistoryMinimal', start_date: string, end_date: string, start_row: number, end_row: number, criteria?: Array | undefined}; export type CreateApiReportsArgs = {body: SearchQuery}; export type CreateApiReportsOptions = {baseUrl?: string | undefined, Passcode?: string | undefined, fetch?: typeof fetch | undefined}; export const searchRecord = z.object({row_id: z.number().optional(), trn_id: z.number().optional(), trn_date_time: z.string().optional(), trn_type: z.string().optional(), trn_order_number: z.string().optional(), trn_payment_method: z.string().optional(), trn_comments: z.string().optional(), trn_masked_card: z.string().optional(), trn_amount: z.number().optional(), trn_returns: z.number().optional(), trn_completions: z.number().optional(), trn_voided: z.number().optional(), trn_response: z.number().optional(), trn_card_type: z.string().optional(), trn_batch_no: z.number().optional(), trn_avs_result: z.string().optional(), trn_cvd_result: z.number().optional(), trn_cavv_result: z.string().optional(), trn_card_expiry: z.string().optional(), message_id: z.number().optional(), message_text: z.string().optional(), trn_card_owner: z.string().optional(), trn_ip: z.string().optional(), trn_approval_code: z.string().optional(), trn_reference: z.number().optional(), b_name: z.string().optional(), b_email: z.string().optional(), b_phone: z.string().optional(), b_phone_type: z.enum(["m", "h", "w"]).max(1).optional(), b_address1: z.string().optional(), b_address2: z.string().optional(), b_city: z.string().optional(), b_province: z.string().optional(), b_postal: z.string().optional(), b_country: z.string().optional(), s_name: z.string().optional(), s_email: z.string().optional(), s_phone: z.string().optional(), s_address1: z.string().optional(), s_address2: z.string().optional(), s_city: z.string().optional(), s_province: z.string().optional(), s_postal: z.string().optional(), s_country: z.string().optional(), ref1: z.string().optional(), ref2: z.string().optional(), ref3: z.string().optional(), ref4: z.string().optional(), ref5: z.string().optional(), product_name: z.string().optional(), product_id: z.string().optional(), customer_code: z.string().optional(), currency_abbr: z.string().max(3).optional(), merchant_id: z.number().int().optional(), merchant_name: z.string().optional(), entry_method: z.enum(["N", "A", "D", "P", "V", "T"]).max(1).optional(), authorizing_merchant_id: z.number().int().optional(), version_3ds: z.number().int().optional(), flow_type_3ds: z.enum(["F", "C"]).optional(), status_3ds: z.enum(["Success", "Attempted", "Rejected", "Failed", "Unavailable"]).optional(), cof_type: z.enum(["first_installment", "subsequent_installment", "first_recurring", "subsequent_recurring", "first_unscheduled", "subsequent_customer_initiated", "subsequent_unscheduled"]).optional(), reason_merchant_3ds: z.string().optional(), reason_cardholder_3ds: z.string().optional(), downgraded_3ds: z.boolean().optional(), device_channel: z.string().max(2).optional(), method_url_3ds: z.string().optional()}); export const searchResult = z.object({records: z.array(searchRecord).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 }; /** * Search Query * * POST /reports * * Also accepts auth: Sub-Merchant-Id */ export const createApiReports = async (args: CreateApiReportsArgs, options?: CreateApiReportsOptions) => { const {baseUrl = "https://api.na.bambora.com/v1", fetch: fetchFn = fetch, Passcode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Passcode !== undefined) headers.set("Authorization", Passcode) const url = `${baseUrl.replace(/\/+$/, '')}/reports${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(searchResult, await res.json()) };