import {z} from 'zod' export type CreateApiSimulateDepositArgs = {'Idempotency-Key'?: string | undefined, body: {amount: number, deposit_account_id: string, payer_bankname?: string | undefined, payer_country?: string | undefined, payer_name?: string | undefined, reference?: string | undefined, statement_ref?: string | undefined, status?: ('pending' | 'settled' | 'rejected') | undefined}}; export type CreateApiSimulateDepositOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiSimulateDepositResponse = z.object({amount: z.number().int().gte(0), deposit_account_id: z.string(), payer_bankname: z.string().min(1).optional(), payer_country: z.string().optional(), payer_name: z.string().min(1).optional(), reference: z.string().min(1).optional(), statement_ref: z.string().min(1).optional(), status: z.enum(["pending", "settled", "rejected"]).optional(), currency: z.string(), created_at: z.string(), deposit_type: z.enum(["debit", "credit"]), fee_amount: z.number().int().gte(0).optional(), fee_currency: z.object({currency_code: z.string(), precision: z.number().int(), value: z.string().min(1)}).optional(), 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 a deposit * * POST /simulate/deposit * * Also accepts auth: OAuth2 */ export const createApiSimulateDeposit = async (args: CreateApiSimulateDepositArgs, options?: CreateApiSimulateDepositOptions) => { const {baseUrl = "https://core.sivo.com", 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['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/simulate/deposit${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(createApiSimulateDepositResponse, await res.json()) };