import {z} from 'zod' export type GetApiPayoutHoldsArgs = {Authorization: string, created_before?: string | undefined, created_after?: string | undefined, start_date_before?: string | undefined, start_date_after?: string | undefined, end_date_before?: string | undefined, end_date_after?: string | undefined}; export type GetApiPayoutHoldsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const payoutHold = z.object({id: z.string().optional(), account_id: z.string().optional(), hold_type: z.enum(["first_payment", "manual"]).optional(), issued_by: z.enum(["system", "platform"]).optional(), start_date: z.string().optional(), end_date: z.string().nullable().optional(), active: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const pageInfo = z.object({end_cursor: z.string().optional(), has_next: z.boolean().optional(), has_previous: z.boolean().optional(), start_cursor: z.string().optional()}); export const getApiPayoutHoldsResponse = z.object({id: z.number().optional(), type: z.string().optional(), data: z.array(payoutHold).optional(), page_info: pageInfo.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 }; /** * List Payout Holds * * GET /payout_holds */ export const getApiPayoutHolds = async (args: GetApiPayoutHoldsArgs, options?: GetApiPayoutHoldsOptions) => { const {baseUrl = "https://api.justifi.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.start_date_before != null) query.append("start_date_before", String(args.start_date_before)) if (args.start_date_after != null) query.append("start_date_after", String(args.start_date_after)) if (args.end_date_before != null) query.append("end_date_before", String(args.end_date_before)) if (args.end_date_after != null) query.append("end_date_after", String(args.end_date_after)) const url = `${baseUrl.replace(/\/+$/, '')}/payout_holds${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(getApiPayoutHoldsResponse, await res.json()) };