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 CreateApiSearchRawExportArgs = {export: string, dedup?: boolean | undefined, body: RawSearchRequest}; export type CreateApiSearchRawExportOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const exportResponse = z.object({status: z.string(), message: z.string(), export_id: 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 }; /** * Queue a raw export job * * POST /search/raw/export */ export const createApiSearchRawExport = async (args: CreateApiSearchRawExportArgs, options?: CreateApiSearchRawExportOptions) => { 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.export != null) query.append("export", String(args.export)) if (args.dedup != null) query.append("dedup", String(args.dedup)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/search/raw/export${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(exportResponse, await res.json()) };