import {z} from 'zod' export type GetApiV1NotificationsTypesOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationTypeView = z.object({event_type: z.string(), category: z.string(), display_name: z.string(), description: z.string(), default_severity: z.string(), supported_channels: z.array(z.string()), recommended: z.boolean(), template_id: z.string().nullable(), threshold_based: z.boolean(), threshold_config_schema: z.record(z.string(), z.unknown()).nullable(), payload_schema: z.record(z.string(), z.unknown()).nullable(), enabled: z.boolean(), severity_override: z.string().nullable(), threshold_config: z.record(z.string(), z.unknown()).nullable(), effective_severity: z.string(), delivery_mode: z.string(), rate_limit_per_hour: z.number().int().nullable(), resolved_destination_ids: z.array(z.string()), has_destination: z.boolean()}); export const getApiV1NotificationsTypesResponse = z.array(notificationTypeView); 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 }; /** * Browse the notification catalog merged with this org's config * * GET /v1/notifications/types */ export const getApiV1NotificationsTypes = async (options: GetApiV1NotificationsTypesOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/notifications/types${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(getApiV1NotificationsTypesResponse, await res.json()) };