From 98c1e4178cedaba10e5455c53fc947823f57f86a Mon Sep 17 00:00:00 2001 From: Divyanshu Kumar <130997107+Divyanshu-2907@users.noreply.github.com> Date: Tue, 1 Dec 2026 15:56:17 +0530 Subject: [PATCH] Pass per-request requestOptions in getAccount and *BySearchId functions --- CHANGELOG.md | 3 +++ src/serpapi.ts | 4 +++ src/types.ts | 3 +++ src/utils.ts | 5 ++-- tests/serpapi_test.ts | 63 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81122eb..4e2fd44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,9 @@ and this project adheres to ### Fixed +- Pass per-request `requestOptions` in `getAccount`, `getJsonBySearchId`, + `getHtmlBySearchId` and `getMdBySearchId`. + ### Removed - Remove all types for engine parameters and responses. SerpApi's diff --git a/src/serpapi.ts b/src/serpapi.ts index 3766893..c8c9965 100644 --- a/src/serpapi.ts +++ b/src/serpapi.ts @@ -295,6 +295,7 @@ export async function getJsonBySearchId( { api_key: key, output: "json", + requestOptions: parameters.requestOptions, }, timeout, ); @@ -336,6 +337,7 @@ export async function getHtmlBySearchId( { api_key: key, output: "html", + requestOptions: parameters.requestOptions, }, timeout, ); @@ -377,6 +379,7 @@ export async function getMdBySearchId( { api_key: key, output: "md", + requestOptions: parameters.requestOptions, }, timeout, ); @@ -411,6 +414,7 @@ export async function getAccount( ACCOUNT_PATH, { api_key: key, + requestOptions: parameters.requestOptions, }, timeout, ); diff --git a/src/types.ts b/src/types.ts index 7be8162..86f36c3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,16 +9,19 @@ export type BaseResponse = Record; export type GetBySearchIdParameters = { api_key?: string; timeout?: number; + requestOptions?: http.RequestOptions; }; export type AccountApiParameters = { api_key?: string; timeout?: number; + requestOptions?: http.RequestOptions; }; export type LocationsApiParameters = { q?: string; limit?: number; timeout?: number; + requestOptions?: http.RequestOptions; }; export type ExtractApiParameters = EngineParameters & { diff --git a/src/utils.ts b/src/utils.ts index a7f1e19..823fd41 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -79,13 +79,14 @@ export function buildRequestOptions( export function execute( path: string, - parameters: qs.ParsedUrlQueryInput, + parameters: Record, timeout: number, ): Promise { + // `requestOptions` is not a query parameter, buildRequestOptions removes it. const options = buildRequestOptions(path, { ...parameters, source: getSource(), - }); + } as qs.ParsedUrlQueryInput); return new Promise((resolve, reject) => { let timer: ReturnType; diff --git a/tests/serpapi_test.ts b/tests/serpapi_test.ts index 44a4e16..b3d17ba 100644 --- a/tests/serpapi_test.ts +++ b/tests/serpapi_test.ts @@ -1015,6 +1015,7 @@ describe( assertSpyCallArg(executeStub, 0, 1, { api_key: "test_api_key", output: "md", + requestOptions: undefined, }); }); }, @@ -1097,3 +1098,65 @@ describe( }); }, ); + +describe("per-request requestOptions", () => { + const requestOptions = { headers: { "X-Custom-Header": "param-value" } }; + let executeStub: Stub; + + beforeAll(() => { + executeStub = stub(_internals, "execute", () => Promise.resolve("{}")); + }); + + afterAll(() => { + executeStub.restore(); + }); + + it("are passed through by getAccount", async () => { + await getAccount({ api_key: "test_api_key", requestOptions }); + assertSpyCallArg(executeStub, 0, 1, { + api_key: "test_api_key", + requestOptions, + }); + }); + + it("are passed through by getJsonBySearchId", async () => { + await getJsonBySearchId("search-id", { + api_key: "test_api_key", + requestOptions, + }); + assertSpyCallArg(executeStub, 1, 1, { + api_key: "test_api_key", + output: "json", + requestOptions, + }); + }); + + it("are passed through by getHtmlBySearchId", async () => { + await getHtmlBySearchId("search-id", { + api_key: "test_api_key", + requestOptions, + }); + assertSpyCallArg(executeStub, 2, 1, { + api_key: "test_api_key", + output: "html", + requestOptions, + }); + }); + + it("are passed through by getMdBySearchId", async () => { + await getMdBySearchId("search-id", { + api_key: "test_api_key", + requestOptions, + }); + assertSpyCallArg(executeStub, 3, 1, { + api_key: "test_api_key", + output: "md", + requestOptions, + }); + }); + + it("are passed through by getLocations", async () => { + await getLocations({ q: "Austin", requestOptions }); + assertSpyCallArg(executeStub, 4, 1, { q: "Austin", requestOptions }); + }); +});