import {z} from 'zod' export type GetApiV1DisputesEligibilityArgs = {customerUuid?: string | undefined, accountUuid?: string | undefined, transactionIds?: Array | undefined, transactionDates?: Array | undefined}; export type GetApiV1DisputesEligibilityOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dtoEligibilityErrorCode = z.enum(["ELIGIBILITY_ERROR_CODE_UNKNOWN", "RAIL_MISMATCH", "TRANSACTION_INELIGIBLE", "MEMBER_INELIGIBLE", "TRANSACTION_NOT_FOUND", "TRANSACTION_NOT_SETTLED", "TRANSACTION_ALREADY_DISPUTED", "TRANSACTION_OUTSIDE_ELIGIBILITY_WINDOW", "CARD_MISMATCH", "TRANSACTION_NOT_DISPUTABLE"]); export const dtoEligibilityError = z.object({code: dtoEligibilityErrorCode.optional(), message: z.string().optional(), transactionIds: z.array(z.string()).optional()}); export const v1CheckDisputeEligibilityResponse = z.object({eligible: z.boolean().optional(), errors: z.array(dtoEligibilityError).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 }; /** * Eligibility Check * * GET /v1/disputes/eligibility */ export const getApiV1DisputesEligibility = async (args: GetApiV1DisputesEligibilityArgs, options: GetApiV1DisputesEligibilityOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.customerUuid != null) query.append("customerUuid", String(args.customerUuid)) if (args.accountUuid != null) query.append("accountUuid", String(args.accountUuid)) for (const item of args.transactionIds ?? []) query.append("transactionIds", String(item)) for (const item of args.transactionDates ?? []) query.append("transactionDates", String(item)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/disputes/eligibility${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(v1CheckDisputeEligibilityResponse, await res.json()) };