import {z} from 'zod' export type GetApiSnapToRoadsJsonArgs = {key: unknown, path: unknown, timestamps?: unknown | undefined, radiuses?: unknown | undefined, approaches?: ('unrestricted' | 'curb') | undefined, avoid?: ('toll' | 'ferry' | 'highway' | 'none') | undefined, geometry?: ('polyline' | 'polyline6' | 'geojson') | undefined, mode?: ('car' | 'truck') | undefined, tolerate_outlier?: boolean | undefined, option?: 'flexible' | undefined, road_info?: 'max_speed' | undefined}; export type GetApiSnapToRoadsJsonOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSnapToRoadsJsonResponse = z.object({status: z.string().optional(), msg: z.string().optional(), snappedPoints: z.array(z.object({location: z.object({latitude: z.number(), longitude: z.number()}), originalIndex: z.number().int(), distance: z.number(), name: z.string(), bearing: z.number()})).optional(), distance: z.number().int().optional(), geometry: z.array(z.string()).optional(), geojson: z.object({type: z.string().optional(), geometry: z.object({type: z.string().optional(), coordinates: z.array(z.number()).optional()}).optional(), properties: z.string().optional()}).optional(), road_info: z.object({max_speed: z.array(z.object({offset: z.number().int().optional(), length: z.number().int().optional(), value: z.number().optional()})).optional()}).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 }; /** * Snap To Road * * GET /snapToRoads/json */ export const getApiSnapToRoadsJson = async (args: GetApiSnapToRoadsJsonArgs, options?: GetApiSnapToRoadsJsonOptions) => { const {baseUrl = "https://api.nextbillion.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.key != null) query.append("key", String(args.key)) if (args.path != null) query.append("path", String(args.path)) if (args.timestamps != null) query.append("timestamps", String(args.timestamps)) if (args.radiuses != null) query.append("radiuses", String(args.radiuses)) if (args.approaches != null) query.append("approaches", String(args.approaches)) if (args.avoid != null) query.append("avoid", String(args.avoid)) if (args.geometry != null) query.append("geometry", String(args.geometry)) if (args.mode != null) query.append("mode", String(args.mode)) if (args.tolerate_outlier != null) query.append("tolerate_outlier", String(args.tolerate_outlier)) if (args.option != null) query.append("option", String(args.option)) if (args.road_info != null) query.append("road_info", String(args.road_info)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/snapToRoads/json${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(getApiSnapToRoadsJsonResponse, await res.json()) };