import {z} from 'zod' export type CreateApiCardsArgs = {accountId: string, body: {budgetId: string, name?: string | undefined, cardOwnerType: 'BUDGET', budgetCardPhoneNumber?: string | undefined, budgetCardPhoneNumberPersonId?: string | undefined, mfa?: {sessionId: string, token: string} | undefined} | {cardType?: ('VIRTUAL' | 'PHYSICAL') | undefined, personId: string, name?: string | undefined, cardOwnerType: 'PEOPLE', budgets: Array<{budgetId: string, active?: boolean | undefined}>, fundingSourceBudgetId?: string | undefined, recipientAddress?: ({firstName: string, middleName?: string | undefined, lastName: string, address1: string, address2?: string | undefined, city: string, state?: string | undefined, postalCode: string, country: string, phone?: string | undefined}) | undefined, careOfLine?: string | undefined, expedite?: boolean | undefined, mfa?: {sessionId: string, token: string} | undefined}}; export type CreateApiCardsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCardsResponse = z.object({id: z.string().max(36).optional(), personId: z.string().max(36).nullable().optional(), accountId: z.string().optional(), careOfLine: z.string().nullable().optional(), cardProductName: z.string().optional(), lastFour: z.string().optional(), expiration: z.string().optional(), expirationTime: z.string().optional(), pinIsSet: z.boolean().optional(), state: z.enum(["ACTIVE", "REPLACED", "TERMINATED", "SUSPENDED", "UNACTIVATED"]).optional(), stateReason: z.string().optional(), fulfillmentStatus: z.enum(["ISSUED", "ORDERED", "REORDERED", "REJECTED", "SHIPPED", "DIGITALLY_PRESENTED"]).optional(), fulfillment: z.object({shippingMethod: z.enum(["EXPRESSLOCALMAIL", "COURIER", "AIRMAIL", "STANDARDMAIL"])}).nullable().optional(), cardType: z.enum(["VIRTUAL_PAN", "PHYSICAL_MSR", "PHYSICAL_COMBO"]).optional(), recipientAddress: z.object({firstName: z.string(), middleName: z.string().optional(), lastName: z.string(), address1: z.string().min(1), address2: z.string().optional(), city: z.string(), state: z.string().optional(), postalCode: z.string(), country: z.string().max(40), phone: z.string().optional()}).nullable().optional(), cardOwnerType: z.enum(["PEOPLE", "BUDGET"]).optional(), name: z.string().optional(), budgetCardPhoneNumber: z.string().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 }; /** * Create a new virtual card * * POST /cards */ export const createApiCards = async (args: CreateApiCardsArgs, options?: CreateApiCardsOptions) => { const {baseUrl = "https://api.equalsmoney.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.accountId != null) query.append("accountId", String(args.accountId)) 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(createApiCardsResponse, await res.json()) };