Skip to content

Commit ca43e2f

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: added userAgent option to RequestOptions to allow setting User-Agent header
PiperOrigin-RevId: 621573081
1 parent 636f020 commit ca43e2f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/functions/post_request.ts

+16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
CountTokensRequest,
2323
RequestOptions,
2424
} from '../types/content';
25+
import {ClientError} from '../types/errors';
2526
import * as constants from '../util/constants';
2627

2728
/**
@@ -58,13 +59,28 @@ export async function postRequest({
5859
vertexEndpoint += '?alt=sse';
5960
}
6061

62+
if (
63+
requestOptions?.apiClient &&
64+
(requestOptions?.apiClient.includes('\n') ||
65+
requestOptions?.apiClient.includes('\r'))
66+
) {
67+
throw new ClientError(
68+
'Found line break in apiClient request option field, please remove ' +
69+
'the line break and try again.'
70+
);
71+
}
72+
73+
const extraHeaders: HeadersInit = requestOptions?.apiClient
74+
? {'X-Goog-Api-Client': requestOptions?.apiClient}
75+
: {};
6176
return fetch(vertexEndpoint, {
6277
...getFetchOptions(requestOptions),
6378
method: 'POST',
6479
headers: {
6580
Authorization: `Bearer ${token}`,
6681
'Content-Type': 'application/json',
6782
'User-Agent': constants.USER_AGENT,
83+
...extraHeaders,
6884
},
6985
body: JSON.stringify(data),
7086
});

src/types/content.ts

+6
Original file line numberDiff line numberDiff line change
@@ -890,4 +890,10 @@ export declare interface StartChatSessionRequest extends StartChatParams {
890890
export interface RequestOptions {
891891
/** timeout in milli seconds. time out value needs to be non negative. */
892892
timeout?: number;
893+
/**
894+
* Value for x-goog-api-client header to set on the API request. This is
895+
* intended for wrapper SDKs to set additional SDK identifiers for the
896+
* backend.
897+
*/
898+
apiClient?: string;
893899
}

0 commit comments

Comments
 (0)