Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ and this project adheres to
- Remove `settings.json`, update CONTRIBUTING.md.
- Make types more lenient so newly supported engines/parameters whose types have
not yet been updated do not throw warnings.
- Raise the default `timeout` from 60 to 95 seconds, so slow searches are not
cut off before SerpApi's 90 second server-side timeout.

### Fixed

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,15 @@ request to SerpApi includes fetching the page:
const page = await extract({
url: "https://example.com/",
fetch_timeout: 10000, // sent to the Extract API as `timeout`
timeout: 60000, // request timeout of this library
timeout: 95000, // request timeout of this library
});
```

## Configuration

You can declare a global `api_key` and `timeout` value by modifying the `config`
object. `timeout` is defined in milliseconds and defaults to 60 seconds.
object. `timeout` is defined in milliseconds and defaults to 95 seconds, a bit
longer than SerpApi's 90 second server-side timeout.

All functions, other than `getLocations`, accepts an optional `api_key` and
`timeout` that will take precedence over the values defined in `config`.
Expand All @@ -205,7 +206,7 @@ All functions, other than `getLocations`, accepts an optional `api_key` and
import { config, getJson } from "serpapi";

config.api_key = API_KEY;
config.timeout = 60000;
config.timeout = 95000;

await getJson({ engine: "google", q: "coffee" }); // uses the API key defined in the config
await getJson({ engine: "google", api_key: API_KEY_2, q: "coffee" }); // API_KEY_2 will be used
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export type Config = {

export const config: Config = {
api_key: null,
timeout: 60000,
timeout: 95000,
};
11 changes: 10 additions & 1 deletion tests/validators_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,17 @@ describe("validateApiKey", () => {
});

describe("validateTimeout", () => {
const defaultTimeout = config.timeout;

afterEach(() => {
config.timeout = 60000;
config.timeout = defaultTimeout;
});

it("with default timeout", () => {
// SerpApi times out searches after 90 seconds on its side, so the
// default must wait longer than that.
assertEquals(defaultTimeout > 90000, true);
assertEquals(validateTimeout(undefined), defaultTimeout);
});

it("with invalid timeout", () => {
Expand Down