import {z} from 'zod' export type JumpcloudHttpAssetsBatchUpdateReference = {id: string, name?: string | undefined, type?: ('user' | 'user_group' | 'location' | 'select' | 'device_group') | undefined}; export type JumpcloudHttpAssetsBatchUpdateFieldValue = {label: string, value: JumpcloudHttpAssetsBatchUpdateReference}; export type JumpcloudHttpAssetsBatchUpdateDeviceAssetsRequest = {assetIds: Array, fieldValue?: JumpcloudHttpAssetsBatchUpdateFieldValue | undefined, fields?: {[key: string]: unknown} | undefined}; export type PatchApiApiV2AssetsDevicesBatchArgs = {body: JumpcloudHttpAssetsBatchUpdateDeviceAssetsRequest}; export type PatchApiApiV2AssetsDevicesBatchOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const jumpcloudHttpAssetsBatchUpdateDeviceAssetsResponse = z.object({updatedAssetCount: z.number().int(), notUpdatedAssetCount: z.number().int()}); 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 }; /** * Batch update device assets * * PATCH /api/v2/assets/devices/batch */ export const patchApiApiV2AssetsDevicesBatch = async (args: PatchApiApiV2AssetsDevicesBatchArgs, options?: PatchApiApiV2AssetsDevicesBatchOptions) => { const {baseUrl = "https://console.jumpcloud.com", fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/assets/devices/batch${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(jumpcloudHttpAssetsBatchUpdateDeviceAssetsResponse, await res.json()) };