import {z} from 'zod' export type CreateApiV2TransactionSearchArgs = {authorization: string, 'Content-Type': string, body: {timezone?: string | undefined, page?: string | undefined, perPage?: string | undefined, sortBy?: string | undefined, orderBy?: string | undefined, startDate?: string | undefined, endDate?: string | undefined, searchType?: ('transactionId' | 'uuid' | 'userId' | 'name' | 'phone' | 'email' | 'locationId' | 'merchantId' | 'default') | undefined, filters?: ({paymentType?: string | undefined, referenceId?: string | undefined}) | undefined}}; export type CreateApiV2TransactionSearchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2TransactionSearchResponse = z.object({transactions: z.array(z.object({id: z.string().optional(), amount: z.object({}).optional(), status: z.string().optional(), paymentType: z.string().optional(), attributes: z.array(z.unknown()).optional(), userId: z.string().optional(), title: z.string().optional(), createdDate: z.string().optional(), modifiedDate: z.string().optional(), apFee: z.string().optional(), isRtp: z.boolean().optional(), merchantId: z.number().int().optional(), locationId: z.number().int().optional(), userAccountId: z.number().int().optional(), customerName: z.string().optional(), locationName: z.string().optional(), merchantName: z.string().optional()})).optional(), paging: z.object({totalPages: z.number().int().optional(), totalItems: z.number().int().optional(), currentPage: z.number().int().optional(), itemsPerPage: z.number().int().optional()}).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 }; /** * transactionSearch * * POST /v2/transactionSearch */ export const createApiV2TransactionSearch = async (args: CreateApiV2TransactionSearchArgs, options?: CreateApiV2TransactionSearchOptions) => { const {baseUrl = "https://api.sandbox-pay.aero.inc", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.authorization != null) headers.set("authorization", String(args.authorization)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/transactionSearch${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV2TransactionSearchResponse, await res.json()) };