import {z} from 'zod' export type DeviceId = string; export type FirmwareTypes = 'APP' | 'MODEM' | 'BOOT' | 'SOFTDEVICE' | 'BOOTLOADER' | 'MDM_FULL' | 'SMP' | 'CUSTOM'; export type GetApiFotaJobExecutionsDeviceIdCurrentArgs = {deviceId: DeviceId, firmwareType?: FirmwareTypes | undefined}; export type GetApiFotaJobExecutionsDeviceIdCurrentOptions = {baseUrl?: string | undefined, SimpleToken?: string | undefined, fetch?: typeof fetch | undefined}; export const tenantId = z.string(); export const fotaV2JobExecutionEvents = z.enum(["QUEUED", "IN_PROGRESS", "FAILED", "SUCCEEDED", "TIMED_OUT", "CANCELLED", "REJECTED", "DOWNLOADING"]); export const uUID = z.string(); export const jobId = z.string(); export const firmwareTypes = z.enum(["APP", "MODEM", "BOOT", "SOFTDEVICE", "BOOTLOADER", "MDM_FULL", "SMP", "CUSTOM"]); export const firmwareVersion = z.string(); export const bundleId = z.string(); export const fotaV2JobDocumentModel = z.object({firmwareType: firmwareTypes, fileSize: z.number(), host: z.string(), path: z.string(), version: firmwareVersion, bundleId: bundleId}); export const deviceId = z.string(); export const fOTAJobExecution = z.object({tenantId: tenantId, status: fotaV2JobExecutionEvents, executionId: uUID, jobId: jobId, statusDetail: z.string().optional(), jobDocument: fotaV2JobDocumentModel, createdAt: z.string(), deviceId: deviceId, lastUpdatedAt: 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 }; /** * FetchCurrentPendingFOTAJobExecution * * GET /fota-job-executions/{deviceId}/current * * Also accepts auth: JSON Web Token */ export const getApiFotaJobExecutionsDeviceIdCurrent = async (args: GetApiFotaJobExecutionsDeviceIdCurrentArgs, options?: GetApiFotaJobExecutionsDeviceIdCurrentOptions) => { 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") if (args.firmwareType != null) query.append("firmwareType", String(args.firmwareType)) if (SimpleToken !== undefined) headers.set('authorization', `Bearer ${SimpleToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/fota-job-executions/${encodeURIComponent(String(args.deviceId))}/current${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(fOTAJobExecution, await res.json()) };