import {z} from 'zod' export type CardTransactionStatus = 'pending' | 'approved' | 'cleared' | 'reversed' | 'failed' | 'expired'; export type ListCardTransactionsFilter = {card_id?: (string | null) | undefined, statuses?: (Array | null) | undefined, merchant_name?: (string | null) | undefined, trace_id?: (string | null) | undefined, from_authorized_at?: (string | null) | undefined, to_authorized_at?: (string | null) | undefined, amount_min?: (number | string | null) | undefined, amount_max?: (number | string | null) | undefined}; export type GetApiCardsTransactionsArgs = {public_filters?: ListCardTransactionsFilter | undefined, limit?: number | undefined, offset?: (string | null) | undefined, 'x-entity-id'?: string | undefined}; export type GetApiCardsTransactionsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cardTransactionType = z.enum(["authorization", "clearing", "refund", "reversal", "original_credit"]); export const cardTransactionStatus = z.enum(["pending", "approved", "cleared", "reversed", "failed", "expired"]); export const iso4217Code = z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BOV", "BRL", "BSD", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLF", "CLP", "CNY", "COP", "COU", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "INR", "IQD", "IRR", "ISK", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRU", "MUR", "MVR", "MWK", "MXN", "MXV", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SLE", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "SSP", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "USN", "UYI", "UYU", "UYW", "UZS", "VED", "VES", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "XCD", "XDR", "XOF", "XPD", "XPF", "XPT", "XSU", "XTS", "XUA", "XXX", "YER", "ZAR", "ZMW", "ZWG", "ZWL"]); export const cardTransactionMerchant = z.object({name: z.string(), website: z.string().nullable().optional()}); export const cardTransactionCategory = z.enum(["Advertising", "Airlines", "Books and Newspapers", "Car Rental", "Charity", "Clothing", "Electronics", "Entertainment", "Facilities Expenses", "Financial Institutions and Fees", "Fuel", "Furniture", "Gifts and Flowers", "Government Services", "Grocery", "Ground Transportation", "Insurance", "Legal", "Lodging", "Meals", "Medical", "Memberships and Dues", "Office Supplies", "Parking", "Personal Care", "Political", "Professional Services", "Recruiting", "Rent", "Restaurants", "Shipping", "Software", "Taxes", "Taxis and Rideshare", "Technology Infrastructure", "Training and Education", "Travel Services", "Utilities", "Vehicle Expenses"]); export const timestamp = z.string(); export const cardTransactionResponse = z.object({id: z.string(), card_id: z.string(), card_last_four: z.string(), type: cardTransactionType, status: cardTransactionStatus, description: z.string(), amount: z.string(), currency: iso4217Code, merchant_amount: z.string(), merchant_currency: iso4217Code, merchant: cardTransactionMerchant.nullable().optional(), category: cardTransactionCategory.nullable().optional(), memo: z.string().nullable().optional(), trace_id: z.string().nullable().optional(), decline_reason: z.string().nullable().optional(), authorized_at: timestamp, created_at: timestamp, updated_at: timestamp.nullable().optional()}); export const pageMetadata = z.object({nextOffset: z.string().nullable().optional()}); export const cardTransactionsResponse = z.object({data: z.array(cardTransactionResponse), page: pageMetadata.nullable().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 Card Transactions * * GET /cards/transactions */ export const getApiCardsTransactions = async (args: GetApiCardsTransactionsArgs, options?: GetApiCardsTransactionsOptions) => { const {baseUrl = "https://api.meow.com/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.public_filters != null) query.append("public_filters", JSON.stringify(args.public_filters)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args['x-entity-id'] != null) headers.set("x-entity-id", String(args['x-entity-id'])) if (apiKeyAuth !== undefined) headers.set("x-api-key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/cards/transactions${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(cardTransactionsResponse, await res.json()) };