import {z} from 'zod' export type DeviceId = string; export type GetApiDevicesDeviceIdArgs = {deviceId: DeviceId, transform?: Array | undefined}; export type GetApiDevicesDeviceIdOptions = {baseUrl?: string | undefined, SimpleToken?: string | undefined, fetch?: typeof fetch | undefined}; export const deviceId = z.string(); export const deviceName = z.string(); export const url = z.string(); export const deviceTag = z.string(); export const tenantId = z.string(); export const nonGatewayIPDeviceSubType = z.enum(["jitp-generic", "jitp-nordic-hardware", "unknown"]); export const deviceFirmwareArgs = z.object({supports: z.array(z.enum(["APP", "MODEM", "BOOT", "SOFTDEVICE", "BOOTLOADER", "MDM_FULL", "SMP", "CUSTOM"])).optional(), app: z.object({version: z.string().optional(), name: z.string().optional()}).optional(), boot: z.string().optional(), modem: z.string().optional(), smpDeviceAppVer: z.string().optional()}); export const deviceDocMeta = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const deviceTypesGeneric = z.literal("Generic"); export const genericDeviceState = z.object({reported: z.record(z.string(), z.unknown()).optional(), desired: z.record(z.string(), z.unknown()).optional(), delta: z.record(z.string(), z.unknown()).optional(), metadata: z.record(z.string(), z.unknown()).optional(), version: z.number().optional()}); export const genericDevice = z.object({id: deviceId, name: deviceName, image: url.optional(), tags: z.array(deviceTag), tenantId: tenantId, subType: z.union([nonGatewayIPDeviceSubType, z.string()]), firmware: deviceFirmwareArgs.optional(), $meta: deviceDocMeta, type: deviceTypesGeneric, state: genericDeviceState.optional()}); export const jSONataTransformedResponse = z.unknown(); export const getApiDevicesDeviceIdResponse = z.union([genericDevice, jSONataTransformedResponse]); 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 }; /** * FetchDevice * * GET /devices/{deviceId} */ export const getApiDevicesDeviceId = async (args: GetApiDevicesDeviceIdArgs, options?: GetApiDevicesDeviceIdOptions) => { const {baseUrl = "https://api.nrfcloud.com/v1", fetch: fetchFn = fetch, SimpleToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.transform ?? []) query.append("transform", String(item)) if (SimpleToken !== undefined) headers.set('authorization', `Bearer ${SimpleToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/devices/${encodeURIComponent(String(args.deviceId))}${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(getApiDevicesDeviceIdResponse, await res.json()) };