import {z} from 'zod' export type Search = {fields?: string | undefined, filter?: Record | undefined, searchFilter?: Record | undefined}; export type CreateApiApiSearchCommandsArgs = {fields?: string | undefined, filter?: string | undefined, limit?: number | undefined, skip?: number | undefined, 'x-org-id'?: string | undefined, body: Search}; export type CreateApiApiSearchCommandsOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const commandslist = z.object({results: z.array(z.object({_id: z.string().optional(), command: z.string().optional(), commandType: z.string().optional(), launchType: z.string().optional(), listensTo: z.string().optional(), name: z.string().optional(), organization: z.string().optional(), schedule: z.string().optional(), scheduleRepeatType: z.string().optional(), trigger: z.string().optional(), description: z.string().optional(), aiGenerated: z.boolean().optional(), templatingRequired: z.boolean().optional(), managedBy: z.enum(["enterprise", "organization"]).optional()})).optional(), totalCount: z.number().int().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 }; /** * Search Commands * * POST /api/search/commands */ export const createApiApiSearchCommands = async (args: CreateApiApiSearchCommandsArgs, options?: CreateApiApiSearchCommandsOptions) => { 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.fields != null) query.append("fields", String(args.fields)) if (args.filter != null) query.append("filter", String(args.filter)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.skip != null) query.append("skip", String(args.skip)) 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/search/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(commandslist, await res.json()) };