Skip to content

Commit db00dda

Browse files
Merge pull request #8 from serpapi/more-lenient-types
More lenient TypeScript types
2 parents 4ce8f9e + 98d2cfc commit db00dda

11 files changed

Lines changed: 75 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ and this project adheres to
1010

1111
### Added
1212

13+
- Expose `EngineName`, `EngineParameters` and `AllowArbitraryParams` types.
14+
1315
### Changed
1416

1517
- Remove `settings.json`, update CONTRIBUTING.md.
18+
- Make types more lenient so newly supported engines/parameters whose types have
19+
not yet been updated do not throw warnings.
1620

1721
### Fixed
1822

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ export { InvalidTimeoutError, MissingApiKeyError } from "./src/errors.ts";
66
export type {
77
AccountApiParameters,
88
AccountInformation,
9+
AllowArbitraryParams,
910
BaseParameters,
1011
BaseResponse,
12+
EngineName,
13+
EngineParameters,
1114
GetBySearchIdParameters,
1215
Location,
1316
Locations,

src/serpapi.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import {
22
AccountApiParameters,
33
AccountInformation,
4+
AllowArbitraryParams,
45
BaseResponse,
6+
EngineName,
7+
EngineParameters,
58
GetBySearchIdParameters,
69
Locations,
710
LocationsApiParameters,
811
} from "./types.ts";
9-
import { EngineMap } from "./engines/engine_map.ts";
1012
import {
1113
_internals,
1214
execute,
@@ -69,11 +71,12 @@ const SEARCH_ARCHIVE_PATH = `/searches`;
6971
* });
7072
*/
7173
export async function getJson<
72-
E extends keyof EngineMap,
74+
E extends EngineName = EngineName,
75+
P extends AllowArbitraryParams<EngineParameters<E>> = EngineParameters<E>,
7376
>(
7477
engine: E,
75-
parameters: EngineMap[E]["parameters"],
76-
callback?: (json: BaseResponse<EngineMap[E]["parameters"]>) => void,
78+
parameters: P,
79+
callback?: (json: BaseResponse<E>) => void,
7780
) {
7881
const key = validateApiKey(parameters.api_key, true);
7982
const timeout = validateTimeout(parameters.timeout);
@@ -87,9 +90,7 @@ export async function getJson<
8790
},
8891
timeout,
8992
);
90-
const json = await response.json() as BaseResponse<
91-
EngineMap[E]["parameters"]
92-
>;
93+
const json = await response.json() as BaseResponse<E>;
9394
const nextParametersFromResponse = extractNextParameters<E>(json);
9495
if (
9596
// https://github.com/serpapi/public-roadmap/issues/562
@@ -126,9 +127,12 @@ export async function getJson<
126127
* // callback
127128
* getHtml("google", { api_key: API_KEY, q: "coffee" }, console.log);
128129
*/
129-
export async function getHtml<E extends keyof EngineMap>(
130+
export async function getHtml<
131+
E extends EngineName = EngineName,
132+
P extends AllowArbitraryParams<EngineParameters<E>> = EngineParameters<E>,
133+
>(
130134
engine: E,
131-
parameters: EngineMap[E]["parameters"],
135+
parameters: P,
132136
callback?: (html: string) => void,
133137
) {
134138
const key = validateApiKey(parameters.api_key, true);

src/types.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
import { EngineMap } from "./engines/engine_map.ts";
2+
3+
/**
4+
* Allow arbitrary parameters in addition to parameters in T.
5+
*/
6+
export type AllowArbitraryParams<T> = T & Record<string, unknown>;
7+
18
export type BaseParameters = {
29
/**
310
* Parameter defines the device to use to get the results. It can be set to
@@ -39,7 +46,19 @@ export type BaseParameters = {
3946
*/
4047
timeout?: number;
4148
};
42-
export type BaseResponse<P = Record<string | number | symbol, never>> = {
49+
50+
// https://github.com/microsoft/TypeScript/issues/29729
51+
// deno-lint-ignore ban-types
52+
type AnyEngineName = string & {};
53+
export type EngineName = (keyof EngineMap) | AnyEngineName;
54+
export type EngineParameters<
55+
E extends EngineName = EngineName,
56+
> = {
57+
[K in E]: K extends keyof EngineMap ? EngineMap[K]["parameters"]
58+
: BaseParameters & Record<string, unknown>;
59+
}[E];
60+
61+
export type BaseResponse<E extends EngineName = EngineName> = {
4362
search_metadata: {
4463
id: string;
4564
status: "Queued" | "Processing" | "Success";
@@ -49,14 +68,15 @@ export type BaseResponse<P = Record<string | number | symbol, never>> = {
4968
raw_html_file: string;
5069
total_time_taken: number;
5170
};
52-
search_parameters:
53-
& { engine: string }
54-
& Omit<BaseParameters & P, "api_key" | "no_cache" | "async" | "timeout">;
71+
search_parameters: Omit<
72+
EngineParameters<E>,
73+
"api_key" | "no_cache" | "async" | "timeout"
74+
>;
5575
serpapi_pagination?: { next: string };
5676
pagination?: { next: string };
5777
next?: (
58-
callback?: (json: BaseResponse<P>) => void,
59-
) => Promise<BaseResponse<P>>;
78+
callback?: (json: BaseResponse<E>) => void,
79+
) => Promise<BaseResponse<E>>;
6080
// deno-lint-ignore no-explicit-any
6181
[key: string]: any; // TODO(seb): use recursive type
6282
};

0 commit comments

Comments
 (0)