import {z} from 'zod' export type GetApiApiAttendancePropertyIdArgs = {propertyId: number}; export type GetApiApiAttendancePropertyIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const attendanceResource = z.object({id: z.number().int().optional(), property_id: z.number().int().optional(), season_id: z.number().int().optional(), season: z.string().optional(), league_name: z.string().optional(), attendance_average: z.number().int().optional(), attendance_total: z.number().int().optional(), is_added_by_api: z.boolean().optional(), stats_mapping_id: z.number().int().nullable().optional(), stats_sport: z.string().nullable().optional(), stats_sport_id: z.number().int().nullable().optional(), stats_league_id: z.number().int().nullable().optional(), stats_league_abbreviation: z.string().nullable().optional(), stats_league_name: z.string().nullable().optional(), stats_team_id: z.number().int().nullable().optional(), stats_team_name: z.string().nullable().optional(), stats_api: z.string().nullable().optional()}); export const getApiApiAttendancePropertyIdResponse = z.array(attendanceResource); 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 attendance per property by property ID * * GET /api/attendance/{propertyId} */ export const getApiApiAttendancePropertyId = async (args: GetApiApiAttendancePropertyIdArgs, options: GetApiApiAttendancePropertyIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/attendance/${encodeURIComponent(String(args.propertyId))}${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(getApiApiAttendancePropertyIdResponse, await res.json()) };