feat: typed Google 搜索 parameters and responses (fixes #33) #37
新建议题
Have a question about this project? 注册 for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “注册 for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? 登录 to your account
Open
kenrogers
wants to merge
1
commit into
serpapi:master
Choose a base branch
from
kenrogers:feat/improved-typescript-types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
提交
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /** Parameters for the Google 搜索 engine. */ | ||
| export interface Google搜索Parameters { | ||
| engine: "google"; | ||
| q: string; | ||
| api_key?: string; | ||
| timeout?: number; | ||
|
|
||
| // Geographic Location | ||
| location?: string; | ||
| uule?: string; | ||
| lat?: number; | ||
| lon?: number; | ||
| radius?: number; | ||
|
|
||
| // Localization | ||
| google_domain?: string; | ||
| gl?: string; | ||
| hl?: string; | ||
| cr?: string; | ||
| lr?: string; | ||
|
|
||
| // 搜索 Type | ||
| tbm?: "isch" | "lcl" | "vid" | "nws" | "shop" | "pts"; | ||
|
|
||
| // Pagination | ||
| start?: number; | ||
| num?: number; | ||
|
|
||
| // Advanced Filters | ||
| tbs?: string; | ||
| safe?: "active" | "off"; | ||
| nfpr?: 0 | 1; | ||
| filter?: 0 | 1; | ||
|
|
||
| // Advanced Google Parameters | ||
| ludocid?: string; | ||
| lsig?: string; | ||
| kgmid?: string; | ||
| si?: string; | ||
|
|
||
| // SerpApi Parameters | ||
| device?: "desktop" | "tablet" | "mobile"; | ||
| no_cache?: boolean; | ||
| async?: boolean; | ||
|
|
||
| // deno-lint-ignore no-explicit-any | ||
| [key: string]: any; | ||
|
catherine-serpapi marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** Metadata returned with every SerpApi response. */ | ||
| export interface 搜索Metadata { | ||
| id: string; | ||
| status: string; | ||
| json_endpoint: string; | ||
| created_at: string; | ||
| processed_at: string; | ||
| google_url: string; | ||
| raw_html_file: string; | ||
| total_time_taken: number; | ||
| } | ||
|
|
||
| /** Echoed search parameters in the response. */ | ||
| export interface 搜索Parameters { | ||
| engine: string; | ||
| q: string; | ||
| google_domain?: string; | ||
| hl?: string; | ||
| gl?: string; | ||
| device?: string; | ||
| location_requested?: string; | ||
| location_used?: string; | ||
| [key: string]: unknown; | ||
| } | ||
|
|
||
| /** 搜索 result info. */ | ||
| export interface 搜索Information { | ||
| query_displayed: string; | ||
| total_results: number; | ||
| time_taken_displayed: number; | ||
| organic_results_state: string; | ||
| page_number?: number; | ||
| } | ||
|
|
||
| /** A single organic search result. */ | ||
| export interface OrganicResult { | ||
| position: number; | ||
| title: string; | ||
| link: string; | ||
| redirect_link?: string; | ||
| displayed_link: string; | ||
| snippet: string; | ||
| snippet_highlighted_words?: string[]; | ||
| date?: string; | ||
| sitelinks?: { | ||
| inline?: { title: string; link: string }[]; | ||
| expanded?: { title: string; link: string; snippet: string }[]; | ||
| }; | ||
| rich_snippet?: Record<string, unknown>; | ||
| source?: string; | ||
| } | ||
|
|
||
| /** Knowledge graph result. */ | ||
| export interface KnowledgeGraph { | ||
| title?: string; | ||
| type?: string; | ||
| description?: string; | ||
| source?: { name: string; link: string }; | ||
| [key: string]: unknown; | ||
| } | ||
|
|
||
| /** "People also ask" question. */ | ||
| export interface RelatedQuestion { | ||
| question: string; | ||
| snippet?: string; | ||
| title?: string; | ||
| link?: string; | ||
| } | ||
|
|
||
| /** Google 搜索 JSON response. */ | ||
| export interface Google搜索Response { | ||
| search_metadata: 搜索Metadata; | ||
| search_parameters: 搜索Parameters; | ||
| search_information?: 搜索Information; | ||
| organic_results?: OrganicResult[]; | ||
| knowledge_graph?: KnowledgeGraph; | ||
| related_questions?: RelatedQuestion[]; | ||
| pagination?: { | ||
| current: number; | ||
| next?: string; | ||
| other_pages?: Record<string, string>; | ||
| }; | ||
| [key: string]: unknown; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * Type-level tests for engine-specific types. | ||
| * These verify compile-time behavior — if this file fails to type-check, | ||
| * the types are broken. | ||
| */ | ||
| import type { | ||
| EngineParameters, | ||
| Google搜索Parameters, | ||
| Google搜索Response, | ||
| OrganicResult, | ||
| } from "../src/types.ts"; | ||
| import { getJson } from "../src/serpapi.ts"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| import { | ||
|
catherine-serpapi marked this conversation as resolved.
|
||
| describe, | ||
| it, | ||
| } from "https://deno.land/std@0.170.0/testing/bdd.ts"; | ||
| import { | ||
| assertEquals, | ||
| } from "https://deno.land/std@0.170.0/testing/asserts.ts"; | ||
|
|
||
| describe("Google搜索Parameters", () => { | ||
| it("accepts valid Google 搜索 params", () => { | ||
| const params: Google搜索Parameters = { | ||
| engine: "google", | ||
| q: "coffee", | ||
| location: "Austin, Texas", | ||
| gl: "us", | ||
| hl: "en", | ||
| num: 10, | ||
| start: 0, | ||
| safe: "active", | ||
| device: "desktop", | ||
| }; | ||
| assertEquals(params.engine, "google"); | ||
| assertEquals(params.q, "coffee"); | ||
| }); | ||
|
|
||
| it("allows tbm search type values", () => { | ||
| const params: Google搜索Parameters = { | ||
| engine: "google", | ||
| q: "coffee", | ||
| tbm: "nws", | ||
| }; | ||
| assertEquals(params.tbm, "nws"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Google搜索Response", () => { | ||
| it("has correct shape", () => { | ||
| const response: Google搜索Response = { | ||
| search_metadata: { | ||
| id: "123", | ||
| status: "Success", | ||
| json_endpoint: "https://serpapi.com/searches/123.json", | ||
| created_at: "2025-01-01", | ||
| processed_at: "2025-01-01", | ||
| google_url: "https://www.google.com/search?q=coffee", | ||
| raw_html_file: "https://serpapi.com/searches/123.html", | ||
| total_time_taken: 1.5, | ||
| }, | ||
| search_parameters: { | ||
| engine: "google", | ||
| q: "coffee", | ||
| }, | ||
| organic_results: [ | ||
| { | ||
| position: 1, | ||
| title: "Coffee - 维基pedia", | ||
| link: "https://en.wikipedia.org/wiki/Coffee", | ||
| displayed_link: "en.wikipedia.org", | ||
| snippet: "Coffee is a brewed drink...", | ||
| }, | ||
| ], | ||
| }; | ||
| assertEquals(response.search_metadata.status, "Success"); | ||
|
|
||
| const firstResult: OrganicResult = response.organic_results![0]; | ||
| assertEquals(firstResult.position, 1); | ||
| assertEquals(firstResult.title, "Coffee - 维基pedia"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("backwards compatibility", () => { | ||
| it("generic EngineParameters still works", () => { | ||
| const params: EngineParameters = { | ||
| engine: "bing", | ||
| q: "anything", | ||
| custom_field: 123, | ||
| }; | ||
| assertEquals(params.engine, "bing"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we should extract out SerpApi Parameters and have the other engines' types extend it since nearly every other engine has the same SerpApi-specific params:https://serpapi.com/search-api#api-parameters-serpapi-parameters
What do you think @zyc9012
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This library used to have types, but we removed them (#15) because all the parameters are unstable. We can add new engines/parameters from time to time, which makes it hard to sync with SDKs. I'm not sure if we want to bring types back.