import {z} from 'zod' export type GetApiCardsCardIdArgs = {card_id: string, filter?: string | undefined}; export type GetApiCardsCardIdOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const bin = z.object({number: z.string().optional(), scheme: z.enum(["visa", "mastercard", "amex"]).optional(), type: z.enum(["charge", "credit", "debit", "deferred_debit", "prepaid"]).optional(), product: z.enum(["consumer", "commercial"]).optional(), card_level: z.enum(["aarp", "acquiring", "advantage", "airmiles", "atm_only", "black", "blue", "bonus", "business", "cash", "centurion", "charge_card", "check_card", "classic", "commerce", "commercial", "corporate", "delta", "diamond", "distribution", "electric_orange", "electron", "electronic", "executive_business", "fleet_card", "flying_blue", "gift", "global_payment", "gm_card", "gold", "government", "green", "gsa", "hsa_non_substantiated", "hsa_substantiated", "icard", "infinite", "interlink", "mastermoney", "new_world", "optima", "ourocard", "payment_card", "paypass_card", "personal", "pin_only", "platinum", "platinum_edge", "plus", "premier_miles", "premium", "prepaid", "private", "professional", "propriety", "purchasing", "q_cash", "quantum", "rebate", "red", "reserved", "revolving", "rewards", "salute", "sears", "signature", "small_business", "standard", "standard_health", "unembossed", "student", "titanium", "top_up_card", "traditional", "travel", "ultimate", "v_pay", "virtual", "world", "healthcare", "prepaid_maestro_travel", "maestro_delayed_debit"]).optional(), issuer: z.string().optional(), issuer_country: z.string().optional(), issuer_country_code: z.string().optional(), eea: z.boolean().optional(), non_reloadable: z.boolean().optional()}); export const getApiCardsCardIdResponse = z.object({card_id: z.string().optional(), customer_id: z.string().optional(), card: z.object({holder_name: z.string().min(2).max(45).optional(), scheme: z.enum(["visa", "mastercard", "amex"]).optional(), number: z.string().optional(), expiry_month: z.number().int().gte(1).lte(12).optional(), expiry_year: z.number().int().gte(26).lte(56).optional(), network_token: z.enum(["active", "pending", "suspended", "deleted", "not_created"]).optional()}).optional(), bin: bin.optional(), source: z.enum(["card", "apple_pay", "google_pay"]).optional(), created: z.string().optional(), last_updated: z.string().optional(), is_active: z.boolean().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 card details * * GET /cards/{card_id} */ export const getApiCardsCardId = async (args: GetApiCardsCardIdArgs, options?: GetApiCardsCardIdOptions) => { const {baseUrl = "https://test-api.acquired.com/v1", fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiCardsCardIdResponse, await res.json()) };