import {z} from 'zod' export type EventRule = {id?: string | undefined, self?: string | undefined, disabled?: boolean | undefined, conditions?: ({operator: 'and' | 'or', subconditions: Array<{operator: 'exists' | 'nexists' | 'equals' | 'nequals' | 'contains' | 'ncontains' | 'matches' | 'nmatches', parameters: {path: string, value: string, options?: Record | undefined}}>}) | undefined, time_frame?: ({active_between?: {start_time: number, end_time: number} | undefined, scheduled_weekly?: {start_time: number, duration: number, timezone: string, weekdays: Array} | undefined}) | undefined, variables?: Array<{type: 'regex', name: string, parameters: {value: string, path: string}}> | undefined, position?: number | undefined, catch_all?: boolean | undefined, actions?: ({annotate?: ({value: string} | null) | undefined, event_action?: ({value: 'trigger' | 'resolve'} | null) | undefined, extractions?: (Array<{target: string, source: string, regex: string} | {target: string, template: string}>) | undefined, priority?: ({value: string} | null) | undefined, severity?: ({value: 'info' | 'warning' | 'error' | 'critical'} | null) | undefined, suppress?: ({value: boolean, threshold_value?: number | undefined, threshold_time_unit?: ('seconds' | 'minutes' | 'hours') | undefined, threshold_time_amount?: number | undefined}) | undefined, suspend?: ({value: number} | null) | undefined, route?: ({value: string} | null) | undefined}) | undefined}; export type CreateApiRulesetsIdRulesArgs = {Accept: string, 'Content-Type': 'application/json', id: string, body: {rule: EventRule}}; export type CreateApiRulesetsIdRulesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const eventRule = z.object({id: z.string().optional(), self: z.string().optional(), disabled: z.boolean().optional(), conditions: z.object({operator: z.enum(["and", "or"]), subconditions: z.array(z.object({operator: z.enum(["exists", "nexists", "equals", "nequals", "contains", "ncontains", "matches", "nmatches"]), parameters: z.object({path: z.string(), value: z.string(), options: z.object({}).optional()})}))}).optional(), time_frame: z.object({active_between: z.object({start_time: z.number().int(), end_time: z.number().int()}).optional(), scheduled_weekly: z.object({start_time: z.number().int(), duration: z.number().int(), timezone: z.string(), weekdays: z.array(z.number().int())}).optional()}).optional(), variables: z.array(z.object({type: z.literal("regex"), name: z.string(), parameters: z.object({value: z.string(), path: z.string()})})).optional(), position: z.number().int().optional(), catch_all: z.boolean().optional(), actions: z.object({annotate: z.object({value: z.string()}).nullable().optional(), event_action: z.object({value: z.enum(["trigger", "resolve"])}).nullable().optional(), extractions: z.array(z.union([z.object({target: z.string(), source: z.string(), regex: z.string()}), z.object({target: z.string(), template: z.string()})])).optional(), priority: z.object({value: z.string()}).nullable().optional(), severity: z.object({value: z.enum(["info", "warning", "error", "critical"])}).nullable().optional(), suppress: z.object({value: z.boolean(), threshold_value: z.number().int().optional(), threshold_time_unit: z.enum(["seconds", "minutes", "hours"]).optional(), threshold_time_amount: z.number().int().optional()}).optional(), suspend: z.object({value: z.number().int()}).nullable().optional(), route: z.object({value: z.string()}).nullable().optional()}).optional()}); export const createApiRulesetsIdRulesResponse = z.object({rule: eventRule.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 }; /** * Create an Event Rule * * POST /rulesets/{id}/rules */ export const createApiRulesetsIdRules = async (args: CreateApiRulesetsIdRulesArgs, options?: CreateApiRulesetsIdRulesOptions) => { const {baseUrl = "https://api.pagerduty.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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 (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/rulesets/${encodeURIComponent(String(args.id))}/rules${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiRulesetsIdRulesResponse, await res.json()) };