import {z} from 'zod' export type GetApiV2PreauthTransactionsArgs = {uuid?: string | undefined, level?: string | undefined, dateStart?: string | undefined, dateEnd?: string | undefined, timezone?: string | undefined, page?: number | undefined, perPage?: number | undefined, sortBy?: string | undefined, orderBy?: string | undefined, 'Content-Type': string, authorization: string}; export type GetApiV2PreauthTransactionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const preauthTransactionResponse = z.object({transaction: z.object({id: z.string().optional(), amount: z.object({currency: z.string().optional(), amount: z.number().int().optional()}).optional(), status: z.string().optional(), userId: z.string().optional(), description: z.string().optional(), attributes: z.object({}).optional(), createdDate: z.string().optional(), expiryDate: z.string().optional(), userName: z.string().optional(), userEmail: z.string().optional(), merchantId: z.number().int().optional(), userAccountId: z.number().int().optional(), locationId: z.number().int().optional(), referenceId: z.string().optional()}).optional()}); export const getApiV2PreauthTransactionsResponse = z.object({paging: z.object({totalPages: z.number().int().optional(), totalItems: z.number().int().optional(), currentPage: z.number().int().optional(), itemsPerPage: z.number().int().optional()}).optional(), transactions: z.array(preauthTransactionResponse).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 }; /** * preauthTransactions * * GET /v2/preauthTransactions */ export const getApiV2PreauthTransactions = async (args: GetApiV2PreauthTransactionsArgs, options?: GetApiV2PreauthTransactionsOptions) => { const {baseUrl = "https://api.sandbox-pay.aero.inc", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.uuid != null) query.append("uuid", String(args.uuid)) if (args.level != null) query.append("level", String(args.level)) if (args.dateStart != null) query.append("dateStart", String(args.dateStart)) if (args.dateEnd != null) query.append("dateEnd", String(args.dateEnd)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/preauthTransactions${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(getApiV2PreauthTransactionsResponse, await res.json()) };