Skip to content

Commit 4405244

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

File tree

2 files changed

+78
-47
lines changed

2 files changed

+78
-47
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(() => {

Diff for: tests/utils_test.ts

+57-33
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,24 @@ import {
1111
assertInstanceOf,
1212
assertMatch,
1313
} from "https://deno.land/[email protected]/testing/asserts.ts";
14-
import { _internals, buildUrl, execute, getSource } from "../src/utils.ts";
14+
import {
15+
_internals,
16+
buildRequestOptions,
17+
execute,
18+
getSource,
19+
} from "../src/utils.ts";
1520
import { RequestTimeoutError } from "../src/errors.ts";
1621

1722
loadSync({ export: true });
18-
const BASE_URL = Deno.env.get("ENV_TYPE") === "local"
19-
? "http://localhost:3000"
20-
: "https://serpapi.com";
23+
const BASE_OPTIONS = Deno.env.get("ENV_TYPE") === "local"
24+
? {
25+
hostname: "localhost",
26+
port: 3000,
27+
}
28+
: {
29+
hostname: "serpapi.com",
30+
port: 443,
31+
};
2132

2233
describe("getSource", () => {
2334
it("use runtime version", async () => {
@@ -28,67 +39,80 @@ describe("getSource", () => {
2839
});
2940
});
3041

31-
describe("buildUrl", () => {
42+
describe("buildRequestOptions", () => {
3243
let urlStub: Stub;
3344

3445
beforeAll(() => {
35-
urlStub = stub(_internals, "getBaseUrl", () => BASE_URL);
46+
urlStub = stub(_internals, "getHostnameAndPort", () => BASE_OPTIONS);
3647
});
3748

3849
afterAll(() => {
3950
urlStub.restore();
4051
});
4152

4253
it("with blank path and empty parameters", async () => {
43-
assertEquals(await buildUrl("", {}), `${BASE_URL}?`);
54+
assertEquals(await buildRequestOptions("", {}), BASE_OPTIONS);
4455
});
4556

4657
it("with path and empty parameters", async () => {
47-
assertEquals(await buildUrl("/", {}), `${BASE_URL}/?`);
58+
assertEquals(await buildRequestOptions("/", {}), BASE_OPTIONS);
4859
});
4960

5061
it("with path and parameters", async () => {
5162
assertEquals(
52-
await buildUrl("/search", { q: "coffee", gl: "us" }),
53-
`${BASE_URL}/search?q=coffee&gl=us`,
63+
await buildRequestOptions("/search", { q: "coffee", gl: "us" }),
64+
BASE_OPTIONS,
5465
);
5566
});
5667

5768
it("with source", async () => {
58-
const url = await buildUrl("/search", { source: await getSource() });
69+
const options = await buildRequestOptions("/search", {
70+
source: await getSource(),
71+
});
5972
assertMatch(
60-
url,
73+
options.path as string,
6174
/source=(nodejs|deno)%40\d+\.\d+\.\d+%2Cserpapi%40\d+\.\d+\.\d+$/,
6275
);
6376
});
6477

6578
it("with undefined parameters", async () => {
6679
assertEquals(
67-
await buildUrl("/search", { q: "coffee", gl: undefined, hl: null }),
68-
`${BASE_URL}/search?q=coffee&hl=`,
80+
await buildRequestOptions("/search", {
81+
q: "coffee",
82+
gl: undefined,
83+
hl: null,
84+
}),
85+
{
86+
...BASE_OPTIONS,
87+
path: "/search?q=coffee&hl=",
88+
},
6989
);
7090
});
7191
});
7292

73-
describe("execute", {
74-
sanitizeOps: false,
75-
sanitizeResources: false,
76-
}, () => {
77-
let urlStub: Stub;
93+
describe(
94+
"execute",
95+
{
96+
sanitizeOps: false,
97+
sanitizeResources: false,
98+
},
99+
() => {
100+
let urlStub: Stub;
78101

79-
beforeAll(() => {
80-
urlStub = stub(_internals, "getBaseUrl", () => BASE_URL);
81-
});
102+
beforeAll(() => {
103+
urlStub = stub(_internals, "getHostnameAndPort", () => BASE_OPTIONS);
104+
});
82105

83-
afterAll(() => {
84-
urlStub.restore();
85-
});
106+
afterAll(() => {
107+
urlStub.restore();
108+
});
86109

87-
it("with short timeout", async () => {
88-
try {
89-
await execute("/search", { q: "coffee", gl: "us" }, 1);
90-
} catch (e) {
91-
assertInstanceOf(e, RequestTimeoutError);
92-
}
93-
});
94-
});
110+
it("with short timeout", async () => {
111+
try {
112+
await execute("/search", { q: "coffee", gl: "us" }, 1);
113+
} catch (e) {
114+
assertInstanceOf(e, RequestTimeoutError);
115+
}
116+
});
117+
},
118+
);

0 commit comments

Comments
 (0)