Skip to content

Commit fa2a4aa

Browse files
committed
add toonify endpoint
1 parent 04394d2 commit fa2a4aa

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

scrapegraph-js/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export { sendFeedback } from './src/feedback.js';
99
export { crawl, getCrawlRequest } from './src/crawl.js';
1010
export { generateSchema, getSchemaStatus, pollSchemaGeneration } from './src/schema.js';
1111
export { sitemap } from './src/sitemap.js';
12+
export { toonify } from './src/toonify.js';
1213
export {
1314
createScheduledJob,
1415
getScheduledJobs,

scrapegraph-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "scrapegraph-js",
33
"author": "ScrapeGraphAI",
4-
"version": "0.2.3",
4+
"version": "0.2.4",
55
"description": "Scrape and extract structured data from a webpage using ScrapeGraphAI's APIs. Supports cookies for authentication, infinite scrolling, and pagination.",
66
"repository": {
77
"type": "git",

scrapegraph-js/src/toonify.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import axios from 'axios';
2+
import handleError from './utils/handleError.js';
3+
import { isMockEnabled, getMockConfig } from './utils/mockConfig.js';
4+
import { getMockResponse } from './utils/mockResponse.js';
5+
6+
/**
7+
* Converts data to toon format.
8+
*
9+
* @param {string} apiKey - Your ScrapeGraph AI API key.
10+
* @param {Object} data - The data object to be converted to toon format.
11+
* @param {Object} options - Optional configuration options.
12+
* @param {boolean} options.mock - Override mock mode for this request
13+
* @returns {Promise<Object>} A promise that resolves to the toonified data response.
14+
* @throws {Error} Throws an error if the HTTP request fails.
15+
*
16+
* @example
17+
* // Example usage:
18+
* const apiKey = 'your-api-key';
19+
* const data = {
20+
* products: [
21+
* { sku: "LAP-001", name: "Gaming Laptop", price: 1299.99 },
22+
* { sku: "MOU-042", name: "Wireless Mouse", price: 29.99 }
23+
* ]
24+
* };
25+
*
26+
* try {
27+
* const result = await toonify(apiKey, data);
28+
* console.log('Toonified result:', result);
29+
* } catch (error) {
30+
* console.error('Error toonifying data:', error);
31+
* }
32+
*/
33+
export async function toonify(apiKey, data, options = {}) {
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 toonify request');
41+
const mockConfig = getMockConfig();
42+
const mockData = getMockResponse('POST', 'https://api.scrapegraphai.com/v1/toonify', mockConfig.customResponses, mockConfig.customHandler);
43+
return mockData;
44+
}
45+
46+
const endpoint = 'https://api.scrapegraphai.com/v1/toonify';
47+
const headers = {
48+
'accept': 'application/json',
49+
'SGAI-APIKEY': apiKey,
50+
'Content-Type': 'application/json',
51+
};
52+
53+
try {
54+
const response = await axios.post(endpoint, data, { headers });
55+
return response.data;
56+
} catch (error) {
57+
handleError(error);
58+
}
59+
}

0 commit comments

Comments
 (0)