import {z} from 'zod' export type CustomDeviceId = string; export type NrfMessageType = number; export type Mcc = number; export type Mnc = number | string; export type Tac = number; export type Eci = number; export type Rsrp = number; export type FilteredEphemerisFlag = boolean; export type SatelliteMaskAngle = number; export type AssistanceRequest = {types?: Array | undefined, mcc?: Mcc | undefined, mnc?: Mnc | undefined, tac?: Tac | undefined, eci?: Eci | undefined, rsrp?: Rsrp | undefined, filtered?: FilteredEphemerisFlag | undefined, mask?: SatelliteMaskAngle | undefined}; export type CreateApiLocationAgnssArgs = {range?: string | undefined, 'x-custom-device-id'?: CustomDeviceId | undefined, body: AssistanceRequest}; export type CreateApiLocationAgnssOptions = {baseUrl?: string | undefined, OrganizationAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const assistanceResponseBuffer = z.string(); 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 }; /** * GetAssistanceData * * POST /location/agnss * * Also accepts auth: JSON Web Token */ export const createApiLocationAgnss = async (args: CreateApiLocationAgnssArgs, options?: CreateApiLocationAgnssOptions) => { 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.range != null) headers.set("range", String(args.range)) 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/agnss${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(assistanceResponseBuffer, await res.json()) };