import {z} from 'zod' export type GetApiWorkspacesWorkspaceIdAlertsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const modelsAlertError = z.object({code: z.string().optional(), message: z.string().optional()}); export const modelsAlert = z.object({errors: z.array(modelsAlertError).optional(), id: z.number().int().optional(), object_type: z.number().int().optional(), project_id: z.number().int().optional(), receiver_groups: z.string().optional(), receiver_roles: z.string().optional(), receiver_users: z.string().optional(), receivers: z.number().int().optional(), source_kind: z.string().optional(), threshold: z.number().int().optional(), threshold_type: z.string().optional(), thresholds: z.string().optional(), wid: z.number().int().optional()}); export const getApiWorkspacesWorkspaceIdAlertsResponse = z.array(modelsAlert); 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 }; /** * Alerts * * GET /workspaces/{workspace_id}/alerts */ export const getApiWorkspacesWorkspaceIdAlerts = async (options: GetApiWorkspacesWorkspaceIdAlertsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/workspaces/${encodeURIComponent(String(args.workspace_id))}/alerts${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(getApiWorkspacesWorkspaceIdAlertsResponse, await res.json()) };