import {z} from 'zod' export type PageLimit = number; export type PageNextToken = string; export type GetApiFotaJobsArgs = {pageLimit?: PageLimit | undefined, pageNextToken?: PageNextToken | undefined}; export type GetApiFotaJobsOptions = {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 jobSchedule = z.object({startJobAtEpochMs: z.number().optional(), lockTargetListAtEpochMs: z.number().optional()}); export const targetListType = z.enum(["LOCKED", "UNLOCKED"]); export const fotaJobV2Plus = z.object({jobId: jobId, target: z.object({firmwareVersions: z.array(firmwareVersion).optional(), firmwareType: firmwareTypes.optional(), tags: z.array(deviceTag), deviceIds: z.array(deviceId)}), 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())}), schedule: jobSchedule.optional(), targetListType: targetListType.optional()}); export const paginatedResultFOTAV2JobOrFotaJobV2Plus = z.object({items: z.array(z.union([fOTAV2Job, fotaJobV2Plus])), total: z.number().int().gte(0).optional(), pageNextToken: z.string().optional()}); 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 }; /** * ListFOTAJobs * * GET /fota-jobs */ export const getApiFotaJobs = async (args: GetApiFotaJobsArgs, options?: GetApiFotaJobsOptions) => { 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.pageLimit != null) query.append("pageLimit", String(args.pageLimit)) if (args.pageNextToken != null) query.append("pageNextToken", String(args.pageNextToken)) if (SimpleToken !== undefined) headers.set('authorization', `Bearer ${SimpleToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/fota-jobs${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(paginatedResultFOTAV2JobOrFotaJobV2Plus, await res.json()) };