import {z} from 'zod' export type ServiceLevelObjectiveUpdateSchema = {name?: string | undefined, description?: string | undefined, labels?: (Array<{id?: string | undefined}>) | undefined, isEnabled?: boolean | undefined, isArchived?: boolean | undefined, sliType?: ('Monitor Uptime' | 'Metric') | undefined, multiMonitorMode?: ('Any Monitor Down' | 'Monitor Seconds Average') | undefined, monitors?: (Array<{id?: string | undefined}>) | undefined, monitorLabels?: (Array<{id?: string | undefined}>) | undefined, metricQueryConfig?: Record | undefined, targetPercentage?: number | undefined, windowType?: ('Rolling' | 'Calendar Month') | undefined, windowDays?: number | undefined, timezone?: string | undefined, atRiskThresholdPercentage?: number | undefined}; export type UpdateApiServiceLevelObjectiveIdArgs = {id: string, body: {data: ServiceLevelObjectiveUpdateSchema}}; export type UpdateApiServiceLevelObjectiveIdOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceLevelObjectiveReadSchema = z.object({_id: z.string().optional(), createdAt: z.object({}).optional(), updatedAt: z.object({}).optional(), deletedAt: z.object({}).optional(), projectId: z.string().optional(), name: z.string(), description: z.string().optional(), slug: z.string().optional(), labels: z.array(z.object({id: z.string().optional()})).optional(), isEnabled: z.boolean().optional(), isArchived: z.boolean().optional(), archivedAt: z.object({}).optional(), archivedByUserId: z.string().optional(), sliType: z.enum(["Monitor Uptime", "Metric"]).optional(), multiMonitorMode: z.enum(["Any Monitor Down", "Monitor Seconds Average"]).optional(), monitors: z.array(z.object({id: z.string().optional()})).optional(), monitorLabels: z.array(z.object({id: z.string().optional()})).optional(), autoAddedMonitors: z.array(z.object({id: z.string().optional()})).optional(), downtimeMonitorStatuses: z.array(z.object({id: z.string().optional()})).optional(), metricQueryConfig: z.object({}).optional(), targetPercentage: z.number(), windowType: z.enum(["Rolling", "Calendar Month"]).optional(), windowDays: z.number().optional(), timezone: z.string().optional(), atRiskThresholdPercentage: z.number().optional(), currentSliPercentage: z.number().optional(), errorBudgetRemainingPercentage: z.number().optional(), errorBudgetRemainingSeconds: z.number().optional(), errorBudgetTotalSeconds: z.number().optional(), currentBurnRate: z.number().optional(), sloStatus: z.enum(["Healthy", "At Risk", "Budget Exhausted", "Misconfigured", "Paused"]).optional(), statusChangeNotificationSentAt: z.object({}).optional(), lastEvaluatedAt: z.object({}).optional(), nextEvaluationAt: z.object({}).optional(), lastAccumulatedBucketEndAt: z.object({}).optional(), createdByUserId: z.string().optional()}); export const updateApiServiceLevelObjectiveIdResponse = z.object({data: serviceLevelObjectiveReadSchema.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 Service Level Objective * * PUT /service-level-objective/{id} */ export const updateApiServiceLevelObjectiveId = async (args: UpdateApiServiceLevelObjectiveIdArgs, options?: UpdateApiServiceLevelObjectiveIdOptions) => { const {baseUrl = "https://oneuptime.com/api", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("APIKey", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/service-level-objective/${encodeURIComponent(String(args.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(updateApiServiceLevelObjectiveIdResponse, await res.json()) };