import {z} from 'zod' export type IncidentSeverityDTO = 'minor' | 'major' | 'critical'; export type IncidentStatusDTO = 'reported' | 'investigating' | 'fixing' | 'resolved'; export type PublicApiUpdateIncidentDTO = {description?: string | undefined, resolutionNotes?: string | undefined, severity?: IncidentSeverityDTO | undefined, status?: IncidentStatusDTO | undefined, title?: string | undefined}; export type CreateApiApiV1IncidentsIncidentIdArgs = {incidentId: string, body: PublicApiUpdateIncidentDTO}; export type CreateApiApiV1IncidentsIncidentIdOptions = {baseUrl?: string | undefined, basicAuthApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const incidentSeverityDTO = z.enum(["minor", "major", "critical"]); export const incidentStatusDTO = z.enum(["reported", "investigating", "fixing", "resolved"]); export const incidentContentDTO = z.object({description: z.string(), resolutionNotes: z.string().optional(), severity: incidentSeverityDTO, cloudUrl: z.string().optional(), id: z.string().optional(), name: z.string().optional(), number: z.number().int().optional(), status: incidentStatusDTO.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 incident * * POST /api/v1/incidents/{incidentId} * * Also accepts auth: cookieToken */ export const createApiApiV1IncidentsIncidentId = async (args: CreateApiApiV1IncidentsIncidentIdArgs, options?: CreateApiApiV1IncidentsIncidentIdOptions) => { const {baseUrl = "https://cloud.soda.io", fetch: fetchFn = fetch, basicAuthApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuthApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuthApiKey.username}:${basicAuthApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/incidents/${encodeURIComponent(String(args.incidentId))}${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(incidentContentDTO, await res.json()) };