import {z} from 'zod' export type NNotificationTypeEnum = 'email' | 'domain' | 'advanced' | 'raw' | 'dark_web'; export type NotificationLeakTypeEnum = 'ALL' | 'EMPLOYEES' | 'THIRD_PARTIES' | 'CUSTOMERS'; export type NotificationCreateRequest = {method_id?: (number | null) | undefined, dataset?: ('stealer_logs' | 'combolist' | 'raw_files' | 'dark_web') | undefined, type: NNotificationTypeEnum, value?: (string | null) | undefined, filters?: ({[key: string]: unknown} | null) | undefined, auto_unlock?: (boolean | null) | undefined, max_points?: (number | null) | undefined, second_type?: (NotificationLeakTypeEnum | null) | undefined, bulk?: (boolean | null) | undefined}; export type CreateApiNotificationsArgs = {body: NotificationCreateRequest}; export type CreateApiNotificationsOptions = {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 createApiNotificationsResponse = z.union([z.array(notificationOut), 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 }; /** * Create notification * * POST /notifications */ export const createApiNotifications = async (args: CreateApiNotificationsArgs, options?: CreateApiNotificationsOptions) => { const {baseUrl = "https://api.leakradar.io", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiNotificationsResponse, await res.json()) };