import {z} from 'zod' export type GetApiApiLeagueStatisticsCompletionArgs = {league_ids?: Array | undefined, scouting_markets?: Array | undefined, season: number}; export type GetApiApiLeagueStatisticsCompletionOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiLeagueStatisticsCompletionResponse = z.object({completion: z.object({companies_total: z.number().int().optional(), sponsor_companies: z.number().int().optional(), sponsor_in_venue: z.number().int().optional(), tv_companies: z.number().int().optional(), radio_companies: z.number().int().optional(), social_companies: z.number().int().optional(), digital_companies: z.number().int().optional(), vendor_companies: z.number().int().optional(), sponsor_subcategories_count: z.number().int().optional()}).optional(), total_properties: 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 }; /** * Fetch per-metric data-quality completion percentages for a league scope. * * GET /api/league/statistics/completion */ export const getApiApiLeagueStatisticsCompletion = async (args: GetApiApiLeagueStatisticsCompletionArgs, options: GetApiApiLeagueStatisticsCompletionOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.league_ids != null) query.append("league_ids", args.league_ids.join(',')) if (args.scouting_markets != null) query.append("scouting_markets", args.scouting_markets.join(',')) if (args.season != null) query.append("season", String(args.season)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/league/statistics/completion${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(getApiApiLeagueStatisticsCompletionResponse, await res.json()) };