import {z} from 'zod' export type RequestType = 'rtLocation' | 'rtAssistance' | 'custom'; export type NrfGpsMessageType = 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 HeadApiLocationAgpsArgs = {requestType?: RequestType | undefined, customTypes?: Array | undefined, 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 HeadApiLocationAgpsOptions = {baseUrl?: string | undefined, OrganizationAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const assistanceBuffer = z.object({}); 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 }; /** * GetFileSize * * HEAD /location/agps * * Also accepts auth: JSON Web Token */ export const headApiLocationAgps = async (args: HeadApiLocationAgpsArgs, options?: HeadApiLocationAgpsOptions) => { 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.requestType != null) query.append("requestType", String(args.requestType)) for (const item of args.customTypes ?? []) query.append("customTypes", String(item)) for (const item of args.types ?? []) query.append("types", String(item)) 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.filtered != null) query.append("filtered", String(args.filtered)) if (args.mask != null) query.append("mask", String(args.mask)) if (OrganizationAuthToken !== undefined) headers.set('authorization', `Bearer ${OrganizationAuthToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/location/agps${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "HEAD", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(assistanceBuffer, await res.json()) };