|
| 1 | +/** |
| 2 | + * Example demonstrating how to use the ScrapeGraph AI SDK in mock mode. |
| 3 | + * |
| 4 | + * This example shows how to: |
| 5 | + * 1. Enable mock mode globally or per-request |
| 6 | + * 2. Use custom mock responses |
| 7 | + * 3. Use custom mock handlers |
| 8 | + * 4. Test different endpoints in mock mode |
| 9 | + * 5. Demonstrate environment variable activation |
| 10 | + * |
| 11 | + * Requirements: |
| 12 | + * - Node.js 16+ |
| 13 | + * - scrapegraph-js |
| 14 | + * |
| 15 | + * Usage: |
| 16 | + * node mock_mode_example.js |
| 17 | + * |
| 18 | + * Or with environment variable: |
| 19 | + * SGAI_MOCK=1 node mock_mode_example.js |
| 20 | + */ |
| 21 | + |
| 22 | +import { |
| 23 | + scrape, |
| 24 | + getScrapeRequest, |
| 25 | + smartScraper, |
| 26 | + getSmartScraperRequest, |
| 27 | + searchScraper, |
| 28 | + getSearchScraperRequest, |
| 29 | + markdownify, |
| 30 | + getMarkdownifyRequest, |
| 31 | + crawl, |
| 32 | + getCrawlRequest, |
| 33 | + agenticScraper, |
| 34 | + getAgenticScraperRequest, |
| 35 | + getCredits, |
| 36 | + submitFeedback |
| 37 | +} from '../index.js'; |
| 38 | + |
| 39 | +import { |
| 40 | + initMockConfig, |
| 41 | + enableMock, |
| 42 | + disableMock, |
| 43 | + setMockResponses, |
| 44 | + setMockHandler |
| 45 | +} from '../src/utils/mockConfig.js'; |
| 46 | + |
| 47 | +// Configuration |
| 48 | +const API_KEY = process.env.SGAI_API_KEY || 'sgai-00000000-0000-0000-0000-000000000000'; |
| 49 | + |
| 50 | +/** |
| 51 | + * Basic mock mode usage demonstration |
| 52 | + */ |
| 53 | +async function basicMockUsage() { |
| 54 | + console.log('\n=== Basic Mock Usage ==='); |
| 55 | + |
| 56 | + // Enable mock mode globally |
| 57 | + enableMock(); |
| 58 | + |
| 59 | + try { |
| 60 | + // Test scrape endpoint |
| 61 | + console.log('\n-- Testing scrape endpoint --'); |
| 62 | + const scrapeResult = await scrape(API_KEY, 'https://example.com', { renderHeavyJs: true }); |
| 63 | + console.log('Scrape result:', scrapeResult); |
| 64 | + |
| 65 | + // Test getScrapeRequest endpoint |
| 66 | + console.log('\n-- Testing getScrapeRequest endpoint --'); |
| 67 | + const scrapeStatus = await getScrapeRequest(API_KEY, 'mock-request-id'); |
| 68 | + console.log('Scrape status:', scrapeStatus); |
| 69 | + |
| 70 | + // Test smartScraper endpoint |
| 71 | + console.log('\n-- Testing smartScraper endpoint --'); |
| 72 | + const smartResult = await smartScraper(API_KEY, 'https://example.com', 'Extract the title'); |
| 73 | + console.log('SmartScraper result:', smartResult); |
| 74 | + |
| 75 | + // Test getCredits endpoint |
| 76 | + console.log('\n-- Testing getCredits endpoint --'); |
| 77 | + const credits = await getCredits(API_KEY); |
| 78 | + console.log('Credits:', credits); |
| 79 | + |
| 80 | + // Test submitFeedback endpoint |
| 81 | + console.log('\n-- Testing submitFeedback endpoint --'); |
| 82 | + const feedback = await submitFeedback(API_KEY, 'mock-request-id', 5, 'Great service!'); |
| 83 | + console.log('Feedback result:', feedback); |
| 84 | + |
| 85 | + } catch (error) { |
| 86 | + console.error('Error in basic mock usage:', error.message); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Mock mode with custom responses |
| 92 | + */ |
| 93 | +async function mockWithCustomResponses() { |
| 94 | + console.log('\n=== Mock Mode with Custom Responses ==='); |
| 95 | + |
| 96 | + // Set custom responses for specific endpoints |
| 97 | + setMockResponses({ |
| 98 | + '/v1/credits': { |
| 99 | + remaining_credits: 42, |
| 100 | + total_credits_used: 58, |
| 101 | + custom_field: 'This is a custom response' |
| 102 | + }, |
| 103 | + '/v1/smartscraper': () => ({ |
| 104 | + request_id: 'custom-mock-request-id', |
| 105 | + custom_data: 'Generated by custom function' |
| 106 | + }) |
| 107 | + }); |
| 108 | + |
| 109 | + try { |
| 110 | + // Test credits with custom response |
| 111 | + console.log('\n-- Testing credits with custom response --'); |
| 112 | + const credits = await getCredits(API_KEY); |
| 113 | + console.log('Custom credits:', credits); |
| 114 | + |
| 115 | + // Test smartScraper with custom response |
| 116 | + console.log('\n-- Testing smartScraper with custom response --'); |
| 117 | + const smartResult = await smartScraper(API_KEY, 'https://example.com', 'Extract data'); |
| 118 | + console.log('Custom smartScraper result:', smartResult); |
| 119 | + |
| 120 | + } catch (error) { |
| 121 | + console.error('Error in custom responses:', error.message); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Mock mode with custom handler |
| 127 | + */ |
| 128 | +async function mockWithCustomHandler() { |
| 129 | + console.log('\n=== Mock Mode with Custom Handler ==='); |
| 130 | + |
| 131 | + // Set a custom handler that overrides all responses |
| 132 | + setMockHandler((method, url) => { |
| 133 | + return { |
| 134 | + custom_handler: true, |
| 135 | + method: method, |
| 136 | + url: url, |
| 137 | + timestamp: new Date().toISOString(), |
| 138 | + message: 'This response was generated by a custom handler' |
| 139 | + }; |
| 140 | + }); |
| 141 | + |
| 142 | + try { |
| 143 | + // Test various endpoints with custom handler |
| 144 | + console.log('\n-- Testing with custom handler --'); |
| 145 | + |
| 146 | + const scrapeResult = await scrape(API_KEY, 'https://example.com'); |
| 147 | + console.log('Scrape with custom handler:', scrapeResult); |
| 148 | + |
| 149 | + const smartResult = await smartScraper(API_KEY, 'https://example.com', 'Test prompt'); |
| 150 | + console.log('SmartScraper with custom handler:', smartResult); |
| 151 | + |
| 152 | + const credits = await getCredits(API_KEY); |
| 153 | + console.log('Credits with custom handler:', credits); |
| 154 | + |
| 155 | + } catch (error) { |
| 156 | + console.error('Error in custom handler:', error.message); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Per-request mock mode (without global enable) |
| 162 | + */ |
| 163 | +async function perRequestMockMode() { |
| 164 | + console.log('\n=== Per-Request Mock Mode ==='); |
| 165 | + |
| 166 | + // Disable global mock mode |
| 167 | + disableMock(); |
| 168 | + |
| 169 | + try { |
| 170 | + // Test individual requests with mock enabled |
| 171 | + console.log('\n-- Testing per-request mock mode --'); |
| 172 | + |
| 173 | + const scrapeResult = await scrape(API_KEY, 'https://example.com', { mock: true }); |
| 174 | + console.log('Per-request mock scrape:', scrapeResult); |
| 175 | + |
| 176 | + const smartResult = await smartScraper(API_KEY, 'https://example.com', 'Test', null, null, null, null, { mock: true }); |
| 177 | + console.log('Per-request mock smartScraper:', smartResult); |
| 178 | + |
| 179 | + const scrapeStatus = await getScrapeRequest(API_KEY, 'test-id', { mock: true }); |
| 180 | + console.log('Per-request mock getScrapeRequest:', scrapeStatus); |
| 181 | + |
| 182 | + } catch (error) { |
| 183 | + console.error('Error in per-request mock mode:', error.message); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * Test all available endpoints in mock mode |
| 189 | + */ |
| 190 | +async function testAllEndpoints() { |
| 191 | + console.log('\n=== Testing All Endpoints in Mock Mode ==='); |
| 192 | + |
| 193 | + enableMock(); |
| 194 | + |
| 195 | + try { |
| 196 | + // Test all available endpoints |
| 197 | + console.log('\n-- Testing all endpoints --'); |
| 198 | + |
| 199 | + // Scrape endpoints |
| 200 | + const scrapeResult = await scrape(API_KEY, 'https://example.com'); |
| 201 | + console.log('Scrape:', scrapeResult.request_id ? '✅' : '❌'); |
| 202 | + |
| 203 | + const scrapeStatus = await getScrapeRequest(API_KEY, 'mock-id'); |
| 204 | + console.log('GetScrapeRequest:', scrapeStatus.status ? '✅' : '❌'); |
| 205 | + |
| 206 | + // SmartScraper endpoints |
| 207 | + const smartResult = await smartScraper(API_KEY, 'https://example.com', 'Extract title'); |
| 208 | + console.log('SmartScraper:', smartResult.request_id ? '✅' : '❌'); |
| 209 | + |
| 210 | + const smartStatus = await getSmartScraperRequest(API_KEY, 'mock-id'); |
| 211 | + console.log('GetSmartScraperRequest:', smartStatus.status ? '✅' : '❌'); |
| 212 | + |
| 213 | + // SearchScraper endpoints |
| 214 | + const searchResult = await searchScraper(API_KEY, 'Search for information'); |
| 215 | + console.log('SearchScraper:', searchResult.request_id ? '✅' : '❌'); |
| 216 | + |
| 217 | + const searchStatus = await getSearchScraperRequest(API_KEY, 'mock-id'); |
| 218 | + console.log('GetSearchScraperRequest:', searchStatus.status ? '✅' : '❌'); |
| 219 | + |
| 220 | + // Markdownify endpoints |
| 221 | + const markdownResult = await markdownify(API_KEY, 'https://example.com'); |
| 222 | + console.log('Markdownify:', markdownResult.request_id ? '✅' : '❌'); |
| 223 | + |
| 224 | + const markdownStatus = await getMarkdownifyRequest(API_KEY, 'mock-id'); |
| 225 | + console.log('GetMarkdownifyRequest:', markdownStatus.status ? '✅' : '❌'); |
| 226 | + |
| 227 | + // Crawl endpoints |
| 228 | + const crawlResult = await crawl(API_KEY, 'https://example.com'); |
| 229 | + console.log('Crawl:', crawlResult.crawl_id ? '✅' : '❌'); |
| 230 | + |
| 231 | + const crawlStatus = await getCrawlRequest(API_KEY, 'mock-id'); |
| 232 | + console.log('GetCrawlRequest:', crawlStatus.status ? '✅' : '❌'); |
| 233 | + |
| 234 | + // AgenticScraper endpoints |
| 235 | + const agenticResult = await agenticScraper(API_KEY, 'https://example.com', ['click button']); |
| 236 | + console.log('AgenticScraper:', agenticResult.request_id ? '✅' : '❌'); |
| 237 | + |
| 238 | + const agenticStatus = await getAgenticScraperRequest(API_KEY, 'mock-id'); |
| 239 | + console.log('GetAgenticScraperRequest:', agenticStatus.status ? '✅' : '❌'); |
| 240 | + |
| 241 | + // Utility endpoints |
| 242 | + const credits = await getCredits(API_KEY); |
| 243 | + console.log('GetCredits:', credits.remaining_credits ? '✅' : '❌'); |
| 244 | + |
| 245 | + const feedback = await submitFeedback(API_KEY, 'mock-id', 5, 'Great!'); |
| 246 | + console.log('SubmitFeedback:', feedback.status ? '✅' : '❌'); |
| 247 | + |
| 248 | + } catch (error) { |
| 249 | + console.error('Error testing endpoints:', error.message); |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +/** |
| 254 | + * Environment variable activation test |
| 255 | + */ |
| 256 | +async function testEnvironmentActivation() { |
| 257 | + console.log('\n=== Environment Variable Activation Test ==='); |
| 258 | + |
| 259 | + console.log('Current SGAI_MOCK value:', process.env.SGAI_MOCK || 'not set'); |
| 260 | + |
| 261 | + // Reinitialize mock config to check environment |
| 262 | + initMockConfig(); |
| 263 | + |
| 264 | + try { |
| 265 | + const credits = await getCredits(API_KEY); |
| 266 | + console.log('Credits with env check:', credits); |
| 267 | + } catch (error) { |
| 268 | + console.error('Error in environment test:', error.message); |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +/** |
| 273 | + * Main function to run all examples |
| 274 | + */ |
| 275 | +async function main() { |
| 276 | + console.log('🧪 ScrapeGraph AI SDK - Mock Mode Examples'); |
| 277 | + console.log('=========================================='); |
| 278 | + |
| 279 | + try { |
| 280 | + await basicMockUsage(); |
| 281 | + await mockWithCustomResponses(); |
| 282 | + await mockWithCustomHandler(); |
| 283 | + await perRequestMockMode(); |
| 284 | + await testAllEndpoints(); |
| 285 | + await testEnvironmentActivation(); |
| 286 | + |
| 287 | + console.log('\n✅ All mock mode examples completed successfully!'); |
| 288 | + |
| 289 | + } catch (error) { |
| 290 | + console.error('\n❌ Error running examples:', error.message); |
| 291 | + } |
| 292 | +} |
| 293 | + |
| 294 | +// Run the examples |
| 295 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 296 | + main(); |
| 297 | +} |
0 commit comments