import {z} from 'zod' export type JobId = string; export type GetApiFotaJobsJobIdArgs = {jobId: JobId}; export type GetApiFotaJobsJobIdOptions = {baseUrl?: string | undefined, SimpleToken?: string | undefined, fetch?: typeof fetch | undefined}; export const jobId = z.string(); export const deviceId = z.string(); export const deviceTag = z.string(); export const fotaV2JobTargetModel = z.object({deviceIds: z.array(deviceId), tags: z.array(deviceTag)}); export const fotaV2JobEvents = z.enum(["CREATED", "IN_PROGRESS", "CANCELLED", "DELETION_IN_PROGRESS", "COMPLETED"]); export const bundleId = z.string(); export const uUID = z.string(); export const firmwareTypes = z.enum(["APP", "MODEM", "BOOT", "SOFTDEVICE", "BOOTLOADER", "MDM_FULL", "SMP", "CUSTOM"]); export const firmwareVersion = z.string(); export const fOTAV2Job = z.object({jobId: jobId, target: fotaV2JobTargetModel, description: z.string().optional(), status: fotaV2JobEvents, statusDetail: z.string().optional(), createdAt: z.string().optional(), lastUpdatedAt: z.string().optional(), completedAt: z.string().optional(), name: z.string().optional(), executionStats: z.object({completedExecutions: z.number(), executions: z.number(), devices: z.number(), timedOut: z.number(), removed: z.number(), downloading: z.number(), inProgress: z.number(), queued: z.number(), rejected: z.number(), failed: z.number(), succeeded: z.number(), cancelled: z.number()}), firmware: z.object({bundleId: z.union([bundleId, uUID]), host: z.string(), fileSize: z.number(), firmwareType: firmwareTypes, version: firmwareVersion, uris: z.array(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 }; /** * FetchFOTAJob * * GET /fota-jobs/{jobId} */ export const getApiFotaJobsJobId = async (args: GetApiFotaJobsJobIdArgs, options?: GetApiFotaJobsJobIdOptions) => { 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-jobs/${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(fOTAV2Job, await res.json()) };