import {z} from 'zod' export type GetApiCardEventIdArgs = {id: string}; export type GetApiCardEventIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiCardEventIdResponse = z.object({amount: z.string().optional(), approved_amount: z.string().optional(), atm: z.object({network: z.string().optional(), network_fee: z.string().optional()}).optional(), bank_id: z.string().optional(), card_id: z.string().optional(), currency: z.string().optional(), decline_reason: z.string().optional(), id: z.string().optional(), merchant: z.object({address: z.object({city: z.string().optional(), country: z.string().optional(), postal_code: z.string().optional(), state: z.string().optional()}).optional(), mcc: z.string().optional(), mid: z.string().optional(), name: z.string().optional()}).optional(), message_type: z.string().optional(), network: z.string().optional(), network_created_at: z.string().optional(), networkdata: z.object({}).optional(), org_id: z.string().optional(), processor: z.string().optional(), status: z.string().optional(), trace_id: 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 }; /** * Fetch a Card Event * * GET /card_event/{id} * * Also accepts auth: apiKey */ export const getApiCardEventId = async (args: GetApiCardEventIdArgs, options?: GetApiCardEventIdOptions) => { const {baseUrl = "https://api.treasuryprime.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/card_event/${encodeURIComponent(String(args.id))}${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(getApiCardEventIdResponse, await res.json()) };