import {z} from 'zod' export type GetApiV1DisputesArgs = {from_date?: string | undefined, to_date?: string | undefined, from_settled_date?: string | undefined, to_settled_date?: string | undefined, status?: ('needs_response' | 'under_review' | 'lost' | 'won') | undefined, page?: string | undefined, display_size?: string | undefined}; export type GetApiV1DisputesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({href: z.string().optional(), method: z.string().optional()}); export const searchNavigationLinks = z.object({first_page: link.optional(), last_page: link.optional(), next_page: link.optional(), prev_page: link.optional(), self: link.optional()}); export const disputeLinksForSearch = z.object({payment: link.optional()}); export const settlementSummary = z.object({settled_date: z.string().optional()}); export const disputeDetailForSearch = z.object({_links: disputeLinksForSearch.optional(), amount: z.number().int().optional(), created_date: z.string().optional(), dispute_id: z.string().optional(), evidence_due_date: z.string().optional(), fee: z.number().int().optional(), net_amount: z.number().int().optional(), payment_id: z.string().optional(), reason: z.string().optional(), settlement_summary: settlementSummary.optional(), status: z.string().optional()}); export const disputesSearchResults = z.object({count: z.number().int().optional(), links: searchNavigationLinks.optional(), page: z.number().int().optional(), results: z.array(disputeDetailForSearch).optional(), total: z.number().int().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 }; /** * Search disputes * * GET /v1/disputes */ export const getApiV1Disputes = async (args: GetApiV1DisputesArgs, options?: GetApiV1DisputesOptions) => { const {baseUrl = "https://publicapi.payments.service.gov.uk", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (args.from_settled_date != null) query.append("from_settled_date", String(args.from_settled_date)) if (args.to_settled_date != null) query.append("to_settled_date", String(args.to_settled_date)) if (args.status != null) query.append("status", String(args.status)) if (args.page != null) query.append("page", String(args.page)) if (args.display_size != null) query.append("display_size", String(args.display_size)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(disputesSearchResults, await res.json()) };