import {z} from 'zod' export type GetApiEventstvPhpArgs = {d: string, a?: string | undefined, s?: string | undefined}; export type GetApiEventstvPhpOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const tvEventSchema = z.object({id: z.string().optional(), idEvent: z.string().optional(), intDivision: z.string().nullable().optional(), strSport: z.string().optional(), strEvent: z.string().optional(), strEventThumb: z.string().nullable().optional(), strEventPoster: z.string().nullable().optional(), strEventBanner: z.string().nullable().optional(), strEventSquare: z.string().nullable().optional(), idChannel: z.string().optional(), strCountry: z.string().optional(), strEventCountry: z.string().nullable().optional(), strLogo: z.string().optional(), strChannel: z.string().optional(), strSeason: z.string().optional(), strTime: z.string().optional(), dateEvent: z.string().optional(), strTimeStamp: z.string().nullable().optional()}); export const tvEventList = z.array(tvEventSchema); export const tvEventsResponse = z.object({tvevents: tvEventList.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 }; /** * Get Events by TV * * GET /eventstv.php */ export const getApiEventstvPhp = async (args: GetApiEventstvPhpArgs, options?: GetApiEventstvPhpOptions) => { const {baseUrl = "https://www.thesportsdb.com/api/v1/json/123", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.d != null) query.append("d", String(args.d)) if (args.a != null) query.append("a", String(args.a)) if (args.s != null) query.append("s", String(args.s)) const url = `${baseUrl.replace(/\/+$/, '')}/eventstv.php${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(tvEventsResponse, await res.json()) };