import {z} from 'zod' export type CreateApiCardsArgs = {'Accept-Version': string, body: {cardType: 'Virtual' | 'Physical', spendLimit?: number | undefined, customerType: 'Business' | 'Consumer', kyc: {firstName: string, lastName?: (string | null) | undefined, dob: string, nationality?: string | undefined, residentialAddress: {line1: string, line2?: (string | null) | undefined, line3?: (string | null) | undefined, line4?: (string | null) | undefined, line5?: (string | null) | undefined, country: string, postalCode?: (string | null) | undefined, city: string, state?: (string | string | null) | undefined}, idDocumentType: 'Passport' | 'Health' | 'NationalID' | 'TaxIDNumber' | 'SocialService' | 'DriversLicense', idDocumentNumber: string, signature?: string | undefined, providerId?: string | undefined, issuerCustomerId?: string | undefined, expiresAt?: string | undefined} | {fullName: string, entityType: 'Person' | 'Company', registeredAddress: {line1: string, line3?: (string | null) | undefined, line4?: (string | null) | undefined, line5?: (string | null) | undefined, country: string, postalCode?: (string | null) | undefined, city: string, state?: (string | string | null) | undefined, line2?: (string | null) | undefined}, businessName: string, businessRegistrationNumber: string, businessOperationAddress: {line1: string, line3?: (string | null) | undefined, line4?: (string | null) | undefined, line5?: (string | null) | undefined, country: string, postalCode?: (string | null) | undefined, city: string, state?: (string | string | null) | undefined, line2?: (string | null) | undefined}, cardholder?: ({firstName: string, lastName?: (string | null) | undefined, dob: string, nationality?: string | undefined, residentialAddress: {line1: string, line2?: (string | null) | undefined, line3?: (string | null) | undefined, line4?: (string | null) | undefined, line5?: (string | null) | undefined, country: string, postalCode?: (string | null) | undefined, city: string, state?: (string | string | null) | undefined}, idDocumentType: 'Passport' | 'Health' | 'NationalID' | 'TaxIDNumber' | 'SocialService' | 'DriversLicense', idDocumentNumber: string, signature?: string | undefined, providerId?: string | undefined, issuerCustomerId?: string | undefined, expiresAt?: string | undefined} | Record) | undefined, issuerCustomerId?: string | undefined, entityId?: string | undefined} | {fullName: string, entityType: 'Person' | 'Company', registeredAddress: {line1: string, line3?: (string | null) | undefined, line4?: (string | null) | undefined, line5?: (string | null) | undefined, country: string, postalCode?: (string | null) | undefined, city: string, state?: (string | string | null) | undefined, line2?: (string | null) | undefined}, businessName: string, businessRegistrationNumber: string, businessOperationAddress: {line1: string, line3?: (string | null) | undefined, line4?: (string | null) | undefined, line5?: (string | null) | undefined, country: string, postalCode?: (string | null) | undefined, city: string, state?: (string | string | null) | undefined, line2?: (string | null) | undefined}, issuerCustomerId?: string | undefined, cardholder: {firstName: string, lastName: string, email: string, dob?: string | undefined, nationalityOfEmployee?: string | undefined, nationality: unknown, placeOfBirth?: string | undefined, countryOfBirth?: string | undefined, countryOfResidence?: string | undefined}, entityId: string}, expiryDate?: (string | null) | undefined, preferredCardName: string, secondaryCardName?: string | undefined, meta: {otpPhoneNumber: {dialCode: number, phoneNumber: string}, id: string, email?: (string | null) | undefined}, cardDesign?: string | undefined, topUpWallet?: ({currency?: 'USDC' | undefined}) | undefined, programType?: ('commercial' | 'retail') | undefined, scheme?: 'visa' | undefined}}; export type CreateApiCardsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCardsResponse = z.object({id: z.string()}); 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://sandbox.api.caas.reap.global", 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['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${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()) };