Skip to content

Commit d49ad1d

Browse files
committed
Add AllowArbitraryParams utility type
1 parent 322e2c8 commit d49ad1d

9 files changed

Lines changed: 28 additions & 9 deletions

File tree

examples/deno/basic_ts/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ deno run example.ts
2020
following:
2121
```ts
2222
import {
23+
AllowArbitraryParams,
2324
config,
2425
getJson,
2526
GoogleParameters,

examples/deno/basic_ts/example.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
2-
import { config, getJson, GoogleParameters } from "../../../mod.ts";
2+
import {
3+
AllowArbitraryParams,
4+
config,
5+
getJson,
6+
GoogleParameters,
7+
} from "../../../mod.ts";
38

49
const { API_KEY: apiKey } = loadSync();
510
const params = {
611
q: "Coffee",
712
api_key: apiKey,
8-
} satisfies GoogleParameters;
13+
} satisfies AllowArbitraryParams<GoogleParameters>;
914

1015
// Show result as JSON (async/await)
1116
const response1 = await getJson("google", params);

examples/deno/pagination_ts/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ deno run example.ts
2020
following:
2121
```ts
2222
import {
23+
AllowArbitraryParams,
2324
config,
2425
getJson,
2526
GoogleParameters,

examples/deno/pagination_ts/example.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
2-
import { config, getJson, GoogleParameters } from "../../../mod.ts";
2+
import {
3+
AllowArbitraryParams,
4+
config,
5+
getJson,
6+
GoogleParameters,
7+
} from "../../../mod.ts";
38

49
const { API_KEY: apiKey } = loadSync();
510

@@ -9,7 +14,7 @@ const extractLinks = (results: { link: string }[]) =>
914
const params = {
1015
q: "Coffee",
1116
api_key: apiKey,
12-
} satisfies GoogleParameters;
17+
} satisfies AllowArbitraryParams<GoogleParameters>;
1318

1419
// Pagination (async/await)
1520
let page1 = await getJson("google", params);

examples/node/basic_ts_esm/example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as Dotenv from "dotenv";
2-
import { config, getJson, GoogleParameters } from "serpapi";
2+
import { AllowArbitraryParams, config, getJson, GoogleParameters } from "serpapi";
33

44
Dotenv.config();
55
const apiKey = process.env.API_KEY;
66

77
const params = {
88
q: "Coffee",
99
api_key: apiKey,
10-
} satisfies GoogleParameters;
10+
} satisfies AllowArbitraryParams<GoogleParameters>;
1111

1212
// Show result as JSON (async/await)
1313
const response1 = await getJson("google", params);

examples/node/pagination_ts_esm/example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Dotenv from "dotenv";
2-
import { config, getJson, GoogleParameters } from "serpapi";
2+
import { AllowArbitraryParams, config, getJson, GoogleParameters } from "serpapi";
33

44
Dotenv.config();
55
const apiKey = process.env.API_KEY;
@@ -10,7 +10,7 @@ const extractLinks = (results: { link: string }[]) =>
1010
const params = {
1111
q: "Coffee",
1212
api_key: apiKey,
13-
} satisfies GoogleParameters;
13+
} satisfies AllowArbitraryParams<GoogleParameters>;
1414

1515
// Pagination (async/await)
1616
let page1 = await getJson("google", params);

mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export { InvalidTimeoutError, MissingApiKeyError } from "./src/errors.ts";
66
export type {
77
AccountApiParameters,
88
AccountInformation,
9+
AllowArbitraryParams,
910
BaseParameters,
1011
BaseResponse,
1112
EngineName,

src/serpapi.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
AccountApiParameters,
33
AccountInformation,
4+
AllowArbitraryParams,
45
BaseResponse,
56
EngineName,
67
EngineParameters,
@@ -71,7 +72,7 @@ const SEARCH_ARCHIVE_PATH = `/searches`;
7172
*/
7273
export async function getJson<
7374
E extends EngineName = EngineName,
74-
P extends (EngineParameters<E> & Record<string, unknown>) = EngineParameters<
75+
P extends AllowArbitraryParams<EngineParameters<E>> = EngineParameters<
7576
E
7677
>,
7778
>(

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { EngineMap } from "./engines/engine_map.ts";
22

3+
/**
4+
* Allow arbitrary parameters in addition to parameters in T.
5+
*/
6+
export type AllowArbitraryParams<T> = T & Record<string, unknown>;
7+
38
export type BaseParameters = {
49
/**
510
* Parameter defines the device to use to get the results. It can be set to

0 commit comments

Comments
 (0)