import {z} from 'zod' export type ResolutionReason = 'intentional' | 'remediated' | 'self_resolved' | 'not_an_anomaly'; export type IncidentStatus = 'active' | 'investigating' | 'resolved'; export type UpdateIncidentStatusRequest = {resolution_reason?: (ResolutionReason | null) | undefined, resolution_reason_detail?: (string | null) | undefined, status: IncidentStatus}; export type PatchApiV2IncidentsIncidentIdArgs = {incidentId: string, body: UpdateIncidentStatusRequest}; export type PatchApiV2IncidentsIncidentIdOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const resolutionReason = z.enum(["intentional", "remediated", "self_resolved", "not_an_anomaly"]); export const incidentStatus = z.enum(["active", "investigating", "resolved"]); export const incidentStatusUpdate = z.object({incident_id: z.string(), resolution_reason: resolutionReason.nullable(), resolution_reason_detail: z.string().nullable(), status: incidentStatus}); 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 incident * * PATCH /v2/incidents/{incidentId} */ export const patchApiV2IncidentsIncidentId = async (args: PatchApiV2IncidentsIncidentIdArgs, options?: PatchApiV2IncidentsIncidentIdOptions) => { const {baseUrl = "https://api.cloudzero.com", fetch: fetchFn = fetch, APIKeyAuth} = 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("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/incidents/${encodeURIComponent(String(args.incidentId))}${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(incidentStatusUpdate, await res.json()) };