import {z} from 'zod' export type IncidentSlaCreateSchema = {projectId?: string | undefined, incidentId: string, incidentSlaRuleId: string, responseDeadline?: Record | undefined, resolutionDeadline?: Record | undefined, status?: ('On Track' | 'At Risk' | 'Response Breached' | 'Resolution Breached' | 'Met') | undefined, respondedAt?: Record | undefined, resolvedAt?: Record | undefined, lastInternalNoteReminderSentAt?: Record | undefined, lastPublicNoteReminderSentAt?: Record | undefined, breachNotificationSentAt?: Record | undefined, slaStartedAt: Record}; export type CreateApiIncidentSlaArgs = {body: {data: IncidentSlaCreateSchema}}; export type CreateApiIncidentSlaOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const incidentSlaReadSchema = z.object({_id: z.string().optional(), createdAt: z.object({}).optional(), updatedAt: z.object({}).optional(), deletedAt: z.object({}).optional(), projectId: z.string().optional(), incidentId: z.string(), incidentSlaRuleId: z.string(), responseDeadline: z.object({}).optional(), resolutionDeadline: z.object({}).optional(), status: z.enum(["On Track", "At Risk", "Response Breached", "Resolution Breached", "Met"]).optional(), respondedAt: z.object({}).optional(), resolvedAt: z.object({}).optional(), lastInternalNoteReminderSentAt: z.object({}).optional(), lastPublicNoteReminderSentAt: z.object({}).optional(), breachNotificationSentAt: z.object({}).optional(), slaStartedAt: z.object({}), createdByUserId: z.string().optional()}); export const createApiIncidentSlaResponse = z.object({data: incidentSlaReadSchema.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 }; /** * Create Incident SLA * * POST /incident-sla */ export const createApiIncidentSla = async (args: CreateApiIncidentSlaArgs, options?: CreateApiIncidentSlaOptions) => { 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(/\/+$/, '')}/incident-sla${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiIncidentSlaResponse, await res.json()) };