import {z} from 'zod' export type PayoutId = string; export type GetApiV2PayoutsPayoutIdArgs = {payout_id: PayoutId}; export type GetApiV2PayoutsPayoutIdOptions = {baseUrl?: string | undefined, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const payoutId = z.string().min(30).max(50); export const reference = z.string().min(1).max(50); export const accountId = z.string().min(30).max(50); export const accountLegalName = z.string().min(1).max(250); export const sellerRef = z.object({id: accountId, legal_name: accountLegalName}); export const payoutStatus = z.enum(["initiated", "movements_assigned", "transfer_initiated", "completed", "rejected"]); export const payoutReason = z.object({code: z.string(), description: z.string()}); export const amountMinor = z.number().int(); export const currency = z.string().min(3).max(3); export const createdAt = z.string().min(24).max(24); export const updatedAt = z.string().min(24).max(24); export const dateTime = z.string().min(24).max(24); export const payoutCutoff = z.object({from: dateTime, to: dateTime}); export const payout = z.object({id: payoutId, reference: reference, account: sellerRef, status: payoutStatus, reason: payoutReason.optional(), amount: amountMinor, currency: currency, created_at: createdAt, updated_at: updatedAt, cutoff: payoutCutoff}); 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 a payout * * GET /v2/payouts/{payout_id} */ export const getApiV2PayoutsPayoutId = async (args: GetApiV2PayoutsPayoutIdArgs, options?: GetApiV2PayoutsPayoutIdOptions) => { const {baseUrl = "https://api.getpaid.io", fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/payouts/${encodeURIComponent(String(args.payout_id))}${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(payout, await res.json()) };