Skip to content

Commit 658a0da

Browse files
authored
add apiClient option (#84)
1 parent dfeb692 commit 658a0da

File tree

7 files changed

+62
-5
lines changed

7 files changed

+62
-5
lines changed

.changeset/small-cheetahs-itch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@google/generative-ai": minor
3+
---
4+
5+
Add `apiClient` configuration option to `RequestOptions`.

.github/workflows/test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ jobs:
2424
runs-on: ubuntu-latest
2525
strategy:
2626
matrix:
27-
node-version: ['18.x', '20.x']
27+
# lock version 20 for now as 20.12.0 makes global fetch unstubbable
28+
# until we can rewrite tests to stub some other way
29+
node-version: ['18.x', '20.11.1']
2830
steps:
2931
- uses: actions/checkout@v4
3032
- name: Use Node.js
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [RequestOptions](./generative-ai.requestoptions.md) &gt; [apiClient](./generative-ai.requestoptions.apiclient.md)
4+
5+
## RequestOptions.apiClient property
6+
7+
Additional attribution information to include in the x-goog-api-client header. Used by wrapper SDKs.
8+
9+
**Signature:**
10+
11+
```typescript
12+
apiClient?: string;
13+
```

docs/reference/generative-ai.requestoptions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface RequestOptions
1616

1717
| Property | Modifiers | Type | Description |
1818
| --- | --- | --- | --- |
19+
| [apiClient?](./generative-ai.requestoptions.apiclient.md) | | string | _(Optional)_ Additional attribution information to include in the x-goog-api-client header. Used by wrapper SDKs. |
1920
| [apiVersion?](./generative-ai.requestoptions.apiversion.md) | | string | _(Optional)_ Version of API endpoint to call (e.g. "v1" or "v1beta"). If not specified, defaults to latest stable version. |
2021
| [baseUrl?](./generative-ai.requestoptions.baseurl.md) | | string | _(Optional)_ Base endpoint url. Defaults to "https://generativelanguage.googleapis.com" |
2122
| [timeout?](./generative-ai.requestoptions.timeout.md) | | number | _(Optional)_ Request timeout in milliseconds. |

packages/main/src/requests/request.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,33 @@ describe("request methods", () => {
119119
ok: true,
120120
} as Response);
121121
const response = await makeRequest(fakeRequestUrl, "");
122-
expect(fetchStub).to.be.calledOnce;
122+
expect(fetchStub).to.be.calledWith(fakeRequestUrl.toString(), {
123+
method: "POST",
124+
headers: {
125+
"Content-Type": "application/json",
126+
"x-goog-api-client": "genai-js/__PACKAGE_VERSION__",
127+
"x-goog-api-key": fakeRequestUrl.apiKey,
128+
},
129+
body: "",
130+
});
131+
expect(response.ok).to.be.true;
132+
});
133+
it("passes apiClient", async () => {
134+
const fetchStub = stub(globalThis, "fetch").resolves({
135+
ok: true,
136+
} as Response);
137+
const response = await makeRequest(fakeRequestUrl, "", {
138+
apiClient: "client/version",
139+
});
140+
expect(fetchStub).to.be.calledWith(fakeRequestUrl.toString(), {
141+
method: "POST",
142+
headers: {
143+
"Content-Type": "application/json",
144+
"x-goog-api-client": "client/version genai-js/__PACKAGE_VERSION__",
145+
"x-goog-api-key": fakeRequestUrl.apiKey,
146+
},
147+
body: "",
148+
});
123149
expect(response.ok).to.be.true;
124150
});
125151
it("error with timeout", async () => {

packages/main/src/requests/request.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ export class RequestUrl {
5959
/**
6060
* Simple, but may become more complex if we add more versions to log.
6161
*/
62-
function getClientHeaders(): string {
63-
return `${PACKAGE_LOG_HEADER}/${PACKAGE_VERSION}`;
62+
export function getClientHeaders(requestOptions: RequestOptions): string {
63+
const clientHeaders = [];
64+
if (requestOptions?.apiClient) {
65+
clientHeaders.push(requestOptions.apiClient);
66+
}
67+
clientHeaders.push(`${PACKAGE_LOG_HEADER}/${PACKAGE_VERSION}`);
68+
return clientHeaders.join(" ");
6469
}
6570

6671
export async function makeRequest(
@@ -75,7 +80,7 @@ export async function makeRequest(
7580
method: "POST",
7681
headers: {
7782
"Content-Type": "application/json",
78-
"x-goog-api-client": getClientHeaders(),
83+
"x-goog-api-client": getClientHeaders(requestOptions),
7984
"x-goog-api-key": url.apiKey,
8085
},
8186
body,

packages/main/types/requests.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ export interface RequestOptions {
116116
* defaults to latest stable version.
117117
*/
118118
apiVersion?: string;
119+
/**
120+
* Additional attribution information to include in the x-goog-api-client header.
121+
* Used by wrapper SDKs.
122+
*/
123+
apiClient?: string;
119124
/**
120125
* Base endpoint url. Defaults to "https://generativelanguage.googleapis.com"
121126
*/

0 commit comments

Comments
 (0)