import {z} from 'zod' export type TransactionType = 'DEPOSIT' | 'WITHDRAWAL' | 'REFERRAL BONUS' | 'PROMO CODE' | 'REFUND'; export type OrderByTransaction = 'created_at' | 'amount'; export type OrderDirection = 'asc' | 'desc'; export type GetApiApiV1TransactionArgs = {filter_by?: (TransactionType | null) | undefined, order_by?: OrderByTransaction | undefined, order_direction?: OrderDirection | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiApiV1TransactionOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionType = z.enum(["DEPOSIT", "WITHDRAWAL", "REFERRAL BONUS", "PROMO CODE", "REFUND"]); export const responseTransactionDTO = z.object({amount: z.string(), currency: z.string().nullable().optional(), type: transactionType, details: z.string(), paymentProvider: z.string().optional(), id: z.string(), createdAt: z.string()}); export const pageResponseTransactionDTO = z.object({items: z.array(responseTransactionDTO), total: z.number().int().gte(0), page: z.number().int().gte(1), size: z.number().int().gte(1), pages: z.number().int().gte(0)}); 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 my transactions * * GET /api/v1/transaction */ export const getApiApiV1Transaction = async (args: GetApiApiV1TransactionArgs, options: GetApiApiV1TransactionOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter_by != null) query.append("filter_by", String(args.filter_by)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.order_direction != null) query.append("order_direction", String(args.order_direction)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/transaction${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(pageResponseTransactionDTO, await res.json()) };