import {z} from 'zod' export type GetApiSearchLeagueLeagueNameArgs = {leagueName: string}; export type GetApiSearchLeagueLeagueNameOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const league = z.object({idLeague: z.string().optional(), idSoccerXML: z.string().nullable().optional(), idAPIfootball: z.string().optional(), strSport: z.string().optional(), strLeague: z.string().optional(), strLeagueAlternate: z.string().optional(), intDivision: z.string().optional(), idCup: z.string().optional(), strCurrentSeason: z.string().optional(), intFormedYear: z.string().optional(), dateFirstEvent: z.string().optional(), strGender: z.enum(["Male", "Female", "Mixed"]).optional(), strCountry: z.string().optional(), strWebsite: z.string().optional(), strFacebook: z.string().optional(), strInstagram: z.string().optional(), strTwitter: z.string().optional(), strYoutube: z.string().optional(), strRSS: z.string().optional(), strDescriptionEN: z.string().optional(), strTvRights: z.string().optional(), strBanner: z.string().optional(), strBadge: z.string().optional(), strLogo: z.string().optional(), strPoster: z.string().optional(), strTrophy: z.string().optional(), strFanart1: z.string().optional(), strFanart2: z.string().optional(), strFanart3: z.string().optional(), strFanart4: z.string().optional(), strNaming: z.string().optional(), strComplete: z.enum(["yes", "no"]).optional(), strLocked: z.enum(["locked", "unlocked"]).optional()}); export const leagueSearchResponse = z.object({search: z.array(league).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 }; /** * Search sports league * * GET /search/league/{leagueName} */ export const getApiSearchLeagueLeagueName = async (args: GetApiSearchLeagueLeagueNameArgs, options?: GetApiSearchLeagueLeagueNameOptions) => { const {baseUrl = "https://www.thesportsdb.com/api/v2/json", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("X-API-KEY", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/search/league/${encodeURIComponent(String(args.leagueName))}${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(leagueSearchResponse, await res.json()) };