Skip to content

Commit 5edf1e8

Browse files
committed
[Examples] Add basic Deno TS example
1 parent 58ba285 commit 5edf1e8

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"version.ts",
55
"src",
66
"tests",
7-
"scripts"
7+
"scripts",
8+
"examples/deno"
89
],
910
"deno.inlayHints.enumMemberValues.enabled": false,
1011
"deno.inlayHints.functionLikeReturnTypes.enabled": false,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_KEY=YOUR_API_KEY

examples/deno/basic_ts/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Deno basic TypeScript example
2+
3+
## Usage
4+
5+
1. Setup environment variables
6+
7+
- Duplicate `.env.example` and name it `.env`.
8+
- Replace `YOUR_API_KEY` with your SerpApi API key.
9+
10+
2. Run the example
11+
12+
```
13+
deno run example.ts
14+
```
15+
16+
## Notes
17+
18+
- Imports rely on `mod.ts` found in the root folder.
19+
- To import the published module from deno.land, update the import to the
20+
following:
21+
```ts
22+
import {
23+
config,
24+
getJson,
25+
GoogleParameters,
26+
} from "https://deno.land/x/serpapi/mod.ts";
27+
```

examples/deno/basic_ts/example.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
2+
import { config, getJson, GoogleParameters } from "../../../mod.ts";
3+
4+
const { API_KEY: apiKey } = loadSync();
5+
const params = {
6+
q: "Coffee",
7+
api_key: apiKey,
8+
} satisfies GoogleParameters;
9+
10+
// Show result as JSON (async/await)
11+
const response1 = await getJson("google", params);
12+
console.log(response1["organic_results"]);
13+
14+
// Show result as JSON (callback)
15+
getJson("google", params, (json) => console.log(json["organic_results"]));
16+
17+
// Use global config
18+
config.api_key = apiKey;
19+
const response2 = await getJson("google", { q: "Coffee" });
20+
console.log(response2["organic_results"]);

0 commit comments

Comments
 (0)