import {z} from 'zod' export type GetApiV1AuctionTransactionsPageArgs = {page: number}; export type GetApiV1AuctionTransactionsPageOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiEnchantments = z.object({levels: z.record(z.string(), z.number().int()).optional()}); export const apiTrim = z.object({material: z.string().optional(), pattern: z.string().optional()}); export const apiItemData = z.object({enchantments: apiEnchantments.optional(), trim: apiTrim.optional()}); export const apiContainerItem = z.object({count: z.number().int().optional(), display_name: z.string().optional(), enchants: apiItemData.optional(), id: z.string().optional()}); export const apiItem = z.object({contents: z.array(apiContainerItem).optional(), count: z.number().int().optional(), display_name: z.string().optional(), enchants: apiItemData.optional(), id: z.string().optional(), lore: z.array(z.string()).optional()}); export const apiSeller = z.object({name: z.string().optional(), uuid: z.string().optional()}); export const apiPurchaseItem = z.object({item: apiItem.optional(), price: z.number().optional(), seller: apiSeller.optional(), unixMillisDateSold: z.number().int().optional()}); export const apiTransactionHistoryResponse = z.object({result: z.array(apiPurchaseItem).optional(), status: z.number().int().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 }; /** * Sends all current Auction House transaction data * * GET /v1/auction/transactions/{page} */ export const getApiV1AuctionTransactionsPage = async (args: GetApiV1AuctionTransactionsPageArgs, options: GetApiV1AuctionTransactionsPageOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set("Authorization", BearerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/auction/transactions/${encodeURIComponent(String(args.page))}${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(apiTransactionHistoryResponse, await res.json()) };