import {z} from 'zod' export type GetApiRawDownloadPreviewArgs = {sha256_original?: (string | null) | undefined, container_id?: (number | null) | undefined, entry_path?: (string | null) | undefined}; export type GetApiRawDownloadPreviewOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const rawDownloadPreviewResponse = z.object({sha256_original: z.string(), raw_file_id: z.number().int(), file_name: z.string().nullable().optional(), original_file_name: z.string().nullable().optional(), source_size_bytes: z.number().int(), already_unlocked: z.boolean(), cost_bytes: z.number().int(), user_gb_available_bytes: z.number().int(), can_unlock: z.boolean(), file_password: z.string().nullable().optional(), downloaded_before: z.boolean().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 }; /** * Preview unlock cost for a raw file * * GET /raw/download/preview */ export const getApiRawDownloadPreview = async (args: GetApiRawDownloadPreviewArgs, options?: GetApiRawDownloadPreviewOptions) => { 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.sha256_original != null) query.append("sha256_original", String(args.sha256_original)) 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/raw/download/preview${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(rawDownloadPreviewResponse, await res.json()) };