import {z} from 'zod' export type CreateApiCardsCardIdReplaceArgs = {cardId: string, body: {terminateCardReason: 'DAMAGED' | 'EXPIRING_SOON' | 'LOST' | 'STOLEN' | 'OTHER', expiryPeriodMonths: number}}; export type CreateApiCardsCardIdReplaceOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCardsCardIdReplaceResponse = z.object({cardId: z.string().optional(), type: z.enum(["VIRTUAL", "SINGLE_USE", "PHYSICAL", "BLACK", "TRAVEL"]).nullable().optional(), cardholderId: z.string().optional(), organizationId: z.string().optional(), transactionLimit: z.object({value: z.number().optional(), currency: z.string().optional()}).optional(), limit: z.object({value: z.number().optional(), currency: z.string().optional()}).optional(), limitRenewFrequency: z.enum(["DAILY", "WEEKLY", "MONTHLY", "QUARTERLY", "ANNUAL", "TOTAL"]).optional(), expiryDate: z.string().optional(), label: z.string().max(40).nullable().optional(), purpose: z.string().nullable().optional(), cardDesign: z.string().optional(), cardDesignId: z.string().optional(), productRef: z.string().optional(), platformFee: z.boolean().optional(), cardConfig: z.string().optional(), replacedByCardId: z.string().optional(), replacedCardId: z.string().optional(), replacementReason: z.enum(["DAMAGED", "EXPIRING_SOON", "LOST", "OTHER", "STOLEN"]).optional(), cardAccountId: z.string().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 }; /** * Replace Card * * POST /cards/{cardId}/replace */ export const createApiCardsCardIdReplace = async (args: CreateApiCardsCardIdReplaceArgs, options?: CreateApiCardsCardIdReplaceOptions) => { const {baseUrl = "https://partner-api.getpliant.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) 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()) };