import {z} from 'zod' export type GetApiV1MetricsArgs = {name?: string | undefined, startTime?: string | undefined, endTime?: string | undefined, pageSize?: number | undefined, skipToken?: string | undefined}; export type GetApiV1MetricsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const commonMetricInfo = z.object({name: z.string(), displayName: z.string().nullable().optional(), description: z.string().nullable().optional(), units: z.string().nullable().optional(), formula: z.string().nullable().optional(), lastReportedTime: z.string().nullable().optional()}); export const commonPageInfo = z.object({prevPage: z.string(), nextPage: z.string()}); export const getApiV1MetricsResponse = z.object({metricsInfo: z.array(commonMetricInfo), pageInfo: commonPageInfo}); 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 }; /** * List metrics * * GET /v1/metrics */ export const getApiV1Metrics = async (args: GetApiV1MetricsArgs, options?: GetApiV1MetricsOptions) => { const {baseUrl = "https://api.na-01.cloud.solarwinds.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.skipToken != null) query.append("skipToken", String(args.skipToken)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/metrics${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(getApiV1MetricsResponse, await res.json()) };