Skip to content

Commit c1bf3cd

Browse files
authored
fix: Renaming service functions to getAuth and getInstanceId (#1142)
1 parent 8d16ac0 commit c1bf3cd

File tree

8 files changed

+114
-28
lines changed

8 files changed

+114
-28
lines changed

etc/firebase-admin.api.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class Auth extends BaseAuth {
8484
}
8585

8686
// @public
87-
export function auth(app?: App): Auth;
87+
export function auth(app?: App): auth.Auth;
8888

8989
// @public (undocumented)
9090
export namespace auth {
@@ -400,6 +400,12 @@ export function getApp(name?: string): App;
400400
// @public (undocumented)
401401
export function getApps(): App[];
402402

403+
// @public (undocumented)
404+
export function getAuth(app?: App): Auth;
405+
406+
// @public (undocumented)
407+
export function getInstanceId(app?: App): InstanceId;
408+
403409
// @public
404410
export interface GetUsersResult {
405411
notFound: UserIdentifier[];
@@ -427,7 +433,7 @@ export class InstanceId {
427433
}
428434

429435
// @public
430-
export function instanceId(app?: App): InstanceId;
436+
export function instanceId(app?: App): instanceId.InstanceId;
431437

432438
// @public (undocumented)
433439
export namespace instanceId {

src/app/firebase-app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export class FirebaseApp implements app.App {
277277
* @return The Auth service instance of this app.
278278
*/
279279
public auth(): Auth {
280-
const fn = require('../auth/index').auth;
280+
const fn = require('../auth/index').getAuth;
281281
return fn(this);
282282
}
283283

@@ -332,7 +332,7 @@ export class FirebaseApp implements app.App {
332332
* @return The InstanceId service instance of this app.
333333
*/
334334
public instanceId(): InstanceId {
335-
const fn = require('../instance-id/index').instanceId;
335+
const fn = require('../instance-id/index').getInstanceId;
336336
return fn(this);
337337
}
338338

src/auth/auth-namespace.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ import {
9595
UserRecord as TUserRecord,
9696
} from './user-record';
9797

98+
export function getAuth(app?: App): Auth {
99+
if (typeof app === 'undefined') {
100+
app = getApp();
101+
}
102+
103+
const firebaseApp: FirebaseApp = app as FirebaseApp;
104+
return firebaseApp.getOrInitService('auth', (app) => new Auth(app));
105+
}
106+
98107
/**
99108
* Gets the {@link auth.Auth `Auth`} service for the default app or a
100109
* given app.
@@ -116,14 +125,7 @@ import {
116125
* ```
117126
*
118127
*/
119-
export function auth(app?: App): Auth {
120-
if (typeof app === 'undefined') {
121-
app = getApp();
122-
}
123-
124-
const firebaseApp: FirebaseApp = app as FirebaseApp;
125-
return firebaseApp.getOrInitService('auth', (app) => new Auth(app));
126-
}
128+
export declare function auth(app?: App): auth.Auth;
127129

128130
/* eslint-disable @typescript-eslint/no-namespace */
129131
export namespace auth {

src/auth/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,4 @@ export {
9191
UserRecord,
9292
} from './user-record';
9393

94-
export { auth } from './auth-namespace';
94+
export { getAuth, auth } from './auth-namespace';

src/instance-id/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ import { InstanceId } from './instance-id';
2020

2121
export { InstanceId };
2222

23+
export function getInstanceId(app?: App): InstanceId {
24+
if (typeof app === 'undefined') {
25+
app = getApp();
26+
}
27+
28+
const firebaseApp: FirebaseApp = app as FirebaseApp;
29+
return firebaseApp.getOrInitService('instanceId', (app) => new InstanceId(app));
30+
}
31+
2332
/**
2433
* Gets the {@link instanceId.InstanceId `InstanceId`} service for the
2534
* default app or a given app.
@@ -50,14 +59,7 @@ export { InstanceId };
5059
* no app is provided or the `InstanceId` service associated with the
5160
* provided app.
5261
*/
53-
export function instanceId(app?: App): InstanceId {
54-
if (typeof app === 'undefined') {
55-
app = getApp();
56-
}
57-
58-
const firebaseApp: FirebaseApp = app as FirebaseApp;
59-
return firebaseApp.getOrInitService('instanceId', (app) => new InstanceId(app));
60-
}
62+
export declare function instanceId(app?: App): instanceId.InstanceId;
6163

6264
import { InstanceId as TInstanceId } from './instance-id';
6365

test/unit/auth/index.spec.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*!
2+
* @license
3+
* Copyright 2021 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
'use strict';
19+
20+
import * as chai from 'chai';
21+
import * as sinonChai from 'sinon-chai';
22+
import * as chaiAsPromised from 'chai-as-promised';
23+
24+
import * as mocks from '../../resources/mocks';
25+
import { App } from '../../../src/app/index';
26+
import { getAuth, Auth } from '../../../src/auth/index';
27+
28+
chai.should();
29+
chai.use(sinonChai);
30+
chai.use(chaiAsPromised);
31+
32+
const expect = chai.expect;
33+
34+
describe('InstanceId', () => {
35+
let mockApp: App;
36+
let mockCredentialApp: App;
37+
38+
const noProjectIdError = 'Failed to determine project ID for Auth. Initialize the SDK '
39+
+ 'with service account credentials or set project ID as an app option. Alternatively set the '
40+
+ 'GOOGLE_CLOUD_PROJECT environment variable.';
41+
42+
beforeEach(() => {
43+
mockApp = mocks.app();
44+
mockCredentialApp = mocks.mockCredentialApp();
45+
});
46+
47+
describe('getAuth()', () => {
48+
it('should throw when default app is not available', () => {
49+
expect(() => {
50+
return getAuth();
51+
}).to.throw('The default Firebase app does not exist.');
52+
});
53+
54+
it('should reject given an invalid credential without project ID', () => {
55+
// Project ID not set in the environment.
56+
delete process.env.GOOGLE_CLOUD_PROJECT;
57+
delete process.env.GCLOUD_PROJECT;
58+
const auth = getAuth(mockCredentialApp);
59+
return auth.getUser('uid')
60+
.should.eventually.rejectedWith(noProjectIdError);
61+
});
62+
63+
it('should not throw given a valid app', () => {
64+
expect(() => {
65+
return getAuth(mockApp);
66+
}).not.to.throw();
67+
});
68+
69+
it('should return the same instance for a given app instance', () => {
70+
const auth1: Auth = getAuth(mockApp);
71+
const auth2: Auth = getAuth(mockApp);
72+
expect(auth1).to.equal(auth2);
73+
});
74+
});
75+
});

test/unit/index.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import './utils/api-request.spec';
3030

3131
// Auth
3232
import './auth/auth.spec';
33+
import './auth/index.spec';
3334
import './auth/user-record.spec';
3435
import './auth/token-generator.spec';
3536
import './auth/token-verifier.spec';

test/unit/instance-id/index.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import * as chaiAsPromised from 'chai-as-promised';
2323

2424
import * as mocks from '../../resources/mocks';
2525
import { App } from '../../../src/app/index';
26-
import { instanceId, InstanceId } from '../../../src/instance-id/index';
26+
import { getInstanceId, InstanceId } from '../../../src/instance-id/index';
2727

2828
chai.should();
2929
chai.use(sinonChai);
@@ -44,31 +44,31 @@ describe('InstanceId', () => {
4444
mockCredentialApp = mocks.mockCredentialApp();
4545
});
4646

47-
describe('instanceId()', () => {
47+
describe('getInstanceId()', () => {
4848
it('should throw when default app is not available', () => {
4949
expect(() => {
50-
return instanceId();
50+
return getInstanceId();
5151
}).to.throw('The default Firebase app does not exist.');
5252
});
5353

5454
it('should reject given an invalid credential without project ID', () => {
5555
// Project ID not set in the environment.
5656
delete process.env.GOOGLE_CLOUD_PROJECT;
5757
delete process.env.GCLOUD_PROJECT;
58-
const iid = instanceId(mockCredentialApp);
58+
const iid = getInstanceId(mockCredentialApp);
5959
return iid.deleteInstanceId('iid')
6060
.should.eventually.rejectedWith(noProjectIdError);
6161
});
6262

6363
it('should not throw given a valid app', () => {
6464
expect(() => {
65-
return instanceId(mockApp);
65+
return getInstanceId(mockApp);
6666
}).not.to.throw();
6767
});
6868

6969
it('should return the same instance for a given app instance', () => {
70-
const iid1: InstanceId = instanceId(mockApp);
71-
const iid2: InstanceId = instanceId(mockApp);
70+
const iid1: InstanceId = getInstanceId(mockApp);
71+
const iid2: InstanceId = getInstanceId(mockApp);
7272
expect(iid1).to.equal(iid2);
7373
});
7474
});

0 commit comments

Comments
 (0)