Skip to content

Commit 3332d7f

Browse files
authored
Merge pull request #29 from serpapi/support-proxy
Add support for request options
2 parents 38dcecb + 67ce0d4 commit 3332d7f

File tree

16 files changed

+918
-614
lines changed

16 files changed

+918
-614
lines changed

.github/workflows/build.yml

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ on: [push]
55
jobs:
66
build:
77
name: "Deno tests and build npm files"
8-
runs-on: ubuntu-22.04
8+
runs-on: ubuntu-latest
99
steps:
1010
- name: Checkout repo
1111
uses: actions/checkout@v3
1212

1313
- name: Setup Deno
14-
uses: denoland/setup-deno@v1.1.1
14+
uses: denoland/setup-deno@v2
1515
with:
16-
deno-version: v1.x
16+
deno-version: v2.x
1717

1818
- name: Check formatting
1919
run: deno fmt --check
@@ -24,13 +24,13 @@ jobs:
2424
- name: Run tests
2525
env:
2626
SERPAPI_TEST_KEY: ${{ secrets.SERPAPI_TEST_KEY }}
27-
run: deno task test:ci
27+
run: deno task test
2828

2929
- name: Setup Node
30-
uses: actions/setup-node@v3
30+
uses: actions/setup-node@v4
3131
with:
32-
node-version: '18.x' # Build files using a fixed node version
33-
registry-url: 'https://registry.npmjs.org'
32+
node-version: "22.x" # Build files using a fixed node version
33+
registry-url: "https://registry.npmjs.org"
3434

3535
- name: Build npm files
3636
run: deno task npm
@@ -39,7 +39,7 @@ jobs:
3939
run: zip npm.zip ./npm -r
4040

4141
- name: Upload build files for smoke tests
42-
uses: actions/upload-artifact@v3
42+
uses: actions/upload-artifact@v4
4343
with:
4444
name: npm
4545
path: npm.zip
@@ -48,10 +48,29 @@ jobs:
4848
smoke-tests-commonjs:
4949
name: "Smoke tests (CommonJS)"
5050
needs: build
51-
runs-on: ubuntu-22.04
51+
runs-on: ubuntu-latest
5252
strategy:
5353
matrix:
54-
node-version: [7.x, 8.x, 9.x, 10.x, 11.x, 12.x, 13.x, 14.x, 15.x, 16.x, 17.x, 18.x, 19.x]
54+
node-version: [
55+
7.x,
56+
8.x,
57+
9.x,
58+
10.x,
59+
11.x,
60+
12.x,
61+
13.x,
62+
14.x,
63+
15.x,
64+
16.x,
65+
17.x,
66+
18.x,
67+
19.x,
68+
20.x,
69+
21.x,
70+
22.x,
71+
23.x,
72+
24.x,
73+
]
5574
include:
5675
- command: test
5776
- command: test:use-openssl-ca
@@ -63,16 +82,16 @@ jobs:
6382

6483
steps:
6584
- name: Checkout repo
66-
uses: actions/checkout@v3
85+
uses: actions/checkout@v4
6786

6887
- name: Setup Node
69-
uses: actions/setup-node@v3
88+
uses: actions/setup-node@v4
7089
with:
7190
node-version: ${{ matrix.node-version }}
72-
registry-url: 'https://registry.npmjs.org'
91+
registry-url: "https://registry.npmjs.org"
7392

7493
- name: Download build files
75-
uses: actions/download-artifact@v3
94+
uses: actions/download-artifact@v4
7695
with:
7796
name: npm
7897

