import {z} from 'zod' export type RenameCollectorRequest = {name: string}; export type PatchApiApiOrgIdFleetCollectorsIdArgs = {org_id: string, id: string, body: RenameCollectorRequest}; export type PatchApiApiOrgIdFleetCollectorsIdOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const deliveryState = z.enum(["assigned", "sent", "accepted", "running", "rejected"]); export const collectorFleet = z.object({id: z.string(), name: z.string().nullable().optional()}); export const collectorStatus = z.enum(["unknown", "online", "degraded", "offline"]); export const collector = z.object({arch: z.string().nullable().optional(), config: z.string().nullable().optional(), config_version: z.number().int().nullable().optional(), delivery: deliveryState.nullable().optional(), delivery_error: z.string().nullable().optional(), distribution: z.string().nullable().optional(), enrolled_at: z.number().int(), fleet: collectorFleet, hostname: z.string().nullable().optional(), id: z.string(), last_connected_from: z.string().nullable().optional(), last_seen: z.number().int(), name: z.string(), os: z.string().nullable().optional(), outdated: z.boolean(), status: collectorStatus, token_id: z.string().nullable().optional(), version: z.string().nullable().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 }; /** * Set a collector's nickname; an empty name falls back to the host name * * PATCH /api/{org_id}/fleet/collectors/{id} */ export const patchApiApiOrgIdFleetCollectorsId = async (args: PatchApiApiOrgIdFleetCollectorsIdArgs, options: PatchApiApiOrgIdFleetCollectorsIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/fleet/collectors/${encodeURIComponent(String(args.id))}${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(collector, await res.json()) };