import {z} from 'zod' export type CardStatusUpdate = 'active' | 'inactive'; export type SpendingControls = {per_transaction_limit: number, daily_limit?: (number | null) | undefined, weekly_limit?: (number | null) | undefined, monthly_limit?: (number | null) | undefined, yearly_limit?: (number | null) | undefined, all_time_limit?: (number | null) | undefined}; export type CardTransactionCategory = '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 type MerchantRestrictionType = 'allow' | 'block'; export type CardSpendingRestrictionRequest = {type: MerchantRestrictionType, merchants: Array}; export type UpdateCardRequest = {status?: (CardStatusUpdate | null) | undefined, nickname?: (string | null) | undefined, spending_controls?: (SpendingControls | null) | undefined, allowed_categories?: (Array | null) | undefined, spending_restriction?: (CardSpendingRestrictionRequest | null) | undefined}; export type PatchApiCardsCardIdArgs = {card_id: string, 'x-entity-id'?: string | undefined, body: UpdateCardRequest}; export type PatchApiCardsCardIdOptions = {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 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 }; /** * Update Card * * PATCH /cards/{card_id} */ export const patchApiCardsCardId = async (args: PatchApiCardsCardIdArgs, options?: PatchApiCardsCardIdOptions) => { const {baseUrl = "https://api.meow.com/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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/${encodeURIComponent(String(args.card_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(cardResponse, await res.json()) };