import {z} from 'zod' export type PageNextToken = string; export type PageLimit = number; export type GetApiFirmwaresArgs = {pageNextToken?: PageNextToken | undefined, pageLimit?: PageLimit | undefined, modemOnly?: boolean | undefined}; export type GetApiFirmwaresOptions = {baseUrl?: string | undefined, SimpleToken?: string | undefined, fetch?: typeof fetch | undefined}; export const firmwareVersion = z.string(); export const firmwareTypes = z.enum(["APP", "MODEM", "BOOT", "SOFTDEVICE", "BOOTLOADER", "MDM_FULL", "SMP", "CUSTOM"]); export const bundleId = z.string(); export const firmware = z.object({name: z.string().optional(), description: z.string().optional(), version: firmwareVersion, type: firmwareTypes, bundleId: bundleId, filenames: z.array(z.string()), size: z.number(), lastModified: z.string().optional()}); export const paginatedResultFirmware = z.object({items: z.array(firmware), 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 }; /** * ListFirmware * * GET /firmwares */ export const getApiFirmwares = async (args: GetApiFirmwaresArgs, options?: GetApiFirmwaresOptions) => { 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.pageNextToken != null) query.append("pageNextToken", String(args.pageNextToken)) if (args.pageLimit != null) query.append("pageLimit", String(args.pageLimit)) if (args.modemOnly != null) query.append("modemOnly", String(args.modemOnly)) if (SimpleToken !== undefined) headers.set('authorization', `Bearer ${SimpleToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/firmwares${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(paginatedResultFirmware, await res.json()) };