import {z} from 'zod' export type GetApiCommandsIdResultsOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const commandresult = z.object({_id: z.string().optional(), command: z.string().optional(), files: z.array(z.string()).optional(), name: z.string().optional(), organization: z.string().optional(), managedBy: z.enum(["enterprise", "organization"]).optional(), managedByLabel: z.string().optional(), requestTime: z.string().nullable().optional(), response: z.object({data: z.object({exitCode: z.number().int().optional(), output: z.string().optional()}).optional(), error: z.string().optional(), id: z.string().optional()}).optional(), responseTime: z.string().nullable().optional(), sudo: z.boolean().optional(), system: z.string().optional(), systemId: z.string().optional(), user: z.string().optional(), workflowId: z.string().optional(), workflowInstanceId: z.string().optional()}); export const getApiCommandsIdResultsResponse = z.array(commandresult); 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 }; /** * Get results for a specific command * * GET /commands/{id}/results */ export const getApiCommandsIdResults = async (options?: GetApiCommandsIdResultsOptions) => { const {baseUrl = "https://console.jumpcloud.com/api", fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/commands/${encodeURIComponent(String(args.id))}/results${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(getApiCommandsIdResultsResponse, await res.json()) };