Skip to content

Commit 5903759

Browse files
authored
Merge pull request #8 from ScrapeGraphAI/js-schema-implementation
Implemented support for requests with schema.
2 parents 34777af + baf933b commit 5903759

11 files changed

+94
-37
lines changed

package.json

-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
},
1010
"author": "ScrapeGraphAI",
1111
"license": "MIT",
12-
"workspaces": [
13-
"scrapegraph-js"
14-
],
1512
"scripts": {
1613
"semantic-release": "semantic-release"
1714
},

scrapegraph-js/examples/.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# ScrapegraphAI API Key
2-
SGAI-APIKEY="your ScrapegraphAI API Key"
2+
SGAI_APIKEY="your ScrapegraphAI API Key"

scrapegraph-js/examples/getCredits_example.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { getCredits } from 'scrapegraph-sdk';
1+
import { getCredits } from 'scrapegraph-js';
22
import 'dotenv/config';
33

4-
try {
5-
const apiKey = process.env.SGAI_APIKEY;
4+
const apiKey = process.env.SGAI_APIKEY;
65

6+
try {
77
const myCredit = await getCredits(apiKey);
8-
98
console.log(myCredit)
109
} catch (error) {
1110
console.error(error)

scrapegraph-js/examples/getSmartScraperRequest_example.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { getSmartScraperRequest } from 'scrapegraph-sdk';
1+
import { getSmartScraperRequest } from 'scrapegraph-js';
22
import 'dotenv/config';
33

4-
try {
5-
const apiKey = process.env.SGAI_APIKEY;
6-
const requestId = '3fa85f64-5717-4562-b3fc-2c963f66afa6'
4+
const apiKey = process.env.SGAI_APIKEY;
5+
const requestId = '3fa85f64-5717-4562-b3fc-2c963f66afa6'
76

7+
try {
88
const requestInfo = await getSmartScraperRequest(apiKey, requestId);
9-
109
console.log(requestInfo);
1110
} catch (error) {
1211
console.error(error);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { smartScraper } from 'scrapegraph-js';
2+
import { z } from 'zod';
3+
import 'dotenv/config';
4+
5+
const apiKey = process.env.SGAI_APIKEY;
6+
const url = 'https://scrapegraphai.com/';
7+
const prompt = 'What does the company do? and ';
8+
9+
const schema = 2;
10+
11+
try {
12+
const response = await smartScraper(apiKey, url, prompt, schema);
13+
console.log(response.result);
14+
} catch (error) {
15+
console.error(error);
16+
}

scrapegraph-js/examples/sendFeedback_example.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { sendFeedback } from 'scrapegraph-sdk';
1+
import { sendFeedback } from 'scrapegraph-js';
22
import 'dotenv/config';
33

4-
try {
5-
const apiKey = process.env.SGAI_APIKEY;
6-
const requestId = '16a63a80-c87f-4cde-b005-e6c3ecda278b';
7-
const rating = 5;
8-
const feedbackMessage = 'This is a test feedback message.';
4+
const apiKey = process.env.SGAI_APIKEY;
5+
const requestId = '16a63a80-c87f-4cde-b005-e6c3ecda278b';
6+
const rating = 5;
7+
const feedbackMessage = 'This is a test feedback message.';
98

9+
try {
1010
const feedback_response = await sendFeedback(apiKey, requestId, rating, feedbackMessage);
1111
console.log(feedback_response);
1212
} catch (error) {

scrapegraph-js/examples/smartScraper_example.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { smartScraper } from 'scrapegraph-sdk';
1+
import { smartScraper } from 'scrapegraph-js';
22
import 'dotenv/config';
33

4-
try {
5-
const apiKey = process.env.SGAI_APIKEY;
6-
const url = 'https://scrapegraphai.com';
7-
const prompt = 'What does the company do?';
4+
const apiKey = process.env.SGAI_APIKEY;
5+
const url = 'https://scrapegraphai.com';
6+
const prompt = 'What does the company do?';
87

8+
try {
99
const response = await smartScraper(apiKey, url, prompt);
10-
1110
console.log(response);
1211
} catch (error) {
1312
console.error(error);

scrapegraph-js/package-lock.json

+23-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scrapegraph-js/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"type": "module",
2525
"dependencies": {
2626
"axios": "^1.6.0",
27-
"zod": "^3.23.8"
27+
"zod": "^3.23.8",
28+
"zod-to-json-schema": "^3.23.5"
2829
},
2930
"devDependencies": {
3031
"dotenv": "^16.4.5"

scrapegraph-js/readme.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,34 @@ const prompt = 'Extract the main heading and description.';
7777
```
7878

7979
#### Scraping with Custom Output Schema
80+
> [!NOTE]
81+
> To use this feature, it is necessary to employ the [Zod](https://www.npmjs.com/package/zod) package for schema creation.
82+
83+
Here is a real-world example:
8084

8185
```javascript
82-
//TODO
86+
import { smartScraper } from 'scrapegraph-js';
87+
import { z } from 'zod';
88+
import 'dotenv/config';
89+
90+
const apiKey = 'your-api-key';
91+
const url = 'https://scrapegraphai.com/';
92+
const prompt = 'What does the company do? and ';
93+
94+
const schema = z.object({
95+
title: z.string().describe('The title of the webpage'),
96+
description: z.string().describe('The description of the webpage'),
97+
summary: z.string().describe('A brief summary of the webpage')
98+
});
99+
100+
(async () => {
101+
try {
102+
const response = await smartScraper(apiKey, url, prompt, schema);
103+
console.log(response.result);
104+
} catch (error) {
105+
console.error('Error:', error);
106+
}
107+
})();
83108
```
84109

85110
### Checking API Credits

scrapegraph-js/src/smartScraper.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import axios from 'axios';
2-
import handleError from './utils/handleError.js'
2+
import handleError from './utils/handleError.js';
3+
import { ZodType } from 'zod';
4+
import { zodToJsonSchema } from 'zod-to-json-schema';
35

46
/**
57
* Scrape and extract structured data from a webpage using ScrapeGraph AI.
@@ -25,12 +27,11 @@ export async function smartScraper(apiKey, url, prompt, schema = null) {
2527
};
2628

2729
if (schema) {
28-
payload.output_schema = {
29-
description: schema.title || 'Schema',
30-
name: schema.title || 'Schema',
31-
properties: schema.properties || {},
32-
required: schema.required || []
33-
};
30+
if (schema instanceof ZodType) {
31+
payload.output_schema = zodToJsonSchema(schema);
32+
} else {
33+
throw new Error('The schema must be an instance of a valid Zod schema');
34+
}
3435
}
3536

3637
try {

0 commit comments

Comments
 (0)