import {z} from 'zod' export type GetApiDriversDriverIdArgs = {driver_id: string}; export type GetApiDriversDriverIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const driverResponse = z.object({id: z.string(), name: z.string(), first_name: z.string(), last_name: z.string(), full_name: z.string(), abbreviation: z.string(), gender: z.string(), date_of_birth: z.string(), place_of_birth: z.string(), nationality_country_id: z.string(), country_of_birth_country_id: z.string(), second_nationality_country_id: z.string().nullable().optional(), permanent_number: z.string().nullable().optional(), date_of_death: z.string().nullable().optional(), total_championship_wins: z.number().int(), total_race_entries: z.number().int(), total_race_starts: z.number().int(), total_race_wins: z.number().int(), total_race_laps: z.number().int(), total_podiums: z.number().int(), total_points: z.string(), total_championship_points: z.string(), total_pole_positions: z.number().int(), total_fastest_laps: z.number().int(), total_driver_of_the_day: z.number().int(), total_grand_slams: z.number().int(), best_championship_position: z.number().int().nullable().optional(), best_starting_grid_position: z.number().int().nullable().optional(), best_race_result: z.number().int().nullable().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 Driver by ID * * GET /drivers/{driver_id} */ export const getApiDriversDriverId = async (args: GetApiDriversDriverIdArgs, options: GetApiDriversDriverIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/drivers/${encodeURIComponent(String(args.driver_id))}${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(driverResponse, await res.json()) };