import {z} from 'zod' export type UnlockedLeakStatusEnum = 'new' | 'in_progress' | 'fixed' | 'accepted_risk'; export type GetApiNotificationRunsArgs = {page?: number | undefined, page_size?: number | undefined, dataset?: ('stealer_logs' | 'combolist' | 'databases' | 'raw_files' | 'dark_web' | null) | undefined, status?: (UnlockedLeakStatusEnum | null) | undefined, sent_after?: (string | null) | undefined, sent_before?: (string | null) | undefined, notification_id?: (number | null) | undefined, q?: (string | null) | undefined}; export type GetApiNotificationRunsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationRunBase = z.object({id: z.number().int(), user_id: z.number().int(), notification_id: z.number().int().nullable().optional(), sent_at: z.string(), new_leaks_count: z.number().int(), unlocked_count: z.number().int(), points_spent: z.number().int(), sent_ok: z.boolean(), delivery_skipped: z.boolean().optional(), notification_type: z.string().nullable().optional(), notification_value: z.string().nullable().optional(), notification_label: z.string().nullable().optional(), notification_filters: z.record(z.string(), z.unknown()).nullable().optional(), dataset: z.enum(["stealer_logs", "combolist", "databases", "raw_files", "dark_web"]).optional(), status: z.string().optional()}); export const paginatedNotificationRuns = z.object({items: z.array(notificationRunBase), total: z.number().int(), page: z.number().int(), page_size: z.number().int(), blacklisted_value: z.string().nullable().optional(), exportable_total: z.number().int(), export_run_limit: z.number().int()}); 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 notification runs * * GET /notification_runs */ export const getApiNotificationRuns = async (args: GetApiNotificationRunsArgs, options?: GetApiNotificationRunsOptions) => { 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 (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.dataset != null) query.append("dataset", String(args.dataset)) if (args.status != null) query.append("status", String(args.status)) if (args.sent_after != null) query.append("sent_after", String(args.sent_after)) if (args.sent_before != null) query.append("sent_before", String(args.sent_before)) if (args.notification_id != null) query.append("notification_id", String(args.notification_id)) if (args.q != null) query.append("q", String(args.q)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/notification_runs${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(paginatedNotificationRuns, await res.json()) };