Skip to content

Commit

Permalink
feat: add support for custom user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon committed Jan 3, 2025
1 parent 20a12da commit dd20563
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ interface ConnectorOptions {
* Defaults to `googleapis.com`.
*/
universeDomain?: string;
userAgent?: string;
}

// The Connector class is the main public API to interact
Expand All @@ -164,6 +165,7 @@ export class Connector {
loginAuth: opts.auth,
sqlAdminAPIEndpoint: opts.sqlAdminAPIEndpoint,
universeDomain: opts.universeDomain,
userAgent: opts.userAgent,
});
this.localProxies = new Set();
this.sockets = new Set();
Expand Down
3 changes: 3 additions & 0 deletions src/sqladmin-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export interface SQLAdminFetcherOptions {
loginAuth?: GoogleAuth<AuthClient> | AuthClient;
sqlAdminAPIEndpoint?: string;
universeDomain?: string;
userAgent?: string;
}

export class SQLAdminFetcher {
Expand All @@ -92,6 +93,7 @@ export class SQLAdminFetcher {
loginAuth,
sqlAdminAPIEndpoint,
universeDomain,
userAgent,
}: SQLAdminFetcherOptions = {}) {
let auth: GoogleAuth<AuthClient>;

Expand All @@ -111,6 +113,7 @@ export class SQLAdminFetcher {
{
product: 'cloud-sql-nodejs-connector',
version: 'LIBRARY_SEMVER_VERSION',
comment: userAgent,
},
],
universeDomain: universeDomain,
Expand Down
20 changes: 20 additions & 0 deletions test/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,23 @@ t.test('Connector, custom universeDomain', async t => {

t.same(actualUniverseDomain, expectedUniverseDomain);
});

t.test('Connector, custom userAgent', async t => {
const expectedUserAgent = 'custom-agent';
let actualUserAgent: string | undefined;
// mocks sql admin fetcher to check that the custom
// userAgent is correctly passed into it
const {Connector} = t.mockRequire('../src/connector', {
'../src/sqladmin-fetcher': {
SQLAdminFetcher: class {
constructor({userAgent}: SQLAdminFetcherOptions) {
actualUserAgent = userAgent;
}
},
},
});

new Connector({userAgent: expectedUserAgent});

t.same(actualUserAgent, expectedUserAgent);
});

0 comments on commit dd20563

Please sign in to comment.