Skip to content

Commit af63d00

Browse files
committed
feat: add files
1 parent 710129e commit af63d00

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

scrapegraph-js/src/agenticScraper.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import axios from 'axios';
22
import handleError from './utils/handleError.js';
3+
import { isMockEnabled, getMockConfig } from './utils/mockConfig.js';
4+
import { getMockResponse } from './utils/mockResponse.js';
35

46
/**
57
* Perform automated browser actions on a webpage using AI-powered agentic scraping.
@@ -11,6 +13,8 @@ import handleError from './utils/handleError.js';
1113
* @param {string} [userPrompt=null] - Prompt for AI extraction (required when aiExtraction=true)
1214
* @param {Object} [outputSchema=null] - Schema for structured data extraction (optional, used with aiExtraction=true)
1315
* @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
1418
* @returns {Promise<Object>} Response from the API containing request_id and initial status
1519
* @throws {Error} Will throw an error in case of an HTTP failure or invalid parameters.
1620
*
@@ -60,7 +64,19 @@ import handleError from './utils/handleError.js';
6064
* console.error('Error:', error.message);
6165
* }
6266
*/
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+
6480
const endpoint = 'https://api.scrapegraphai.com/v1/agentic-scrapper';
6581
const headers = {
6682
'accept': 'application/json',

scrapegraph-js/src/crawl.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import axios from 'axios';
22
import handleError from './utils/handleError.js';
33
import { ZodType } from 'zod';
44
import { zodToJsonSchema } from 'zod-to-json-schema';
5+
import { isMockEnabled, getMockConfig } from './utils/mockConfig.js';
6+
import { getMockResponse } from './utils/mockResponse.js';
57

68
/**
79
* Start a crawl job using the ScrapeGraphAI API.
@@ -18,6 +20,7 @@ import { zodToJsonSchema } from 'zod-to-json-schema';
1820
* @param {boolean} [options.sameDomainOnly=true] - Whether to only crawl pages from the same domain
1921
* @param {boolean} [options.sitemap] - Whether to use sitemap for better page discovery
2022
* @param {number} [options.batchSize=1] - Batch size for processing pages (1-10)
23+
* @param {boolean} [options.mock] - Override mock mode for this request
2124
* @returns {Promise<Object>} The crawl job response
2225
* @throws {Error} Throws an error if the HTTP request fails
2326
*/
@@ -28,6 +31,17 @@ export async function crawl(
2831
schema,
2932
options = {}
3033
) {
34+
const { mock = null } = options;
35+
36+
// Check if mock mode is enabled
37+
const useMock = mock !== null ? mock : isMockEnabled();
38+
39+
if (useMock) {
40+
console.log('🧪 Mock mode active. Returning stub for crawl request');
41+
const mockConfig = getMockConfig();
42+
const mockData = getMockResponse('POST', 'https://api.scrapegraphai.com/v1/crawl', mockConfig.customResponses, mockConfig.customHandler);
43+
return mockData;
44+
}
3145
const endpoint = 'https://api.scrapegraphai.com/v1/crawl';
3246
const headers = {
3347
'accept': 'application/json',
@@ -81,7 +95,19 @@ export async function crawl(
8195
* @returns {Promise<Object>} The crawl result
8296
* @throws {Error} Throws an error if the HTTP request fails
8397
*/
84-
export async function getCrawlRequest(apiKey, crawlId) {
98+
export async function getCrawlRequest(apiKey, crawlId, options = {}) {
99+
const { mock = null } = options;
100+
101+
// Check if mock mode is enabled
102+
const useMock = mock !== null ? mock : isMockEnabled();
103+
104+
if (useMock) {
105+
console.log('🧪 Mock mode active. Returning stub for getCrawlRequest');
106+
const mockConfig = getMockConfig();
107+
const mockData = getMockResponse('GET', `https://api.scrapegraphai.com/v1/crawl/${crawlId}`, mockConfig.customResponses, mockConfig.customHandler);
108+
return mockData;
109+
}
110+
85111
const endpoint = `https://api.scrapegraphai.com/v1/crawl/${crawlId}`;
86112
const headers = {
87113
'accept': 'application/json',

0 commit comments

Comments
 (0)