import {z} from 'zod' export type GetApiV2QueriesVolumesArgs = {df?: string | undefined, ts?: string | undefined, cursor?: string | undefined, pageSize?: number | undefined, include?: string | undefined}; export type GetApiV2QueriesVolumesOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2QueriesVolumesResponse = z.object({cursor: z.object({name: z.string().optional(), offset: z.number().int().optional(), count: z.number().int().optional(), expires: z.number().optional()}).optional(), results: z.array(z.object({name: z.string().optional(), driveLetter: z.string().optional(), label: z.string().optional(), deviceType: z.string().optional(), fileSystem: z.string().optional(), autoMount: z.boolean().optional(), compressed: z.boolean().optional(), capacity: z.number().int().optional(), freeSpace: z.number().int().optional(), serialNumber: z.string().optional(), bitLockerStatus: z.object({conversionStatus: z.enum(["FULLY_DECRYPTED", "FULLY_ENCRYPTED", "ENCRYPTION_IN_PROGRESS", "DECRYPTION_IN_PROGRESS", "ENCRYPTION_PAUSED", "DECRYPTION_PAUSED", "UNKNOWN"]).optional(), encryptionMethod: z.enum(["NONE", "AES_128_WITH_DIFFUSER", "AES_256_WITH_DIFFUSER", "AES_128", "AES_256", "HARDWARE_ENCRYPTION", "XTS_AES_128", "XTS_AES_256", "UNKNOWN"]).optional(), protectionStatus: z.enum(["UNPROTECTED", "PROTECTED", "UNKNOWN", "PENDING"]).optional(), lockStatus: z.enum(["UNKNOWN", "UNLOCKED", "LOCKED"]).optional(), initializedForProtection: z.boolean().optional()}).optional(), deviceId: z.number().int().optional(), timestamp: z.number().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 }; /** * Disk volumes report * * GET /v2/queries/volumes * * Also accepts auth: sessionKey */ export const getApiV2QueriesVolumes = async (args: GetApiV2QueriesVolumesArgs, options: GetApiV2QueriesVolumesOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.df != null) query.append("df", String(args.df)) if (args.ts != null) query.append("ts", String(args.ts)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.include != null) query.append("include", String(args.include)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/queries/volumes${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(getApiV2QueriesVolumesResponse, await res.json()) };