import {z} from 'zod' export type BundleId = string; export type UUID = string; export type DeviceTag = string; export type CreateJobInputTags = {bundleId: BundleId | UUID, autoApply?: boolean | undefined, name?: string | undefined, description?: string | undefined, tags: Array}; export type DeviceId = string; export type CreateJobInputTagsAndDeviceIds = {bundleId: BundleId | UUID, autoApply?: boolean | undefined, name?: string | undefined, description?: string | undefined, tags: Array, deviceIds: Array} | {bundleId: BundleId | UUID, autoApply?: boolean | undefined, name?: string | undefined, description?: string | undefined, tags: Array, deviceIdentifiers: Array}; export type CreateJobInputDeviceIds = {bundleId: BundleId | UUID, autoApply?: boolean | undefined, name?: string | undefined, description?: string | undefined, deviceIds: Array}; export type CreateJobInputDeviceIdentifiers = {bundleId: BundleId | UUID, autoApply?: boolean | undefined, name?: string | undefined, description?: string | undefined, deviceIdentifiers: Array}; export type CreateJobInputIdOrIdentifier = CreateJobInputDeviceIds | CreateJobInputDeviceIdentifiers; export type FirmwareVersion = string; export type JobSchedule = {startJobAtEpochMs?: number | undefined, lockTargetListAtEpochMs?: number | undefined}; export type CreateJobInputCriteria = {bundleId: BundleId | UUID, autoApply?: boolean | undefined, name?: string | undefined, description?: string | undefined, tags?: Array | undefined, deviceIds?: Array | undefined, firmwareType?: string | undefined, firmwareVersions?: Array | undefined, deviceIdentifiers?: Array | undefined, schedule?: JobSchedule | undefined, targetListType?: string | undefined}; export type CreateJobInput = CreateJobInputTags | CreateJobInputTagsAndDeviceIds | CreateJobInputIdOrIdentifier | CreateJobInputCriteria; export type CreateApiFotaJobsArgs = {body: CreateJobInput}; export type CreateApiFotaJobsOptions = {baseUrl?: string | undefined, SimpleToken?: string | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string(); export const createApiFotaJobsResponse = z.object({jobId: 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 }; /** * CreateFOTAJob * * POST /fota-jobs */ export const createApiFotaJobs = async (args: CreateApiFotaJobsArgs, options?: CreateApiFotaJobsOptions) => { const {baseUrl = "https://api.nrfcloud.com/v1", fetch: fetchFn = fetch, SimpleToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (SimpleToken !== undefined) headers.set('authorization', `Bearer ${SimpleToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/fota-jobs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiFotaJobsResponse, await res.json()) };