import {z} from 'zod' export type GetApiDisputeDisputesRunsArgs = {'filter[runType]'?: string | undefined, 'filter[status]'?: ('queued' | 'dispatched' | 'done') | undefined, 'filter[resolution]'?: ('success' | 'failed' | 'cancelled') | undefined}; export type GetApiDisputeDisputesRunsOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiDisputeDisputesRunsResponse = z.object({links: z.object({self: z.string(), prev: z.string().optional(), next: z.string().optional(), first: z.string().optional(), last: z.string().optional()}).optional(), results: z.array(z.object({id: z.string(), workspaceId: z.string().optional(), runType: z.string(), status: z.enum(["queued", "dispatched", "done"]), resolution: z.enum(["success", "failed", "cancelled"]).optional(), errors: z.array(z.string()).optional(), params: z.record(z.string(), z.unknown()).optional(), summary: z.record(z.string(), z.unknown()).optional(), input: z.record(z.string(), z.unknown()).optional(), parentRunId: z.string().optional(), startedAt: z.string().optional(), completedAt: z.string().optional(), createdAt: z.string(), updatedAt: z.string(), jobs: z.array(z.object({id: z.string(), runId: z.string(), eventId: z.string().optional(), externalReference: z.string().optional(), status: z.enum(["queued", "dispatched", "done"]), resolution: z.enum(["success", "failed", "cancelled"]).optional(), errors: z.array(z.string()).optional(), summary: z.record(z.string(), z.unknown()).optional(), input: z.record(z.string(), z.unknown()).optional(), startedAt: z.string().optional(), completedAt: z.string().optional(), createdAt: 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 }; /** * List dispute service runs * * GET /dispute/disputes/runs */ export const getApiDisputeDisputesRuns = async (args: GetApiDisputeDisputesRunsArgs, options?: GetApiDisputeDisputesRunsOptions) => { const {baseUrl = "https://api.staging.payrails.io", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter[runType]'] != null) query.append("filter[runType]", String(args['filter[runType]'])) if (args['filter[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (args['filter[resolution]'] != null) query.append("filter[resolution]", String(args['filter[resolution]'])) if (BearerToken !== undefined) headers.set('authorization', `Bearer ${BearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/dispute/disputes/runs${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(getApiDisputeDisputesRunsResponse, await res.json()) };