import {z} from 'zod' export type CountQuery = {sql: string, stream: string, stream_type: string}; export type CountSource = {mode: 'single_query', query: {good_expr: string, scope?: (string | null) | undefined, stream: string, stream_type: string}} | {mode: 'dual_query', query: {good: CountQuery, total: CountQuery}} | {mode: 'prom_ql', query: {good: string, total: string}}; export type Operator = '=' | '!=' | '>' | '>=' | '<' | '<=' | 'contains' | 'not_contains' | 'is_null' | 'is_not_null' | 'is_empty' | 'is_not_empty'; export type QueryLanguage = 'sql' | 'prom_ql'; export type Slo = {config: {source: CountSource}, sli_type: 'count', group_by?: (Array | null) | undefined, slice_interval_secs: number, window_secs: number, definition_generation?: number | undefined, description?: string | undefined, enabled?: boolean | undefined, folder_id?: string | undefined, groups_estimate?: (number | null) | undefined, groups_reserved?: number | undefined, id?: string | undefined, name: string, org?: string | undefined, owner?: (string | null) | undefined, tags?: Array | undefined, target: number} | {config: {absent_is_bad?: boolean | undefined, comparator: Operator, query: string, query_language: QueryLanguage, scope?: (string | null) | undefined, stream: string, stream_type: string, threshold: number}, sli_type: 'time_slice', group_by?: (Array | null) | undefined, slice_interval_secs: number, window_secs: number, definition_generation?: number | undefined, description?: string | undefined, enabled?: boolean | undefined, folder_id?: string | undefined, groups_estimate?: (number | null) | undefined, groups_reserved?: number | undefined, id?: string | undefined, name: string, org?: string | undefined, owner?: (string | null) | undefined, tags?: Array | undefined, target: number} | {config: {alert_id: string}, sli_type: 'alert', group_by?: (Array | null) | undefined, slice_interval_secs: number, window_secs: number, definition_generation?: number | undefined, description?: string | undefined, enabled?: boolean | undefined, folder_id?: string | undefined, groups_estimate?: (number | null) | undefined, groups_reserved?: number | undefined, id?: string | undefined, name: string, org?: string | undefined, owner?: (string | null) | undefined, tags?: Array | undefined, target: number}; export type UpdateApiApiOrgIdSlosSloIdArgs = {org_id: string, slo_id: string, body: Slo}; export type UpdateApiApiOrgIdSlosSloIdOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const httpResponse = z.object({code: z.number().int().gte(0), error_detail: z.string().nullable().optional(), hint: z.string().nullable().optional(), id: z.string().nullable().optional(), message: z.string(), name: z.string().nullable().optional(), suggestions: z.array(z.string()).nullable().optional(), trace_id: z.string().nullable().optional(), warnings: z.array(z.string()).nullable().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 an SLO * * PUT /api/{org_id}/slos/{slo_id} */ export const updateApiApiOrgIdSlosSloId = async (args: UpdateApiApiOrgIdSlosSloIdArgs, options: UpdateApiApiOrgIdSlosSloIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/slos/${encodeURIComponent(String(args.slo_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(httpResponse, await res.json()) };