import {z} from 'zod' export type GetApiRulesetsIdRulesArgs = {Accept: string, 'Content-Type': 'application/json', limit?: number | undefined, offset?: number | undefined, total?: boolean | undefined, id: string}; export type GetApiRulesetsIdRulesOptions = {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 getApiRulesetsIdRulesResponse = z.object({offset: z.number().int().optional(), limit: z.number().int().optional(), more: z.boolean().optional(), total: z.number().int().nullable().optional(), rules: z.array(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 }; /** * List Event Rules * * GET /rulesets/{id}/rules */ export const getApiRulesetsIdRules = async (args: GetApiRulesetsIdRulesArgs, options?: GetApiRulesetsIdRulesOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.total != null) query.append("total", String(args.total)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiRulesetsIdRulesResponse, await res.json()) };