Skip to content

Commit 25f5ccd

Browse files
feat: add support for custom user agent (#410)
Add support for custom user agent via user agent comments. HTTP user agent's take the form of `<product> / <product-version> <comment>`
1 parent 5bbd6be commit 25f5ccd

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/connector.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ interface ConnectorOptions {
148148
* Defaults to `googleapis.com`.
149149
*/
150150
universeDomain?: string;
151+
userAgent?: string;
151152
}
152153

153154
// The Connector class is the main public API to interact
@@ -164,6 +165,7 @@ export class Connector {
164165
loginAuth: opts.auth,
165166
sqlAdminAPIEndpoint: opts.sqlAdminAPIEndpoint,
166167
universeDomain: opts.universeDomain,
168+
userAgent: opts.userAgent,
167169
});
168170
this.localProxies = new Set();
169171
this.sockets = new Set();

src/sqladmin-fetcher.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export interface SQLAdminFetcherOptions {
8282
loginAuth?: GoogleAuth<AuthClient> | AuthClient;
8383
sqlAdminAPIEndpoint?: string;
8484
universeDomain?: string;
85+
userAgent?: string;
8586
}
8687

8788
export class SQLAdminFetcher {
@@ -92,6 +93,7 @@ export class SQLAdminFetcher {
9293
loginAuth,
9394
sqlAdminAPIEndpoint,
9495
universeDomain,
96+
userAgent,
9597
}: SQLAdminFetcherOptions = {}) {
9698
let auth: GoogleAuth<AuthClient>;
9799

@@ -111,6 +113,7 @@ export class SQLAdminFetcher {
111113
{
112114
product: 'cloud-sql-nodejs-connector',
113115
version: 'LIBRARY_SEMVER_VERSION',
116+
comment: userAgent,
114117
},
115118
],
116119
universeDomain: universeDomain,

test/connector.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,3 +545,23 @@ t.test('Connector, custom universeDomain', async t => {
545545

546546
t.same(actualUniverseDomain, expectedUniverseDomain);
547547
});
548+
549+
t.test('Connector, custom userAgent', async t => {
550+
const expectedUserAgent = 'custom-agent';
551+
let actualUserAgent: string | undefined;
552+
// mocks sql admin fetcher to check that the custom
553+
// userAgent is correctly passed into it
554+
const {Connector} = t.mockRequire('../src/connector', {
555+
'../src/sqladmin-fetcher': {
556+
SQLAdminFetcher: class {
557+
constructor({userAgent}: SQLAdminFetcherOptions) {
558+
actualUserAgent = userAgent;
559+
}
560+
},
561+
},
562+
});
563+
564+
new Connector({userAgent: expectedUserAgent});
565+
566+
t.same(actualUserAgent, expectedUserAgent);
567+
});

0 commit comments

Comments
 (0)