Skip to content

Commit eec82f5

Browse files
authored
feat: Improve Universe Domain Ergonomics (#1732)
1 parent 058a503 commit eec82f5

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/auth/googleauth.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ export interface GoogleAuthOptions<T extends AuthClient = JSONClient> {
122122
* Your project ID.
123123
*/
124124
projectId?: string;
125+
126+
/**
127+
* The default service domain for a given Cloud universe.
128+
*
129+
* This is an ergonomic equivalent to {@link clientOptions}'s `universeDomain`
130+
* property and will be set for all generated {@link AuthClient}s.
131+
*/
132+
universeDomain?: string;
125133
}
126134

127135
export const CLOUD_SDK_CLIENT_ID =
@@ -175,7 +183,7 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
175183
defaultScopes?: string | string[];
176184
private keyFilename?: string;
177185
private scopes?: string | string[];
178-
private clientOptions?: AuthClientOptions;
186+
private clientOptions: AuthClientOptions = {};
179187

180188
/**
181189
* The cached universe domain.
@@ -208,7 +216,12 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
208216
this.keyFilename = opts.keyFilename || opts.keyFile;
209217
this.scopes = opts.scopes;
210218
this.jsonContent = opts.credentials || null;
211-
this.clientOptions = opts.clientOptions;
219+
this.clientOptions = opts.clientOptions || {};
220+
221+
if (opts.universeDomain) {
222+
this.clientOptions.universeDomain = opts.universeDomain;
223+
this.#universeDomain = opts.universeDomain;
224+
}
212225
}
213226

214227
// GAPIC client libraries should always use self-signed JWTs. The following

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {GoogleAuth} from './auth/googleauth';
1515

1616
export * as gcpMetadata from 'gcp-metadata';
1717

18-
export {AuthClient} from './auth/authclient';
18+
export {AuthClient, DEFAULT_UNIVERSE} from './auth/authclient';
1919
export {Compute, ComputeOptions} from './auth/computeclient';
2020
export {
2121
CredentialBody,

test/test.googleauth.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,13 @@ describe('googleauth', () => {
15511551
});
15521552

15531553
describe('getUniverseDomain', () => {
1554+
it('should prefer `universeDomain` > metadata service when available', async () => {
1555+
const universeDomain = 'my.universe.com';
1556+
const auth = new GoogleAuth({universeDomain});
1557+
1558+
assert.equal(await auth.getUniverseDomain(), universeDomain);
1559+
});
1560+
15541561
it('should prefer `clientOptions` > metadata service when available', async () => {
15551562
const universeDomain = 'my.universe.com';
15561563
const auth = new GoogleAuth({clientOptions: {universeDomain}});

0 commit comments

Comments
 (0)