Skip to content

Commit e4f71bb

Browse files
authored
Merge pull request #11 from serpapi/widen-node-support
Support Node.js 7 and higher
2 parents 24b0192 + 257a77a commit e4f71bb

51 files changed

Lines changed: 853 additions & 392 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/build.yml

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on: push
44

55
jobs:
66
build:
7+
name: "Deno tests and build npm files"
78
runs-on: ubuntu-22.04
89
steps:
910
- name: Checkout repo
@@ -28,8 +29,84 @@ jobs:
2829
- name: Setup Node
2930
uses: actions/setup-node@v3
3031
with:
31-
node-version: '18.x'
32+
node-version: '18.x' # Build files using a fixed node version
3233
registry-url: 'https://registry.npmjs.org'
3334

34-
- name: Test building of npm files
35+
- name: Build npm files
3536
run: deno task npm
37+
38+
- name: Zip build files
39+
run: zip npm.zip ./npm -r
40+
41+
- name: Upload build files for smoke tests
42+
uses: actions/upload-artifact@v3
43+
with:
44+
name: npm
45+
path: npm.zip
46+
retention-days: 1
47+
48+
smoke-tests-commonjs:
49+
name: "Smoke tests (CommonJS)"
50+
needs: build
51+
runs-on: ubuntu-22.04
52+
strategy:
53+
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]
55+
steps:
56+
- name: Checkout repo
57+
uses: actions/checkout@v3
58+
59+
- name: Setup Node
60+
uses: actions/setup-node@v3
61+
with:
62+
node-version: ${{ matrix.node-version }}
63+
registry-url: 'https://registry.npmjs.org'
64+
65+
- name: Download build files
66+
uses: actions/download-artifact@v3
67+
with:
68+
name: npm
69+
70+
- name: Unzip build files
71+
run: unzip npm.zip
72+
73+
- name: Run smoke tests
74+
env:
75+
SERPAPI_TEST_KEY: ${{ secrets.SERPAPI_TEST_KEY }}
76+
run: |
77+
cd smoke_tests/commonjs
78+
npm i
79+
npm test
80+
81+
smoke-tests-esm:
82+
name: "Smoke tests (ESM)"
83+
needs: build
84+
runs-on: ubuntu-22.04
85+
strategy:
86+
matrix:
87+
node-version: [14.x, 15.x, 16.x, 17.x, 18.x, 19.x]
88+
steps:
89+
- name: Checkout repo
90+
uses: actions/checkout@v3
91+
92+
- name: Setup Node
93+
uses: actions/setup-node@v3
94+
with:
95+
node-version: ${{ matrix.node-version }}
96+
registry-url: 'https://registry.npmjs.org'
97+
98+
- name: Download build files
99+
uses: actions/download-artifact@v3
100+
with:
101+
name: npm
102+
103+
- name: Unzip build files
104+
run: unzip npm.zip
105+
106+
- name: Run smoke tests
107+
env:
108+
SERPAPI_TEST_KEY: ${{ secrets.SERPAPI_TEST_KEY }}
109+
run: |
110+
cd smoke_tests/esm
111+
npm i
112+
npm test

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ and this project adheres to
1111
### Added
1212

1313
- Expose `EngineParameters` type.
14-
- Expose `InvalidArgumentTypesError` error.
14+
- Expose `InvalidArgumentError` error.
1515

1616
### Changed
1717