@@ -90,22 +109,34 @@ jobs:
90109
smoke-tests-esm:
91110
name: "Smoke tests (ESM)"
92111
needs: build
93-
runs-on: ubuntu-22.04
112+
runs-on: ubuntu-latest
94113
strategy:
95114
matrix:
96-
node-version: [14.x, 15.x, 16.x, 17.x, 18.x, 19.x]
115+
node-version: [
116+
14.x,
117+
15.x,
118+
16.x,
119+
17.x,
120+
18.x,
121+
19.x,
122+
20.x,
123+
21.x,
124+
22.x,
125+
23.x,
126+
24.x,
127+
]
97128
steps:
98129
- name: Checkout repo
99-
uses: actions/checkout@v3
130+
uses: actions/checkout@v4
100131

101132
- name: Setup Node
102-
uses: actions/setup-node@v3
133+
uses: actions/setup-node@v4
103134
with:
104135
node-version: ${{ matrix.node-version }}
105-
registry-url: 'https://registry.npmjs.org'
136+
registry-url: "https://registry.npmjs.org"
106137

107138
- name: Download build files
108-
uses: actions/download-artifact@v3
139+
uses: actions/download-artifact@v4
109140
with:
110141
name: npm
111142

.github/workflows/release.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ on:
55
branches:
66
- master
77
paths:
8-
- 'version.ts'
8+
- "version.ts"
99

1010
jobs:
1111
release:
12-
runs-on: ubuntu-22.04
12+
runs-on: ubuntu-latest
1313
steps:
1414
- name: Checkout repo
15-
uses: actions/checkout@v3
15+
uses: actions/checkout@v4
1616

1717
- name: Setup Deno
18-
uses: denoland/setup-deno@v1.1.1
18+
uses: denoland/setup-deno@v2
1919
with:
20-
deno-version: v1.x
20+
deno-version: v2.x
2121

2222
- name: Check formatting
2323
run: deno fmt --check
@@ -28,13 +28,13 @@ jobs:
2828
- name: Run tests
2929
env:
3030
SERPAPI_TEST_KEY: ${{ secrets.SERPAPI_TEST_KEY }}
31-
run: deno task test:ci
31+
run: deno task test
3232

3333
- name: Setup Node
34-
uses: actions/setup-node@v3
34+
uses: actions/setup-node@v4
3535
with:
36-
node-version: '18.x'
37-
registry-url: 'https://registry.npmjs.org'
36+
node-version: "22.x"
37+
registry-url: "https://registry.npmjs.org"
3838

3939
- name: Build npm files
4040
run: deno task npm

CONTRIBUTING.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ and you're good to go.
6666

6767
### Tests that require an API key
6868

69-
- When testing functionality that hits either `localhost` or
70-
`https://serpapi.com`, and requires an API key, you need to ensure your API
71-
key is specified as an environment variable.
69+
- When testing functionality that requires an API key, you need to ensure your
70+
API key is specified as an environment variable.
7271
- Create a `.env` file with the following contents, replacing `YOUR_API_KEY`
7372
with your API key for the appropriate environment.
7473

