import {z} from 'zod' export type IncidentImpactRelatedObject = 'incident' | 'created_by_user' | 'last_modified_by_user'; export type IncidentImpactFieldsObject = {[key: string]: unknown}; export type IncidentImpactPatchAttributes = {description?: string | undefined, end_at?: (string | null) | undefined, fields?: IncidentImpactFieldsObject | undefined, start_at?: string | undefined}; export type IncidentImpactType = 'incident_impacts'; export type IncidentImpactPatchData = {attributes?: IncidentImpactPatchAttributes | undefined, type: IncidentImpactType}; export type IncidentImpactPatchRequest = {data: IncidentImpactPatchData}; export type PatchApiApiV2IncidentsIncidentIdImpactsImpactIdArgs = {incident_id: string, impact_id: string, include?: Array | undefined, body: IncidentImpactPatchRequest}; export type PatchApiApiV2IncidentsIncidentIdImpactsImpactIdOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const incidentImpactFieldsObject = z.record(z.string(), z.unknown()); export const incidentImpactAttributes = z.object({created: z.string().optional(), description: z.string(), end_at: z.string().nullable().optional(), fields: incidentImpactFieldsObject.optional(), impact_type: z.string().optional(), modified: z.string().optional(), start_at: z.string()}); export const usersType = z.literal("users"); export const relationshipToUserData = z.object({id: z.string(), type: usersType}); export const relationshipToUser = z.object({data: relationshipToUserData}); export const incidentType = z.literal("incidents"); export const relationshipToIncidentData = z.object({id: z.string(), type: incidentType}); export const relationshipToIncident = z.object({data: relationshipToIncidentData}); export const incidentImpactRelationships = z.object({created_by_user: relationshipToUser.optional(), incident: relationshipToIncident.optional(), last_modified_by_user: relationshipToUser.optional()}); export const incidentImpactType = z.literal("incident_impacts"); export const incidentImpactResponseData = z.object({attributes: incidentImpactAttributes.optional(), id: z.string(), relationships: incidentImpactRelationships.optional(), type: incidentImpactType}); export const incidentUserAttributes = z.object({email: z.string().optional(), handle: z.string().optional(), icon: z.string().optional(), name: z.string().nullable().optional(), uuid: z.string().optional()}); export const incidentUserData = z.object({attributes: incidentUserAttributes.optional(), id: z.string().optional(), type: usersType.optional()}); export const incidentImpactResponse = z.object({data: incidentImpactResponseData, included: z.array(incidentUserData).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 }; /** * Update an incident impact * * PATCH /api/v2/incidents/{incident_id}/impacts/{impact_id} * * Also accepts auth: AuthZ */ export const patchApiApiV2IncidentsIncidentIdImpactsImpactId = async (args: PatchApiApiV2IncidentsIncidentIdImpactsImpactIdArgs, options?: PatchApiApiV2IncidentsIncidentIdImpactsImpactIdOptions) => { 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 (args.include != null) query.append("include", args.include.join(',')) 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/${encodeURIComponent(String(args.incident_id))}/impacts/${encodeURIComponent(String(args.impact_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(incidentImpactResponse, await res.json()) };