import {z} from 'zod' export type GetApiV6NotificationsArgs = {limit?: number | undefined, offset?: number | undefined, having_all_tags?: Array | undefined, event_id?: string | undefined, subscription_id?: string | undefined}; export type GetApiV6NotificationsOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const zonedDateTime = z.string(); export const notificationResponse = z.object({cause_event_id: z.string(), cause_user_id: z.string(), channels: z.array(z.enum(["EMAIL", "EXTOLE_CLIENT_SLACK", "SLACK", "THIRD_PARTY_EMAIL", "WEBHOOK"])), client_id: z.string(), client_version: z.number().int(), data: z.record(z.string(), z.string()), event_id: z.string(), event_time: zonedDateTime, level: z.enum(["ERROR", "INFO", "WARN"]), message: z.string(), name: z.string(), snooze_id: z.string(), subscription_dedupe_duration_ms: z.number().int(), subscription_id: z.string(), tags: z.array(z.string()), user_id: z.string()}); export const getApiV6NotificationsResponse = z.array(notificationResponse); 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 }; /** * List recent notifications * * GET /v6/notifications * * Also accepts auth: QUERY, COOKIE */ export const getApiV6Notifications = async (args: GetApiV6NotificationsArgs, options?: GetApiV6NotificationsOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) for (const item of args.having_all_tags ?? []) query.append("having_all_tags", String(item)) if (args.event_id != null) query.append("event_id", String(args.event_id)) if (args.subscription_id != null) query.append("subscription_id", String(args.subscription_id)) if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v6/notifications${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(getApiV6NotificationsResponse, await res.json()) };