Skip to content

Commit cde2e47

Browse files
committed
Overload getJson and getHtml, and clarify parameter explanation
1 parent 29d1a01 commit cde2e47

1 file changed

Lines changed: 87 additions & 10 deletions

File tree

src/serpapi.ts

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ const SEARCH_PATH = "/search";
2121
const SEARCH_ARCHIVE_PATH = `/searches`;
2222

2323
/**
24-
* Get a JSON response based on search parameters.
25-
* - Accepts an optional callback.
26-
* - Get the next page of results by calling the `.next()` method on the returned response object.
24+
* Get JSON response based on search parameters.
2725
*
28-
* @param {object} parameters - search query parameters for the engine
29-
* @param {fn=} callback - optional callback
26+
* @param {object} parameters Search query parameters for the engine. Refer to https://serpapi.com/search-api for parameter explanations.
27+
* @param {fn=} callback Optional callback.
3028
* @example
3129
* // single call (async/await)
3230
* const json = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });
@@ -67,6 +65,63 @@ const SEARCH_ARCHIVE_PATH = `/searches`;
6765
* }
6866
* });
6967
*/
68+
export function getJson(
69+
parameters: EngineParameters,
70+
callback?: (json: BaseResponse) => void,
71+
): Promise<BaseResponse>;
72+
73+
/**
74+
* Get JSON response based on search parameters.
75+
*
76+
* @param {string} engine Engine name. Refer to https://serpapi.com/search-api for valid engines.
77+
* @param {object} parameters Search query parameters for the engine. Refer to https://serpapi.com/search-api for parameter explanations.
78+
* @param {fn=} callback Optional callback.
79+
* @example
80+
* // single call (async/await)
81+
* const json = await getJson("google", { api_key: API_KEY, q: "coffee" });
82+
*
83+
* // single call (callback)
84+
* getJson("google", { api_key: API_KEY, q: "coffee" }, console.log);
85+
*
86+
* @example
87+
* // pagination (async/await)
88+
* const page1 = await getJson("google", { q: "coffee", start: 15 });
89+
* const page2 = await page1.next?.();
90+
*
91+
* @example
92+
* // pagination (callback)
93+
* getJson("google", { q: "coffee", start: 15 }, (page1) => {
94+
* page1.next?.((page2) => {
95+
* console.log(page2);
96+
* });
97+
* });
98+
*
99+
* @example
100+
* // pagination loop (async/await)
101+
* const organicResults = [];
102+
* let page = await getJson("google", { api_key: API_KEY, q: "coffee" });
103+
* while (page) {
104+
* organicResults.push(...page.organic_results);
105+
* if (organicResults.length >= 30) break;
106+
* page = await page.next?.();
107+
* }
108+
*
109+
* @example
110+
* // pagination loop (callback)
111+
* const organicResults = [];
112+
* getJson("google", { api_key: API_KEY, q: "coffee" }, (page) => {
113+
* organicResults.push(...page.organic_results);
114+
* if (organicResults.length < 30 && page.next) {
115+
* page.next();
116+
* }
117+
* });
118+
*/
119+
export function getJson(
120+
engine: string,
121+
parameters: EngineParameters,
122+
callback?: (json: BaseResponse) => void,
123+
): Promise<BaseResponse>;
124+
70125
export function getJson(
71126
...args:
72127
| [
@@ -134,19 +189,41 @@ async function _getJson(
134189
}
135190

136191
/**
137-
* Get a HTML response based on search parameters.
138-
* - Accepts an optional callback.
139-
* - Responds with a JSON string if the search request hasn't completed.
192+
* Get raw HTML response based on search parameters.
140193
*
141-
* @param {object} parameters - search query parameters for the engine
142-
* @param {fn=} callback - optional callback
194+
* @param {object} parameters Search query parameters for the engine. Refer to https://serpapi.com/search-api for parameter explanations.
195+
* @param {fn=} callback Optional callback.
196+
* @example
197+
* // async/await
198+
* const html = await getHtml({ engine: "google", api_key: API_KEY, q: "coffee" });
199+
*
200+
* // callback
201+
* getHtml({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);
202+
*/
203+
export function getHtml(
204+
parameters: EngineParameters,
205+
callback?: (html: string) => void,
206+
): Promise<string>;
207+
208+
/**
209+
* Get raw HTML response based on search parameters.
210+
*
211+
* @param {string} engine Engine name. Refer to https://serpapi.com/search-api for valid engines.
212+
* @param {object} parameters Search query parameters for the engine. Refer to https://serpapi.com/search-api for parameter explanations.
213+
* @param {fn=} callback Optional callback.
143214
* @example
144215
* // async/await
145216
* const html = await getHtml({ engine: "google", api_key: API_KEY, q: "coffee" });
146217
*
147218
* // callback
148219
* getHtml({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);
149220
*/
221+
export function getHtml(
222+
engine: string,
223+
parameters: EngineParameters,
224+
callback?: (html: string) => void,
225+
): Promise<string>;
226+
150227
export function getHtml(
151228
...args:
152229
| [

0 commit comments

Comments
 (0)