import {z} from 'zod' export type GetApiApiV2IncidentsConfigRulesRuleIdArgs = {rule_id: string}; export type GetApiApiV2IncidentsConfigRulesRuleIdOptions = {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 }; /** * Get an incident rule * * GET /api/v2/incidents/config/rules/{rule_id} * * Also accepts auth: AuthZ */ export const getApiApiV2IncidentsConfigRulesRuleId = async (args: GetApiApiV2IncidentsConfigRulesRuleIdArgs, options?: GetApiApiV2IncidentsConfigRulesRuleIdOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) 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()) };