Skip to content

Commit c832e48

Browse files
committed
More lenient types for search parameters
1 parent 4ce8f9e commit c832e48

5 files changed

Lines changed: 42 additions & 20 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` and `EngineParameters` 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

mod.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export type {
88
AccountInformation,
99
BaseParameters,
1010
BaseResponse,
11+
EngineName,
12+
EngineParameters,
1113
GetBySearchIdParameters,
1214
Location,
1315
Locations,

src/serpapi.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import {
22
AccountApiParameters,
33
AccountInformation,
44
BaseResponse,
5+
EngineName,
6+
EngineParameters,
57
GetBySearchIdParameters,
68
Locations,
79
LocationsApiParameters,
810
} from "./types.ts";
9-
import { EngineMap } from "./engines/engine_map.ts";
1011
import {
1112
_internals,
1213
execute,
@@ -69,11 +70,12 @@ const SEARCH_ARCHIVE_PATH = `/searches`;
6970
* });
7071
*/
7172
export async function getJson<
72-
E extends keyof EngineMap,
73+
E extends EngineName = EngineName,
74+
P extends EngineParameters<E> = EngineParameters<E>,
7375
>(
7476
engine: E,
75-
parameters: EngineMap[E]["parameters"],
76-
callback?: (json: BaseResponse<EngineMap[E]["parameters"]>) => void,
77+
parameters: P,
78+
callback?: (json: BaseResponse<P>) => void,
7779
) {
7880
const key = validateApiKey(parameters.api_key, true);
7981
const timeout = validateTimeout(parameters.timeout);
@@ -87,9 +89,7 @@ export async function getJson<
8789
},
8890
timeout,
8991
);
90-
const json = await response.json() as BaseResponse<
91-
EngineMap[E]["parameters"]
92-
>;
92+
const json = await response.json() as BaseResponse<P>;
9393
const nextParametersFromResponse = extractNextParameters<E>(json);
9494
if (
9595
// https://github.com/serpapi/public-roadmap/issues/562
@@ -126,9 +126,12 @@ export async function getJson<
126126
* // callback
127127
* getHtml("google", { api_key: API_KEY, q: "coffee" }, console.log);
128128
*/
129-
export async function getHtml<E extends keyof EngineMap>(
129+
export async function getHtml<
130+
E extends EngineName = EngineName,
131+
P extends EngineParameters<E> = EngineParameters<E>,
132+
>(
130133
engine: E,
131-
parameters: EngineMap[E]["parameters"],
134+
parameters: P,
132135
callback?: (html: string) => void,
133136
) {
134137
const key = validateApiKey(parameters.api_key, true);

src/types.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { EngineMap } from "./engines/engine_map.ts";
2+
13
export type BaseParameters = {
24
/**
35
* Parameter defines the device to use to get the results. It can be set to
@@ -39,7 +41,19 @@ export type BaseParameters = {
3941
*/
4042
timeout?: number;
4143
};
42-
export type BaseResponse<P = Record<string | number | symbol, never>> = {
44+
45+
// https://github.com/microsoft/TypeScript/issues/29729
46+
// deno-lint-ignore ban-types
47+
type AnyEngineName = string & {};
48+
export type EngineName = (keyof EngineMap) | AnyEngineName;
49+
export type EngineParameters<
50+
E extends EngineName = EngineName,
51+
> = {
52+
[K in E]: K extends keyof EngineMap ? EngineMap[K]["parameters"]
53+
: BaseParameters & Record<string, unknown>;
54+
}[E];
55+
56+
export type BaseResponse<P = Record<string, unknown>> = {
4357
search_metadata: {
4458
id: string;
4559
status: "Queued" | "Processing" | "Success";

src/utils.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EngineMap } from "./engines/engine_map.ts";
1+
import type { EngineName, EngineParameters } from "./types.ts";
22
import { version } from "../version.ts";
33

44
type UrlParameters = Record<
@@ -35,16 +35,15 @@ function getBaseUrl() {
3535
return "https://serpapi.com";
3636
}
3737

38-
type NextParametersKeys<E extends keyof EngineMap> = Omit<
39-
EngineMap[E]["parameters"],
40-
"api_key" | "no_cache" | "async" | "timeout"
41-
>;
42-
type NextParameters<E extends keyof EngineMap> = {
43-
[K in keyof NextParametersKeys<E>]: string;
38+
type NextParameters<E extends EngineName = EngineName> = {
39+
[
40+
K in keyof Omit<
41+
EngineParameters<E>,
42+
"api_key" | "no_cache" | "async" | "timeout"
43+
>
44+
]: string;
4445
};
45-
export function extractNextParameters<
46-
E extends keyof EngineMap,
47-
>(json: {
46+
export function extractNextParameters<E extends EngineName = EngineName>(json: {
4847
serpapi_pagination?: { next: string };
4948
pagination?: { next: string };
5049
}) {

0 commit comments

Comments
 (0)