import {z} from 'zod' export type GetApiTransactionsAmountArgs = {start_date?: string | undefined, end_date?: string | undefined, type?: ('checkout' | 'payout' | 'refund') | undefined, customer_id?: string | undefined, customer_phone?: string | undefined, device_id?: string | undefined, 'X-Amzn-Trace-Id'?: string | undefined}; export type GetApiTransactionsAmountOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionAmountObject = z.object({amount: z.number().int().optional(), currency: 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 }; /** * Returns total amount of transactions * * GET /transactions/amount */ export const getApiTransactionsAmount = async (args: GetApiTransactionsAmountArgs, options?: GetApiTransactionsAmountOptions) => { const {baseUrl = "https://api.test.bictorys.com/pay/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") 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.type != null) query.append("type", String(args.type)) if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.customer_phone != null) query.append("customer_phone", String(args.customer_phone)) if (args.device_id != null) query.append("device_id", String(args.device_id)) if (args['X-Amzn-Trace-Id'] != null) headers.set("X-Amzn-Trace-Id", String(args['X-Amzn-Trace-Id'])) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/amount${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(transactionAmountObject, await res.json()) };