Skip to content

Commit 743e942

Browse files
committed
Update examples
1 parent a60cf28 commit 743e942

8 files changed

Lines changed: 48 additions & 40 deletions

File tree

examples/deno/basic_ts/example.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@ import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
22
import {
33
AllowArbitraryParams,
44
config,
5+
EngineParameters,
56
getJson,
6-
GoogleParameters,
77
} from "../../../mod.ts";
88

99
const { API_KEY: apiKey } = loadSync();
1010
const params = {
11+
engine: "google",
1112
q: "Coffee",
1213
api_key: apiKey,
13-
} satisfies AllowArbitraryParams<GoogleParameters>;
14+
} satisfies AllowArbitraryParams<EngineParameters<"google">>;
1415

1516
// Show result as JSON (async/await)
16-
const response1 = await getJson("google", params);
17+
const response1 = await getJson(params);
1718
console.log(response1["organic_results"]);
1819

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

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

examples/deno/pagination_ts/example.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
22
import {
33
AllowArbitraryParams,
44
config,
5+
EngineParameters,
56
getJson,
6-
GoogleParameters,
77
} from "../../../mod.ts";
88

99
const { API_KEY: apiKey } = loadSync();
@@ -12,12 +12,13 @@ const extractLinks = (results: { link: string }[]) =>
1212
results.map((r) => r.link);
1313

1414
const params = {
15+
engine: "google",
1516
q: "Coffee",
1617
api_key: apiKey,
17-
} satisfies AllowArbitraryParams<GoogleParameters>;
18+
} satisfies AllowArbitraryParams<EngineParameters<"google">>;
1819

