import {z} from 'zod' export type CreateApiV1ProjectsProjectSlugIncidentsIncidentIdArgs = {projectSlug: string, incidentId: string}; export type CreateApiV1ProjectsProjectSlugIncidentsIncidentIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const monitorMetric = z.union([z.object({kind: z.literal("count")}), z.object({kind: z.literal("errorRate")}), z.object({kind: z.literal("cacheHitRate")}), z.object({kind: z.literal("avg"), field: z.enum(["duration", "cost", "tokens"])}), z.object({kind: z.literal("sum"), field: z.enum(["duration", "cost", "tokens"])}), z.object({kind: z.literal("min"), field: z.enum(["duration", "cost", "tokens"])}), z.object({kind: z.literal("max"), field: z.enum(["duration", "cost", "tokens"])}), z.object({kind: z.literal("median"), field: z.enum(["duration", "cost", "tokens"])})]); export const alertDuration = z.union([z.object({unit: z.literal("minutes"), minutes: z.number().gt(0)}), z.object({unit: z.literal("hours"), hours: z.number().gt(0)}), z.object({unit: z.literal("days"), days: z.number().gt(0)})]); export const alertBaseline = z.object({kind: z.enum(["average", "period"]), lookback: alertDuration}); export const alertMetricThreshold = z.union([z.object({mode: z.literal("absolute"), value: z.number().gt(0)}), z.object({mode: z.literal("multiplier"), factor: z.number().gt(0), baseline: alertBaseline}), z.object({mode: z.literal("expected"), sensitivity: z.number().int().gte(1).lte(6).optional()})]); export const alertThresholdCondition = z.object({trigger: z.literal("threshold"), metric: monitorMetric, threshold: alertMetricThreshold, direction: z.enum(["above", "below"]).optional()}); export const alertEscalatingCondition = z.object({trigger: z.literal("escalating"), metric: monitorMetric, threshold: z.union([z.object({mode: z.literal("absolute"), value: z.number().gt(0)}), z.object({mode: z.literal("multiplier"), factor: z.number().gt(0), baseline: alertBaseline}), z.object({mode: z.literal("expected"), sensitivity: z.number().int().gte(1).lte(6).optional()})]).optional(), direction: z.enum(["above", "below"]).optional(), sensitivity: z.number().int().gte(1).lte(6).optional(), window: z.object({minutes: z.number().int().gte(5)})}); export const incident = z.object({id: z.string().min(24).max(24), organizationId: z.string().min(24).max(24), projectId: z.string().min(24).max(24), sourceType: z.enum(["monitor", "signal"]), sourceId: z.string().min(24).max(24), severity: z.enum(["low", "medium", "high", "urgent"]), startedAt: z.string(), endedAt: z.string().nullable(), createdAt: z.string(), condition: z.union([alertThresholdCondition, alertEscalatingCondition]).nullable()}); 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 }; /** * Resolve incident * * POST /v1/projects/{projectSlug}/incidents/{incidentId} */ export const createApiV1ProjectsProjectSlugIncidentsIncidentId = async (args: CreateApiV1ProjectsProjectSlugIncidentsIncidentIdArgs, options?: CreateApiV1ProjectsProjectSlugIncidentsIncidentIdOptions) => { const {baseUrl = "https://api.latitude.so", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectSlug))}/incidents/${encodeURIComponent(String(args.incidentId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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()) };