import {z} from 'zod' export type GetApiV2IncidentsIncidentIdArgs = {incidentId: string}; export type GetApiV2IncidentsIncidentIdOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const resolutionReason = z.enum(["intentional", "remediated", "self_resolved", "not_an_anomaly"]); export const incident = z.object({affected_resources: z.array(z.record(z.string(), z.unknown())), anomalous_days: z.record(z.string(), z.number()), attribution_tags: z.record(z.string(), z.array(z.string())), cost_impact: z.number(), cost_type: z.string(), created_at: z.string(), description: z.string(), end_date: z.string(), incident_id: z.string(), monitor_id: z.string().nullable().optional(), monitor_name: z.string(), pct_above_baseline: z.number().nullable().optional(), resolution_reason: resolutionReason.nullable().optional(), resolution_reason_detail: z.string().nullable().optional(), spend_status: z.string(), start_date: z.string(), status: z.string(), summary: z.string().max(250).nullable().optional(), title: z.string().min(1), updated_at: z.string()}); 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 incident * * GET /v2/incidents/{incidentId} */ export const getApiV2IncidentsIncidentId = async (args: GetApiV2IncidentsIncidentIdArgs, options?: GetApiV2IncidentsIncidentIdOptions) => { const {baseUrl = "https://api.cloudzero.com", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(incident, await res.json()) };