import {z} from 'zod' export type GetApiV1DisputesArgs = {'X-Request-Id'?: string | undefined, limit?: number | undefined, cursor?: string | undefined, state?: ('DRAFT' | 'OPEN' | 'PENDING_EVIDENCE' | 'WON' | 'LOST') | undefined, category?: ('BANK_FEE_ERROR' | 'UNRECOGNIZED_CHARGE' | 'DUPLICATE_TRANSACTION' | 'OTHER') | undefined, date_from?: string | undefined, date_to?: string | undefined}; export type GetApiV1DisputesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const evidenceResponse = z.object({comment: z.string().optional(), disputeId: z.string().optional(), fileUrl: z.string().optional(), id: z.string().optional(), submittedAt: z.string().optional(), submittedBy: z.string().optional()}); export const disputeResponse = z.object({category: z.enum(["BANK_FEE_ERROR", "UNRECOGNIZED_CHARGE", "DUPLICATE_TRANSACTION", "OTHER"]).optional(), createdAt: z.string().optional(), description: z.string().optional(), evidence: z.array(evidenceResponse).optional(), exceptionId: z.string().optional(), id: z.string().optional(), openedBy: z.string().optional(), reopenReason: z.string().optional(), resolution: z.string().optional(), state: z.enum(["DRAFT", "OPEN", "PENDING_EVIDENCE", "WON", "LOST"]).optional(), updatedAt: z.string().optional()}); export const listDisputesResponse = z.object({items: z.array(disputeResponse).optional(), nextCursor: z.string().optional(), prevCursor: z.string().optional(), limit: z.number().int().optional(), hasMore: z.boolean().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 disputes * * GET /v1/disputes */ export const getApiV1Disputes = async (args: GetApiV1DisputesArgs, options?: GetApiV1DisputesOptions) => { const {baseUrl = "https://identity.sandbox.lerian.net", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Request-Id'] != null) headers.set("X-Request-Id", String(args['X-Request-Id'])) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.state != null) query.append("state", String(args.state)) if (args.category != null) query.append("category", String(args.category)) if (args.date_from != null) query.append("date_from", String(args.date_from)) if (args.date_to != null) query.append("date_to", String(args.date_to)) 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(listDisputesResponse, await res.json()) };