Skip to content

Commit 112d932

Browse files
committed
[Examples] Add pagination TS ESM Node example
1 parent 7599217 commit 112d932

6 files changed

Lines changed: 275 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_KEY=YOUR_API_KEY
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Node.js pagination TypeScript (ESM) example
2+
3+
## Usage
4+
5+
1. Build module
6+
7+
```bash
8+
cd ../../../ # Navigate to the root folder
9+
deno task npm
10+
```
11+
12+
2. Setup environment variables
13+
14+
- Duplicate `.env.example` and name it `.env`.
15+
- Replace `YOUR_API_KEY` with your SerpApi API key.
16+
17+
3. Install dependencies and run example
18+
19+
```bash
20+
cd examples/node/pagination_ts_esm
21+
npm i
22+
npm start
23+
```
24+
25+
## Notes
26+
27+
- If you want to run the example without building the module, you can update
28+
`package.json` to depend on the published `serpapi` npm module instead:
29+
```json
30+
{
31+
"type": "module",
32+
"dependencies": {
33+
"dotenv": "*",
34+
"serpapi": "*" // Relies on the npm module
35+
},
36+
"devDependencies": {
37+
"@types/node": "*",
38+
"typescript": "*"
39+
},
40+
"scripts": {
41+
"start": "npx ts-node example.ts"
42+
}
43+
}
44+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import * as Dotenv from "dotenv";
2+
import { config, getJson, GoogleParameters } from "serpapi";
3+
4+
Dotenv.config();
5+
const apiKey = process.env.API_KEY;
6+
7+
const extractLinks = (results: { link: string }[]) =>
8+
results.map((r) => r.link);
9+
10+
const params = {
11+
q: "Coffee",
12+
api_key: apiKey,
13+
} satisfies GoogleParameters;
14+
15+
// Pagination (async/await)
16+
let page1 = await getJson("google", params);
17+
console.log(
18+
"First page links",
19+
extractLinks(page1.organic_results),
20+
);
21+
let page2 = await page1.next?.();
22+
console.log(
23+
"Second page links",
24+
extractLinks(page2?.organic_results),
25+
);
26+
27+
// Pagination (callback)
28+
getJson("google", params, (page1) => {
29+
console.log(
30+
"First page links",
31+
extractLinks(page1.organic_results),
32+
);
33+
page1.next?.((page2) => {
34+
console.log(
35+
"Second page links",
36+
extractLinks(page2.organic_results),
37+
);
38+
});
39+
});
40+
41+
// Use global config
42+
config.api_key = apiKey;
43+
page1 = await getJson("google", { q: "Coffee" });
44+
page2 = await page1.next?.();
45+
console.log(
46+
"Second page links",
47+
extractLinks(page2?.organic_results),
48+
);
49+
50+
// Pagination loop (async/await)
51+
let links: string[] = [];
52+
let page;
53+
page = await getJson("google", { q: "Coffee" });
54+
while (page) {
55+
links.push(...extractLinks(page.organic_results));
56+
if (links.length >= 30) break;
57+
page = await page.next?.();
58+
}
59+
console.log(links);
60+
61+
// Pagination loop (callback)
62+
links = [];
63+
getJson("google", { q: "Coffee" }, (page) => {
64+
links.push(...extractLinks(page.organic_results));
65+
if (links.length < 30 && page.next) {
66+
page.next();
67+
} else {
68+
console.log(links);
69+
}
70+
});

examples/node/pagination_ts_esm/package-lock.json

Lines changed: 136 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"type": "module",
3+
"dependencies": {
4+
"dotenv": "*",
5+
"serpapi": "../../../npm"
6+
},
7+
"devDependencies": {
8+
"@types/node": "*",
9+
"typescript": "*"
10+
},
11+
"scripts": {
12+
"start": "npx ts-node example.ts"
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ESNext",
4+
"target": "ESNext",
5+
"moduleResolution": "Node"
6+
},
7+
"ts-node": {
8+
"esm": true
9+
}
10+
}

0 commit comments

Comments
 (0)