import {z} from 'zod' export type GetApiApiV1AnalyticsSystemArgs = {period?: ('24h' | '7d' | '30d') | undefined}; export type GetApiApiV1AnalyticsSystemOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1AnalyticsSystemResponse = z.object({type: z.string(), properties: z.object({overview: z.object({type: z.string(), properties: z.object({totalOrganizations: z.object({type: z.string()}), totalUsers: z.object({type: z.string()}), totalBids: z.object({type: z.string()}), totalVolume: z.object({type: z.string()}), avgOrganizationSize: z.object({type: z.string()})})}), growth: z.object({type: z.string(), properties: z.object({newOrganizations: z.object({type: z.string()}), newUsers: z.object({type: z.string()}), growthRate: z.object({type: z.string()})})}), activity: z.object({type: z.string(), properties: z.object({activeOrganizations: z.object({type: z.string()}), dailyApiCalls: z.object({type: z.string()}), avgBidsPerOrg: z.object({type: z.string()})})})})}); 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 system-wide statistics * * GET /api/v1/analytics/system */ export const getApiApiV1AnalyticsSystem = async (args: GetApiApiV1AnalyticsSystemArgs, options?: GetApiApiV1AnalyticsSystemOptions) => { const {baseUrl = "https://xga-auction-system.workers.dev", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period != null) query.append("period", String(args.period)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/analytics/system${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(getApiApiV1AnalyticsSystemResponse, await res.json()) };