Skip to content

Commit 24b0192

Browse files
authored
Merge pull request #9 from serpapi/breaking-params-as-first-arg
Search parameters as first argument
2 parents db00dda + 91ad1ec commit 24b0192

28 files changed

Lines changed: 620 additions & 268 deletions

CHANGELOG.md

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

1111
### Added
1212

13-
- Expose `EngineName`, `EngineParameters` and `AllowArbitraryParams` types.
13+
- Expose `EngineParameters` type.
14+
- Expose `InvalidArgumentTypesError` error.
1415

1516
### Changed
1617

README.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ npm install serpapi
2525

2626
```js
2727
import { getJson } from "serpapi";
28-
const response = await getJson("google", {
28+
const response = await getJson({
29+
engine: "google",
2930
api_key: API_KEY, // Get your API_KEY from https://serpapi.com/manage-api-key
3031
q: "coffee",
3132
location: "Austin, Texas",
@@ -68,8 +69,8 @@ import { config, getJson } from "serpapi";
6869
config.api_key = API_KEY;
6970
config.timeout = 60000;
7071

71-
await getJson("google", { q: "coffee" }); // uses the API key defined in the config
72-
await getJson("google", { api_key: API_KEY_2, q: "coffee" }); // API_KEY_2 will be used
72+
await getJson({ engine: "google", q: "coffee" }); // uses the API key defined in the config
73+
await getJson({ engine: "google", api_key: API_KEY_2, q: "coffee" }); // API_KEY_2 will be used
7374
```
7475

7576
## Pagination
@@ -86,7 +87,7 @@ pagination is not supported for the search engine or there are no more pages to
8687
be retrieved.
8788

