import { ChunkCallbackFunction, Collection, SpiderCoreResponse, SpiderParams, SearchRequestParams, RequestParamsTransform, AIRequestParams, AIStudioTier } from "./config"; import { SpiderBrowser, type SpiderBrowserOptions } from "spider-browser"; /** * Rate limit state from API response headers. */ export interface RateLimitInfo { /** Maximum requests allowed per minute. */ limit: number; /** Requests remaining in the current window. */ remaining: number; /** Seconds until the rate limit window resets. */ resetSeconds: number; } /** * Generic params for core request. */ export type GenericParams = Omit; /** * Configuration interface for Spider. */ export interface SpiderConfig { apiKey?: string | null; } /** * A class to interact with the Spider API. */ export declare class Spider { private apiKey?; private aiRateLimiter; private aiStudioTier; /** The latest rate limit state from API response headers. */ rateLimit: RateLimitInfo; /** * Create an instance of Spider. * @param {string | null} apiKey - The API key used to authenticate to the Spider API. If null, attempts to source from environment variables. * @param {AIStudioTier} aiStudioTier - The AI Studio subscription tier for rate limiting. Defaults to 'starter'. * @throws Will throw an error if the API key is not provided. */ constructor(props?: SpiderConfig & { aiStudioTier?: AIStudioTier; }); /** * Update the AI Studio subscription tier (adjusts rate limiting). * @param {AIStudioTier} tier - The new subscription tier. */ setAIStudioTier(tier: AIStudioTier): void; /** * Update rate limit state from response headers. */ private _updateRateLimit; /** * Internal method to handle POST requests. * @param {string} endpoint - The API endpoint to which the POST request should be sent. * @param {Record} data - The JSON data to be sent in the request body. * @param {boolean} [stream=false] - Whether to stream the response back without parsing. * @returns {Promise} The response in JSON if not streamed, or the Response object if streamed. */ private _apiPost; /** * Internal method to handle GET requests. * @param {string} endpoint - The API endpoint from which data should be retrieved. * @returns {Promise} The data returned from the endpoint in JSON format. */ private _apiGet; /** * Scrapes data from a specified URL. * @param {string} url - The URL to scrape. * @param {GenericParams} [params={}] - Additional parameters for the scraping request. * @returns {Promise} The scraped data from the URL. */ scrapeUrl(url: string, params?: GenericParams): Promise; /** * Initiates a crawling job starting from the specified URL. * @param {string} url - The URL to start crawling. * @param {GenericParams} [params={}] - Additional parameters for the crawl. * @param {boolean} [stream=false] - Whether to receive the response as a stream. * @param {function} [callback=function] - The callback function when streaming per chunk. If this is set with stream you will not get a end response. * @returns {Promise} The result of the crawl, either structured data or a Response object if streaming. */ crawlUrl(url: string, params?: GenericParams, stream?: boolean, cb?: ChunkCallbackFunction): Promise; /** * Retrieves all links from the specified URL. * @param {string} url - The URL from which to gather links. * @param {GenericParams} [params={}] - Additional parameters for the crawl. * @param {boolean} [stream=false] - Whether to receive the response as a stream. * @param {function} [callback=function] - The callback function when streaming per chunk. If this is set with stream you will not get a end response. * @returns {Promise} The result of the crawl, either structured data or a Response object if streaming. */ links(url: string, params?: GenericParams, stream?: boolean, cb?: ChunkCallbackFunction): Promise; /** * Takes a screenshot of the website starting from this URL. * @param {string} url - The URL to start the screenshot. * @param {GenericParams} [params={}] - Configuration parameters for the screenshot. * @returns {Promise} The screenshot data. */ screenshot(url: string, params?: GenericParams): Promise; /** * Unblock a challenging url to get data. * @param {string} url - The URL to get data from. * @param {GenericParams} [params={}] - Configuration parameters for the screenshot. * @returns {Promise} The screenshot data. */ unblocker(url: string, params?: GenericParams): Promise; /** * Perform a search and gather a list of websites to start crawling and collect resources. * @param {string} search - The search query. * @param {GenericParams} [params={}] - Configuration parameters for the search. * @returns {Promise} The result of the crawl, either structured data or a Response object if streaming. */ search(q: string, params?: SearchRequestParams): Promise; /** * Transform HTML to Markdown or text. You can send up to 10MB of data at once. * @param {object} data - The data to trasnform, a list of objects with the key 'html' and optional 'url' key for readability. * @param {object} [params={}] - Configuration parameters for the transformation. * @returns {Promise} The transformation result. */ transform(data: { html: string; url?: string; }[], params?: RequestParamsTransform): Promise; /** * Retrieves the number of credits available on the account. * @returns {Promise} The current credit balance. */ getCredits(): Promise; /** * Send a POST request to insert data into a specified table. * @param {string} table - The table name in the database. * @param {object} data - The data to be inserted. * @returns {Promise} The response from the server. */ postData(collection: Collection, data: GenericParams | Record): Promise; /** * Prepares common headers for each API request. * @returns {HeadersInit} A headers object for fetch requests. */ get prepareHeaders(): { "Content-Type": string; Authorization: string; "User-Agent": string; }; /** * Prepares common headers for each API request with JSONl content-type suitable for streaming. * @returns {HeadersInit} A headers object for fetch requests. */ get prepareHeadersJsonL(): { "Content-Type": string; Authorization: string; "User-Agent": string; }; /** * Internal method to handle AI Studio POST requests with rate limiting. * @param {string} endpoint - The AI Studio endpoint. * @param {Record} data - The request data including prompt. * @returns {Promise} The response data. * @throws {AIStudioSubscriptionRequired} When subscription is not active. * @throws {AIStudioRateLimitExceeded} When rate limit is exceeded server-side. */ private _aiApiPost; /** * AI-guided crawling using natural language prompts. * Requires an active AI Studio subscription. * @param {string} url - The URL to start crawling. * @param {string} prompt - Natural language instruction for what to crawl and extract. * @param {AIRequestParams} [params={}] - Additional parameters for the crawl. * @returns {Promise} The crawl results guided by the AI prompt. * @throws {AIStudioSubscriptionRequired} When subscription is not active. */ aiCrawl(url: string, prompt: string, params?: Omit): Promise; /** * AI-guided scraping using natural language prompts. * Requires an active AI Studio subscription. * @param {string} url - The URL to scrape. * @param {string} prompt - Natural language description of data to extract. * @param {AIRequestParams} [params={}] - Additional parameters for the scrape. * @returns {Promise} The scraped data guided by the AI prompt. * @throws {AIStudioSubscriptionRequired} When subscription is not active. */ aiScrape(url: string, prompt: string, params?: Omit): Promise; /** * AI-enhanced web search using natural language queries. * Requires an active AI Studio subscription. * @param {string} prompt - Natural language search query. * @param {SearchRequestParams} [params={}] - Additional search parameters. * @returns {Promise} The search results with AI-enhanced relevance. * @throws {AIStudioSubscriptionRequired} When subscription is not active. */ aiSearch(prompt: string, params?: SearchRequestParams): Promise; /** * AI-guided browser automation using natural language commands. * Requires an active AI Studio subscription. * @param {string} url - The URL to automate. * @param {string} prompt - Natural language description of browser actions. * @param {AIRequestParams} [params={}] - Additional parameters for automation. * @returns {Promise} The automation results. * @throws {AIStudioSubscriptionRequired} When subscription is not active. */ aiBrowser(url: string, prompt: string, params?: Omit): Promise; /** * AI-guided link extraction and filtering. * Requires an active AI Studio subscription. * @param {string} url - The URL to extract links from. * @param {string} prompt - Natural language description of what links to find. * @param {AIRequestParams} [params={}] - Additional parameters. * @returns {Promise} The filtered links based on AI analysis. * @throws {AIStudioSubscriptionRequired} When subscription is not active. */ aiLinks(url: string, prompt: string, params?: Omit): Promise; /** * Scrapes data from a specified URL on the Unlimited plan. * Requires an active Unlimited subscription - a flat monthly rate billed by * purchased concurrency seats (requests in flight at once) instead of per-request credits. * Requests are not queued: when all seats are in flight the API returns an immediate * 429 with a Retry-After header, so retry with backoff. * AI/LLM extraction params (e.g. `prompt`, `extraction_schema`) are not allowed and * return a 400; AI usage is billed separately via the AI methods. * See https://spider.cloud/docs/api/unlimited for details and * https://spider.cloud/pricing?plan=unlimited for plans. * @param {string} url - The URL to scrape. * @param {GenericParams} [params={}] - Additional parameters for the scraping request. * @returns {Promise} The scraped data from the URL. */ unlimitedScrape(url: string, params?: GenericParams): Promise; /** * Initiates a crawling job starting from the specified URL on the Unlimited plan. * Requires an active Unlimited subscription - a flat monthly rate billed by * purchased concurrency seats (requests in flight at once) instead of per-request credits. * Requests are not queued: when all seats are in flight the API returns an immediate * 429 with a Retry-After header, so retry with backoff. * AI/LLM extraction params (e.g. `prompt`, `extraction_schema`) are not allowed and * return a 400; AI usage is billed separately via the AI methods. * See https://spider.cloud/docs/api/unlimited for details and * https://spider.cloud/pricing?plan=unlimited for plans. * @param {string} url - The URL to start crawling. * @param {GenericParams} [params={}] - Additional parameters for the crawl. * @param {boolean} [stream=false] - Whether to receive the response as a stream. * @param {function} [callback=function] - The callback function when streaming per chunk. If this is set with stream you will not get a end response. * @returns {Promise} The result of the crawl, either structured data or a Response object if streaming. */ unlimitedCrawl(url: string, params?: GenericParams, stream?: boolean, cb?: ChunkCallbackFunction): Promise; /** * Retrieves all links from the specified URL on the Unlimited plan. * Requires an active Unlimited subscription - a flat monthly rate billed by * purchased concurrency seats (requests in flight at once) instead of per-request credits. * Requests are not queued: when all seats are in flight the API returns an immediate * 429 with a Retry-After header, so retry with backoff. * AI/LLM extraction params (e.g. `prompt`, `extraction_schema`) are not allowed and * return a 400; AI usage is billed separately via the AI methods. * See https://spider.cloud/docs/api/unlimited for details and * https://spider.cloud/pricing?plan=unlimited for plans. * @param {string} url - The URL from which to gather links. * @param {GenericParams} [params={}] - Additional parameters for the crawl. * @param {boolean} [stream=false] - Whether to receive the response as a stream. * @param {function} [callback=function] - The callback function when streaming per chunk. If this is set with stream you will not get a end response. * @returns {Promise} The result of the crawl, either structured data or a Response object if streaming. */ unlimitedLinks(url: string, params?: GenericParams, stream?: boolean, cb?: ChunkCallbackFunction): Promise; /** * Creates a SpiderBrowser instance for WebSocket-based browser automation (CDP/BiDi). * @param {Omit} [options] - Browser options (excluding apiKey, which is inherited from the client). * @returns {SpiderBrowser} A new SpiderBrowser instance. */ browser(options?: Omit): SpiderBrowser; /** * Generates the API URL for a recording session's video/metadata. * @param {string} sessionId - The recording session ID. * @returns {string} The full URL to the recording endpoint. */ static getRecordingVideoUrl(sessionId: string): string; /** * Handles errors from API requests. * @param {Response} response - The fetch response object. * @param {string} action - Description of the attempted action. * @throws Will throw an error with detailed status information. */ handleError(response: Response, action: string): void; }