import {z} from 'zod' export type GetApiConstructorsArgs = {page?: number | undefined, limit?: number | undefined}; export type GetApiConstructorsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const constructorResponse = z.object({id: z.string(), name: z.string(), full_name: z.string(), country_id: z.string(), 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_1_and_2_finishes: z.number().int(), total_race_laps: z.number().int(), total_podiums: z.number().int(), total_podium_races: 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_sprint_race_starts: z.number().int(), total_sprint_race_wins: 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(), best_sprint_race_result: z.number().int().nullable().optional()}); export const pagedResponseConstructorResponse = z.object({data: z.array(constructorResponse), 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 Constructors * * GET /constructors */ export const getApiConstructors = async (args: GetApiConstructorsArgs, options: GetApiConstructorsOptions) => { 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)) const url = `${baseUrl.replace(/\/+$/, '')}/constructors${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(pagedResponseConstructorResponse, await res.json()) };