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 87cb3f5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
46 changes: 46 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,52 @@ 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("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
11 changes: 11 additions & 0 deletions packages/main/src/requests/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class RequestUrl {
* Simple, but may become more complex if we add more versions to log.
*/
export function getClientHeaders(requestOptions: RequestOptions): string {
// FIXME: Should we do this for an apiClient entry in customHeaders?
const clientHeaders = [];
if (requestOptions?.apiClient) {
clientHeaders.push(requestOptions.apiClient);
Expand All @@ -76,6 +77,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 87cb3f5

Please sign in to comment.