import {z} from 'zod' export type GetApiSearchRawPartArgs = {container_id: number, entry_path: string, seq: number, trim_overlap?: boolean | undefined, overlap_chars?: number | undefined, auto_unlock?: boolean | undefined}; export type GetApiSearchRawPartOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const partResponse = z.object({container_id: z.string(), entry_path: z.string(), seq: z.number().int(), offset: z.number().int(), size_bytes: z.number().int().nullable().optional(), ingested_at: z.string().nullable().optional(), trim_overlap: z.boolean(), overlap_chars: z.number().int(), text: z.string(), censored: z.boolean()}); 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 }; /** * Retrieve a raw entry part * * GET /search/raw/part */ export const getApiSearchRawPart = async (args: GetApiSearchRawPartArgs, options?: GetApiSearchRawPartOptions) => { 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.container_id != null) query.append("container_id", String(args.container_id)) if (args.entry_path != null) query.append("entry_path", String(args.entry_path)) if (args.seq != null) query.append("seq", String(args.seq)) if (args.trim_overlap != null) query.append("trim_overlap", String(args.trim_overlap)) if (args.overlap_chars != null) query.append("overlap_chars", String(args.overlap_chars)) if (args.auto_unlock != null) query.append("auto_unlock", String(args.auto_unlock)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/search/raw/part${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(partResponse, await res.json()) };