import {z} from 'zod' export type DeviceBulkSelectionFilters = {limit?: number | undefined, pageIdsSelected?: Array | undefined, pagesSelected?: Array | undefined, states?: Array | undefined, hitsPerPage?: number | undefined, orgid?: number | undefined, query?: string | undefined, sort?: Record | undefined, filters?: Record | undefined} | {[key: string]: unknown}; export type CreateApiDevicesBatchStateArgs = {body: {deviceids: Array, allSelected?: boolean | undefined, useSearch?: boolean | undefined, deviceFilters?: DeviceBulkSelectionFilters | undefined, excludedDeviceIds?: Array | undefined, pagesSelected?: Array | undefined, state: string} | {deviceids?: Array | undefined, allSelected?: boolean | undefined, useSearch?: boolean | undefined, deviceFilters: DeviceBulkSelectionFilters, excludedDeviceIds?: Array | undefined, pagesSelected?: Array | undefined, state: string}}; export type CreateApiDevicesBatchStateOptions = {baseUrl?: string | undefined, apiKeyAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const successProperty = z.boolean(); export const batchJobResponse = z.object({success: successProperty.optional(), data: z.object({jobid: z.number().int().optional(), preview: z.boolean().optional(), valid_tasks: z.array(z.object({})).optional()}).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 }; /** * Change SIM state (batch) * * POST /devices/batch/state */ export const createApiDevicesBatchState = async (args: CreateApiDevicesBatchStateArgs, options?: CreateApiDevicesBatchStateOptions) => { const {baseUrl = "https://dashboard.hologram.io/api/1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKeyAuth.username}:${apiKeyAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/devices/batch/state${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(batchJobResponse, await res.json()) };