Skip to content

Commit 58ba285

Browse files
committed
[Examples] Add basic TS ESM Node example
1 parent 70349c7 commit 58ba285

9 files changed

Lines changed: 232 additions & 3 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.env
22
cov_profile/
3-
npm/
3+
npm/
4+
node_modules/

deno.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
},
1010
"fmt": {
1111
"files": {
12-
"exclude": ["npm/"]
12+
"exclude": ["npm/", "examples/node"]
1313
}
1414
},
1515
"lint": {
1616
"files": {
17-
"exclude": ["npm/"]
17+
"exclude": ["npm/", "examples/node"]
1818
}
1919
}
2020
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_KEY=YOUR_API_KEY
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Node.js basic TypeScript (ESM) example
2+
3+
## Usage
4+
5+
1. Build module
6+
7+
```bash
8+
cd ../../../ # Navigate to the root folder
9+
deno task npm
10+
```
11+
12+
2. Setup environment variables
13+
14+
- Duplicate `.env.example` and name it `.env`.
15+
- Replace `YOUR_API_KEY` with your SerpApi API key.
16+
17+
3. Install dependencies and run example
18+
19+
```bash
20+
cd examples/node/basic_ts_esm
21+
npm i
22+
npm start
23+
```
24+
25+
## Notes
26+
27+
- If you want to run the example without building the module, you can update
28+
`package.json` to depend on the published `serpapi` npm module instead:
29+
```json
30+
{
31+
"type": "module",
32+
"dependencies": {
33+
"dotenv": "*",
34+
"serpapi": "*" // Relies on the npm module
35+
},
36+
"devDependencies": {
37+
"@types/node": "*",
38+
"typescript": "*"
39+
},
40+
"scripts": {
41+
"start": "npx ts-node example.ts"
42+
}
43+
}
44+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as Dotenv from "dotenv";
2+
import { config, getJson, GoogleParameters } from "serpapi";
3+
4+
Dotenv.config();
5+
const apiKey = process.env.API_KEY;
6+
7+
const params = {
8+
q: "Coffee",
9+
api_key: apiKey,
10+
} satisfies GoogleParameters;
11+
12+
// Show result as JSON (async/await)
13+
const response1 = await getJson("google", params);
14+
console.log(response1["organic_results"]);
15+
16+
// Show result as JSON (callback)
17+
getJson("google", params, (json) => console.log(json["organic_results"]));
18+
19+
// Use global config
20+
config.api_key = apiKey;
21+
const response2 = await getJson("google", { q: "Coffee" });
22+
console.log(response2["organic_results"]);

examples/node/basic_ts_esm/package-lock.json

Lines changed: 136 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"type": "module",
3+
"dependencies": {
4+
"dotenv": "*",
5+
"serpapi": "../../../npm"
6+
},
7+
"devDependencies": {
8+
"@types/node": "*",
9+
"typescript": "*"
10+
},
11+
"scripts": {
12+
"start": "npx ts-node example.ts"
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ESNext",
4+
"target": "ESNext",
5+
"moduleResolution": "Node"
6+
},
7+
"ts-node": {
8+
"esm": true
9+
}
10+
}

scripts/build_npm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ await emptyDir("./npm");
55

66
await build({
77
entryPoints: ["./mod.ts"],
8+
rootTestDir: "./tests",
89
outDir: "./npm",
910
shims: {
1011
deno: true, // Required for `Deno.test`, `Deno.env`, etc.

0 commit comments

Comments
 (0)