Skip to content

Commit ced71a7

Browse files
committed
Refactor BaseResponse to depend on engine name
1 parent c832e48 commit ced71a7

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

src/serpapi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export async function getJson<
7575
>(
7676
engine: E,
7777
parameters: P,
78-
callback?: (json: BaseResponse<P>) => void,
78+
callback?: (json: BaseResponse<E>) => void,
7979
) {
8080
const key = validateApiKey(parameters.api_key, true);
8181
const timeout = validateTimeout(parameters.timeout);
@@ -89,7 +89,7 @@ export async function getJson<
8989
},
9090
timeout,
9191
);
92-
const json = await response.json() as BaseResponse<P>;
92+
const json = await response.json() as BaseResponse<E>;
9393
const nextParametersFromResponse = extractNextParameters<E>(json);
9494
if (
9595
// https://github.com/serpapi/public-roadmap/issues/562

src/types.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export type EngineParameters<
5353
: BaseParameters & Record<string, unknown>;
5454
}[E];
5555

56-
export type BaseResponse<P = Record<string, unknown>> = {
56+
export type BaseResponse<E extends EngineName = EngineName> = {
5757
search_metadata: {
5858
id: string;
5959
status: "Queued" | "Processing" | "Success";
@@ -63,14 +63,15 @@ export type BaseResponse<P = Record<string, unknown>> = {
6363
raw_html_file: string;
6464
total_time_taken: number;
6565
};
66-
search_parameters:
67-
& { engine: string }
68-
& Omit<BaseParameters & P, "api_key" | "no_cache" | "async" | "timeout">;
66+
search_parameters: Omit<
67+
EngineParameters<E>,
68+
"api_key" | "no_cache" | "async" | "timeout"
69+
>;
6970
serpapi_pagination?: { next: string };
7071
pagination?: { next: string };
7172
next?: (
72-
callback?: (json: BaseResponse<P>) => void,
73-
) => Promise<BaseResponse<P>>;
73+
callback?: (json: BaseResponse<E>) => void,
74+
) => Promise<BaseResponse<E>>;
7475
// deno-lint-ignore no-explicit-any
7576
[key: string]: any; // TODO(seb): use recursive type
7677
};

0 commit comments

Comments
 (0)