import {z} from 'zod' export type CreateApiV3PayoutQuoteArgs = {body: {payout_type: string, local?: ({fund_transfer_network?: string | undefined}) | undefined, holding_info?: ({amount?: number | undefined, currency: string}) | undefined, payout_info: {amount: number, currency: string}, destination_info?: ({amount?: number | undefined, currency: string, country: string}) | undefined, local_payment_network?: ({type?: string | undefined}) | undefined, wallet?: ({type?: string | undefined}) | undefined, swift?: ({charge_type?: string | undefined}) | undefined, entity_id?: string | undefined}}; export type CreateApiV3PayoutQuoteOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiV3PayoutQuoteResponse = z.object({id: z.string().optional(), object: z.string().optional(), payout_type: z.string().optional(), holding_info: z.object({currency: z.string().optional(), amount: z.number().optional()}).optional(), payout_info: z.object({currency: z.string().optional(), amount: z.number().optional()}).optional(), destination_info: z.object({currency: z.string().optional(), amount: z.number().optional()}).optional(), fee_info: z.object({fixed: z.object({in_holding_currency: z.number().optional(), in_payout_currency: z.number().optional()}).optional(), variable: z.object({in_holding_currency: z.number().optional(), in_payout_currency: z.number().optional()}).optional()}).optional(), exchange_rates: z.object({holding_to_payout: z.number().optional(), payout_to_destination: z.number().optional()}).optional(), valid_until: z.string().optional(), status: z.string().optional(), created_at: z.string().optional(), updated_at: 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 }; /** * POST /v3/payout/quote */ export const createApiV3PayoutQuote = async (args: CreateApiV3PayoutQuoteArgs, options?: CreateApiV3PayoutQuoteOptions) => { const {baseUrl = "https://service-sandbox.tazapay.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/payout/quote${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(createApiV3PayoutQuoteResponse, await res.json()) };