import {z} from 'zod' export type GetApiApiV1SlaReportsPkStatsArgs = {pk: string, start_date?: string | undefined, end_date?: string | undefined, output?: ('json' | 'pdf' | 'xls') | undefined}; export type GetApiApiV1SlaReportsPkStatsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const sLAReportStats = z.object({start_date: z.string().optional(), end_date: z.string().optional(), output: z.enum(["json", "pdf", "xls"]).optional(), has_response_time_data: z.boolean().optional(), uses_daily_response_time_data: z.boolean().optional(), uptime: z.object({}).optional(), groups: z.array(z.object({})).optional(), response_time: z.array(z.object({})).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 }; /** * Generates the report for the specified time period * * GET /api/v1/sla-reports/{pk}/stats/ */ export const getApiApiV1SlaReportsPkStats = async (args: GetApiApiV1SlaReportsPkStatsArgs, options: GetApiApiV1SlaReportsPkStatsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.output != null) query.append("output", String(args.output)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/sla-reports/${encodeURIComponent(String(args.pk))}/stats/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(sLAReportStats, await res.json()) };