import {z} from 'zod' export type Mcc = number; export type Mnc = number | string; export type Tac = number; export type Eci = number; export type Rsrp = number; export type SingleCellFormat = 'json' | 'binary'; export type CustomDeviceId = string; export type GetApiLocationSingleCellArgs = {mcc: Mcc, mnc: Mnc, tac: Tac, eci: Eci, rsrp?: Rsrp | undefined, format?: SingleCellFormat | undefined, 'x-custom-device-id'?: CustomDeviceId | undefined}; export type GetApiLocationSingleCellOptions = {baseUrl?: string | undefined, OrganizationAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const singleCellResponseBuffer = z.string(); export const latitude = z.number(); export const longitude = z.number(); export const uncertainty = z.number(); export const cellResponse = z.object({lat: latitude, lon: longitude, uncertainty: uncertainty}); export const getApiLocationSingleCellResponse = z.union([singleCellResponseBuffer, cellResponse]); 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 }; /** * GetSingleCellLocation * * GET /location/single-cell * * Also accepts auth: JSON Web Token */ export const getApiLocationSingleCell = async (args: GetApiLocationSingleCellArgs, options?: GetApiLocationSingleCellOptions) => { const {baseUrl = "https://api.nrfcloud.com/v1", fetch: fetchFn = fetch, OrganizationAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.mcc != null) query.append("mcc", String(args.mcc)) if (args.mnc != null) query.append("mnc", String(args.mnc)) if (args.tac != null) query.append("tac", String(args.tac)) if (args.eci != null) query.append("eci", String(args.eci)) if (args.rsrp != null) query.append("rsrp", String(args.rsrp)) if (args.format != null) query.append("format", String(args.format)) 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/single-cell${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(getApiLocationSingleCellResponse, await res.json()) };