Skip to content

Commit e4e9e5b

Browse files
committed
Simplify EngineParameters and BaseResponse
1 parent 8fd0b05 commit e4e9e5b

10 files changed

Lines changed: 21 additions & 87 deletions

File tree

examples/deno/basic_ts/example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
2-
import { config, EngineParameters, getJson } from "../../../mod.ts";
2+
import { config, getJson } from "../../../mod.ts";
33

44
const { API_KEY: apiKey } = loadSync();
55
const params = {
66
engine: "google",
77
q: "Coffee",
88
api_key: apiKey,
9-
} as EngineParameters;
9+
};
1010

1111
// Show result as JSON (async/await)
1212
const response1 = await getJson(params);

examples/deno/pagination_ts/example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { loadSync } from "https://deno.land/std@0.173.0/dotenv/mod.ts";
2-
import { config, EngineParameters, getJson } from "../../../mod.ts";
2+
import { config, getJson } from "../../../mod.ts";
33

44
const { API_KEY: apiKey } = loadSync();
55

@@ -10,7 +10,7 @@ const params = {
1010
engine: "google",
1111
q: "Coffee",
1212
api_key: apiKey,
13-
} satisfies EngineParameters;
13+
};
1414

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

examples/node/basic_ts_node_14_up/example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import * as Dotenv from "dotenv";
10-
import { config, EngineParameters, getJson } from "serpapi";
10+
import { config, getJson } from "serpapi";
1111

1212
Dotenv.config();
1313
const apiKey = process.env.API_KEY;
@@ -16,7 +16,7 @@ const params = {
1616
engine: "google",
1717
q: "Coffee",
1818
api_key: apiKey,
19-
} satisfies EngineParameters;
19+
};
2020

2121
// Show result as JSON (async/await)
2222
const response1 = await getJson(params);

examples/node/pagination_ts_node_14_up/example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
import * as Dotenv from "dotenv";
12-
import { config, EngineParameters, getJson } from "serpapi";
12+
import { config, getJson } from "serpapi";
1313

1414
Dotenv.config();
1515
const apiKey = process.env.API_KEY;
@@ -21,7 +21,7 @@ const params = {
2121
engine: "google",
2222
q: "Coffee",
2323
api_key: apiKey,
24-
} satisfies EngineParameters;
24+
};
2525

2626
// Pagination (async/await)
2727
let page1 = await getJson(params);

