import {z} from 'zod' export type GetApiMicrodepositsMicrodepositIdArgs = {'Finix-Version'?: string | undefined}; export type GetApiMicrodepositsMicrodepositIdOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createdAt = z.string(); export const updatedAt = z.string(); export const applicationId = z.string().min(1); export const currency = z.enum(["CAD", "USD"]); export const microdeposit = z.object({id: z.string().optional(), created_at: createdAt.optional(), updated_at: updatedAt.optional(), application: applicationId.optional(), currency: currency.optional(), expires_at: z.string().optional(), failure_code: z.enum(["INVALID_BANK_ACCOUNT_ROUTING_NUMBER", "MICRODEPOSIT_RETURNED", "MICRODEPOSIT_VERIFICATION_ATTEMPTS_EXCEEDED", "MICRODEPOSIT_VERIFICATION_EXPIRED", "TOTAL_MICRODEPOSIT_ATTEMPTS_EXCEEDED_ACCOUNT_LOCKED"]).nullable().optional(), failure_message: z.string().nullable().optional(), instrument: z.string().optional(), rail: z.string().optional(), state: z.enum(["PENDING", "VERIFIED", "FAILED"]).optional(), verification_attempts: z.number().int().optional(), verification_attempts_remaining: z.number().int().optional(), _links: z.object({instrument: z.object({href: z.string().optional()}).optional(), self: z.object({href: z.string().optional()}).optional()}).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 }; /** * Fetch a Microdeposit * * GET /microdeposits/{microdeposit_id} */ export const getApiMicrodepositsMicrodepositId = async (args: GetApiMicrodepositsMicrodepositIdArgs, options?: GetApiMicrodepositsMicrodepositIdOptions) => { const {baseUrl = "https://finix.sandbox-payments-api.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Finix-Version'] != null) headers.set("Finix-Version", String(args['Finix-Version'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/microdeposits/${encodeURIComponent(String(args.microdeposit_id))}${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(microdeposit, await res.json()) };