import {z} from 'zod' export type GetApiNotificationsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const nNotificationTypeEnum = z.enum(["email", "domain", "advanced", "raw", "dark_web"]); export const notificationLeakTypeEnum = z.enum(["ALL", "EMPLOYEES", "THIRD_PARTIES", "CUSTOMERS"]); export const nNotificationMethodTypeEnum = z.enum(["email", "slack", "telegram", "webhook", "discord"]); export const notificationOut = z.object({id: z.number().int(), method_id: z.number().int(), type: nNotificationTypeEnum, dataset: z.enum(["stealer_logs", "combolist", "databases", "raw_files", "dark_web"]).optional(), second_type: notificationLeakTypeEnum.nullable().optional(), value: z.string().nullable().optional(), filters: z.record(z.string(), z.unknown()).nullable().optional(), label: z.string().nullable().optional(), last_searched_at: z.string().nullable().optional(), is_active: z.boolean(), timestamp: z.string(), method_type: nNotificationMethodTypeEnum.nullable().optional(), method_name: z.string().nullable().optional(), method_value: z.string().nullable().optional(), method_status: z.enum(["active", "suspended", "paused"]).nullable().optional(), method_extra_emails: z.array(z.string()).nullable().optional(), auto_unlock: z.boolean().nullable().optional(), max_points: z.number().int().nullable().optional(), daily_spent: z.number().int().nullable().optional(), daily_reset_at: z.string().nullable().optional(), total_notifications: z.number().int().nullable().optional(), total_cost: z.number().int().nullable().optional()}); export const getApiNotificationsResponse = z.array(notificationOut); 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 notifications * * GET /notifications */ export const getApiNotifications = async (options?: GetApiNotificationsOptions) => { const {baseUrl = "https://api.leakradar.io", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiNotificationsResponse, await res.json()) };