import {z} from 'zod' export type DeviceId = string; export type PageLimit = number; export type PageNextToken = string; export type GetApiFotaJobExecutionsDeviceIdArgs = {deviceId: DeviceId, pageLimit?: PageLimit | undefined, pageNextToken?: PageNextToken | undefined}; export type GetApiFotaJobExecutionsDeviceIdOptions = {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 paginatedResultFOTAJobExecution = z.object({items: z.array(fOTAJobExecution), 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 }; /** * ListFOTAJobExecutionsForDevice * * GET /fota-job-executions/{deviceId} */ export const getApiFotaJobExecutionsDeviceId = async (args: GetApiFotaJobExecutionsDeviceIdArgs, options?: GetApiFotaJobExecutionsDeviceIdOptions) => { 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-job-executions/${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(paginatedResultFOTAJobExecution, await res.json()) };