import {z} from 'zod' export type GetApiApplicationsApplicationIdInstancesInstanceIdMetricsDataJsonArgs = {application_id: number, instance_id: number, names: Array, values?: Array | undefined, from?: string | undefined, to?: string | undefined, period?: number | undefined, summarize?: boolean | undefined, raw?: boolean | undefined}; export type GetApiApplicationsApplicationIdInstancesInstanceIdMetricsDataJsonOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const timesliceResponse = z.object({from: z.string().optional(), to: z.string().optional(), values: z.object({}).optional()}); export const metricResponse = z.object({name: z.string().optional(), timeslices: z.array(timesliceResponse).optional()}); export const metricDataResponseType = z.object({from: z.string().optional(), metrics: z.array(metricResponse).optional(), metrics_found: z.string().optional(), metrics_not_found: z.string().optional(), to: z.string().optional()}); export const metricDataResponse = z.object({metric_data: metricDataResponseType.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 }; /** * Metric Data * * GET /applications/{application_id}/instances/{instance_id}/metrics/data.json */ export const getApiApplicationsApplicationIdInstancesInstanceIdMetricsDataJson = async (args: GetApiApplicationsApplicationIdInstancesInstanceIdMetricsDataJsonArgs, options?: GetApiApplicationsApplicationIdInstancesInstanceIdMetricsDataJsonOptions) => { const {baseUrl = "https://api.newrelic.com/v2", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.names != null) query.append("names", args.names.join(',')) if (args.values != null) query.append("values", args.values.join(',')) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.period != null) query.append("period", String(args.period)) if (args.summarize != null) query.append("summarize", String(args.summarize)) if (args.raw != null) query.append("raw", String(args.raw)) if (APIKeyHeader !== undefined) headers.set("Api-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/applications/${encodeURIComponent(String(args.application_id))}/instances/${encodeURIComponent(String(args.instance_id))}/metrics/data.json${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(metricDataResponse, await res.json()) };