8889
```js
89-
const page1 = await getJson("google", { q: "coffee", start: 15 });
90+
const page1 = await getJson({ engine: "google", q: "coffee", start: 15 });
9091
const page2 = await page1.next?.();
9192
```
9293
@@ -134,9 +135,6 @@ Get a JSON response based on search parameters.
134135
135136
#### Parameters
136137
137-
- `engine`
138-
**[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
139-
engine name
140138
- `parameters`
141139
**[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
142140
search query parameters for the engine
@@ -146,21 +144,21 @@ Get a JSON response based on search parameters.
146144
147145
```javascript
148146
// single call (async/await)
149-
const json = await getJson("google", { api_key: API_KEY, q: "coffee" });
147+
const json = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });
150148

151149
// single call (callback)
152-
getJson("google", { api_key: API_KEY, q: "coffee" }, console.log);
150+
getJson({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);
153151
```
154152
155153
```javascript
156154
// pagination (async/await)
157-
const page1 = await getJson("google", { q: "coffee", start: 15 });
155+
const page1 = await getJson({ engine: "google", q: "coffee", start: 15 });
158156
const page2 = await page1.next?.();
159157
```
160158
161159
```javascript
162160
// pagination (callback)
163-
getJson("google", { q: "coffee", start: 15 }, (page1) => {
161+
getJson({ engine: "google", q: "coffee", start: 15 }, (page1) => {
164162
page1.next?.((page2) => {
165163
console.log(page2);
166164
});
@@ -170,7 +168,7 @@ getJson("google", { q: "coffee", start: 15 }, (page1) => {
170168
```javascript
171169
// pagination loop (async/await)
172170
const organicResults = [];
173-
let page = await getJson("google", { api_key: API_KEY, q: "coffee" });
171+
let page = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });
174172
while (page) {
175173
organicResults.push(...page.organic_results);
176174
if (organicResults.length >= 30) break;
@@ -181,7 +179,7 @@ while (page) {
181179
```javascript
182180
// pagination loop (callback)
183181
const organicResults = [];
184-
getJson("google", { api_key: API_KEY, q: "coffee" }, (page) => {
182+
getJson({ engine: "google", api_key: API_KEY, q: "coffee" }, (page) => {
185183
organicResults.push(...page.organic_results);
186184
if (organicResults.length < 30 && page.next) {
187185
page.next();
@@ -198,9 +196,6 @@ Get a HTML response based on search parameters.
198196
199197
#### Parameters
200198
201-
- `engine`
202-
**[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
203-
engine name
204199
- `parameters`
205200
**[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
206201
search query parameters for the engine
@@ -210,10 +205,10 @@ Get a HTML response based on search parameters.
210205
211206
```javascript
212207
// async/await
213-
const html = await getHtml("google", { api_key: API_KEY, q: "coffee" });
208+
const html = await getHtml({ engine: "google", api_key: API_KEY, q: "coffee" });
214209

215210
// callback
216-
getHtml("google", { api_key: API_KEY, q: "coffee" }, console.log);
211+
getHtml({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);
217212
```
218213
219214
### getJsonBySearchId
@@ -245,7 +240,8 @@ Get a JSON response given a search ID.
245240
#### Examples
246241
247242
```javascript
248-
const response = await getJson("google", {
243+
const response = await getJson({
244+
engine: "google",
249245
api_key: API_KEY,
250246
async: true,
251247
q: "coffee",
@@ -290,7 +286,8 @@ Get a HTML response given a search ID.
290286
#### Examples
291287
292288
```javascript
293-
const response = await getJson("google", {
289+
const response = await getJson({
290+
engine: "google",
294291
api_key: API_KEY,
295292
async: true,
296293
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
@@ -63,15 +63,15 @@ migrate over to the `serpapi` npm package.
6363
engine.json({ q: "coffee", api_key: undefined });
6464

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

6969
## Added
7070

7171
- TypeScript types for supported parameters.
7272
- First-class Promises support.
7373
```js
74-
const json = await getJson("google", { q: "coffee" });
74+
const json = await getJson({ engine: "google", q: "coffee" });
7575
```
7676
- `config` object to configure global `api_key` and `timeout` values.
7777
```js
@@ -81,6 +81,6 @@ migrate over to the `serpapi` npm package.
8181
```
8282
- Error classes (`MissingApiKeyError` and `InvalidTimeoutError`).
8383
```js
84-
getJson("google", { api_key: "" }); // Throws `MissingApiKeyError`
84+
getJson({ engine: "google", api_key: "" }); // Throws `MissingApiKeyError`
8585
getAccount({ api_key: API_KEY, timeout: 0 }); // Throws `InvalidTimeoutError`
8686
```

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_commonjs/example.js

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

77
const run = async () => {
88
const params = {
9+
engine: "google",
910
q: "Coffee",
1011
api_key: apiKey,
1112
};
1213

1314
// Show result as JSON (async/await)
14-
const response1 = await getJson("google", params);
15+
const response1 = await getJson(params);
1516
console.log(response1["organic_results"]);
1617

1718
// Show result as JSON (callback)
18-
getJson("google", params, (json) => console.log(json["organic_results"]));
19+
getJson(params, (json) => console.log(json["organic_results"]));
1920

2021
// Use global config
2122
config.api_key = apiKey;
22-
const response2 = await getJson("google", { q: "Coffee" });
23+
const response2 = await getJson({ engine: "google", q: "Coffee" });
2324
console.log(response2["organic_results"]);
2425
};
2526

examples/node/basic_js_esm/example.js

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

77
const params = {
8+
engine: "google",
89
q: "Coffee",
910
api_key: apiKey,
1011
};
1112

1213
// Show result as JSON (async/await)
13-
const response1 = await getJson("google", params);
14+
const response1 = await getJson(params);
1415
console.log(response1["organic_results"]);
1516

1617
// Show result as JSON (callback)
17-
getJson("google", params, (json) => console.log(json["organic_results"]));
18+
getJson(params, (json) => console.log(json["organic_results"]));
1819

1920
// Use global config
2021
config.api_key = apiKey;
21-
const response2 = await getJson("google", { q: "Coffee" });
22+
const response2 = await getJson({ engine: "google", q: "Coffee" });
2223
console.log(response2["organic_results"]);
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import * as Dotenv from "dotenv";
2-
import { AllowArbitraryParams, config, getJson, GoogleParameters } from "serpapi";
2+
import { config, EngineParameters, getJson } from "serpapi";
33

44
Dotenv.config();
55
const apiKey = process.env.API_KEY;
66

77
const params = {
8+
engine: "google",
89
q: "Coffee",
910
api_key: apiKey,
10-
} satisfies AllowArbitraryParams<GoogleParameters>;
11+
} satisfies EngineParameters<"google">;
1112

1213
// Show result as JSON (async/await)
13-
const response1 = await getJson("google", params);
14+
const response1 = await getJson(params);
1415
console.log(response1["organic_results"]);
1516

1617
// Show result as JSON (callback)
17-
getJson("google", params, (json) => console.log(json["organic_results"]));
18+
getJson(params, (json) => console.log(json["organic_results"]));
1819

1920
// Use global config
2021
config.api_key = apiKey;
21-
const response2 = await getJson("google", { q: "Coffee" });
22+
const response2 = await getJson({ engine: "google", q: "Coffee" });
2223
console.log(response2["organic_results"]);

0 commit comments

Comments
 (0)