import {z} from 'zod' export type GetApiApiV1MaintenanceThreadsArgs = {hideSystem?: boolean | undefined}; export type GetApiApiV1MaintenanceThreadsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const errorEntity = z.object({errorCode: z.string().optional(), message: z.string().optional(), fieldName: z.string().optional()}); export const threadDescriptorView = z.object({name: z.string(), id: z.number().int(), daemon: z.boolean(), priority: z.number().int(), state: z.string(), deadlocked: z.boolean(), stackTrace: z.array(z.string()), lockedMonitors: z.array(z.string()), lockedSynchronizers: z.array(z.string()), lockInfo: z.string().optional(), lockOwnerName: z.string().optional(), lockOwnerId: z.number().int().optional()}); export const threadDumpView = z.object({serverId: z.string(), serverName: z.string().optional(), timestamp: z.string(), vmInfo: z.string(), threadCount: z.number().int(), deadlockedCount: z.number().int(), threads: z.array(threadDescriptorView)}); export const messageEntity = z.object({message: z.string().optional()}); export const pagination = z.object({currentPage: z.number().int().optional(), perPage: z.number().int().optional(), totalEntries: z.number().int().optional()}); export const responseEntityThreadDumpView = z.object({errors: z.array(errorEntity).optional(), entity: threadDumpView.optional(), messages: z.array(messageEntity).optional(), i18nMessagesMap: z.record(z.string(), z.string()).optional(), permissions: z.array(z.string()).optional(), pagination: pagination.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 }; /** * JVM thread dump * * GET /api/v1/maintenance/_threads */ export const getApiApiV1MaintenanceThreads = async (args: GetApiApiV1MaintenanceThreadsArgs, options: GetApiApiV1MaintenanceThreadsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.hideSystem != null) query.append("hideSystem", String(args.hideSystem)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/maintenance/_threads${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(responseEntityThreadDumpView, await res.json()) };