import {z} from 'zod' export type GetApiGeofenceContainArgs = {geofences?: unknown | undefined, locations: string, verbose?: string | undefined, key: unknown}; export type GetApiGeofenceContainOptions = {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 geofenceMatchRes = z.object({contain: z.boolean().optional(), location_index: z.number().int().optional()}); export const geofenceContainStatus = z.object({geofence_id: z.string().optional(), geofence_detail: dtoGeofence.optional(), result: z.array(geofenceMatchRes).optional()}); export const dtoGetContainGeofenceResp = z.object({status: commonStatus.optional(), data: z.object({result_list: z.array(geofenceContainStatus).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 }; /** * Geofence Contains * * GET /geofence/contain */ export const getApiGeofenceContain = async (args: GetApiGeofenceContainArgs, options?: GetApiGeofenceContainOptions) => { 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.geofences != null) query.append("geofences", String(args.geofences)) if (args.locations != null) query.append("locations", String(args.locations)) if (args.verbose != null) query.append("verbose", String(args.verbose)) if (args.key != null) query.append("key", String(args.key)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/geofence/contain${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(dtoGetContainGeofenceResp, await res.json()) };