-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommonjs.js
141 lines (116 loc) · 3.69 KB
/
commonjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Smoke test works for Node.js 7 and newer.
*/
const Dotenv = require("dotenv");
const process = require("process");
const {
config,
getJson,
getHtml,
getJsonBySearchId,
getHtmlBySearchId,
getAccount,
getLocations,
} = require("serpapi");
Dotenv.config();
const apiKey = process.env.SERPAPI_TEST_KEY;
const run = async () => {
console.log("running", process.versions.node);
const params = {
q: "Coffee",
api_key: apiKey,
};
let searchId;
{
console.log("getJson async await");
const page1 = await getJson(Object.assign({ engine: "google" }, params));
searchId = page1["search_metadata"]["id"];
if (!page1["organic_results"]) throw new Error("No organic results");
}
{
console.log("getJson callback");
getJson(Object.assign({ engine: "google" }, params), (page1) => {
if (!page1["organic_results"]) throw new Error("No organic results");
});
}
{
console.log("getJson using global config");
config.api_key = apiKey;
const page1 = await getJson({ engine: "google", q: "Coffee" });
if (!page1["organic_results"]) throw new Error("No organic results");
}
{
console.log("getJson (old API) async await");
const page1 = await getJson("google", params);
searchId = page1["search_metadata"]["id"];
if (!page1["organic_results"]) throw new Error("No organic results");
}
{
console.log("getJson (old API) callback");
getJson("google", params, (page1) => {
if (!page1["organic_results"]) throw new Error("No organic results");
});
}
{
console.log("getJson (old API) using global config");
config.api_key = apiKey;
const page1 = await getJson("google", { q: "Coffee" });
if (!page1["organic_results"]) throw new Error("No organic results");
}
{
console.log("getHtml");
const html = await getHtml(Object.assign({ engine: "google" }, params));
if (html.length < 1000) throw new Error("Incorrect HTML");
getHtml(Object.assign({ engine: "google" }, params), (html) => {
if (html.length < 1000) throw new Error("Incorrect HTML");
});
}
{
console.log("getHtml (old API)");
const html = await getHtml("google", params);
if (html.length < 1000) throw new Error("Incorrect HTML");
getHtml("google", params, (html) => {
if (html.length < 1000) throw new Error("Incorrect HTML");
});
}
{
console.log("getJsonBySearchId");
config.api_key = apiKey;
const json = await getJsonBySearchId(searchId);
if (!json["organic_results"]) throw new Error("No organic results");
getJsonBySearchId(searchId, {}, (json) => {
if (!json["organic_results"]) throw new Error("No organic results");
});
}
{
console.log("getHtmlBySearchId");
config.api_key = apiKey;
const html = await getHtmlBySearchId(searchId);
if (html.length < 1000) throw new Error("Incorrect HTML");
getHtmlBySearchId(searchId, {}, (html) => {
if (html.length < 1000) throw new Error("Incorrect HTML");
});
}
{
console.log("getAccount");
config.api_key = apiKey;
const info = await getAccount();
if (!info["account_email"]) throw new Error("Incorrect account info");
getAccount({}, (info) => {
if (!info["account_email"]) throw new Error("Incorrect account info");
});
}
{
console.log("getLocations");
const locations = await getLocations({ limit: 3 });
if (locations.length !== 3) throw new Error("Incorrect locations length");
getLocations({ limit: 3 }, (locations) => {
if (locations.length !== 3) throw new Error("Incorrect locations length");
});
}
console.log("success", process.versions.node);
};
run().catch((e) => {
console.error(e);
process.exitCode = 1;
});