import {z} from 'zod' export type CardStatus = 'pending' | 'active' | 'suspended' | 'closed' | 'failed'; export type CardFormFactor = 'physical' | 'virtual'; export type ListCardsFilter = {query?: (string | null) | undefined, statuses?: (Array | null) | undefined, card_types?: Array | undefined, from_created_time?: (string | null) | undefined, to_created_time?: (string | null) | undefined}; export type GetApiCardsArgs = {filters?: ListCardsFilter | undefined, limit?: number | undefined, offset?: (string | null) | undefined, 'x-entity-id'?: string | undefined}; export type GetApiCardsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cardStatus = z.enum(["pending", "active", "suspended", "closed", "failed"]); export const cardCardholder = z.object({name: z.string(), public_id: z.string().nullable().optional()}); export const timestamp = z.string(); export const merchantRestrictionType = z.enum(["allow", "block"]); export const cardSpendingRestriction = z.object({type: merchantRestrictionType, merchants: z.array(z.string())}); 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 cardResponse = z.object({id: z.string(), display_name: z.string(), last_four: z.string().nullable(), status: cardStatus, is_physical: z.boolean(), is_single_use: z.boolean().optional(), cardholder: cardCardholder, created_at: timestamp, expiration: timestamp.nullable().optional(), spending_restriction: cardSpendingRestriction.nullable().optional(), allowed_categories: z.array(cardTransactionCategory).nullable().optional()}); export const pageMetadata = z.object({nextOffset: z.string().nullable().optional()}); export const cardsResponse = z.object({data: z.array(cardResponse), 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 Cards * * GET /cards */ export const getApiCards = async (args: GetApiCardsArgs, options?: GetApiCardsOptions) => { 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.filters != null) query.append("filters", JSON.stringify(args.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${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(cardsResponse, await res.json()) };