Skip to content

Commit d09a548

Browse files
committed
Add Firebase-Appversion header
1 parent 767cd77 commit d09a548

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

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

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { DEFAULT_API_VERSION } from '../constants';
2525
import { VertexAIErrorCode } from '../types';
2626
import { VertexAIError } from '../errors';
2727
import { getMockResponse } from '../../test-utils/mock-response';
28+
import { SDK_VERSION } from '@firebase/app';
2829

2930
use(sinonChai);
3031
use(chaiAsPromised);
@@ -126,10 +127,6 @@ describe('request methods', () => {
126127
const headers = await getHeaders(fakeUrl);
127128
expect(headers.get('x-goog-api-key')).to.equal('key');
128129
});
129-
it('adds app id if automatedDataCollectionEnabled is undefined', async () => {
130-
const headers = await getHeaders(fakeUrl);
131-
expect(headers.get('X-Firebase-AppId')).to.equal('my-appid');
132-
});
133130
it('adds app id if automatedDataCollectionEnabled is true', async () => {
134131
const fakeApiSettings: ApiSettings = {
135132
apiKey: 'key',
@@ -148,7 +145,11 @@ describe('request methods', () => {
148145
{}
149146
);
150147
const headers = await getHeaders(fakeUrl);
151-
expect(headers.get('X-Firebase-AppId')).to.equal('my-appid');
148+
expect(headers.get('X-Firebase-Appid')).to.equal('my-appid');
149+
});
150+
it('does not add app id if automatedDataCollectionEnabled is undefined', async () => {
151+
const headers = await getHeaders(fakeUrl);
152+
expect(headers.get('X-Firebase-Appid')).to.be.null;
152153
});
153154
it('does not add app id if automatedDataCollectionEnabled is false', async () => {
154155
const fakeApiSettings: ApiSettings = {
@@ -168,7 +169,51 @@ describe('request methods', () => {
168169
{}
169170
);
170171
const headers = await getHeaders(fakeUrl);
171-
expect(headers.get('X-Firebase-AppId')).to.be.null;
172+
expect(headers.get('X-Firebase-Appid')).to.be.null;
173+
});
174+
it('adds app version if automatedDataCollectionEnabled is true', async () => {
175+
const fakeApiSettings: ApiSettings = {
176+
apiKey: 'key',
177+
project: 'myproject',
178+
appId: 'my-appid',
179+
location: 'moon',
180+
automaticDataCollectionEnabled: true,
181+
getAuthToken: () => Promise.resolve({ accessToken: 'authtoken' }),
182+
getAppCheckToken: () => Promise.resolve({ token: 'appchecktoken' })
183+
};
184+
const fakeUrl = new RequestUrl(
185+
'models/model-name',
186+
Task.GENERATE_CONTENT,
187+
fakeApiSettings,
188+
true,
189+
{}
190+
);
191+
const headers = await getHeaders(fakeUrl);
192+
expect(headers.get('X-Firebase-Appversion')).to.equal(SDK_VERSION);
193+
});
194+
it('does not add app version if automatedDataCollectionEnabled is undefined', async () => {
195+
const headers = await getHeaders(fakeUrl);
196+
expect(headers.get('X-Firebase-Appversion')).to.be.null;
197+
});
198+
it('does not add app version if automatedDataCollectionEnabled is false', async () => {
199+
const fakeApiSettings: ApiSettings = {
200+
apiKey: 'key',
201+
project: 'myproject',
202+
appId: 'my-appid',
203+
location: 'moon',
204+
automaticDataCollectionEnabled: false,
205+
getAuthToken: () => Promise.resolve({ accessToken: 'authtoken' }),
206+
getAppCheckToken: () => Promise.resolve({ token: 'appchecktoken' })
207+
};
208+
const fakeUrl = new RequestUrl(
209+
'models/model-name',
210+
Task.GENERATE_CONTENT,
211+
fakeApiSettings,
212+
true,
213+
{}
214+
);
215+
const headers = await getHeaders(fakeUrl);
216+
expect(headers.get('X-Firebase-Appversion')).to.be.null;
172217
});
173218
it('adds app check token if it exists', async () => {
174219
const headers = await getHeaders(fakeUrl);

packages/vertexai/src/requests/request.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
PACKAGE_VERSION
2727
} from '../constants';
2828
import { logger } from '../logger';
29+
import { SDK_VERSION } from '@firebase/app';
2930

3031
export enum Task {
3132
GENERATE_CONTENT = 'generateContent',
@@ -84,8 +85,9 @@ export async function getHeaders(url: RequestUrl): Promise<Headers> {
8485
headers.append('Content-Type', 'application/json');
8586
headers.append('x-goog-api-client', getClientHeaders());
8687
headers.append('x-goog-api-key', url.apiSettings.apiKey);
87-
if (url.apiSettings.automaticDataCollectionEnabled !== false) {
88-
headers.append('X-Firebase-AppId', url.apiSettings.appId);
88+
if (url.apiSettings.automaticDataCollectionEnabled) {
89+
headers.append('X-Firebase-Appid', url.apiSettings.appId);
90+
headers.append('X-Firebase-Appversion', SDK_VERSION);
8991
}
9092
if (url.apiSettings.getAppCheckToken) {
9193
const appCheckToken = await url.apiSettings.getAppCheckToken();

0 commit comments

Comments
 (0)