import {z} from 'zod' export type SloHistoryQuerySchema = {projectId?: (string | {_type: 'NotEqual', value: string} | {_type: 'IsNull'} | {_type: 'NotNull'} | {_type: 'Includes', value: Array}) | undefined, sloId?: (string | {_type: 'NotEqual', value: string} | {_type: 'IsNull'} | {_type: 'NotNull'} | {_type: 'Includes', value: Array}) | undefined, metricName?: (string | {_type: 'NotEqual', value: string} | {_type: 'IsNull'} | {_type: 'NotNull'} | {_type: 'Includes', value: Array} | {_type: 'EqualToOrNull', value: string} | {_type: 'Search', value: string}) | undefined, bucketStart?: (string | {_type: 'NotEqual', value: string} | {_type: 'IsNull'} | {_type: 'NotNull'} | {_type: 'GreaterThan', value: string} | {_type: 'GreaterThanOrEqual', value: string} | {_type: 'LessThan', value: string} | {_type: 'LessThanOrEqual', value: string} | {_type: 'InBetween', startValue: string, endValue: string}) | undefined, value?: (number | {_type: 'NotEqual', value: number} | {_type: 'IsNull'} | {_type: 'NotNull'} | {_type: 'GreaterThan', value: number} | {_type: 'GreaterThanOrEqual', value: number} | {_type: 'LessThan', value: number} | {_type: 'LessThanOrEqual', value: number} | {_type: 'InBetween', startValue: number, endValue: number}) | undefined, version?: (number | string | {_type: 'NotEqual', value: number | string} | {_type: 'IsNull'} | {_type: 'NotNull'} | {_type: 'GreaterThan', value: number | string} | {_type: 'GreaterThanOrEqual', value: number | string} | {_type: 'LessThan', value: number | string} | {_type: 'LessThanOrEqual', value: number | string} | {_type: 'InBetween', startValue: number | string, endValue: number | string}) | undefined, _id?: (string | {_type: 'NotEqual', value: string} | {_type: 'IsNull'} | {_type: 'NotNull'} | {_type: 'Includes', value: Array}) | undefined, createdAt?: (string | {_type: 'NotEqual', value: string} | {_type: 'IsNull'} | {_type: 'NotNull'} | {_type: 'GreaterThan', value: string} | {_type: 'GreaterThanOrEqual', value: string} | {_type: 'LessThan', value: string} | {_type: 'LessThanOrEqual', value: string} | {_type: 'InBetween', startValue: string, endValue: string}) | undefined}; export type CreateApiSloHistoryCountArgs = {body: {query?: SloHistoryQuerySchema | undefined}}; export type CreateApiSloHistoryCountOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiSloHistoryCountResponse = z.object({count: z.number().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 }; /** * Count SLO History * * POST /slo-history/count */ export const createApiSloHistoryCount = async (args: CreateApiSloHistoryCountArgs, options?: CreateApiSloHistoryCountOptions) => { 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(/\/+$/, '')}/slo-history/count${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(createApiSloHistoryCountResponse, await res.json()) };