@@ -49,7 +49,7 @@ import { getJson } from "https://deno.land/x/serpapi/mod.ts";
4949- Promises and async/await support.
5050- Callbacks support.
5151- [ Examples in JavaScript/TypeScript on Node.js/Deno using ESM/CommonJS, and more] ( https://github.com/serpapi/serpapi-javascript/tree/master/examples ) .
52- - (Planned) Pagination support.
52+ - [ Pagination support] ( #pagination ) .
5353- (Planned) More error classes.
5454
5555## Configuration
@@ -72,6 +72,33 @@ await getJson("google", { q: "coffee" }); // uses the API key defined in the con
7272await getJson (" google" , { api_key: API_KEY_2 , q: " coffee" }); // API_KEY_2 will be used
7373```
7474
75+ ## Pagination
76+
77+ Search engines handle pagination in several different ways. Some rely on an
78+ "offset" value to return results starting from a specific index, while some
79+ others rely on the typical notion of a "page". These are often combined with a
80+ "size" value to define how many results are returned in a search.
81+
82+ This module helps you handle pagination easily. After receiving search results
83+ from ` getJson ` , simply call the ` next() ` method on the returned object to
84+ retrieve the next page of results. If there is no ` next() ` method, then either
85+ pagination is not supported for the search engine or there are no more pages to
86+ be retrieved.
87+
88+ ``` js
89+ const page1 = await getJson (" google" , { q: " coffee" , start: 15 });
90+ const page2 = await page1 .next ? .();
91+ ` ` `
92+
93+ You may pass in the engine's supported pagination parameters as per normal. In
94+ the above example, the first page contains the 15th to the 24th result while the
95+ second page contains the 25th to the 34th result.
96+
97+ Note that if you set ` no_cache` to ` true ` , all subsequent ` next ()` calls will
98+ not return cached results.
99+
100+ Refer to the [` getJson` definition below](#getjson) for more examples.
101+
75102## Functions
76103
77104<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
@@ -102,6 +129,8 @@ await getJson("google", { api_key: API_KEY_2, q: "coffee" }); // API_KEY_2 will
102129Get a JSON response based on search parameters.
103130
104131- Accepts an optional callback.
132+ - Get the next page of results by calling the ` .next ()` method on the returned
133+ response object.
105134
106135#### Parameters
107136
@@ -116,13 +145,50 @@ Get a JSON response based on search parameters.
116145#### Examples
117146
118147` ` ` javascript
119- // async/await
148+ // single call ( async/await)
120149const json = await getJson (" google" , { api_key: API_KEY , q: " coffee" });
121150
122- // callback
151+ // single call ( callback)
123152getJson (" google" , { api_key: API_KEY , q: " coffee" }, console .log );
124153` ` `
125154
155+ ` ` ` javascript
156+ // pagination (async/await)
157+ const page1 = await getJson (" google" , { q: " coffee" , start: 15 });
158+ const page2 = await page1 .next ? .();
159+ ` ` `
160+
161+ ` ` ` javascript
162+ // pagination (callback)
163+ getJson (" google" , { q: " coffee" , start: 15 }, (page1 ) => {
164+ page1 .next ? .((page2 ) => {
165+ console .log (page2);
166+ });
167+ });
168+ ` ` `
169+
170+ ` ` ` javascript
171+ // pagination loop (async/await)
172+ const organicResults = [];
173+ let page = await getJson (" google" , { api_key: API_KEY , q: " coffee" });
174+ while (page) {
175+ organicResults .push (... page .organic_results );
176+ if (organicResults .length >= 30 ) break ;
177+ page = await page .next ? .();
178+ }
179+ ` ` `
180+
181+ ` ` ` javascript
182+ // pagination loop (callback)
183+ const organicResults = [];
184+ getJson (" google" , { api_key: API_KEY , q: " coffee" }, (page ) => {
185+ organicResults .push (... page .organic_results );
186+ if (organicResults .length < 30 && page .next ) {
187+ page .next ();
188+ }
189+ });
190+ ` ` `
191+
126192### getHtml
127193
128194Get a HTML response based on search parameters.
0 commit comments