import { Browser, Curl } from "./curl"; export { Curl } from "./curl"; export interface DvRequestOpts { headers?: Record; params?: Record; } export interface DefaultOpts { headers?: Record; } export class ScraperRequestError extends Error { status: number; headers: Record; data: any; constructor({ status, headers, data }) { super(); this.status = status; this.headers = headers; this.data = data; } } export class ScraperError extends Error {} export interface IScraper { proxy?: string; get( url: string, opts?: DvRequestOpts ): Promise<{ data: T; headers: Record; status: number; }>; post( url: string, data: Record, opts?: DvRequestOpts ): Promise<{ data: T; headers: Record; status: number; }>; setDefaultOpts(opts: DvRequestOpts): void; } export interface IScraperAdapter { proxy?: string; userAgent?: string; debug?: boolean; get( url: string, opts?: DvRequestOpts ): Promise<{ data: T; headers: Record; status: number; }>; post( url: string, data: Record, opts?: DvRequestOpts ): Promise<{ data: T; headers: Record; status: number; }>; } export class Scraper implements IScraper { proxy?: string; debug: boolean = false; constructor( private adapter: IScraperAdapter, private defaultOpts?: DefaultOpts ) {} get( url: string, opts?: DvRequestOpts ): Promise<{ data: T; headers: Record; status: number; }> { const headers = { ...(this.defaultOpts?.headers ? this.defaultOpts?.headers : {}), ...(opts?.headers ? opts?.headers : {}), }; return this.adapter.get(url, { ...opts, headers, }); } post( url: string, data: Record, opts?: DvRequestOpts ): Promise<{ data: T; headers: Record; status: number; }> { const headers = { ...(this.defaultOpts?.headers ? this.defaultOpts?.headers : {}), ...(opts?.headers ? opts?.headers : {}), }; return this.adapter.post(url, data, { ...opts, headers, }); } setDebug(enableDebug: boolean) { this.debug = enableDebug; this.adapter.debug = enableDebug; } useProxy(proxy: string) { this.proxy = proxy; this.adapter.proxy = proxy; } setDefaultOpts(opts: DvRequestOpts): void { this.defaultOpts = opts; } setCurlBrowser(browser: Browser) { if (!(this.adapter instanceof Curl)) { throw new Error("Adapter isnt Curl"); } else { this.adapter.setBrowser(browser); } } getBrowser(): Browser { if (!(this.adapter instanceof Curl)) { throw new Error("Adapter isnt Curl"); } else { return this.adapter.getBrowser(); } } }