import {z} from 'zod' export type GetApiSystemInfoOptions = {baseUrl?: string | undefined, APITokens?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSystemInfoResponse = z.object({result: z.enum(["success", "error"]), action: z.literal("getSystemInfo"), data: z.object({rudder: z.object({version: z.string(), buildTime: z.string(), instanceId: z.string(), relays: z.array(z.object({uuid: z.string(), hostname: z.string(), managedNodes: z.number()}))}), system: z.object({os: z.object({name: z.string(), version: z.string()}), jvm: z.object({version: z.string(), cmd: z.string()})}), nodes: z.object({total: z.number(), audit: z.number(), enforce: z.number(), mixed: z.number(), enabled: z.number(), disabled: z.number()}), plugins: z.array(z.object({id: z.string(), name: z.string(), version: z.string(), abiVersion: z.string(), license: z.object({licensee: z.string(), startDate: z.string(), endDate: z.string(), allowedNodesNumber: z.number().optional(), supportedVersions: z.string()}).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 }; /** * Get server information * * GET /system/info */ export const getApiSystemInfo = async (options?: GetApiSystemInfoOptions) => { const {baseUrl = "https://rudder.example.com/rudder/api/latest", fetch: fetchFn = fetch, APITokens} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APITokens !== undefined) headers.set("X-API-Token", APITokens) const url = `${baseUrl.replace(/\/+$/, '')}/system/info${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(getApiSystemInfoResponse, await res.json()) };