import {z} from 'zod' export type GetApiCardsCardIdArgs = {cardId: string, 'Accept-Version': string}; export type GetApiCardsCardIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCardsCardIdResponse = z.object({cardName: z.string(), secondaryCardName: z.string().optional(), cardType: z.string(), last4: z.string(), availableCredit: z.string(), status: z.enum(["ACTIVE", "DELETED", "EXPIRED", "FROZEN", "INACTIVE", "BLOCKED", "INTERNAL_REVIEW_FAILED"]), freezeReason: z.string().optional(), statusReason: z.string().optional(), physicalCardStatus: z.enum(["NOT_PHYSICAL_CARD", "CARD_NOT_ORDERED", "CARD_PRODUCTION_REQUESTED", "CARD_IN_PRODUCTION", "CARD_PRODUCTION_COMPLETED", "CARD_ACTIVATED", "CARD_PRODUCTION_CANCELED", "CARD_PRODUCTION_ONHOLD"]), shippingAddress: z.object({line1: z.string().max(50), line2: z.string().max(50).nullable().optional(), zone: z.string().max(50).nullable().optional(), city: z.string().max(50), postalCode: z.string().max(10), country: z.string().min(2).max(2)}), spendControl: z.object({spendControlCap: z.object({transactionLimit: z.string().optional(), dailyLimit: z.string().optional(), weeklyLimit: z.string().optional(), monthlyLimit: z.string().optional(), yearlyLimit: z.string().optional(), allTimeLimit: z.string().optional()}).optional(), spendControlAmount: z.object({dailySpent: z.string().optional(), weeklySpent: z.string().optional(), monthlySpent: z.string().optional(), yearlySpent: z.string().optional(), allTimeSpent: z.string().optional()}).optional(), atmControl: z.object({dailyFrequency: z.string().optional(), dailyWithdrawal: z.string().optional(), monthlyFrequency: z.string().optional(), monthlyWithdrawal: z.string().optional(), yearlyFrequency: z.string().optional(), yearlyWithdrawal: z.string().optional()}).optional()}).optional(), threeDSForwarding: z.boolean().optional(), cardDesign: z.string().optional(), shippingInformation: z.object({bulkShippingID: z.string().optional(), sku: z.string().optional()}).optional(), meta: z.object({id: z.string(), email: z.string().max(50).nullable().optional(), otpPhoneNumber: z.object({dialCode: z.number(), phoneNumber: 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 }; /** * Retrieve Card * * GET /cards/{cardId} */ export const getApiCardsCardId = async (args: GetApiCardsCardIdArgs, options?: GetApiCardsCardIdOptions) => { const {baseUrl = "https://sandbox.api.caas.reap.global", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Accept-Version'] != null) headers.set("Accept-Version", String(args['Accept-Version'])) if (ApiKeyAuth !== undefined) headers.set("x-reap-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/cards/${encodeURIComponent(String(args.cardId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiCardsCardIdResponse, await res.json()) };