import {z} from 'zod' export type GetApiApiV1AnalyticsOrganizationsIdArgs = {id: string, period?: ('7d' | '30d' | '90d' | '1y') | undefined}; export type GetApiApiV1AnalyticsOrganizationsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1AnalyticsOrganizationsIdResponse = z.object({type: z.string(), properties: z.object({organization: z.object({type: z.string(), properties: z.object({id: z.object({type: z.string()}), name: z.object({type: z.string()})})}), metrics: z.object({type: z.string(), properties: z.object({totalBids: z.object({type: z.string()}), successfulBids: z.object({type: z.string()}), totalVolume: z.object({type: z.string()}), avgResponseTime: z.object({type: z.string()}), apiCalls: z.object({type: z.string()}), errorRate: z.object({type: z.string()}), activeDays: z.object({type: z.string()}), memberCount: z.object({type: z.string()})})}), trends: z.object({type: z.string(), properties: z.object({dailyMetrics: z.object({type: z.string(), items: z.object({type: z.string(), properties: z.object({date: z.object({type: z.string()}), bidCount: z.object({type: z.string()}), volume: z.object({type: z.string()}), apiCalls: 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 organization metrics * * GET /api/v1/analytics/organizations/{id} */ export const getApiApiV1AnalyticsOrganizationsId = async (args: GetApiApiV1AnalyticsOrganizationsIdArgs, options?: GetApiApiV1AnalyticsOrganizationsIdOptions) => { 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/organizations/${encodeURIComponent(String(args.id))}${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(getApiApiV1AnalyticsOrganizationsIdResponse, await res.json()) };