import {z} from 'zod' export type CreateApiCardsCardIdReplaceArgs = {cardId: string, accountId: string, personId?: string | undefined, body: {keepPan?: boolean | undefined, keepDigitalWalletTokens?: boolean | undefined, expedite?: boolean | undefined}}; export type CreateApiCardsCardIdReplaceOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCardsCardIdReplaceResponse = 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 }; /** * Order a replacement card * * POST /cards/{cardId}/replace */ export const createApiCardsCardIdReplace = async (args: CreateApiCardsCardIdReplaceArgs, options?: CreateApiCardsCardIdReplaceOptions) => { 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)) if (args.personId != null) query.append("personId", String(args.personId)) const url = `${baseUrl.replace(/\/+$/, '')}/cards/${encodeURIComponent(String(args.cardId))}/replace${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(createApiCardsCardIdReplaceResponse, await res.json()) };