import {z} from 'zod' export type GetApiGeofenceIdArgs = {id: string, key: unknown}; export type GetApiGeofenceIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const commonStatus = z.string(); export const polygonGeojson = z.object({type: z.string().optional(), coordinates: z.array(z.array(z.number())).optional()}); export const dtoGeofencePosition = z.object({lat: z.number().optional(), lon: z.number().optional()}); export const dtoGeofence = z.object({id: z.string().optional(), name: z.string().optional(), type: z.enum(["circle", "polygon", "isochrone"]).optional(), geojson: polygonGeojson.optional(), circle_center: dtoGeofencePosition.optional(), circle_radius: z.number().optional(), ic_contours_meter: z.number().int().optional(), ic_contours_minute: z.number().int().optional(), ic_coordinates: z.string().optional(), ic_departure_time: z.number().int().optional(), ic_mode: z.number().optional(), ic_denoise: z.number().optional(), tags: z.array(z.string()).optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional(), meta_data: z.object({}).optional()}); export const dtoGetGeofenceResp = z.object({status: commonStatus.optional(), data: z.object({geofence: dtoGeofence.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 a Geofence * * GET /geofence/{id} */ export const getApiGeofenceId = async (args: GetApiGeofenceIdArgs, options?: GetApiGeofenceIdOptions) => { 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 (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/geofence/${encodeURIComponent(String(args.id))}${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(dtoGetGeofenceResp, await res.json()) };