import {z} from 'zod' export type IncidentImpactFieldChoice = {description?: string | undefined, display_name: string, value: string}; export type IncidentImpactFieldValueType = 'dropdown' | 'text' | 'textarray' | 'metrictag' | 'number' | 'datetime' | 'multiselect'; export type IncidentImpactFieldDataAttributesRequest = {display_name: string, field_choices?: Array | undefined, field_type: IncidentImpactFieldValueType, name: string, tag_key?: (string | null) | undefined}; export type IncidentTypeType = 'incident_types'; export type RelationshipToIncidentTypeData = {id: string, type: IncidentTypeType}; export type RelationshipToIncidentType = {data: RelationshipToIncidentTypeData}; export type IncidentImpactFieldRelationshipsRequest = {incident_type: RelationshipToIncidentType}; export type IncidentImpactFieldType = 'impact_fields'; export type IncidentImpactFieldDataRequest = {attributes: IncidentImpactFieldDataAttributesRequest, relationships: IncidentImpactFieldRelationshipsRequest, type: IncidentImpactFieldType}; export type IncidentImpactFieldRequest = {data: IncidentImpactFieldDataRequest}; export type UpdateApiApiV2IncidentsConfigImpactFieldsFieldIdArgs = {field_id: string, body: IncidentImpactFieldRequest}; export type UpdateApiApiV2IncidentsConfigImpactFieldsFieldIdOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const incidentImpactFieldChoice = z.object({description: z.string().optional(), display_name: z.string(), value: z.string()}); export const incidentImpactFieldValueType = z.enum(["dropdown", "text", "textarray", "metrictag", "number", "datetime", "multiselect"]); export const incidentImpactFieldDataAttributesResponse = z.object({display_name: z.string(), field_choices: z.array(incidentImpactFieldChoice).optional(), field_type: incidentImpactFieldValueType, name: z.string(), tag_key: z.string().nullable().optional()}); 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 incidentTypeType = z.literal("incident_types"); export const relationshipToIncidentTypeData = z.object({id: z.string(), type: incidentTypeType}); export const relationshipToIncidentType = z.object({data: relationshipToIncidentTypeData}); export const incidentImpactFieldRelationships = z.object({created_by_user: relationshipToUser.optional(), incident_type: relationshipToIncidentType.optional(), last_modified_by_user: relationshipToUser.optional()}); export const incidentImpactFieldType = z.literal("impact_fields"); export const incidentImpactFieldDataResponse = z.object({attributes: incidentImpactFieldDataAttributesResponse, id: z.string(), relationships: incidentImpactFieldRelationships.optional(), type: incidentImpactFieldType}); 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 incidentImpactFieldResponse = z.object({data: incidentImpactFieldDataResponse, 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 field * * PUT /api/v2/incidents/config/impact-fields/{field_id} * * Also accepts auth: AuthZ */ export const updateApiApiV2IncidentsConfigImpactFieldsFieldId = async (args: UpdateApiApiV2IncidentsConfigImpactFieldsFieldIdArgs, options?: UpdateApiApiV2IncidentsConfigImpactFieldsFieldIdOptions) => { 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/impact-fields/${encodeURIComponent(String(args.field_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(incidentImpactFieldResponse, await res.json()) };