import {z} from 'zod' 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 MerchantRestrictionType = 'allow' | 'block'; export type CardSpendingRestrictionRequest = {type: MerchantRestrictionType, merchants: Array}; 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 Timestamp = string; export type CreateCardRequest = {spending_controls: SpendingControls, nickname: string, spending_restriction?: (CardSpendingRestrictionRequest | null) | undefined, allowed_categories?: (Array | null) | undefined, debit_account?: (string | null) | undefined, purpose?: (string | null) | undefined, expires_at?: (Timestamp | null) | undefined, single_use?: boolean | undefined}; export type CreateApiCardsArgs = {'Idempotency-Key'?: (string | null) | undefined, 'x-entity-id'?: string | undefined, body: CreateCardRequest}; export type CreateApiCardsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const spendingControls = z.object({per_transaction_limit: z.number().int().gt(0).lte(200000), daily_limit: z.number().int().gte(0).lte(1000000000).nullable().optional(), weekly_limit: z.number().int().gte(0).lte(1000000000).nullable().optional(), monthly_limit: z.number().int().gte(0).lte(1000000000).nullable().optional(), yearly_limit: z.number().int().gte(0).lte(1000000000).nullable().optional(), all_time_limit: z.number().int().gte(0).lte(1000000000).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 cardControls = z.object({spending_controls: spendingControls, single_use: z.boolean(), expires_at: timestamp.nullable().optional(), spending_restriction: cardSpendingRestriction.nullable().optional()}); export const cardMetadata = z.object({card_id: z.string(), purpose: z.string().min(1).max(500).nullable().optional(), debit_account: z.string()}); export const createCardResponse = z.object({payment_type: z.literal("card").optional(), controls: cardControls, metadata: cardMetadata}); 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 }; /** * Create Card * * POST /cards */ export const createApiCards = async (args: CreateApiCardsArgs, options?: CreateApiCardsOptions) => { 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['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) 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: "POST", 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(createCardResponse, await res.json()) };