import {z} from 'zod' export type GetApiAlertsViolationsJsonArgs = {page?: number | undefined, start_date?: string | undefined, end_date?: string | undefined, only_open?: boolean | undefined}; export type GetApiAlertsViolationsJsonOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const violationEntityResponse = z.object({group_id: z.number().int().optional(), id: z.number().int().optional(), name: z.string().optional(), product: z.string().optional(), type: z.string().optional()}); export const violationLinksResponse = z.object({condition_id: z.number().int().optional(), incident_id: z.number().int().optional(), policy_id: z.number().int().optional()}); export const violationResponseType = z.object({closed_at: z.number().int().optional(), condition_name: z.string().optional(), duration: z.number().int().optional(), entity: violationEntityResponse.optional(), id: z.number().int().optional(), label: z.string().optional(), links: violationLinksResponse.optional(), opened_at: z.number().int().optional(), policy_name: z.string().optional(), priority: z.string().optional()}); export const violationResponse = z.object({violation: violationResponseType.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_violations.json */ export const getApiAlertsViolationsJson = async (args: GetApiAlertsViolationsJsonArgs, options?: GetApiAlertsViolationsJsonOptions) => { 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.page != null) query.append("page", String(args.page)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.only_open != null) query.append("only_open", String(args.only_open)) if (APIKeyHeader !== undefined) headers.set("Api-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/alerts_violations.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(violationResponse, await res.json()) };