import {z} from 'zod' export type DoReplyFlag = boolean | number; export type HighConfidenceFlag = boolean; export type CustomDeviceId = string; export type MacAddress = string; export type SignalStrength = number; export type WifiAge = number; export type SignalToNoiseRatio = number; export type Frequency = number; export type AccessPointWithFrequency = {macAddress: MacAddress, ssid?: string | undefined, signalStrength?: SignalStrength | undefined, age?: WifiAge | undefined, signalToNoiseRatio?: SignalToNoiseRatio | undefined, frequency?: Frequency | undefined}; export type WifiChannel = number; export type AccessPointWithChannel = {macAddress: MacAddress, ssid?: string | undefined, signalStrength?: SignalStrength | undefined, age?: WifiAge | undefined, signalToNoiseRatio?: SignalToNoiseRatio | undefined, channel?: WifiChannel | undefined}; export type AccessPoint = AccessPointWithFrequency | AccessPointWithChannel; export type WifiAccessPointsRequest = {accessPoints: Array}; export type CreateApiLocationWifiArgs = {doReply?: DoReplyFlag | undefined, hiConf?: HighConfidenceFlag | undefined, 'x-custom-device-id'?: CustomDeviceId | undefined, body: WifiAccessPointsRequest}; export type CreateApiLocationWifiOptions = {baseUrl?: string | undefined, OrganizationAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const latitude = z.number(); export const longitude = z.number(); export const uncertainty = z.number(); export const wifiResponse = z.object({lat: latitude, lon: longitude, uncertainty: uncertainty}); 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 }; /** * GetLocationFromWifiNetworks * * POST /location/wifi * * Also accepts auth: JSON Web Token */ export const createApiLocationWifi = async (args: CreateApiLocationWifiArgs, options?: CreateApiLocationWifiOptions) => { const {baseUrl = "https://api.nrfcloud.com/v1", fetch: fetchFn = fetch, OrganizationAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.doReply != null) query.append("doReply", String(args.doReply)) if (args.hiConf != null) query.append("hiConf", String(args.hiConf)) if (args['x-custom-device-id'] != null) headers.set("x-custom-device-id", String(args['x-custom-device-id'])) if (OrganizationAuthToken !== undefined) headers.set('authorization', `Bearer ${OrganizationAuthToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/location/wifi${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(wifiResponse, await res.json()) };