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 IncidentRuleExecutionType = 1 | 2; export type IncidentRuleTaskIDType = 'jira-create-issue-job' | 'notify-incident-handles-job' | 'servicenow-create-incident-job' | 'slack-create-channel-job' | 'zoom-create-meeting-job' | 'google-meet-create-meeting-job' | 'workflow-automation-job' | 'ms-teams-create-meeting-job' | 'google-chat-create-space-job' | 'zoom-suppress-summarization-job' | 'ms-teams-suppress-summarization-job' | 'google-meet-suppress-summarization-job'; export type IncidentRuleTriggerType = 'incident_saved_trigger' | 'incident_created_trigger' | 'incident_modified_trigger'; export type IncidentRuleDataAttributesRequest = {condition: IncidentRuleQueryCondition, condition_table_type: number, conditions?: Array | undefined, enabled: boolean, execution_type: IncidentRuleExecutionType, incident_type_uuid?: (string | null) | undefined, match_any_condition?: boolean | undefined, task_id: IncidentRuleTaskIDType, task_payload: string, trigger?: IncidentRuleTriggerType | undefined}; export type IncidentRuleType = 'incident_rules'; export type IncidentRuleDataRequest = {attributes: IncidentRuleDataAttributesRequest, type: IncidentRuleType}; export type IncidentRuleRequest = {data: IncidentRuleDataRequest}; export type CreateApiApiV2IncidentsConfigRulesArgs = {body: IncidentRuleRequest}; export type CreateApiApiV2IncidentsConfigRulesOptions = {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 }; /** * Create an incident rule * * POST /api/v2/incidents/config/rules * * Also accepts auth: AuthZ */ export const createApiApiV2IncidentsConfigRules = async (args: CreateApiApiV2IncidentsConfigRulesArgs, options?: CreateApiApiV2IncidentsConfigRulesOptions) => { 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${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(incidentRuleResponse, await res.json()) };