import {z} from 'zod' export type DarkWebSearchRequest = {query?: (string | null) | undefined, title?: (string | null) | undefined, content?: (string | null) | undefined, author?: (string | null) | undefined, source_url?: (string | null) | undefined, logic?: (string | null) | undefined}; export type CreateApiSearchDarkWebArgs = {page?: number | undefined, page_size?: number | undefined, sources?: (string | null) | undefined, date_from?: (string | null) | undefined, date_to?: (string | null) | undefined, sort_by?: string | undefined, sort_order?: string | undefined, body: DarkWebSearchRequest}; export type CreateApiSearchDarkWebOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const darkWebItem = z.object({id: z.string().nullable().optional(), title: z.string().nullable().optional(), content: z.string().nullable().optional(), source: z.string().nullable().optional(), source_name: z.string().nullable().optional(), target: z.string().nullable().optional(), source_ref: z.string().nullable().optional(), author: z.string().nullable().optional(), published_at: z.string().nullable().optional(), ingested_at: z.string().nullable().optional(), truncated: z.boolean().optional(), censored: z.boolean().optional()}); export const darkWebSearchResponse = z.object({items: z.array(darkWebItem), total: z.number().int(), page: z.number().int(), page_size: 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 }; /** * Search dark web mentions * * POST /search/dark-web */ export const createApiSearchDarkWeb = async (args: CreateApiSearchDarkWebArgs, options?: CreateApiSearchDarkWebOptions) => { 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 (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.sources != null) query.append("sources", String(args.sources)) if (args.date_from != null) query.append("date_from", String(args.date_from)) if (args.date_to != null) query.append("date_to", String(args.date_to)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/search/dark-web${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(darkWebSearchResponse, await res.json()) };