Skip to content

Commit a60a11b

Browse files
committed
Backwards compatibility for Node <= 9
1 parent 94e3413 commit a60a11b

File tree

3 files changed

+663
-560
lines changed

3 files changed

+663
-560
lines changed

Diff for: src/utils.ts

+21-14
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ import process from "node:process";
1616
*/
1717
export const _internals = {
1818
execute: execute,
19-
getBaseUrl: getBaseUrl,
19+
getHostnameAndPort: getHostnameAndPort,
2020
};
2121

2222
/** Facilitates stubbing in tests, e.g. localhost as the base url */
23-
function getBaseUrl() {
24-
return "https://serpapi.com";
23+
function getHostnameAndPort() {
24+
return {
25+
hostname: "serpapi.com",
26+
port: 443,
27+
};
2528
}
2629

2730
export function getSource() {
@@ -40,25 +43,35 @@ export function getSource() {
4043
return `nodejs,${moduleSource}`;
4144
}
4245

43-
export function buildUrl(
46+
export function buildRequestOptions(
4447
path: string,
4548
parameters: qs.ParsedUrlQueryInput,
46-
): string {
49+
): http.RequestOptions {
4750
const clonedParams = { ...parameters };
4851
for (const k in clonedParams) {
4952
if (clonedParams[k] === undefined) {
5053
delete clonedParams[k];
5154
}
5255
}
53-
return `${_internals.getBaseUrl()}${path}?${qs.stringify(clonedParams)}`;
56+
const base = {
57+
..._internals.getHostnameAndPort(),
58+
path: `${path}?${qs.stringify(clonedParams)}`,
59+
method: "GET",
60+
};
61+
62+
return {
63+
...base,
64+
...(parameters.requestOptions as http.RequestOptions),
65+
...config.requestOptions,
66+
};
5467
}
5568

5669
export function execute(
5770
path: string,
5871
parameters: qs.ParsedUrlQueryInput,
5972
timeout: number,
6073
): Promise<string> {
61-
const url = buildUrl(path, {
74+
const options = buildRequestOptions(path, {
6275
...parameters,
6376
source: getSource(),
6477
});
@@ -96,13 +109,7 @@ export function execute(
96109
if (timer) clearTimeout(timer);
97110
};
98111

99-
const options = (parameters.requestOptions as http.RequestOptions) ||
100-
config.requestOptions ||
101-
{};
102-
103-
const req = https
104-
.get(url, options, handleResponse)
105-
.on("error", handleError);
112+
const req = https.get(options, handleResponse).on("error", handleError);
106113

107114
if (timeout > 0) {
108115
timer = setTimeout(() => {

0 commit comments

Comments
 (0)