import {z} from 'zod' export type GetApiPaymentsCardsCardIdArgs = {card_id: string}; export type GetApiPaymentsCardsCardIdOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const cardExpiry = z.object({month: z.string(), year: z.string()}); export const cardBrand = z.enum(["visa", "mastercard", "american-express", "discover", "diners-club", "jcb", "unionpay"]); export const cardFunding = z.enum(["debit", "credit", "prepaid", "deferred-debit", "charge"]); export const cardSegment = z.enum(["consumer", "commercial", "business", "government", "payouts", "all"]); export const card = z.object({id: z.string().optional(), number: z.string(), expiry: cardExpiry.optional(), bin: z.string(), lastFour: z.string(), brand: cardBrand.optional(), funding: cardFunding.optional(), segment: cardSegment.optional(), country: z.string().optional(), currency: z.string().optional(), issuer: z.string().optional(), status: z.enum(["active", "replaced", "closed", "invalid"]).optional(), replacement: z.string().nullable().optional(), automaticUpdates: z.enum(["enabled", "issuer-not-enrolled", "disabled"]).optional(), createdAt: z.number().int(), updatedAt: z.number().int().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 }; /** * Retrieve a Card * * GET /payments/cards/{card_id} */ export const getApiPaymentsCardsCardId = async (args: GetApiPaymentsCardsCardIdArgs, options?: GetApiPaymentsCardsCardIdOptions) => { const {baseUrl = "https://api.evervault.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/cards/${encodeURIComponent(String(args.card_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(card, await res.json()) };