Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7599217

Browse filesBrowse files
committed
[Examples] Add pagination Deno TS example
1 parent 02e1539 commit 7599217
Copy full SHA for 7599217

3 files changed

+97Lines changed: 97 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_KEY=YOUR_API_KEY
Collapse file
+27Lines changed: 27 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Deno pagination TypeScript example
2+
3+
## Usage
4+
5+
1. Setup environment variables
6+
7+
- Duplicate `.env.example` and name it `.env`.
8+
- Replace `YOUR_API_KEY` with your SerpApi API key.
9+
10+
2. Run the example
11+
12+
```
13+
deno run example.ts
14+
```
15+
16+
## Notes
17+
18+
- Imports rely on `mod.ts` found in the root folder.
19+
- To import the published module from deno.land, update the import to the
20+
following:
21+
```ts
22+
import {
23+
config,
24+
getJson,
25+
GoogleParameters,
26+
} from "https://deno.land/x/serpapi/mod.ts";
27+
```
Collapse file
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
2+
import { config, getJson, GoogleParameters } from "../../../mod.ts";
3+
4+
const { API_KEY: apiKey } = loadSync();
5+
6+
const extractLinks = (results: { link: string }[]) =>
7+
results.map((r) => r.link);
8+
9+
const params = {
10+
q: "Coffee",
11+
api_key: apiKey,
12+
} satisfies GoogleParameters;
13+
14+
// Pagination (async/await)
15+
let page1 = await getJson("google", params);
16+
console.log(
17+
"First page links",
18+
extractLinks(page1.organic_results),
19+
);
20+
let page2 = await page1.next?.();
21+
console.log(
22+
"Second page links",
23+
extractLinks(page2?.organic_results),
24+
);
25+
26+
// Pagination (callback)
27+
getJson("google", params, (page1) => {
28+
console.log(
29+
"First page links",
30+
extractLinks(page1.organic_results),
31+
);
32+
page1.next?.((page2) => {
33+
console.log(
34+
"Second page links",
35+
extractLinks(page2.organic_results),
36+
);
37+
});
38+
});
39+
40+
// Use global config
41+
config.api_key = apiKey;
42+
page1 = await getJson("google", { q: "Coffee" });
43+
page2 = await page1.next?.();
44+
console.log(
45+
"Second page links",
46+
extractLinks(page2?.organic_results),
47+
);
48+
49+
// Pagination loop (async/await)
50+
let links: string[] = [];
51+
let page;
52+
page = await getJson("google", { q: "Coffee" });
53+
while (page) {
54+
links.push(...extractLinks(page.organic_results));
55+
if (links.length >= 30) break;
56+
page = await page.next?.();
57+
}
58+
console.log(links);
59+
60+
// Pagination loop (callback)
61+
links = [];
62+
getJson("google", { q: "Coffee" }, (page) => {
63+
links.push(...extractLinks(page.organic_results));
64+
if (links.length < 30 && page.next) {
65+
page.next();
66+
} else {
67+
console.log(links);
68+
}
69+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.