import {z} from 'zod' export type Sort = {field?: {[key: string]: string} | undefined}; export type Pageable = {offset?: number | undefined, sort?: Sort | undefined, pageSize?: number | undefined, pageNumber?: number | undefined, unpaged?: boolean | undefined, paged?: boolean | undefined}; export type GetApiRestV2BatchesArgs = {param: {[key: string]: string}, status?: string | undefined, pageable: Pageable, 'api-key': string, 'refund-count'?: string | undefined, 'refund-total'?: string | undefined, 'sales-count'?: string | undefined, 'sales-total'?: string | undefined, 'net-amount'?: string | undefined, 'total-count'?: string | undefined, 'confirmation-code'?: string | undefined, settled?: string | undefined, level?: string | undefined, 'start-date'?: string | undefined, 'end-date'?: string | undefined, page?: number | undefined, 'page-size'?: number | undefined, descending?: boolean | undefined}; export type GetApiRestV2BatchesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const payload = z.object({id: z.string().optional(), status: z.enum(["OPEN", "CLOSED"]).optional(), settled: z.string().optional(), "refund-count": z.string().optional(), "refund-total": z.string().optional(), "sales-count": z.string().optional(), "sales-total": z.string().optional(), "net-amount": z.string().optional(), "total-count": z.string().optional(), "confirmation-code": z.string().optional(), "merchant-id": z.string().optional(), "terminal-id": z.string().optional(), "terminal-name": z.string().optional(), "date-opened": z.string().optional(), "switch-batch-id": z.string().optional()}); export const clearentBatchResponsePayload = z.object({batch: payload.optional(), payloadType: 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 }; /** * Get all transaction batches * * GET /rest/v2/batches */ export const getApiRestV2Batches = async (args: GetApiRestV2BatchesArgs, options: GetApiRestV2BatchesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.param != null) query.append("param", JSON.stringify(args.param)) if (args.status != null) query.append("status", String(args.status)) if (args.pageable != null) query.append("pageable", JSON.stringify(args.pageable)) if (args['api-key'] != null) headers.set("api-key", String(args['api-key'])) if (args['refund-count'] != null) query.append("refund-count", String(args['refund-count'])) if (args['refund-total'] != null) query.append("refund-total", String(args['refund-total'])) if (args['sales-count'] != null) query.append("sales-count", String(args['sales-count'])) if (args['sales-total'] != null) query.append("sales-total", String(args['sales-total'])) if (args['net-amount'] != null) query.append("net-amount", String(args['net-amount'])) if (args['total-count'] != null) query.append("total-count", String(args['total-count'])) if (args['confirmation-code'] != null) query.append("confirmation-code", String(args['confirmation-code'])) if (args.settled != null) query.append("settled", String(args.settled)) if (args.level != null) query.append("level", String(args.level)) if (args['start-date'] != null) query.append("start-date", String(args['start-date'])) if (args['end-date'] != null) query.append("end-date", String(args['end-date'])) if (args.page != null) query.append("page", String(args.page)) if (args['page-size'] != null) query.append("page-size", String(args['page-size'])) if (args.descending != null) query.append("descending", String(args.descending)) const url = `${baseUrl.replace(/\/+$/, '')}/rest/v2/batches${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(clearentBatchResponsePayload, await res.json()) };