1920
// Pagination (async/await)
20-
let page1 = await getJson("google", params);
21+
let page1 = await getJson(params);
2122
console.log(
2223
"First page links",
2324
extractLinks(page1.organic_results),
@@ -29,7 +30,7 @@ console.log(
2930
);
3031

3132
// Pagination (callback)
32-
getJson("google", params, (page1) => {
33+
getJson(params, (page1) => {
3334
console.log(
3435
"First page links",
3536
extractLinks(page1.organic_results),
@@ -44,7 +45,7 @@ getJson("google", params, (page1) => {
4445

4546
// Use global config
4647
config.api_key = apiKey;
47-
page1 = await getJson("google", { q: "Coffee" });
48+
page1 = await getJson({ engine: "google", q: "Coffee" });
4849
page2 = await page1.next?.();
4950
console.log(
5051
"Second page links",
@@ -54,7 +55,7 @@ console.log(
5455
// Pagination loop (async/await)
5556
let links: string[] = [];
5657
let page;
57-
page = await getJson("google", { q: "Coffee" });
58+
page = await getJson({ engine: "google", q: "Coffee" });
5859
while (page) {
5960
links.push(...extractLinks(page.organic_results));
6061
if (links.length >= 30) break;
@@ -64,7 +65,7 @@ console.log(links);
6465

6566
// Pagination loop (callback)
6667
links = [];
67-
getJson("google", { q: "Coffee" }, (page) => {
68+
getJson({ engine: "google", q: "Coffee" }, (page) => {
6869
links.push(...extractLinks(page.organic_results));
6970
if (links.length < 30 && page.next) {
7071
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 { AllowArbitraryParams, 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 AllowArbitraryParams<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"]);

examples/node/pagination_js_commonjs/example.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ const extractLinks = (results) => results.map((r) => r.link);
88

99
const run = async () => {
1010
const params = {
11+
engine: "google",
1112
q: "Coffee",
1213
api_key: apiKey,
1314
};
1415

1516
// Pagination (async/await)
16-
let page1 = await getJson("google", params);
17+
let page1 = await getJson(params);
1718
console.log(
1819
"First page links",
1920
extractLinks(page1.organic_results),
@@ -25,7 +26,7 @@ const run = async () => {
2526
);
2627

2728
// Pagination (callback)
28-
getJson("google", params, (page1) => {
29+
getJson(params, (page1) => {
2930
console.log(
3031
"First page links",
3132
extractLinks(page1.organic_results),
@@ -40,7 +41,7 @@ const run = async () => {
4041

4142
// Use global config
4243
config.api_key = apiKey;
43-
page1 = await getJson("google", { q: "Coffee" });
44+
page1 = await getJson({ engine: "google", q: "Coffee" });
4445
page2 = await page1.next?.();
4546
console.log(
4647
"Second page links",
@@ -49,7 +50,7 @@ const run = async () => {
4950

5051
// Pagination loop (async/await)
5152
let links = [];
52-
let page = await getJson("google", { q: "Coffee" });
53+
let page = await getJson({ engine: "google", q: "Coffee" });
5354
while (page) {
5455
links.push(...extractLinks(page.organic_results));
5556
if (links.length >= 30) break;
@@ -59,7 +60,7 @@ const run = async () => {
5960

6061
// Pagination loop (callback)
6162
links = [];
62-
getJson("google", { q: "Coffee" }, (page) => {
63+
getJson({ engine: "google", q: "Coffee" }, (page) => {
6364
links.push(...extractLinks(page.organic_results));
6465
if (links.length < 30 && page.next) {
6566
page.next();

examples/node/pagination_js_esm/example.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ const apiKey = process.env.API_KEY;
77
const extractLinks = (results) => results.map((r) => r.link);
88

99
const params = {
10+
engine: "google",
1011
q: "Coffee",
1112
api_key: apiKey,
1213
};
1314

1415
// Pagination (async/await)
15-
let page1 = await getJson("google", params);
16+
let page1 = await getJson(params);
1617
console.log(
1718
"First page links",
1819
extractLinks(page1.organic_results),
@@ -24,7 +25,7 @@ console.log(
2425
);
2526

2627
// Pagination (callback)
27-
getJson("google", params, (page1) => {
28+
getJson(params, (page1) => {
2829
console.log(
2930
"First page links",
3031
extractLinks(page1.organic_results),
@@ -39,7 +40,7 @@ getJson("google", params, (page1) => {
3940

4041
// Use global config
4142
config.api_key = apiKey;
42-
page1 = await getJson("google", { q: "Coffee" });
43+
page1 = await getJson({ engine: "google", q: "Coffee" });
4344
page2 = await page1.next?.();
4445
console.log(
4546
"Second page links",
@@ -48,7 +49,7 @@ console.log(
4849

4950
// Pagination loop (async/await)
5051
let links = [];
51-
let page = await getJson("google", { q: "Coffee" });
52+
let page = await getJson({ engine: "google", q: "Coffee" });
5253
while (page) {
5354
links.push(...extractLinks(page.organic_results));
5455
if (links.length >= 30) break;
@@ -58,7 +59,7 @@ console.log(links);
5859

5960
// Pagination loop (callback)
6061
links = [];
61-
getJson("google", { q: "Coffee" }, (page) => {
62+
getJson({ engine: "google", q: "Coffee" }, (page) => {
6263
links.push(...extractLinks(page.organic_results));
6364
if (links.length < 30 && page.next) {
6465
page.next();

examples/node/pagination_ts_esm/example.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Dotenv from "dotenv";
2-
import { AllowArbitraryParams, config, getJson, GoogleParameters } from "serpapi";
2+
import { AllowArbitraryParams, config, EngineParameters, getJson } from "serpapi";
33

44
Dotenv.config();
55
const apiKey = process.env.API_KEY;
@@ -8,12 +8,13 @@ const extractLinks = (results: { link: string }[]) =>
88
results.map((r) => r.link);
99

1010
const params = {
11+
engine: "google",
1112
q: "Coffee",
1213
api_key: apiKey,
13-
} satisfies AllowArbitraryParams<GoogleParameters>;
14+
} satisfies AllowArbitraryParams<EngineParameters<"google">>;
1415

1516
// Pagination (async/await)
16-
let page1 = await getJson("google", params);
17+
let page1 = await getJson(params);
1718
console.log(
1819
"First page links",
1920
extractLinks(page1.organic_results),
@@ -25,7 +26,7 @@ console.log(
2526
);
2627

2728
// Pagination (callback)
28-
getJson("google", params, (page1) => {
29+
getJson(params, (page1) => {
2930
console.log(
3031
"First page links",
3132
extractLinks(page1.organic_results),
@@ -40,7 +41,7 @@ getJson("google", params, (page1) => {
4041

4142
// Use global config
4243
config.api_key = apiKey;
43-
page1 = await getJson("google", { q: "Coffee" });
44+
page1 = await getJson({ engine: "google", q: "Coffee" });
4445
page2 = await page1.next?.();
4546
console.log(
4647
"Second page links",
@@ -50,7 +51,7 @@ console.log(
5051
// Pagination loop (async/await)
5152
let links: string[] = [];
5253
let page;
53-
page = await getJson("google", { q: "Coffee" });
54+
page = await getJson({ engine: "google", q: "Coffee" });
5455
while (page) {
5556
links.push(...extractLinks(page.organic_results));
5657
if (links.length >= 30) break;
@@ -60,7 +61,7 @@ console.log(links);
6061

6162
// Pagination loop (callback)
6263
links = [];
63-
getJson("google", { q: "Coffee" }, (page) => {
64+
getJson({ engine: "google", q: "Coffee" }, (page) => {
6465
links.push(...extractLinks(page.organic_results));
6566
if (links.length < 30 && page.next) {
6667
page.next();

0 commit comments

Comments
 (0)