Skip to content

Commit fd71ea2

Browse files
committed
Merge branch 'master' into widen-node-support
2 parents 0a04ae4 + 24b0192 commit fd71ea2

File tree

28 files changed

+630
-285
lines changed

28 files changed

+630
-285
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ and this project adheres to
1010

1111
### Added
1212

13-
- Expose `EngineName`, `EngineParameters` and `AllowArbitraryParams` types.
14-
- Add support for Node.js 7.10.1 and newer.
13+
- Expose `EngineParameters` type.
14+
- Expose `InvalidArgumentError` error.
1515

1616
### Changed
1717

README.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ You will need to add `"type": "module"` to your `package.json`:
5252

5353
```js
5454
import { getJson } from "serpapi";
55-
const response = await getJson("google", {
55+
const response = await getJson({
56+
engine: "google",
5657
api_key: API_KEY, // Get your API_KEY from https://serpapi.com/manage-api-key
5758
q: "coffee",
5859
location: "Austin, Texas",
@@ -97,8 +98,8 @@ import { config, getJson } from "serpapi";
9798
config.api_key = API_KEY;
9899
config.timeout = 60000;
99100

100-
await getJson("google", { q: "coffee" }); // uses the API key defined in the config
101-
await getJson("google", { api_key: API_KEY_2, q: "coffee" }); // API_KEY_2 will be used
101+
await getJson({ engine: "google", q: "coffee" }); // uses the API key defined in the config
102+
await getJson({ engine: "google", api_key: API_KEY_2, q: "coffee" }); // API_KEY_2 will be used
102103
```
103104

104105
## Pagination
@@ -115,7 +116,7 @@ pagination is not supported for the search engine or there are no more pages to
115116
be retrieved.
116117

