import {z} from 'zod' export type GetApiRegionBylocationLatitudeLongitudeArgs = {latitude: number, longitude: number, limit?: number | undefined, skip?: number | undefined}; export type GetApiRegionBylocationLatitudeLongitudeOptions = {baseUrl?: string | undefined, ApplicationSecret?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const coordinates = z.array(z.number()); export const coordinatesRing = z.array(coordinates); export const polygon = z.object({coordinates: z.array(coordinatesRing), type: z.literal("Polygon")}); export const point = z.object({coordinates: coordinates, type: z.literal("Point")}); export const regionModel = z.object({tags: z.array(z.string()).optional(), address: z.string().optional(), country: z.string().optional(), advancedGeometry: polygon.optional(), category: z.enum(["fence", "poi", "county", "city", "country"]), description: z.string().optional(), distance: z.number().optional(), geometry: point.optional(), icon: z.string().optional(), major: z.number().optional(), name: z.string(), referenceKey: z.string().optional(), timezone: z.string().optional(), timeZoneOffset: z.number().optional()}); export const regionList = z.object({regions: z.array(regionModel).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 }; /** * List nearest regions for this device * * GET /region/bylocation/{latitude}/{longitude} */ export const getApiRegionBylocationLatitudeLongitude = async (args: GetApiRegionBylocationLatitudeLongitudeArgs, options?: GetApiRegionBylocationLatitudeLongitudeOptions) => { const {baseUrl = "https://cloud.notifica.re/api", fetch: fetchFn = fetch, ApplicationSecret} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json; charset=utf-8") if (args.limit != null) query.append("limit", String(args.limit)) if (args.skip != null) query.append("skip", String(args.skip)) if (ApplicationSecret !== undefined) headers.set('authorization', `Basic ${btoa(`${ApplicationSecret.username}:${ApplicationSecret.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/region/bylocation/${encodeURIComponent(String(args.latitude))}/${encodeURIComponent(String(args.longitude))}${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(regionList, await res.json()) };