Skip to content

Commit 405f798

Browse files
authored
Merge pull request #17 from serpapi/remove-pagination
Remove pagination support
2 parents 06a90d5 + 646b1cf commit 405f798

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+266
-2924
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and this project adheres to
2626
- Remove all types for engine parameters and responses. SerpApi's
2727
[documentation](https://serpapi.com/search-api) should be the only source of
2828
truth for valid engines and their parameters.
29+
- Remove pagination support.
2930

3031
## [1.1.1] - 2023-02-15
3132

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ To run [examples](./examples/) on your local source files, follow these steps.
111111
"serpapi": "../../../npm"
112112
},
113113
"scripts": {
114-
"start": "node example.js"
114+
"start": "node basic_example.js"
115115
}
116116
}
117117
```

README.md

Lines changed: 15 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ more.
1818
### Node.js
1919

2020
- Supports Node.js 7.10.1 and newer.
21-
- Refer to [this example](examples/node/basic_js_node_7_up) for help.
21+
- Refer to [this example](examples/node/js_node_7_up) for help.
2222

2323
```bash
2424
npm install serpapi
@@ -40,7 +40,7 @@ getJson({
4040

4141
- If you prefer using the `import` syntax and top-level `await`, you need to use
4242
at least Node.js 14.8.0.
43-
- Refer to [this example](examples/node/basic_js_node_14_up) for help.
43+
- Refer to [this example](examples/node/js_node_14_up) for help.
4444

4545
You will need to add `"type": "module"` to your `package.json`:
4646

@@ -66,10 +66,17 @@ console.log(response);
6666

6767
- Import directly from deno.land.
6868
- Usage is otherwise the same as above.
69-
- Refer to [this example](examples/deno/basic_ts) for help.
69+
- Refer to [this example](examples/deno) for help.
7070

7171
```ts
7272
import { getJson } from "https://deno.land/x/serpapi/mod.ts";
73+
const response = await getJson({
74+
engine: "google",
75+
api_key: API_KEY, // Get your API_KEY from https://serpapi.com/manage-api-key
76+
q: "coffee",
77+
location: "Austin, Texas",
78+
});
79+
console.log(response);
7380
```
7481

7582
## Features
@@ -80,8 +87,6 @@ import { getJson } from "https://deno.land/x/serpapi/mod.ts";
8087
- Promises and async/await support.
8188
- Callbacks support.
8289
- [Examples in JavaScript/TypeScript on Node.js/Deno using ESM/CommonJS, and more](https://github.com/serpapi/serpapi-javascript/tree/master/examples).
83-
- [Pagination support](#pagination).
84-
- (Planned) More error classes.
8590

8691
## Configuration
8792

@@ -105,30 +110,12 @@ await getJson({ engine: "google", api_key: API_KEY_2, q: "coffee" }); // API_KEY
105110

106111
## Pagination
107112

108-
Search engines handle pagination in several different ways. Some rely on an
109-
"offset" value to return results starting from a specific index, while some
110-
others rely on the typical notion of a "page". These are often combined with a
111-
"size" value to define how many results are returned in a search.
113+
Built-in pagination is not supported. Please refer to our pagination examples
114+
for a manual approach:
112115

113-
This module helps you handle pagination easily. After receiving search results
114-
from `getJson`, simply call the `next()` method on the returned object to
115-
retrieve the next page of results. If there is no `next()` method, then either
116-
pagination is not supported for the search engine or there are no more pages to
117-
be retrieved.
118-
119-
```js
120-
const page1 = await getJson({ engine: "google", q: "coffee", start: 15 });
121-
const page2 = await page1.next?.();
122-
```
123-
124-
You may pass in the engine's supported pagination parameters as per normal. In
125-
the above example, the first page contains the 15th to the 24th result while the
126-
second page contains the 25th to the 34th result.
127-
128-
Note that if you set `no_cache` to `true`, all subsequent `next()` calls will
129-
not return cached results.
130-
131-
Refer to the [`getJson` definition below](#getjson) for more examples.
116+
- [Pagination example (Node.js >= 7)](examples/node/js_node_7_up/pagination_example.js)
117+
- [Pagination example (Node.js >= 14)](examples/node/js_node_14_up/pagination_example.js)
118+
- [Pagination example (Deno)](examples/deno/pagination_example.ts)
132119

133120
## Functions
134121

@@ -159,10 +146,6 @@ Refer to the [`getJson` definition below](#getjson) for more examples.
159146

160147
Get a JSON response based on search parameters.
161148

162-
- Accepts an optional callback.
163-
- Get the next page of results by calling the `.next()` method on the returned
164-
response object.
165-
166149
#### Parameters
167150

168151
- `parameters`
@@ -180,43 +163,6 @@ const json = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });
180163
getJson({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);
181164
```
182165

183-
```javascript
184-
// pagination (async/await)
185-
const page1 = await getJson({ engine: "google", q: "coffee", start: 15 });
186-
const page2 = await page1.next?.();
187-
```
188-
189-
```javascript
190-
// pagination (callback)
191-
getJson({ engine: "google", q: "coffee", start: 15 }, (page1) => {
192-
page1.next?.((page2) => {
193-
console.log(page2);
194-
});
195-
});
196-
```
197-
198-
```javascript
199-
// pagination loop (async/await)
200-
const organicResults = [];
201-
let page = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });
202-
while (page) {
203-
organicResults.push(...page.organic_results);
204-
if (organicResults.length >= 30) break;
205-
page = await page.next?.();
206-
}
207-
```
208-
209-
```javascript
210-
// pagination loop (callback)
211-
const organicResults = [];
212-
getJson({ engine: "google", api_key: API_KEY, q: "coffee" }, (page) => {
213-
organicResults.push(...page.organic_results);
214-
if (organicResults.length < 30 && page.next) {
215-
page.next();
216-
}
217-
});
218-
```
219-
220166
### getHtml
221167

222168
Get a HTML response based on search parameters.

deno.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
"dom.iterable",
2929
"deno.ns"
3030
]
31-
}
31+
},
32+
"lock": false
3233
}

docs/migrating_from_google_search_results_nodejs.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,6 @@ migrate over to the `serpapi` npm package.
5252
`getHtml` functions instead.
5353
- The `SerpApiSearch` class is removed as a public class.
5454

55-
## Fixed
56-
57-
- Setting the `api_key` parameter to `null` works for unmetered queries.
58-
```js
59-
// ❌ Previously, error is thrown when api_key is undefined or null.
60-
const engine = new GoogleSearch();
61-
engine.json({ q: "coffee", api_key: undefined });
62-
63-
// ✅ Now, no error is thrown when api_key is null
64-
getJson({ engine: "google", q: "coffee", api_key: null });
65-
```
66-
6755
## Added
6856

6957
- TypeScript support.
File renamed without changes.

examples/deno/basic_ts/README.md renamed to examples/deno/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
2. Run the example
1111

1212
```
13-
deno run example.ts
13+
deno run basic_example.ts
14+
deno run pagination_example.ts
1415
```
1516

1617
## Notes

examples/deno/basic_ts/example.ts renamed to examples/deno/basic_example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
2-
import { config, getJson } from "../../../mod.ts";
2+
import { config, getJson } from "../../mod.ts";
33

44
const { API_KEY: apiKey } = loadSync();
55
const params = {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
2+
import { config, getJson } from "../../mod.ts";
3+
4+
const { API_KEY: apiKey } = loadSync();
5+
config.api_key = apiKey;
6+
7+
// Get the first page
8+
const page = await getJson({ engine: "google", q: "Coffee" });
9+
// Parse SerpApi search URL to the next page
10+
const nextUrl = new URL(page.serpapi_pagination.next);
11+
// Extract the request parameters
12+
const nextParams = Object.fromEntries(nextUrl.searchParams);
13+
// Get the next page
14+
const nextPage = await getJson(nextParams);
15+
console.log(nextPage);

examples/deno/pagination_ts/README.md

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)