import {z} from 'zod' export type GetApiDisputesArgs = {limit?: number | undefined, skip?: number | undefined, from?: string | undefined, to?: string | undefined, id?: string | undefined, entity_ids?: string | undefined, sub_entity_ids?: string | undefined, processing_channel_ids?: string | undefined, segment_ids?: string | undefined, statuses?: string | undefined, payment_id?: string | undefined, payment_reference?: string | undefined, payment_arn?: string | undefined, payment_mcc?: string | undefined, this_channel_only?: boolean | undefined}; export type GetApiDisputesOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const disputeSummary = z.object({id: z.string().optional(), entity_id: z.string().optional(), sub_entity_id: z.string().optional(), processing_channel: z.string().optional(), segment_id: z.string().optional(), category: z.enum(["fraudulent", "unrecognized", "canceled_recurring", "product_service_not_received", "not_as_described", "credit_not_issued", "duplicate", "incorrect_amount", "general"]).optional(), status: z.enum(["evidence_required", "evidence_under_review", "resolved", "won", "lost", "canceled", "expired", "accepted", "arbitration_under_review", "arbitration_won", "arbitration_lost"]).optional(), amount: z.number().optional(), currency: z.string().optional(), reason_code: z.string().optional(), payment_id: z.string().optional(), payment_action_id: z.string().optional(), payment_reference: z.string().optional(), payment_arn: z.string().optional(), payment_mcc: z.string().optional(), payment_method: z.string().optional(), evidence_required_by: z.string().optional(), received_on: z.string().optional(), last_update: z.string().optional(), resolved_reason: z.enum(["rapid_dispute_resolution", "negative_amount", "already_refunded"]).optional(), is_ce_candidate: z.boolean().optional(), _links: z.object({self: z.object({href: z.string().optional()}).optional()}).optional()}); export const disputePaged = z.object({limit: z.number().int().optional(), skip: z.number().int().optional(), from: z.string().optional(), to: z.string().optional(), id: z.string().optional(), entity_ids: z.string().optional(), sub_entity_ids: z.string().optional(), processing_channel_ids: z.string().optional(), segment_ids: z.string().optional(), statuses: z.string().optional(), payment_id: z.string().optional(), payment_reference: z.string().optional(), payment_arn: z.string().optional(), payment_mcc: z.string().optional(), this_channel_only: z.boolean().optional(), total_count: z.number().int().optional(), data: z.array(disputeSummary).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 }; /** * Get disputes * * GET /disputes * * Also accepts auth: ApiSecretKey */ export const getApiDisputes = async (args: GetApiDisputesArgs, options: GetApiDisputesOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.id != null) query.append("id", String(args.id)) if (args.entity_ids != null) query.append("entity_ids", String(args.entity_ids)) if (args.sub_entity_ids != null) query.append("sub_entity_ids", String(args.sub_entity_ids)) if (args.processing_channel_ids != null) query.append("processing_channel_ids", String(args.processing_channel_ids)) if (args.segment_ids != null) query.append("segment_ids", String(args.segment_ids)) if (args.statuses != null) query.append("statuses", String(args.statuses)) if (args.payment_id != null) query.append("payment_id", String(args.payment_id)) if (args.payment_reference != null) query.append("payment_reference", String(args.payment_reference)) if (args.payment_arn != null) query.append("payment_arn", String(args.payment_arn)) if (args.payment_mcc != null) query.append("payment_mcc", String(args.payment_mcc)) if (args.this_channel_only != null) query.append("this_channel_only", String(args.this_channel_only)) if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/disputes${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(disputePaged, await res.json()) };