Skip to content

Commit

Permalink
Add a request option for custom headers
Browse files Browse the repository at this point in the history
  • Loading branch information
dlarocque committed May 1, 2024
1 parent d785a79 commit f3f9600
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
50 changes: 50 additions & 0 deletions packages/main/src/requests/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,56 @@ describe("request methods", () => {
);
expect(request.fetchOptions.signal).to.be.instanceOf(AbortSignal);
});
it("passes custom headers", async () => {
const request = await constructRequest(
"model-name",
Task.GENERATE_CONTENT,
"key",
true,
"",
{
customHeaders: new Headers({ customerHeader: "customerHeaderValue" }),
},
);
expect(
(request.fetchOptions.headers as Headers).get("customerHeader"),
).to.equal("customerHeaderValue");
});
it("passes custom x-goog-api-client header", async () => {
const request = await constructRequest(
"model-name",
Task.GENERATE_CONTENT,
"key",
true,
"",
{
customHeaders: new Headers({ "x-goog-api-client": "client/version" }),
},
);
expect(
(request.fetchOptions.headers as Headers).get("x-goog-api-client"),
).to.equal("genai-js/__PACKAGE_VERSION__, client/version");
});
it("passes apiClient and custom x-goog-api-client header", async () => {
const request = await constructRequest(
"model-name",
Task.GENERATE_CONTENT,
"key",
true,
"",
{
apiClient: "client/version",
customHeaders: new Headers({
"x-goog-api-client": "client/version2",
}),
},
);
expect(
(request.fetchOptions.headers as Headers).get("x-goog-api-client"),
).to.equal(
"client/version genai-js/__PACKAGE_VERSION__, client/version2",
);
});
});
describe("_makeRequestInternal", () => {
it("no error", async () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/main/src/requests/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ export async function getHeaders(url: RequestUrl): Promise<Headers> {
headers.append("Content-Type", "application/json");
headers.append("x-goog-api-client", getClientHeaders(url.requestOptions));
headers.append("x-goog-api-key", url.apiKey);

if (url.requestOptions.customHeaders) {
for (const [
headerName,
headerValue,
] of url.requestOptions.customHeaders.entries()) {
headers.append(headerName, headerValue);
}
}

return headers;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/main/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ export interface RequestOptions {
* Base endpoint url. Defaults to "https://generativelanguage.googleapis.com"
*/
baseUrl?: string;
/**
* Custom HTTP request headers.
*/
customHeaders?: Headers;
}

/**
Expand Down

0 comments on commit f3f9600

Please sign in to comment.