CONTRIBUTING.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ If you use VSCode, use the following settings (`.vscode/settings.json`):
2626
"mod.ts",
2727
"version.ts",
2828
"src",
29-
"tests",
29+
"tests/*.ts",
30+
"tests/engines/",
3031
"scripts",
3132
"examples/deno"
3233
],
@@ -96,6 +97,39 @@ deno task test:cov # Get test coverage by running tests that hit "localhost"
9697
deno task test:ci # Run tests that hit "https://serpapi.com"
9798
```
9899

100+
## Run examples on local source files
101+
102+
To run [examples](./examples/) on your local source files, follow these steps.
103+
104+
1. Run `deno task npm` to build the files.
105+
2. Update the respective example's `package.json` to depend on the local
106+
`serpapi` module instead,
107+
```json
108+
{
109+
"dependencies": {
110+
"dotenv": "*",
111+
"serpapi": "../../../npm"
112+
},
113+
"scripts": {
114+
"start": "node example.js"
115+
}
116+
}
117+
```
118+
119+
## Run smoke tests
120+
121+
These test key functionality on different Node.js versions. They are ran on
122+
GitHub Actions, see the [build workflow](.github/workflows/build.yml) for more
123+
details.
124+
125+
To run these locally, follow these steps.
126+
127+
1. Run `deno task npm` to build the files.
128+
2. Change directory to either the `commonjs` or `esm` folder.
129+
3. Setup the intended Node.js version. For example, if you're using `nvm`, you
130+
can run `nvm use 14` to run Node.js 14 for the current shell.
131+
4. Run `npm i`, then `npm test`.
132+
99133
## Update documentation
100134

101135
- Every exposed function must have associated JSDoc comments.
@@ -112,7 +146,8 @@ deno task docs:gen
112146
TypeScript types are generated from the backend code. Follow these steps to
113147
update the types.
114148

115-
1. Run `bundle exec rails sdk:generate_ts_types` in the backend repository.
149+
1. Run `bundle exec rails libraries:generate_ts_types` in the backend
150+
repository.
116151
2. Replace everything in `src/engines` with the generated files from
117152
`tmp/ts/engines`.
118153
3. Update `mod.ts` with the new engine exports from `tmp/ts/mod.ts`.

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,40 @@ more.
1717

1818
### Node.js
1919

20-
Ensure you're running at least Node.js v16.14.
20+
- Supports Node.js 7.10.1 and newer.
21+
- Refer to [this example](examples/node/basic_js_node_7_up) for help.
2122

2223
```bash
2324
npm install serpapi
2425
```
2526

27+
```js
28+
const { getJson } = require("serpapi");
29+
getJson({
30+
engine: "google",
31+
api_key: API_KEY, // Get your API_KEY from https://serpapi.com/manage-api-key
32+
q: "coffee",
33+
location: "Austin, Texas",
34+
}, (json) => {
35+
console.log(json["organic_results"]);
36+
});
37+
```
38+
39+
### Node.js with ES Modules (ESM) and top-level await
40+
41+
- If you prefer using the `import` syntax and top-level `await`, you need to use
42+
at least Node.js 14.8.0.
43+
- Refer to [this example](examples/node/basic_js_node_14_up) for help.
44+
45+
You will need to add `"type": "module"` to your `package.json`:
46+
47+
```js
48+
{
49+
"type": "module",
50+
// rest of package.json
51+
}
52+
```
53+
2654
```js
2755
import { getJson } from "serpapi";
2856
const response = await getJson({
@@ -36,7 +64,9 @@ console.log(response);
3664

3765
### Deno
3866

39-
Import directly from deno.land. Usage is otherwise the same as above.
67+
- Import directly from deno.land.
68+
- Usage is otherwise the same as above.
69+
- Refer to [this example](examples/deno/basic_ts) for help.
4070

4171
```ts
4272
import { getJson } from "https://deno.land/x/serpapi/mod.ts";

deno.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@
99
},
1010
"fmt": {
1111
"files": {
12-
"exclude": ["npm/", "examples/node"]
12+
"exclude": ["npm/", "examples/node", "smoke_tests/"]
1313
}
1414
},
1515
"lint": {
1616
"files": {
17-
"exclude": ["npm/", "examples/node"]
17+
"exclude": ["npm/", "examples/node", "smoke_tests/"]
1818
}
19+
},
20+
"test": {
21+
"files": {
22+
"include": ["tests/"]
23+
}
24+
},
25+
"compilerOptions": {
26+
"lib": [
27+
"dom",
28+
"dom.iterable",
29+
"deno.ns"
30+
]
1931
}
2032
}

docs/migrating_from_google_search_results_nodejs.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ migrate over to the `serpapi` npm package.
5151
- The `buildUrl`, `execute` and `search` methods are removed. Use `getJson` and
5252
`getHtml` functions instead.
5353
- The `SerpApiSearch` class is removed as a public class.
54-
- Dropped support for Node.js 16.13 and below. This module supports Node.js
55-
16.14 and above.
5654

5755
## Fixed
5856

examples/node/basic_js_commonjs/README.md

Lines changed: 0 additions & 39 deletions
This file was deleted.

examples/node/basic_js_esm/README.md

Lines changed: 0 additions & 40 deletions
This file was deleted.
File renamed without changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Basic JavaScript example for Node.js 14 and newer
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. Install dependencies and run example
11+
12+
```bash
13+
npm i
14+
npm start
15+
```

0 commit comments

Comments
 (0)