import {z} from 'zod' export type IncidentRuleQueryCondition = {normalized_query?: (string | null) | undefined, raw_query?: (string | null) | undefined}; export type IncidentRuleCondition = {field: string, values: Array}; export type IncidentRuleTriggerType = 'incident_saved_trigger' | 'incident_created_trigger' | 'incident_modified_trigger'; export type IncidentRulePatchDataAttributesRequest = {condition?: IncidentRuleQueryCondition | undefined, conditions?: Array | undefined, enabled?: boolean | undefined, task_payload?: string | undefined, trigger?: IncidentRuleTriggerType | undefined}; export type IncidentRuleType = 'incident_rules'; export type IncidentRulePatchDataRequest = {attributes?: IncidentRulePatchDataAttributesRequest | undefined, id: string, type: IncidentRuleType}; export type IncidentRulePatchRequest = {data: IncidentRulePatchDataRequest}; export type PatchApiApiV2IncidentsConfigRulesRuleIdArgs = {rule_id: string, body: IncidentRulePatchRequest}; export type PatchApiApiV2IncidentsConfigRulesRuleIdOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const incidentRuleQueryCondition = z.object({normalized_query: z.string().nullable().optional(), raw_query: z.string().nullable().optional()}); export const incidentRuleCondition = z.object({field: z.string(), values: z.array(z.string())}); export const incidentRuleDataAttributesResponse = z.object({condition: incidentRuleQueryCondition.optional(), condition_table_type: z.number().int().optional(), conditions: z.array(incidentRuleCondition).optional(), created: z.string().optional(), created_by_uuid: z.string().optional(), deleted: z.string().nullable().optional(), enabled: z.boolean().optional(), execution_type: z.number().int().optional(), incident_settings_association_uuid: z.string().nullable().optional(), match_any_condition: z.boolean().optional(), modified: z.string().optional(), modified_by_uuid: z.string().optional(), org_id: z.number().int().optional(), task_id: z.string().nullable().optional(), task_payload: z.string().nullable().optional(), trigger: z.string().optional()}); export const incidentRuleResponseType = z.literal("incidents_rules"); export const incidentRuleDataResponse = z.object({attributes: incidentRuleDataAttributesResponse, id: z.string(), type: incidentRuleResponseType}); export const incidentRuleResponse = z.object({data: incidentRuleDataResponse}); 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 }; /** * Update an incident rule * * PATCH /api/v2/incidents/config/rules/{rule_id} * * Also accepts auth: AuthZ */ export const patchApiApiV2IncidentsConfigRulesRuleId = async (args: PatchApiApiV2IncidentsConfigRulesRuleIdArgs, options?: PatchApiApiV2IncidentsConfigRulesRuleIdOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/incidents/config/rules/${encodeURIComponent(String(args.rule_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(incidentRuleResponse, await res.json()) };