1
1
import axios from 'axios' ;
2
2
import handleError from './utils/handleError.js' ;
3
+ import { isMockEnabled , getMockConfig } from './utils/mockConfig.js' ;
4
+ import { getMockResponse } from './utils/mockResponse.js' ;
3
5
4
6
/**
5
7
* Perform automated browser actions on a webpage using AI-powered agentic scraping.
@@ -11,6 +13,8 @@ import handleError from './utils/handleError.js';
11
13
* @param {string } [userPrompt=null] - Prompt for AI extraction (required when aiExtraction=true)
12
14
* @param {Object } [outputSchema=null] - Schema for structured data extraction (optional, used with aiExtraction=true)
13
15
* @param {boolean } [aiExtraction=false] - Whether to use AI for data extraction from the scraped content
16
+ * @param {Object } options - Optional configuration options
17
+ * @param {boolean } options.mock - Override mock mode for this request
14
18
* @returns {Promise<Object> } Response from the API containing request_id and initial status
15
19
* @throws {Error } Will throw an error in case of an HTTP failure or invalid parameters.
16
20
*
@@ -60,7 +64,19 @@ import handleError from './utils/handleError.js';
60
64
* console.error('Error:', error.message);
61
65
* }
62
66
*/
63
- export async function agenticScraper ( apiKey , url , steps , useSession = true , userPrompt = null , outputSchema = null , aiExtraction = false ) {
67
+ export async function agenticScraper ( apiKey , url , steps , useSession = true , userPrompt = null , outputSchema = null , aiExtraction = false , options = { } ) {
68
+ const { mock = null } = options ;
69
+
70
+ // Check if mock mode is enabled
71
+ const useMock = mock !== null ? mock : isMockEnabled ( ) ;
72
+
73
+ if ( useMock ) {
74
+ console . log ( '🧪 Mock mode active. Returning stub for agenticScraper request' ) ;
75
+ const mockConfig = getMockConfig ( ) ;
76
+ const mockData = getMockResponse ( 'POST' , 'https://api.scrapegraphai.com/v1/agentic-scrapper' , mockConfig . customResponses , mockConfig . customHandler ) ;
77
+ return mockData ;
78
+ }
79
+
64
80
const endpoint = 'https://api.scrapegraphai.com/v1/agentic-scrapper' ;
65
81
const headers = {
66
82
'accept' : 'application/json' ,
@@ -166,7 +182,19 @@ export async function agenticScraper(apiKey, url, steps, useSession = true, user
166
182
* allowing for complex interactions like form filling, clicking buttons,
167
183
* and navigating through multi-step workflows with session management.
168
184
*/
169
- export async function getAgenticScraperRequest ( apiKey , requestId ) {
185
+ export async function getAgenticScraperRequest ( apiKey , requestId , options = { } ) {
186
+ const { mock = null } = options ;
187
+
188
+ // Check if mock mode is enabled
189
+ const useMock = mock !== null ? mock : isMockEnabled ( ) ;
190
+
191
+ if ( useMock ) {
192
+ console . log ( '🧪 Mock mode active. Returning stub for getAgenticScraperRequest' ) ;
193
+ const mockConfig = getMockConfig ( ) ;
194
+ const mockData = getMockResponse ( 'GET' , `https://api.scrapegraphai.com/v1/agentic-scrapper/${ requestId } ` , mockConfig . customResponses , mockConfig . customHandler ) ;
195
+ return mockData ;
196
+ }
197
+
170
198
const endpoint = 'https://api.scrapegraphai.com/v1/agentic-scrapper/' + requestId ;
171
199
const headers = {
172
200
'accept' : 'application/json' ,
0 commit comments