import {z} from 'zod' export type GetApiRestrictionsListArgs = {transform?: boolean | undefined, source?: ('rrt' | 'pbf') | undefined, restriction_type?: ('turn' | 'parking' | 'fixedspeed' | 'maxspeed' | 'closure' | 'truck') | undefined, status?: ('active' | 'inactive') | undefined, state?: ('enabled' | 'disabled' | 'deleted') | undefined, mode?: ('0w' | '2w' | '3w' | '4w' | '6w') | undefined, area: string, name?: string | undefined, limit: number, offset: number, key: unknown}; export type GetApiRestrictionsListOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const richGroupDtoResponse = z.object({id: z.number().int().optional(), name: z.string().optional(), area: z.string().optional(), bbox: z.object({}).optional(), status: z.enum(["active", "inactive"]).optional(), repeat_on: z.string().optional(), comment: z.string().optional(), create_at: z.string().optional(), update_at: z.string().optional(), start_time: z.number().optional(), end_time: z.number().optional(), mode: z.array(z.string()).optional(), restriction_type: z.enum(["closure", "maxspeed", "fixedspeed", "parking", "turn", "truck"]).optional(), highway: z.string().optional(), speed: z.number().optional(), speed_limit: z.number().optional(), direction: z.enum(["forward", "backward", "both"]).optional(), state: z.enum(["enabled", "disabled", "deleted"]).optional(), geofence: z.object({}).optional()}); export const paginated = z.object({data: z.array(richGroupDtoResponse).optional(), meta: z.object({offset: z.number().int().optional(), limit: z.number().int().optional(), total: z.number().int().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 }; /** * Get the paginated list of restrictions * * GET /restrictions/list */ export const getApiRestrictionsList = async (args: GetApiRestrictionsListArgs, options?: GetApiRestrictionsListOptions) => { 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.transform != null) query.append("transform", String(args.transform)) 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 (args.area != null) query.append("area", String(args.area)) if (args.name != null) query.append("name", String(args.name)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.key != null) query.append("key", String(args.key)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/restrictions/list${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(paginated, await res.json()) };