import {z} from 'zod' export type SLAReport = {pk?: number | undefined, url?: string | undefined, stats_url?: string | undefined, services_tags?: Array | undefined, services_selected?: Array | undefined, reporting_groups?: (Array<{id?: number | undefined, group_services: Array, created_at?: string | undefined, modified_at?: string | undefined, name: string}>) | undefined, created_at?: string | undefined, name: string, default_date_range?: ('TODAY' | 'YESTERDAY' | 'LAST_7D' | 'LAST_30D' | 'THIS_WEEK' | 'LAST_WEEK' | 'THIS_MONTH' | 'LAST_MONTH') | undefined, show_uptime_section?: boolean | undefined, show_uptime_sla?: boolean | undefined, filter_with_downtime?: boolean | undefined, filter_uptime_sla_violations?: boolean | undefined, uptime_section_sort?: ('BY_UPTIME' | 'BY_SLA') | undefined, show_response_time_section?: boolean | undefined, show_response_time_sla?: boolean | undefined, filter_slowest?: boolean | undefined, filter_response_time_sla_violations?: boolean | undefined, response_time_section_sort?: ('BY_RESPONSE_TIME' | 'BY_SLA') | undefined, logo?: (string | null) | undefined}; export type CreateApiApiV1SlaReportsArgs = {body: SLAReport}; export type CreateApiApiV1SlaReportsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const sLAReport = z.object({pk: z.number().int().optional(), url: z.string().optional(), stats_url: z.string().optional(), services_tags: z.array(z.string()).optional(), services_selected: z.array(z.string()).optional(), reporting_groups: z.array(z.object({id: z.number().int().optional(), group_services: z.array(z.string()), created_at: z.string().optional(), modified_at: z.string().optional(), name: z.string().max(100)})).optional(), created_at: z.string().optional(), name: z.string().max(100), default_date_range: z.enum(["TODAY", "YESTERDAY", "LAST_7D", "LAST_30D", "THIS_WEEK", "LAST_WEEK", "THIS_MONTH", "LAST_MONTH"]).optional(), show_uptime_section: z.boolean().optional(), show_uptime_sla: z.boolean().optional(), filter_with_downtime: z.boolean().optional(), filter_uptime_sla_violations: z.boolean().optional(), uptime_section_sort: z.enum(["BY_UPTIME", "BY_SLA"]).optional(), show_response_time_section: z.boolean().optional(), show_response_time_sla: z.boolean().optional(), filter_slowest: z.boolean().optional(), filter_response_time_sla_violations: z.boolean().optional(), response_time_section_sort: z.enum(["BY_RESPONSE_TIME", "BY_SLA"]).optional(), logo: 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 }; /** * Create a new SLA report * * POST /api/v1/sla-reports/ */ export const createApiApiV1SlaReports = async (args: CreateApiApiV1SlaReportsArgs, options: CreateApiApiV1SlaReportsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/sla-reports/${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(sLAReport, await res.json()) };