Skip to content

Commit c0f4f23

Browse files
committed
Support node 12
1 parent db00dda commit c0f4f23

5 files changed

Lines changed: 50 additions & 22 deletions

File tree

deno.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@
1616
"files": {
1717
"exclude": ["npm/", "examples/node"]
1818
}
19+
},
20+
"compilerOptions": {
21+
"lib": [
22+
"dom",
23+
"deno.ns"
24+
]
1925
}
2026
}

examples/node/pagination_js_commonjs/example.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,48 @@ const run = async () => {
1818
"First page links",
1919
extractLinks(page1.organic_results),
2020
);
21-
let page2 = await page1.next?.();
22-
console.log(
23-
"Second page links",
24-
extractLinks(page2?.organic_results),
25-
);
21+
if (page1.next) {
22+
let page2 = await page1.next();
23+
console.log(
24+
"Second page links",
25+
extractLinks(page2.organic_results),
26+
);
27+
}
2628

2729
// Pagination (callback)
2830
getJson("google", params, (page1) => {
2931
console.log(
3032
"First page links",
3133
extractLinks(page1.organic_results),
3234
);
33-
page1.next?.((page2) => {
34-
console.log(
35-
"Second page links",
36-
extractLinks(page2.organic_results),
37-
);
38-
});
35+
if (page1.next) {
36+
page1.next((page2) => {
37+
console.log(
38+
"Second page links",
39+
extractLinks(page2.organic_results),
40+
);
41+
});
42+
}
3943
});
4044

4145
// Use global config
4246
config.api_key = apiKey;
4347
page1 = await getJson("google", { q: "Coffee" });
44-
page2 = await page1.next?.();
45-
console.log(
46-
"Second page links",
47-
extractLinks(page2?.organic_results),
48-
);
48+
if (page1.next) {
49+
page2 = await page1.next();
50+
console.log(
51+
"Second page links",
52+
extractLinks(page2.organic_results),
53+
);
54+
}
4955

5056
// Pagination loop (async/await)
5157
let links = [];
5258
let page = await getJson("google", { q: "Coffee" });
5359
while (page) {
5460
links.push(...extractLinks(page.organic_results));
5561
if (links.length >= 30) break;
56-
page = await page.next?.();
62+
page = page.next ? await page.next(): undefined;
5763
}
5864
console.log(links);
5965

scripts/build_npm.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { version } from "../version.ts";
44
await emptyDir("./npm");
55

66
await build({
7+
test: false, // Turned off to avoid publishing tests
78
entryPoints: ["./mod.ts"],
89
rootTestDir: "./tests",
910
outDir: "./npm",
@@ -16,8 +17,11 @@ await build({
1617
// https://deno.land/std/async/delay.ts relies on DOMException.
1718
// This is only used in tests.
1819
domException: "dev", // Only used in tests.
19-
20-
undici: true, // Required for `fetch`
20+
},
21+
compilerOptions: {
22+
// https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping
23+
lib: ["es2019"],
24+
target: "ES2019",
2125
},
2226
package: {
2327
name: "serpapi",

src/utils.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { EngineName, EngineParameters } from "./types.ts";
22
import { version } from "../version.ts";
3+
import fetch from "npm:cross-fetch@3.1.4";
34

45
type UrlParameters = Record<
56
string,
@@ -110,7 +111,7 @@ export function buildUrl<P extends UrlParameters>(
110111
return `${_internals.getBaseUrl()}${path}?${searchParams}`;
111112
}
112113

113-
export async function execute<P extends UrlParameters>(
114+
export function execute<P extends UrlParameters>(
114115
path: string,
115116
parameters: P,
116117
timeout: number,
@@ -119,7 +120,17 @@ export async function execute<P extends UrlParameters>(
119120
...parameters,
120121
source: getSource(),
121122
});
122-
return await _internals.fetch(url, {
123-
signal: AbortSignal.timeout(timeout),
123+
// https://github.com/github/fetch/issues/175#issuecomment-216791333
124+
return new Promise((resolve, reject) => {
125+
const timer = setTimeout(() => reject(new Error("Timeout")), timeout);
126+
_internals.fetch(url)
127+
.then((res) => {
128+
clearTimeout(timer);
129+
resolve(res);
130+
})
131+
.catch((err) => {
132+
clearTimeout(timer);
133+
reject(err);
134+
});
124135
});
125136
}

tests/utils_test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
extractNextParameters,
2424
haveParametersChanged,
2525
} from "../src/utils.ts";
26+
import { Response } from "npm:cross-fetch@3.1.4";
2627

2728
loadSync({ export: true });
2829
const BASE_URL = Deno.env.get("ENV_TYPE") === "local"

0 commit comments

Comments
 (0)