import {z} from 'zod' export type GetApiRecommendationsEventOrchestrationsRulesArgs = {Accept: string, 'Content-Type': 'application/json', service_id?: string | undefined, 'service_ids[]'?: Array | undefined, 'team_ids[]'?: Array | undefined, 'actions[]'?: (Array<'suppress' | 'severity' | 'priority'>) | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiRecommendationsEventOrchestrationsRulesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const recommendedRule = z.object({recommendation_id: z.string().optional(), type: z.literal("service").optional(), status: z.literal("available").optional(), description: z.string().optional(), pcl_expr: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), metrics: z.object({num_events_matching_rule: z.number().int().optional(), num_events_with_action: z.number().int().optional(), num_events_with_action_matching_rule: z.number().int().optional(), precision_metric: z.number().gte(0).lte(1).optional(), recall: z.number().gte(0).lte(1).optional()}).optional(), actions: z.object({suppress: z.boolean().optional(), severity: z.enum(["critical", "high", "warning", "info", "low"]).optional(), priority: z.string().optional()}).optional(), parent: z.object({id: z.string().optional(), type: z.literal("service_reference").optional()}).optional()}); export const getApiRecommendationsEventOrchestrationsRulesResponse = z.object({recommended_rules: z.array(recommendedRule).optional(), next_cursor: z.string().nullable().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 recommended rules * * GET /recommendations/event_orchestrations/rules */ export const getApiRecommendationsEventOrchestrationsRules = async (args: GetApiRecommendationsEventOrchestrationsRulesArgs, options?: GetApiRecommendationsEventOrchestrationsRulesOptions) => { const {baseUrl = "https://api.pagerduty.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args.service_id != null) query.append("service_id", String(args.service_id)) for (const item of args['service_ids[]'] ?? []) query.append("service_ids[]", String(item)) for (const item of args['team_ids[]'] ?? []) query.append("team_ids[]", String(item)) for (const item of args['actions[]'] ?? []) query.append("actions[]", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/recommendations/event_orchestrations/rules${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(getApiRecommendationsEventOrchestrationsRulesResponse, await res.json()) };