import {z} from 'zod' export type DeviceId = string; export type JobId = string; export type GetApiFotaJobExecutionsDeviceIdJobIdArgs = {deviceId: DeviceId, jobId: JobId}; export type GetApiFotaJobExecutionsDeviceIdJobIdOptions = {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 getApiFotaJobExecutionsDeviceIdJobIdResponse = z.union([fOTAJobExecution, z.object({jobDocument: z.object({path: z.unknown(), host: z.unknown(), version: z.unknown(), bundleId: z.literal("BundleId"), fileSize: z.unknown(), firmwareType: z.unknown()}), statusDetail: z.string(), status: fotaV2JobExecutionEvents, lastUpdatedAt: z.string(), createdAt: z.string(), deviceId: deviceId, jobId: uUID, executionId: uUID, tenantId: uUID})]); 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 }; /** * FetchFOTAJobExecution * * GET /fota-job-executions/{deviceId}/{jobId} */ export const getApiFotaJobExecutionsDeviceIdJobId = async (args: GetApiFotaJobExecutionsDeviceIdJobIdArgs, options?: GetApiFotaJobExecutionsDeviceIdJobIdOptions) => { 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 (SimpleToken !== undefined) headers.set('authorization', `Bearer ${SimpleToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/fota-job-executions/${encodeURIComponent(String(args.deviceId))}/${encodeURIComponent(String(args.jobId))}${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(getApiFotaJobExecutionsDeviceIdJobIdResponse, await res.json()) };