117118
```js
118-
const page1 = await getJson("google", { q: "coffee", start: 15 });
119+
const page1 = await getJson({ engine: "google", q: "coffee", start: 15 });
119120
const page2 = await page1.next?.();
120121
```
121122
@@ -163,9 +164,6 @@ Get a JSON response based on search parameters.
163164
164165
#### Parameters
165166
166-
- `engine`
167-
**[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
168-
engine name
169167
- `parameters`
170168
**[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
171169
search query parameters for the engine
@@ -175,21 +173,21 @@ Get a JSON response based on search parameters.
175173
176174
```javascript
177175
// single call (async/await)
178-
const json = await getJson("google", { api_key: API_KEY, q: "coffee" });
176+
const json = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });
179177

180178
// single call (callback)
181-
getJson("google", { api_key: API_KEY, q: "coffee" }, console.log);
179+
getJson({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);
182180
```
183181
184182
```javascript
185183
// pagination (async/await)
186-
const page1 = await getJson("google", { q: "coffee", start: 15 });
184+
const page1 = await getJson({ engine: "google", q: "coffee", start: 15 });
187185
const page2 = await page1.next?.();
188186
```
189187
190188
```javascript
191189
// pagination (callback)
192-
getJson("google", { q: "coffee", start: 15 }, (page1) => {
190+
getJson({ engine: "google", q: "coffee", start: 15 }, (page1) => {
193191
page1.next?.((page2) => {
194192
console.log(page2);
195193
});
@@ -199,7 +197,7 @@ getJson("google", { q: "coffee", start: 15 }, (page1) => {
199197
```javascript
200198
// pagination loop (async/await)
201199
const organicResults = [];
202-
let page = await getJson("google", { api_key: API_KEY, q: "coffee" });
200+
let page = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });
203201
while (page) {
204202
organicResults.push(...page.organic_results);
205203
if (organicResults.length >= 30) break;
@@ -210,7 +208,7 @@ while (page) {
210208
```javascript
211209
// pagination loop (callback)
212210
const organicResults = [];
213-
getJson("google", { api_key: API_KEY, q: "coffee" }, (page) => {
211+
getJson({ engine: "google", api_key: API_KEY, q: "coffee" }, (page) => {
214212
organicResults.push(...page.organic_results);
215213
if (organicResults.length < 30 && page.next) {
216214
page.next();
@@ -227,9 +225,6 @@ Get a HTML response based on search parameters.
227225
228226
#### Parameters
229227
230-
- `engine`
231-
**[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
232-
engine name
233228
- `parameters`
234229
**[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
235230
search query parameters for the engine
@@ -239,10 +234,10 @@ Get a HTML response based on search parameters.
239234
240235
```javascript
241236
// async/await
242-
const html = await getHtml("google", { api_key: API_KEY, q: "coffee" });
237+
const html = await getHtml({ engine: "google", api_key: API_KEY, q: "coffee" });
243238

244239
// callback
245-
getHtml("google", { api_key: API_KEY, q: "coffee" }, console.log);
240+
getHtml({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);
246241
```
247242
248243
### getJsonBySearchId
@@ -274,7 +269,8 @@ Get a JSON response given a search ID.
274269
#### Examples
275270
276271
```javascript
277-
const response = await getJson("google", {
272+
const response = await getJson({
273+
engine: "google",
278274
api_key: API_KEY,
279275
async: true,
280276
q: "coffee",
@@ -319,7 +315,8 @@ Get a HTML response given a search ID.
319315
#### Examples
320316
321317
```javascript
322-
const response = await getJson("google", {
318+
const response = await getJson({
319+
engine: "google",
323320
api_key: API_KEY,
324321
async: true,
325322
q: "coffee",

docs/migrating_from_google_search_results_nodejs.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ migrate over to the `serpapi` npm package.
1717

1818
// ✅ New way, import and use functions directly.
1919
import { getJson } from "serpapi";
20-
getJson("google", { api_key: API_KEY, ... })
20+
getJson({ engine: "google", api_key: API_KEY, ... })
2121
```
2222

2323
- The `search_archive` method is replaced by `getJsonBySearchId` and
@@ -61,15 +61,15 @@ migrate over to the `serpapi` npm package.
6161
engine.json({ q: "coffee", api_key: undefined });
6262

6363
// ✅ Now, no error is thrown when api_key is null
64-
getJson("google", { q: "coffee", api_key: null });
64+
getJson({ engine: "google", q: "coffee", api_key: null });
6565
```
6666

6767
## Added
6868

6969
- TypeScript types for supported parameters.
7070
- First-class Promises support.
7171
```js
72-
const json = await getJson("google", { q: "coffee" });
72+
const json = await getJson({ engine: "google", q: "coffee" });
7373
```
7474
- `config` object to configure global `api_key` and `timeout` values.
7575
```js
@@ -79,6 +79,6 @@ migrate over to the `serpapi` npm package.
7979
```
8080
- Error classes (`MissingApiKeyError` and `InvalidTimeoutError`).
8181
```js
82-
getJson("google", { api_key: "" }); // Throws `MissingApiKeyError`
82+
getJson({ engine: "google", api_key: "" }); // Throws `MissingApiKeyError`
8383
getAccount({ api_key: API_KEY, timeout: 0 }); // Throws `InvalidTimeoutError`
8484
```

examples/deno/basic_ts/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ deno run example.ts
2020
following:
2121
```ts
2222
import {
23-
AllowArbitraryParams,
2423
config,
2524
getJson,
2625
GoogleParameters,

examples/deno/basic_ts/example.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
2-
import {
3-
AllowArbitraryParams,
4-
config,
5-
getJson,
6-
GoogleParameters,
7-
} from "../../../mod.ts";
2+
import { config, EngineParameters, getJson } from "../../../mod.ts";
83

94
const { API_KEY: apiKey } = loadSync();
105
const params = {
6+
engine: "google",
117
q: "Coffee",
128
api_key: apiKey,
13-
} satisfies AllowArbitraryParams<GoogleParameters>;
9+
} satisfies EngineParameters<"google">;
1410

1511
// Show result as JSON (async/await)
16-
const response1 = await getJson("google", params);
12+
const response1 = await getJson(params);
1713
console.log(response1["organic_results"]);
1814

1915
// Show result as JSON (callback)
20-
getJson("google", params, (json) => console.log(json["organic_results"]));
16+
getJson(params, (json) => console.log(json["organic_results"]));
2117

2218
// Use global config
2319
config.api_key = apiKey;
24-
const response2 = await getJson("google", { q: "Coffee" });
20+
const response2 = await getJson({ engine: "google", q: "Coffee" });
2521
console.log(response2["organic_results"]);

examples/deno/pagination_ts/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ deno run example.ts
2020
following:
2121
```ts
2222
import {
23-
AllowArbitraryParams,
2423
config,
2524
getJson,
2625
GoogleParameters,

examples/deno/pagination_ts/example.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
2-
import {
3-
AllowArbitraryParams,
4-
config,
5-
getJson,
6-
GoogleParameters,
7-
} from "../../../mod.ts";
2+
import { config, EngineParameters, getJson } from "../../../mod.ts";
83

94
const { API_KEY: apiKey } = loadSync();
105

116
const extractLinks = (results: { link: string }[]) =>
127
results.map((r) => r.link);
138

149
const params = {
10+
engine: "google",
1511
q: "Coffee",
1612
api_key: apiKey,
17-
} satisfies AllowArbitraryParams<GoogleParameters>;
13+
} satisfies EngineParameters<"google">;
1814

1915
// Pagination (async/await)
20-
let page1 = await getJson("google", params);
16+
let page1 = await getJson(params);
2117
console.log(
2218
"First page links",
2319
extractLinks(page1.organic_results),
@@ -29,7 +25,7 @@ console.log(
2925
);
3026

3127
// Pagination (callback)
32-
getJson("google", params, (page1) => {
28+
getJson(params, (page1) => {
3329
console.log(
3430
"First page links",
3531
extractLinks(page1.organic_results),
@@ -44,7 +40,7 @@ getJson("google", params, (page1) => {
4440

4541
// Use global config
4642
config.api_key = apiKey;
47-
page1 = await getJson("google", { q: "Coffee" });
43+
page1 = await getJson({ engine: "google", q: "Coffee" });
4844
page2 = await page1.next?.();
4945
console.log(
5046
"Second page links",
@@ -54,7 +50,7 @@ console.log(
5450
// Pagination loop (async/await)
5551
let links: string[] = [];
5652
let page;
57-
page = await getJson("google", { q: "Coffee" });
53+
page = await getJson({ engine: "google", q: "Coffee" });
5854
while (page) {
5955
links.push(...extractLinks(page.organic_results));
6056
if (links.length >= 30) break;
@@ -64,7 +60,7 @@ console.log(links);
6460

6561
// Pagination loop (callback)
6662
links = [];
67-
getJson("google", { q: "Coffee" }, (page) => {
63+
getJson({ engine: "google", q: "Coffee" }, (page) => {
6864
links.push(...extractLinks(page.organic_results));
6965
if (links.length < 30 && page.next) {
7066
page.next();

examples/node/basic_js_node_14_up/example.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ Dotenv.config();
1313
const apiKey = process.env.API_KEY;
1414

1515
const params = {
16+
engine: "google",
1617
q: "Coffee",
1718
api_key: apiKey,
1819
};
1920

2021
// Show result as JSON (async/await)
21-
const response1 = await getJson("google", params);
22+
const response1 = await getJson(params);
2223
console.log(response1["organic_results"]);
2324

2425
// Show result as JSON (callback)
25-
getJson("google", params, (json) => console.log(json["organic_results"]));
26+
getJson(params, (json) => console.log(json["organic_results"]));
2627

2728
// Use global config
2829
config.api_key = apiKey;
29-
const response2 = await getJson("google", { q: "Coffee" });
30+
const response2 = await getJson({ engine: "google", q: "Coffee" });
3031
console.log(response2["organic_results"]);

examples/node/basic_js_node_7_up/example.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ const apiKey = process.env.API_KEY;
1010

1111
const run = async () => {
1212
const params = {
13+
engine: "google",
1314
q: "Coffee",
1415
api_key: apiKey,
1516
};
1617

1718
// Show result as JSON (async/await)
18-
const response1 = await getJson("google", params);
19+
const response1 = await getJson(params);
1920
console.log(response1["organic_results"]);
2021

2122
// Show result as JSON (callback)
22-
getJson("google", params, (json) => console.log(json["organic_results"]));
23+
getJson(params, (json) => console.log(json["organic_results"]));
2324

2425
// Use global config
2526
config.api_key = apiKey;
26-
const response2 = await getJson("google", { q: "Coffee" });
27+
const response2 = await getJson({ engine: "google", q: "Coffee" });
2728
console.log(response2["organic_results"]);
2829
};
2930

examples/node/basic_ts_node_14_up/example.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@
77
*/
88

99
import * as Dotenv from "dotenv";
10-
import { AllowArbitraryParams, config, getJson, GoogleParameters } from "serpapi";
10+
import { config, EngineParameters, getJson } from "serpapi";
1111

1212
Dotenv.config();
1313
const apiKey = process.env.API_KEY;
1414

1515
const params = {
16+
engine: "google",
1617
q: "Coffee",
1718
api_key: apiKey,
18-
} satisfies AllowArbitraryParams<GoogleParameters>;
19+
} satisfies EngineParameters<"google">;
1920

2021
// Show result as JSON (async/await)
21-
const response1 = await getJson("google", params);
22+
const response1 = await getJson(params);
2223
console.log(response1["organic_results"]);
2324

2425
// Show result as JSON (callback)
25-
getJson("google", params, (json) => console.log(json["organic_results"]));
26+
getJson(params, (json) => console.log(json["organic_results"]));
2627

2728
// Use global config
2829
config.api_key = apiKey;
29-
const response2 = await getJson("google", { q: "Coffee" });
30+
const response2 = await getJson({ engine: "google", q: "Coffee" });
3031
console.log(response2["organic_results"]);

0 commit comments

Comments
 (0)