import {z} from 'zod' export type FilesS3 = {objectStorageId: string, name: string, destination: string, sha256: string}; export type Command = {command: string, commandRunners?: Array | undefined, commandTarget?: ('enterprise' | 'organization') | undefined, targetOrganizationId?: string | undefined, commandType: string, files?: Array | undefined, launchType?: string | undefined, listensTo?: string | undefined, name: string, organization?: string | undefined, schedule?: string | undefined, scheduleRepeatType?: string | undefined, sudo?: boolean | undefined, systems?: Array | undefined, template?: string | undefined, timeout?: string | undefined, trigger?: string | undefined, user?: string | undefined, shell?: string | undefined, timeToLiveSeconds?: number | undefined, scheduleYear?: number | undefined, filesS3?: Array | undefined, description?: string | undefined, aiGenerated?: boolean | undefined, templatingRequired?: boolean | undefined, managedBy?: ('enterprise' | 'organization') | undefined}; export type CreateApiApiCommandsArgs = {'x-org-id'?: string | undefined, body: Command}; export type CreateApiApiCommandsOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const filesS3 = z.object({objectStorageId: z.string(), name: z.string(), destination: z.string(), sha256: z.string()}); export const command = z.object({command: z.string(), commandRunners: z.array(z.string()).optional(), commandTarget: z.enum(["enterprise", "organization"]).optional(), targetOrganizationId: z.string().optional(), commandType: z.string(), files: z.array(z.string()).optional(), launchType: z.string().optional(), listensTo: z.string().optional(), name: z.string(), organization: z.string().optional(), schedule: z.string().optional(), scheduleRepeatType: z.string().optional(), sudo: z.boolean().optional(), systems: z.array(z.string()).optional(), template: z.string().optional(), timeout: z.string().optional(), trigger: z.string().optional(), user: z.string().optional(), shell: z.string().optional(), timeToLiveSeconds: z.number().int().optional(), scheduleYear: z.number().int().optional(), filesS3: z.array(filesS3).optional(), description: z.string().optional(), aiGenerated: z.boolean().optional(), templatingRequired: z.boolean().optional(), managedBy: z.enum(["enterprise", "organization"]).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 }; /** * Create A Command * * POST /api/commands */ export const createApiApiCommands = async (args: CreateApiApiCommandsArgs, options?: CreateApiApiCommandsOptions) => { 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 (args['x-org-id'] != null) headers.set("x-org-id", String(args['x-org-id'])) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/commands${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(command, await res.json()) };