@@ -91,10 +90,9 @@ it("getJson with api key from config", {
9190
## Run tests
9291

9392
```bash
94-
deno task test # Run tests that hit "localhost"
95-
deno task test:watch # Run tests that hit "localhost" and in watch mode: https://deno.land/manual/getting_started/command_line_interface#watch-mode
96-
deno task test:cov # Get test coverage by running tests that hit "localhost"
97-
deno task test:ci # Run tests that hit "https://serpapi.com"
93+
deno task test # Run tests
94+
deno task test:watch # Run tests and in watch mode: https://deno.land/manual/getting_started/command_line_interface#watch-mode
95+
deno task test:cov # Get test coverage by running tests
9896
```
9997

10098
## Run examples on local source files

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ more.
2424

2525
```bash
2626
npm install serpapi
27+
# or if you prefer yarn
28+
yarn add serpapi
2729
```
2830

2931
```js
@@ -114,6 +116,41 @@ await getJson({ engine: "google", q: "coffee" }); // uses the API key defined in
114116
await getJson({ engine: "google", api_key: API_KEY_2, q: "coffee" }); // API_KEY_2 will be used
115117
```
116118

119+
### Using a Proxy
120+
121+
You can use a proxy by passing `requestOptions` with an `HttpsProxyAgent`
122+
instance. This can be done either globally through the config object or
123+
per-request in the parameters.
124+
125+
First, install the required package:
126+
127+
```bash
128+
npm install https-proxy-agent
129+
# or if you prefer yarn
130+
yarn add https-proxy-agent
131+
```
132+
133+
Then use it in your code:
134+
135+
```js
136+
import { config, getJson } from "serpapi";
137+
import { HttpsProxyAgent } from "https-proxy-agent";
138+
139+
// Global configuration
140+
config.requestOptions = {
141+
agent: new HttpsProxyAgent("http://proxy-server:port"),
142+
};
143+
144+
// Or per-request configuration
145+
await getJson({
146+
engine: "google",
147+
q: "coffee",
148+
requestOptions: {
149+
agent: new HttpsProxyAgent("http://proxy-server:port"),
150+
},
151+
});
152+
```
153+
117154
## Pagination
118155

119156
Built-in pagination is not supported. Please refer to our pagination examples

deno.json

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
11
{
22
"tasks": {
33
"docs:gen": "npx documentation readme src/serpapi.ts --section=Functions --shallow && deno fmt",
4-
"test": "ENV_TYPE=local deno test tests/ --allow-env --allow-read --allow-net --fail-fast",
4+
"test": "deno test tests/ --allow-env --allow-read --allow-net",
55
"test:watch": "deno task test --watch",
66
"test:cov": "rm -rf cov_profile && deno task test --coverage=cov_profile && deno coverage cov_profile",
7-
"test:ci": "deno test tests/ --allow-env --allow-read --allow-net",
87
"npm": "deno run -A scripts/build_npm.ts"
98
},
109
"fmt": {
11-
"files": {
12-
"exclude": ["npm/", "examples/node", "smoke_tests/"]
13-
}
10+
"exclude": ["npm/", "examples/node", "smoke_tests/"]
1411
},
1512
"lint": {
16-
"files": {
17-
"exclude": ["npm/", "examples/node", "smoke_tests/"]
18-
}
13+
"exclude": ["npm/", "examples/node", "smoke_tests/"]
1914
},
2015
"test": {
21-
"files": {
22-
"include": ["tests/"]
23-
}
16+
"include": ["tests/"]
2417
},
2518
"compilerOptions": {
26-
"lib": [
27-
"dom",
28-
"dom.iterable",
29-
"deno.ns"
30-
]
19+
"lib": ["dom", "dom.iterable", "deno.ns"]
3120
},
3221
"lock": false
3322
}

examples/node/js_node_14_up/basic_example.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import * as Dotenv from "dotenv";
10+
import process from "process";
1011
import { config, getJson } from "serpapi";
1112

1213
Dotenv.config();

examples/node/js_node_14_up/pagination_example.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import * as Dotenv from "dotenv";
10+
import process from "process";
1011
import { config, getJson } from "serpapi";
1112

1213
Dotenv.config();

examples/node/js_node_7_up/basic_example.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
const Dotenv = require("dotenv");
6+
const process = require("process");
67
const { config, getJson } = require("serpapi");
78

89
Dotenv.config();

examples/node/js_node_7_up/pagination_example.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Dotenv = require("dotenv");
66
const { config, getJson } = require("serpapi");
77
const url = require("url");
88
const qs = require("querystring");
9+
const process = require("process");
910

1011
Dotenv.config();
1112
config.api_key = process.env.API_KEY;

scripts/build_npm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { build, emptyDir } from "https://deno.land/x/dnt@0.37.0/mod.ts";
1+
import { build, emptyDir } from "https://deno.land/x/dnt@0.40.0/mod.ts";
22
import { version } from "../version.ts";
33

44
await emptyDir("./npm");

0 commit comments

Comments
 (0)