-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathserpapi.ts
More file actions
337 lines (323 loc) · 10.1 KB
/
serpapi.ts
File metadata and controls
337 lines (323 loc) · 10.1 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { InvalidArgumentError } from "./errors.ts";
import {
AccountApiParameters,
BaseResponse,
EngineParameters,
GetBySearchIdParameters,
GoogleSearchParameters,
GoogleSearchResponse,
LocationsApiParameters,
} from "./types.ts";
import { _internals } from "./utils.ts";
import { validateApiKey, validateTimeout } from "./validators.ts";
const ACCOUNT_PATH = "/account";
const LOCATIONS_PATH = "/locations.json";
const SEARCH_PATH = "/search";
const SEARCH_ARCHIVE_PATH = `/searches`;
/**
* Get typed JSON response for Google Search.
*/
export function getJson(
parameters: GoogleSearchParameters,
callback?: (json: GoogleSearchResponse) => void,
): Promise<GoogleSearchResponse>;
/**
* Get JSON response based on search parameters.
*
* @param {object} parameters Search query parameters for the engine. Refer to https://serpapi.com/search-api for parameter explanations.
* @param {fn=} callback Optional callback.
* @example
* // single call (async/await)
* const json = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });
*
* // single call (callback)
* getJson({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);
*/
export function getJson(
parameters: EngineParameters,
callback?: (json: BaseResponse) => void,
): Promise<BaseResponse>;
/**
* Get JSON response based on search parameters.
*
* @param {string} engine Engine name. Refer to https://serpapi.com/search-api for valid engines.
* @param {object} parameters Search query parameters for the engine. Refer to https://serpapi.com/search-api for parameter explanations.
* @param {fn=} callback Optional callback.
* @example
* // single call (async/await)
* const json = await getJson("google", { api_key: API_KEY, q: "coffee" });
*
* // single call (callback)
* getJson("google", { api_key: API_KEY, q: "coffee" }, console.log);
*/
export function getJson(
engine: string,
parameters: EngineParameters,
callback?: (json: BaseResponse) => void,
): Promise<BaseResponse>;
export function getJson(
...args:
// deno-lint-ignore no-explicit-any
| [parameters: EngineParameters, callback?: (json: any) => void]
| [
engine: string,
parameters: EngineParameters,
// deno-lint-ignore no-explicit-any
callback?: (json: any) => void,
]
): Promise<BaseResponse | GoogleSearchResponse> {
if (typeof args[0] === "string" && typeof args[1] === "object") {
const [engine, parameters, callback] = args;
const newParameters = { ...parameters, engine } as EngineParameters;
return _getJson(newParameters, callback);
} else if (
typeof args[0] === "object" &&
typeof args[1] !== "object" &&
(typeof args[1] === "undefined" || typeof args[1] === "function")
) {
const [parameters, callback] = args;
return _getJson(parameters, callback);
} else {
throw new InvalidArgumentError();
}
}
async function _getJson(
parameters: EngineParameters,
callback?: (json: BaseResponse) => void,
): Promise<BaseResponse> {
const key = validateApiKey(parameters.api_key, true);
const timeout = validateTimeout(parameters.timeout);
const response = await _internals.execute(
SEARCH_PATH,
{
...parameters,
api_key: key,
output: "json",
},
timeout,
);
const json = JSON.parse(response) as BaseResponse;
callback?.(json);
return json;
}
/**
* Get raw HTML response based on search parameters.
*
* @param {object} parameters Search query parameters for the engine. Refer to https://serpapi.com/search-api for parameter explanations.
* @param {fn=} callback Optional callback.
* @example
* // async/await
* const html = await getHtml({ engine: "google", api_key: API_KEY, q: "coffee" });
*
* // callback
* getHtml({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);
*/
export function getHtml(
parameters: EngineParameters,
callback?: (html: string) => void,
): Promise<string>;
/**
* Get raw HTML response based on search parameters.
*
* @param {string} engine Engine name. Refer to https://serpapi.com/search-api for valid engines.
* @param {object} parameters Search query parameters for the engine. Refer to https://serpapi.com/search-api for parameter explanations.
* @param {fn=} callback Optional callback.
* @example
* // async/await
* const html = await getHtml({ engine: "google", api_key: API_KEY, q: "coffee" });
*
* // callback
* getHtml({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);
*/
export function getHtml(
engine: string,
parameters: EngineParameters,
callback?: (html: string) => void,
): Promise<string>;
export function getHtml(
...args:
| [parameters: EngineParameters, callback?: (html: string) => void]
| [
engine: string,
parameters: EngineParameters,
callback?: (html: string) => void,
]
): Promise<string> {
if (typeof args[0] === "string" && typeof args[1] === "object") {
const [engine, parameters, callback] = args;
const newParameters = { ...parameters, engine } as EngineParameters;
return _getHtml(newParameters, callback);
} else if (
typeof args[0] === "object" &&
typeof args[1] !== "object" &&
(typeof args[1] === "undefined" || typeof args[1] === "function")
) {
const [parameters, callback] = args;
return _getHtml(parameters, callback);
} else {
throw new InvalidArgumentError();
}
}
async function _getHtml(
parameters: EngineParameters,
callback?: (html: string) => void,
): Promise<string> {
const key = validateApiKey(parameters.api_key, true);
const timeout = validateTimeout(parameters.timeout);
const html = await _internals.execute(
SEARCH_PATH,
{
...parameters,
api_key: key,
output: "html",
},
timeout,
);
callback?.(html);
return html;
}
/**
* Get a JSON response given a search ID.
* - This search ID can be obtained from the `search_metadata.id` key in the response.
* - Typically used together with the `async` parameter.
*
* @param {string} searchId Search ID.
* @param {object} parameters
* @param {string=} [parameters.api_key] API key.
* @param {number=} [parameters.timeout] Timeout in milliseconds.
* @param {fn=} callback Optional callback.
* @example
* const response = await getJson({ engine: "google", api_key: API_KEY, async: true, q: "coffee" });
* const { id } = response.search_metadata;
* await delay(1000); // wait for the request to be processed.
*
* // async/await
* const json = await getJsonBySearchId(id, { api_key: API_KEY });
*
* // callback
* getJsonBySearchId(id, { api_key: API_KEY }, console.log);
*/
export async function getJsonBySearchId(
searchId: string,
parameters: GetBySearchIdParameters = {},
callback?: (json: BaseResponse) => void,
) {
const key = validateApiKey(parameters.api_key);
const timeout = validateTimeout(parameters.timeout);
const response = await _internals.execute(
`${SEARCH_ARCHIVE_PATH}/${searchId}`,
{
api_key: key,
output: "json",
},
timeout,
);
const json = JSON.parse(response) as BaseResponse;
callback?.(json);
return json;
}
/**
* Get a HTML response given a search ID.
* - This search ID can be obtained from the `search_metadata.id` key in the response.
* - Typically used together with the `async` parameter.
*
* @param {string} searchId Search ID.
* @param {object} parameters
* @param {string=} [parameters.api_key] API key.
* @param {number=} [parameters.timeout] Timeout in milliseconds.
* @param {fn=} callback Optional callback.
* @example
* const response = await getJson({ engine: "google", api_key: API_KEY, async: true, q: "coffee" });
* const { id } = response.search_metadata;
* await delay(1000); // wait for the request to be processed.
*
* // async/await
* const html = await getHtmlBySearchId(id, { api_key: API_KEY });
*
* // callback
* getHtmlBySearchId(id, { api_key: API_KEY }, console.log);
*/
export async function getHtmlBySearchId(
searchId: string,
parameters: GetBySearchIdParameters = {},
callback?: (html: string) => void,
) {
const key = validateApiKey(parameters.api_key);
const timeout = validateTimeout(parameters.timeout);
const html = await _internals.execute(
`${SEARCH_ARCHIVE_PATH}/${searchId}`,
{
api_key: key,
output: "html",
},
timeout,
);
callback?.(html);
return html;
}
/**
* Get account information of an API key.
*
* Refer to https://serpapi.com/account-api for response examples.
*
* @param {object} parameters
* @param {string=} [parameters.api_key] API key.
* @param {number=} [parameters.timeout] Timeout in milliseconds.
* @param {fn=} callback Optional callback.
* @example
* // async/await
* const info = await getAccount({ api_key: API_KEY });
*
* // callback
* getAccount({ api_key: API_KEY }, console.log);
*/
export async function getAccount(
parameters: AccountApiParameters = {},
// deno-lint-ignore no-explicit-any
callback?: (info: any) => void,
) {
const key = validateApiKey(parameters.api_key);
const timeout = validateTimeout(parameters.timeout);
const response = await _internals.execute(
ACCOUNT_PATH,
{
api_key: key,
},
timeout,
);
const info = JSON.parse(response);
callback?.(info);
return info;
}
/**
* Get supported locations. Does not require an API key.
*
* Refer to https://serpapi.com/locations-api for response examples.
*
* @param {object} parameters
* @param {string=} [parameters.q] Query for a location.
* @param {number=} [parameters.limit] Limit on number of locations returned.
* @param {number=} [parameters.timeout] Timeout in milliseconds.
* @param {fn=} callback Optional callback.
* @example
* // async/await
* const locations = await getLocations({ limit: 3 });
*
* // callback
* getLocations({ limit: 3 }, console.log);
*/
export async function getLocations(
parameters: LocationsApiParameters = {},
// deno-lint-ignore no-explicit-any
callback?: (locations: any) => void,
) {
const timeout = validateTimeout(parameters.timeout);
const response = await _internals.execute(
LOCATIONS_PATH,
parameters,
timeout,
);
const locations = JSON.parse(response);
callback?.(locations);
return locations;
}