import {z} from 'zod' export type DoReplyFlag = boolean | number; export type FallbackFlag = boolean | number | 'singleWifi'; export type HighConfidenceFlag = boolean; export type CustomDeviceId = string; export type Rsrp = number; export type Rsrq = number; export type Mcc = number; export type Mnc = number | string; export type Eci = number; export type Tac = number; export type Pci = number; export type Earfcn = number; export type TimingAdvance = number; export type TimeDiff = number; export type LteNeighborMeasurement = {rsrp?: Rsrp | undefined, rsrq?: Rsrq | undefined, earfcn: Earfcn, pci: Pci, eci?: Eci | undefined, timeDiff?: TimeDiff | undefined}; export type LteCell = {rsrp?: Rsrp | undefined, rsrq?: Rsrq | undefined, mcc: Mcc, mnc: Mnc, eci: Eci, tac: Tac, pci?: Pci | undefined, earfcn?: Earfcn | undefined, adv?: TimingAdvance | undefined, nmr?: Array | undefined}; export type LteCatMCell = {rsrp?: Rsrp | undefined, rsrq?: Rsrq | undefined, mcc: Mcc, mnc: Mnc, eci: Eci, tac: Tac, pci?: Pci | undefined, earfcn?: Earfcn | undefined, adv?: TimingAdvance | undefined, nmr?: Array | undefined}; export type NbIoTCell = {rsrp?: Rsrp | undefined, rsrq?: Rsrq | undefined, mcc: Mcc, mnc: Mnc, eci: Eci, tac: Tac, pci?: Pci | undefined, earfcn?: Earfcn | undefined, adv?: TimingAdvance | undefined, nmr?: Array | undefined}; export type Lac = number; export type GsmCellId = number; export type Rssi = number; export type Bsic = number; export type GsmArfcn = number; export type GsmNeighborMeasurement = {baseStationId: Bsic, arfcn: GsmArfcn, rssi?: Rssi | undefined}; export type GsmCell = {mcc: Mcc, mnc: Mnc, lac: Lac, cid: GsmCellId, rssi?: Rssi | undefined, nmr?: Array | undefined}; export type WcdmaCid = number; export type Rscp = number; export type Uarfcn = number; export type Psc = number; export type WcdmaNeighborMeasurement = {uarfcn: Uarfcn, psc: Psc, rscp?: Rscp | undefined}; export type WcdmaCell = {mcc: Mcc, mnc: Mnc, lac?: Lac | undefined, cid: WcdmaCid, rscp?: Rscp | undefined, nmr?: Array | undefined}; export type CellRequest = {lte?: Array | undefined, ltecatm?: Array | undefined, nbiot?: Array | undefined, gsm?: Array | undefined, wcdma?: Array | undefined}; export type CreateApiLocationCellArgs = {doReply?: DoReplyFlag | undefined, fallback?: FallbackFlag | undefined, hiConf?: HighConfidenceFlag | undefined, 'x-custom-device-id'?: CustomDeviceId | undefined, body: CellRequest}; export type CreateApiLocationCellOptions = {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 locationServiceTypeMCELL = z.literal("MCELL"); export const locationServiceTypeSCELL = z.literal("SCELL"); export const cellServiceType = z.union([locationServiceTypeMCELL, locationServiceTypeSCELL]); export const cellResponseWithFulfillment = z.object({lat: latitude, lon: longitude, uncertainty: uncertainty, fulfilledWith: cellServiceType}); 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 }; /** * GetLocationFromCells * * POST /location/cell * * Also accepts auth: JSON Web Token */ export const createApiLocationCell = async (args: CreateApiLocationCellArgs, options?: CreateApiLocationCellOptions) => { 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.fallback != null) query.append("fallback", String(args.fallback)) 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/cell${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(cellResponseWithFulfillment, await res.json()) };