feat: typed Google Search parameters and responses (fixes #33)#37
Open
kenrogers wants to merge 1 commit intoserpapi:masterfrom
Open
feat: typed Google Search parameters and responses (fixes #33)#37kenrogers wants to merge 1 commit intoserpapi:masterfrom
kenrogers wants to merge 1 commit intoserpapi:masterfrom
Conversation
Replace Record<string, any> with GoogleSearchParameters and GoogleSearchResponse for the Google engine. Adds a typed overload to getJson() so users get autocomplete and validation when passing engine: "google". Generic types preserved for backwards compatibility. Pattern is extensible to other engines (Bing, YouTube, etc.) by adding files under src/engines/. Fixes serpapi#33
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
GoogleSearchParametersandGoogleSearchResponsetyped interfaces matching the Google Search API docsgetJson()— when you passengine: "google", you get autocomplete and typed responsesEngineParametersandBaseResponsepreserved for backwards compatibility with all other enginesOrganicResult,KnowledgeGraph,RelatedQuestion,SearchMetadata,SearchInformation, andSearchParametersContext
This is a starting point for addressing #33. The current types (
EngineParameters = Record<string, any>) provide no autocomplete, no typo protection, and no response typing. As the issue notes, this is effectively the same as not having TypeScript support.Typing every engine at once felt too large for a single PR. Instead, this PR is meant to be a pattern that can be incrementally applied to other engines.
Approach
Each engine gets its own file under
src/engines/. Adding Bing, YouTube, Amazon, etc. follows the same pattern: one file per engine, one typed overload per function.The index signature
[key: string]: unknownon the response type ensures new API fields work without SDK updates. This was a design decision that could be re-evaluated depending on desired maintenance burden for the SDK.