import {z} from 'zod' export type RawMatchType = 'contains' | 'starts_with' | 'ends_with' | 'exact'; export type RawSearchRequest = {q?: (string | null) | undefined, container_id?: (number | null) | undefined, exts?: (Array | null) | undefined, exts_not?: (Array | null) | undefined, categories?: (Array | null) | undefined, categories_not?: (Array | null) | undefined, file_name?: (Array | null) | undefined, file_name_not?: (Array | null) | undefined, folder_name?: (Array | null) | undefined, folder_name_not?: (Array | null) | undefined, force_and?: (boolean | null) | undefined, file_name_match_type?: (RawMatchType | null) | undefined, file_name_not_match_type?: (RawMatchType | null) | undefined, folder_name_match_type?: (RawMatchType | null) | undefined, folder_name_not_match_type?: (RawMatchType | null) | undefined, file_name_exact?: (boolean | null) | undefined, folder_name_exact?: (boolean | null) | undefined, q_exact?: (boolean | null) | undefined, include_terms?: (Array | null) | undefined, exclude_terms?: (Array | null) | undefined, ingested_at_min?: (string | null) | undefined, ingested_at_max?: (string | null) | undefined}; export type CreateApiSearchRawArgs = {page?: number | undefined, page_size?: number | undefined, cursor?: (string | null) | undefined, dedup?: boolean | undefined, body: RawSearchRequest}; export type CreateApiSearchRawOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const rawSearchItem = z.object({container_id: z.string(), entry_path: z.string(), entry_name: z.string().nullable().optional(), ext: z.string().nullable().optional(), seq: z.number().int().nullable().optional(), offset: z.number().int().nullable().optional(), ingested_at: z.string().nullable().optional(), sha256_original: z.string().nullable().optional(), original_file_name: z.string().nullable().optional(), category: z.string().nullable().optional(), display_name: z.string().nullable().optional(), snippet: z.string().nullable().optional(), already_unlocked: z.boolean().optional(), matching_lines: z.array(z.string()).optional()}); export const rawSearchResponse = z.object({items: z.array(rawSearchItem), total: z.number().int(), page: z.number().int(), page_size: z.number().int(), has_more: z.boolean().nullable().optional(), next_cursor: z.string().nullable().optional(), blacklisted_value: z.string().nullable().optional()}); 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 raw leak blocks * * POST /search/raw */ export const createApiSearchRaw = async (args: CreateApiSearchRawArgs, options?: CreateApiSearchRawOptions) => { 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.cursor != null) query.append("cursor", String(args.cursor)) if (args.dedup != null) query.append("dedup", String(args.dedup)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/search/raw${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(rawSearchResponse, await res.json()) };