import {z} from 'zod' export type GetApiPayoutsArgs = {merchant_reference: string}; export type GetApiPayoutsOptions = {baseUrl?: string | undefined, publicApiKey?: string | undefined, privateSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPayoutsResponse = z.array(z.object({id: z.string().optional(), account_id: z.string().optional(), status: z.string().optional(), merchant_reference: z.string().optional(), purpose: z.string().optional(), country: z.string().optional(), description: z.string().optional(), amount: z.object({currency: z.string().optional(), value: z.number().int().optional()}).optional(), beneficiary: z.object({merchant_beneficiary_id: z.string().optional(), national_entity: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), legal_name: z.string().optional(), email: z.string().optional(), country: z.string().optional(), date_of_birth: z.string().optional(), document: z.object({document_type: z.string().optional(), document_number: z.string().optional()}).optional(), phone: z.object({country_code: z.string().optional(), number: z.string().optional()}).optional(), address: z.object({address_line_1: z.string().optional(), address_line_2: z.string().optional(), country: z.string().optional(), state: z.string().optional(), city: z.string().optional(), zip_code: z.string().optional()}).optional()}).optional(), metadata: z.array(z.unknown()).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), transactions: z.array(z.object({id: z.string().optional(), status: z.string().optional(), type: z.string().optional(), response_code: z.string().optional(), merchant_reference: z.string().optional(), purpose: z.string().optional(), description: z.string().optional(), amount: z.object({currency: z.string().optional(), value: z.number().int().optional()}).optional(), withdrawal_method: z.object({type: z.string().optional(), detail: z.object({bank_transfer: z.unknown(), wallet: z.object({code: z.string().optional(), email: z.unknown(), country: z.unknown(), document: z.unknown(), phone: z.unknown()}).optional()}).optional()}).optional(), provider_data: z.object({id: z.string().optional(), transaction_id: z.string().optional(), account_id: z.string().optional(), beneficiary_id: z.string().optional(), status: z.string().optional(), status_detail: z.string().optional(), raw_response: z.string().optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().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 }; /** * Retrieve Payout by Merchant Reference * * GET /payouts */ export const getApiPayouts = async (args: GetApiPayoutsArgs, options?: GetApiPayoutsOptions) => { const {baseUrl = "https://api-sandbox.y.uno/v1", fetch: fetchFn = fetch, publicApiKey, privateSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.merchant_reference != null) query.append("merchant_reference", String(args.merchant_reference)) if (publicApiKey !== undefined) headers.set("PUBLIC-API-KEY", publicApiKey) if (privateSecretKey !== undefined) headers.set("PRIVATE-SECRET-KEY", privateSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/payouts${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(getApiPayoutsResponse, await res.json()) };