import {z} from 'zod' export type DirectDepositSimulationRequest = {account_number: string, amount: number, company_discretionary_data?: string | undefined, company_entry_description?: string | undefined, company_identification?: string | undefined, company_name?: string | undefined, earlyPayEligible?: boolean | undefined, individual_identification_number?: string | undefined, individual_name?: string | undefined, settlement_date: string, standard_entry_class_code?: string | undefined, token?: string | undefined, type?: ('CREDIT' | 'DEBIT') | undefined}; export type CreateApiSimulationsDirectdepositsDebitArgs = {body: DirectDepositSimulationRequest}; export type CreateApiSimulationsDirectdepositsDebitOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const directDepositSimulationResponse = z.object({type: z.enum(["CREDIT", "DEBIT"]).optional(), state: z.enum(["PENDING", "APPLIED", "REVERSED", "REJECTED"]).optional(), state_reason: z.string().optional(), state_reason_code: z.string().optional(), token: z.string().optional(), user_token: z.string().optional(), amount: z.number().optional(), business_token: z.string().optional(), company_discretionary_data: z.string().optional(), company_entry_description: z.string().optional(), company_identification: z.string().optional(), company_name: z.string().optional(), created_time: z.string().optional(), direct_deposit_account_token: z.string().optional(), individual_identification_number: z.string().optional(), individual_name: z.string().optional(), last_modified_time: z.string().optional(), settlement_date: z.string().optional(), standard_entry_class_code: 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 }; /** * Simulate debit * * POST /simulations/directdeposits/debit */ export const createApiSimulationsDirectdepositsDebit = async (args: CreateApiSimulationsDirectdepositsDebitArgs, options?: CreateApiSimulationsDirectdepositsDebitOptions) => { const {baseUrl = "https://sandbox-api.marqeta.com/v3", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/simulations/directdeposits/debit${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(directDepositSimulationResponse, await res.json()) };