import {z} from 'zod' export type KeysetSeek = 'BEFORE' | 'AFTER'; export type PaymentControllerChargeKeyset = {seek?: (KeysetSeek | null) | undefined, size?: (number | null) | undefined, chargeId?: (number | null) | undefined, created?: (string | null) | undefined}; export type GetApiUsersUserIdTransactionsArgs = {userId: number, keyset: PaymentControllerChargeKeyset}; export type GetApiUsersUserIdTransactionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const chargeType = z.enum(["ADD_CREDITS", "CREDIT_CARD_CHARGE", "REFUND", "USE_COUPON", "USE_CREDITS"]); export const paymentControllerCharge = z.object({id: z.number().int(), userId: z.number().int(), type: chargeType, description: z.string(), amount: z.number(), created: z.string()}); export const keysetSeek = z.enum(["BEFORE", "AFTER"]); export const paymentControllerChargeKeyset = z.object({seek: keysetSeek.nullable().optional(), size: z.number().int().gte(0).lte(50).nullable().optional(), chargeId: z.number().int().gte(0).nullable().optional(), created: z.string().nullable().optional()}); export const keysetPagePaymentControllerChargePaymentControllerChargeKeyset = z.object({data: z.array(paymentControllerCharge), previousKeyset: paymentControllerChargeKeyset.nullable(), nextKeyset: paymentControllerChargeKeyset.nullable()}); 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 /users/{userId}/transactions */ export const getApiUsersUserIdTransactions = async (args: GetApiUsersUserIdTransactionsArgs, options: GetApiUsersUserIdTransactionsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.keyset != null) query.append("keyset", JSON.stringify(args.keyset)) const url = `${baseUrl.replace(/\/+$/, '')}/users/${encodeURIComponent(String(args.userId))}/transactions${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(keysetPagePaymentControllerChargePaymentControllerChargeKeyset, await res.json()) };