mod.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export {
1010
export type {
1111
AccountApiParameters,
1212
AccountInformation,
13-
BaseParameters,
1413
BaseResponse,
1514
EngineParameters,
1615
GetBySearchIdParameters,

src/serpapi.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function getJson(
7575
]
7676
| [
7777
engine: string,
78-
parameters: EngineParameters<false>,
78+
parameters: EngineParameters,
7979
callback?: (json: BaseResponse) => void,
8080
]
8181
): Promise<BaseResponse> {
@@ -87,7 +87,7 @@ export function getJson(
8787
const newParameters = { ...parameters, engine } as EngineParameters;
8888
return _getJson(newParameters, callback);
8989
} else if (
90-
typeof args[0] === "object" &&
90+
typeof args[0] === "object" && typeof args[1] !== "object" &&
9191
(typeof args[1] === "undefined" || typeof args[1] === "function")
9292
) {
9393
const [parameters, callback] = args;
@@ -155,7 +155,7 @@ export function getHtml(
155155
]
156156
| [
157157
engine: string,
158-
parameters: EngineParameters<false>,
158+
parameters: EngineParameters,
159159
callback?: (html: string) => void,
160160
]
161161
): Promise<string> {
@@ -167,7 +167,7 @@ export function getHtml(
167167
const newParameters = { ...parameters, engine } as EngineParameters;
168168
return _getHtml(newParameters, callback);
169169
} else if (
170-
typeof args[0] === "object" &&
170+
typeof args[0] === "object" && typeof args[1] !== "object" &&
171171
(typeof args[1] === "undefined" || typeof args[1] === "function")
172172
) {
173173
const [parameters, callback] = args;

src/types.ts

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,12 @@
1-
export type BaseParameters = {
2-
/**
3-
* Parameter defines the device to use to get the results. It can be set to
4-
* `desktop` (default) to use a regular browser, `tablet` to use a tablet browser
5-
* (currently using iPads), or `mobile` to use a mobile browser (currently
6-
* using iPhones).
7-
*/
8-
device?: "desktop" | "tablet" | "mobile";
9-
10-
/**
11-
* Parameter will force SerpApi to fetch the Google results even if a cached
12-
* version is already present. A cache is served only if the query and all
13-
* parameters are exactly the same. Cache expires after 1h. Cached searches
14-
* are free, and are not counted towards your searches per month. It can be set
15-
* to `false` (default) to allow results from the cache, or `true` to disallow
16-
* results from the cache. `no_cache` and `async` parameters should not be used together.
17-
*/
18-
no_cache?: boolean;
19-
20-
/**
21-
* Parameter defines the way you want to submit your search to SerpApi. It can
22-
* be set to `false` (default) to open an HTTP connection and keep it open until
23-
* you got your search results, or `true` to just submit your search to SerpApi
24-
* and retrieve them later. In this case, you'll need to use our
25-
* [Searches Archive API](https://serpapi.com/search-archive-api) to retrieve
26-
* your results. `async` and `no_cache` parameters should not be used together.
27-
* `async` should not be used on accounts with
28-
* [Ludicrous Speed](https://serpapi.com/plan) enabled.
29-
*/
30-
async?: boolean;
31-
32-
/**
33-
* Parameter defines the SerpApi private key to use.
34-
*/
35-
api_key?: string | null;
36-
37-
/**
38-
* Specify the client-side timeout of the request. In milliseconds.
39-
*/
40-
timeout?: number;
41-
};
42-
43-
export type EngineParameters<
44-
EngineRequired = true,
45-
> =
46-
& (EngineRequired extends true ? { engine: string }
47-
: { engine?: string })
48-
& BaseParameters
49-
& Record<string, unknown>;
1+
// deno-lint-ignore no-explicit-any
2+
export type EngineParameters = Record<string, any>;
503

514
export type BaseResponse = {
52-
search_metadata: {
53-
id: string;
54-
status: "Queued" | "Processing" | "Success";
55-
json_endpoint: string;
56-
created_at: string;
57-
processed_at: string;
58-
raw_html_file: string;
59-
total_time_taken: number;
60-
};
61-
search_parameters: Omit<
62-
EngineParameters,
63-
"api_key" | "no_cache" | "async" | "timeout"
64-
>;
65-
serpapi_pagination?: { next: string };
66-
pagination?: { next: string };
675
next?: (
686
callback?: (json: BaseResponse) => void,
697
) => Promise<BaseResponse>;
708
// deno-lint-ignore no-explicit-any
71-
[key: string]: any; // TODO(seb): use recursive type
72-
};
9+
} & Record<string, any>;
7310

7411
export type GetBySearchIdParameters = {
7512
api_key?: string;

src/utils.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ type NextParameters = {
3131
]: string;
3232
};
3333
export function extractNextParameters(
34-
json: {
35-
serpapi_pagination?: { next: string };
36-
pagination?: { next: string };
37-
},
34+
// deno-lint-ignore no-explicit-any
35+
json: any,
3836
) {
3937
const nextUrlString = json["serpapi_pagination"]?.["next"] ||
4038
json["pagination"]?.["next"];

tests/serpapi_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ describe("getJson", {
293293
it("callback", {
294294
ignore: !HAS_API_KEY,
295295
}, async () => {
296-
const json = await new Promise<BaseResponse<"google">>((done) => {
296+
const json = await new Promise<BaseResponse>((done) => {
297297
getJson({
298298
engine: "google",
299299
q: "Paris",
@@ -305,7 +305,7 @@ describe("getJson", {
305305
assertExists(json.organic_results);
306306

307307
// old API
308-
const json2 = await new Promise<BaseResponse<"google">>((done) => {
308+
const json2 = await new Promise<BaseResponse>((done) => {
309309
getJson("google", {
310310
q: "Paris",
311311
api_key: SERPAPI_TEST_KEY,

tests/utils_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const BASE_URL = Deno.env.get("ENV_TYPE") === "local"
2929
describe("extractNextParameters", () => {
3030
it("with serpapi_pagination property", async () => {
3131
assertEquals(
32-
await extractNextParameters<"google">({
32+
await extractNextParameters({
3333
serpapi_pagination: {
3434
next:
3535
"https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=Austin%2C+Texas%2C+United+States&q=coffee&start=10",
@@ -50,7 +50,7 @@ describe("extractNextParameters", () => {
5050

5151
it("with pagination property", async () => {
5252
assertEquals(
53-
await extractNextParameters<"google_scholar_profiles">(
53+
await extractNextParameters(
5454
{
5555
pagination: {
5656
next:

0 commit comments

Comments
 (0)