import {z} from 'zod' export type GetApiAlertsNrqlConditionsJsonArgs = {policy_id: number, page?: number | undefined}; export type GetApiAlertsNrqlConditionsJsonOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const nrqlResponse = z.object({query: z.string().optional(), since_value: z.string().optional()}); export const iJKTermsType = z.object({duration: z.string().optional(), operator: z.string().optional(), priority: z.string().optional(), threshold: z.string().optional(), time_function: z.string().optional()}); export const nrqlConditionResponseType = z.object({enabled: z.boolean().optional(), expected_groups: z.number().int().optional(), id: z.number().int().optional(), ignore_overlap: z.boolean().optional(), name: z.string().optional(), nrql: nrqlResponse.optional(), runbook_url: z.string().optional(), terms: z.array(iJKTermsType).optional(), type: z.string().optional(), value_function: z.string().optional()}); export const nrqlConditionResponse = z.object({nrql_condition: nrqlConditionResponseType.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 }; /** * List * * GET /alerts_nrql_conditions.json */ export const getApiAlertsNrqlConditionsJson = async (args: GetApiAlertsNrqlConditionsJsonArgs, options?: GetApiAlertsNrqlConditionsJsonOptions) => { 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.policy_id != null) query.append("policy_id", String(args.policy_id)) if (args.page != null) query.append("page", String(args.page)) if (APIKeyHeader !== undefined) headers.set("Api-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/alerts_nrql_conditions.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(nrqlConditionResponse, await res.json()) };