import {z} from 'zod' export type GetApiRestrictionsItemsArgs = {min_lat: number, min_lon: number, max_lat: number, max_lon: number, group_id?: number | undefined, source?: string | undefined, restriction_type?: ('turn' | 'parking' | 'fixedspeed' | 'maxspeed' | 'closure' | 'truck') | undefined, status?: ('active' | 'inactive') | undefined, state?: ('enabled' | 'disabled' | 'deleted') | undefined, mode?: ('0w' | '1w' | '2w' | '3w' | '4w' | '6w') | undefined}; export type GetApiRestrictionsItemsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const restrictionDto = z.object({id: z.number(), mode: z.array(z.enum(["0w", "1w", "2w", "3w", "4w", "6w"])), area: z.string(), coordinate: z.object({lat: z.number().optional(), lon: z.number().optional()}), repeat_on: z.string(), group_id: z.number(), status: z.enum(["active", "inactive"]), state: z.enum(["enabled", "disabled", "deleted"]), group_type: z.enum(["segment", "turn"]), restriction_type: z.enum(["closure", "fixedspeed", "maxspeed", "turn", "truck"]), source: z.enum(["rrt", "pbf"])}); export const getApiRestrictionsItemsResponse = z.array(restrictionDto); 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 restriction items by bbox * * GET /restrictions_items */ export const getApiRestrictionsItems = async (args: GetApiRestrictionsItemsArgs, options?: GetApiRestrictionsItemsOptions) => { 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.min_lat != null) query.append("min_lat", String(args.min_lat)) if (args.min_lon != null) query.append("min_lon", String(args.min_lon)) if (args.max_lat != null) query.append("max_lat", String(args.max_lat)) if (args.max_lon != null) query.append("max_lon", String(args.max_lon)) if (args.group_id != null) query.append("group_id", String(args.group_id)) if (args.source != null) query.append("source", String(args.source)) if (args.restriction_type != null) query.append("restriction_type", String(args.restriction_type)) if (args.status != null) query.append("status", String(args.status)) if (args.state != null) query.append("state", String(args.state)) if (args.mode != null) query.append("mode", String(args.mode)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/restrictions_items${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(getApiRestrictionsItemsResponse, await res.json()) };