import {z} from 'zod' export type GetApiV3MlbProjectionsFormatStartingLineupsByDateDateArgs = {format: 'JSON' | 'XML', date: string}; export type GetApiV3MlbProjectionsFormatStartingLineupsByDateDateOptions = {baseUrl?: string | undefined, apiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const lineup = z.object({PlayerID: z.number().int().optional(), TeamID: z.number().int().optional(), Team: z.string().nullable().optional(), FirstName: z.string().nullable().optional(), LastName: z.string().nullable().optional(), BattingOrder: z.number().int().nullable().optional(), Position: z.string().nullable().optional(), Starting: z.boolean().nullable().optional(), Confirmed: z.boolean().nullable().optional()}); export const startingLineups = z.object({GameID: z.number().int().optional(), Season: z.number().int().optional(), SeasonType: z.number().int().optional(), Day: z.string().nullable().optional(), DateTime: z.string().nullable().optional(), Status: z.string().nullable().optional(), HomeTeamID: z.number().int().nullable().optional(), HomeTeam: z.string().nullable().optional(), AwayTeamID: z.number().int().nullable().optional(), AwayTeam: z.string().nullable().optional(), HomeStartingPitcher: lineup.optional(), HomeBattingLineup: z.array(lineup).optional(), AwayStartingPitcher: lineup.optional(), AwayBattingLineup: z.array(lineup).optional(), HomeTeamOpener: z.boolean().nullable().optional(), AwayTeamOpener: z.boolean().nullable().optional()}); export const getApiV3MlbProjectionsFormatStartingLineupsByDateDateResponse = z.array(startingLineups); 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 }; /** * Starting Lineups - by Date * * GET /v3/mlb/projections/{format}/StartingLineupsByDate/{date} * * Also accepts auth: apiKeyQuery */ export const getApiV3MlbProjectionsFormatStartingLineupsByDateDate = async (args: GetApiV3MlbProjectionsFormatStartingLineupsByDateDateArgs, options?: GetApiV3MlbProjectionsFormatStartingLineupsByDateDateOptions) => { const {baseUrl = "https://api.sportsdata.io", fetch: fetchFn = fetch, apiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeyHeader !== undefined) headers.set("Ocp-Apim-Subscription-Key", apiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v3/mlb/projections/${encodeURIComponent(String(args.format))}/StartingLineupsByDate/${encodeURIComponent(String(args.date))}${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(getApiV3MlbProjectionsFormatStartingLineupsByDateDateResponse, await res.json()) };