import {z} from 'zod' export type GetApiAlertsExternalServiceConditionsJsonArgs = {policy_id: number, page?: number | undefined}; export type GetApiAlertsExternalServiceConditionsJsonOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; 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 externalServiceConditionResponseType = z.object({enabled: z.boolean().optional(), entities: z.array(z.number().int()).optional(), external_service_url: z.string().optional(), id: z.number().int().optional(), metric: z.string().optional(), name: z.string().optional(), runbook_url: z.string().optional(), terms: z.array(iJKTermsType).optional(), type: z.string().optional()}); export const externalServiceConditionResponse = z.object({external_service_condition: externalServiceConditionResponseType.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_external_service_conditions.json */ export const getApiAlertsExternalServiceConditionsJson = async (args: GetApiAlertsExternalServiceConditionsJsonArgs, options?: GetApiAlertsExternalServiceConditionsJsonOptions) => { 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_external_service_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(externalServiceConditionResponse, await res.json()) };