import {z} from 'zod' export type PredictionCount = number; export type PredictionInterval = number; export type StartGpsDay = number; export type StartGpsTime = number; export type CustomDeviceId = string; export type GetApiLocationPgnssArgs = {predictionCount?: PredictionCount | undefined, predictionIntervalMinutes?: PredictionInterval | undefined, startGpsDay?: StartGpsDay | undefined, startGpsTimeOfDaySeconds?: StartGpsTime | undefined, 'x-custom-device-id'?: CustomDeviceId | undefined}; export type GetApiLocationPgnssOptions = {baseUrl?: string | undefined, OrganizationAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const pgpsFilePath = z.string(); export const pgpsHost = z.string(); export const getApiLocationPgnssResponse = z.object({path: pgpsFilePath, host: pgpsHost}); 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 }; /** * GetPredictionData * * GET /location/pgnss * * Also accepts auth: JSON Web Token */ export const getApiLocationPgnss = async (args: GetApiLocationPgnssArgs, options?: GetApiLocationPgnssOptions) => { 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.predictionCount != null) query.append("predictionCount", String(args.predictionCount)) if (args.predictionIntervalMinutes != null) query.append("predictionIntervalMinutes", String(args.predictionIntervalMinutes)) if (args.startGpsDay != null) query.append("startGpsDay", String(args.startGpsDay)) if (args.startGpsTimeOfDaySeconds != null) query.append("startGpsTimeOfDaySeconds", String(args.startGpsTimeOfDaySeconds)) 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/pgnss${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(getApiLocationPgnssResponse, await res.json()) };