import {z} from 'zod' export type DriverOrderField = 'name' | 'date_of_birth' | 'total_championship_wins' | 'total_championship_points' | 'total_race_wins' | 'total_podiums' | 'total_points' | 'total_pole_positions' | 'total_fastest_laps'; export type SortOrder = 'asc' | 'desc'; export type GetApiDriversArgs = {page?: number | undefined, limit?: number | undefined, order_by?: DriverOrderField | undefined, sort_by?: SortOrder | undefined, q?: (string | null) | undefined}; export type GetApiDriversOptions = {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 pagedResponseDriverResponse = z.object({data: z.array(driverResponse), page: z.number().int(), limit: z.number().int(), total: z.number().int(), total_pages: z.number().int(), has_next: z.boolean(), has_previous: z.boolean()}); 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 All Drivers * * GET /drivers */ export const getApiDrivers = async (args: GetApiDriversArgs, options: GetApiDriversOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.q != null) query.append("q", String(args.q)) const url = `${baseUrl.replace(/\/+$/, '')}/drivers${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(pagedResponseDriverResponse, await res.json()) };