import {z} from 'zod' export type GetApiVerificationArgs = {accountId?: string | undefined, providerAccountId?: string | undefined, verificationType?: string | undefined}; export type GetApiVerificationOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const verificationBankTransferCode = z.object({id: z.string().optional(), type: z.enum(["BSB", "IFSC", "ROUTING_NUMBER", "SORT_CODE"]).optional()}); export const verificationAccount = z.object({accountName: z.string().optional(), accountType: z.enum(["SAVINGS", "CHECKING"]), accountNumber: z.string().min(3).max(17), bankTransferCode: verificationBankTransferCode}); export const verificationStatus = z.object({accountId: z.number().int().optional(), reason: z.enum(["DATA_NOT_AVAILABLE", "ACCOUNT_HOLDER_MISMATCH", "FULL_ACCOUNT_NUMBER_AND_BANK_TRANSFER_CODE_NOT_AVAILABLE", "FULL_ACCOUNT_NUMBER_NOT_AVAILABLE", "BANK_TRANSFER_CODE_NOT_AVAILABLE", "EXPIRED", "DATA_MISMATCH", "INSTRUCTION_GENERATION_ERROR"]).optional(), verificationStatus: z.enum(["INITIATED", "DEPOSITED", "SUCCESS", "FAILED"]).optional(), providerAccountId: z.number().int().optional(), verificationType: z.enum(["MATCHING", "CHALLENGE_DEPOSIT"]).optional(), account: verificationAccount.optional(), remainingAttempts: z.number().int().optional(), verificationDate: z.string().optional(), verificationId: z.number().int().optional()}); export const verificationStatusResponse = z.object({verification: z.array(verificationStatus).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 }; /** * Get Verification Status * * GET /verification */ export const getApiVerification = async (args: GetApiVerificationArgs, options: GetApiVerificationOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json;charset=UTF-8") if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.providerAccountId != null) query.append("providerAccountId", String(args.providerAccountId)) if (args.verificationType != null) query.append("verificationType", String(args.verificationType)) const url = `${baseUrl.replace(/\/+$/, '')}/verification${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(verificationStatusResponse, await res.json()) };