Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/serpapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export async function getJsonBySearchId(
{
api_key: key,
output: "json",
requestOptions: parameters.requestOptions,
},
timeout,
);
Expand Down Expand Up @@ -336,6 +337,7 @@ export async function getHtmlBySearchId(
{
api_key: key,
output: "html",
requestOptions: parameters.requestOptions,
},
timeout,
);
Expand Down Expand Up @@ -377,6 +379,7 @@ export async function getMdBySearchId(
{
api_key: key,
output: "md",
requestOptions: parameters.requestOptions,
},
timeout,
);
Expand Down Expand Up @@ -411,6 +414,7 @@ export async function getAccount(
ACCOUNT_PATH,
{
api_key: key,
requestOptions: parameters.requestOptions,
},
timeout,
);
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ export type BaseResponse = Record<string, any>;
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 & {
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ export function buildRequestOptions(

export function execute(
path: string,
parameters: qs.ParsedUrlQueryInput,
parameters: Record<string, unknown>,
timeout: number,
): Promise<string> {
// `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<typeof setTimeout>;
Expand Down
63 changes: 63 additions & 0 deletions tests/serpapi_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,7 @@ describe(
assertSpyCallArg(executeStub, 0, 1, {
api_key: "test_api_key",
output: "md",
requestOptions: undefined,
});
});
},
Expand Down Expand Up @@ -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 });
});
});