import {z} from 'zod' export type GetApiApiV1AlertsPkArgs = {pk: string}; export type GetApiApiV1AlertsPkOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const incidents = z.object({pk: z.number().int().optional(), url: z.string().optional(), created_at: z.string(), ignore_alert_url: z.string().optional(), check_pk: z.number().int(), check_url: z.string().optional(), check_address: z.string(), check_name: z.string(), check_monitoring_service_type: z.string(), check_is_paused: z.boolean(), state_is_up: z.boolean(), ignored: z.boolean().optional(), num_locations_down: z.number().int().gte(0).lte(2147483647), all_alerts: z.array(z.object({pk: z.number().int().optional(), url: z.string().optional(), created_at: z.string().optional(), monitoring_server_name: z.string().optional(), monitoring_server_ipv4: z.string().optional(), monitoring_server_ipv6: z.string().optional(), location: z.string().optional(), output: z.string().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 }; /** * Get the details of a single alert * * GET /api/v1/alerts/{pk}/ */ export const getApiApiV1AlertsPk = async (args: GetApiApiV1AlertsPkArgs, options: GetApiApiV1AlertsPkOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/alerts/${encodeURIComponent(String(args.pk))}/${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(incidents, await res.json()) };