Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Renaming service functions to getAuth and getInstanceId #1142

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions etc/firebase-admin.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class Auth extends BaseAuth {
}

// @public
export function auth(app?: App): Auth;
export function auth(app?: App): auth.Auth;

// @public (undocumented)
export namespace auth {
Expand Down Expand Up @@ -400,6 +400,12 @@ export function getApp(name?: string): App;
// @public (undocumented)
export function getApps(): App[];

// @public (undocumented)
export function getAuth(app?: App): Auth;

// @public (undocumented)
export function getInstanceId(app?: App): InstanceId;

// @public
export interface GetUsersResult {
notFound: UserIdentifier[];
Expand Down Expand Up @@ -427,7 +433,7 @@ export class InstanceId {
}

// @public
export function instanceId(app?: App): InstanceId;
export function instanceId(app?: App): instanceId.InstanceId;

// @public (undocumented)
export namespace instanceId {
Expand Down
4 changes: 2 additions & 2 deletions src/app/firebase-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export class FirebaseApp implements app.App {
* @return The Auth service instance of this app.
*/
public auth(): Auth {
const fn = require('../auth/index').auth;
const fn = require('../auth/index').getAuth;
return fn(this);
}

Expand Down Expand Up @@ -332,7 +332,7 @@ export class FirebaseApp implements app.App {
* @return The InstanceId service instance of this app.
*/
public instanceId(): InstanceId {
const fn = require('../instance-id/index').instanceId;
const fn = require('../instance-id/index').getInstanceId;
return fn(this);
}

Expand Down
18 changes: 10 additions & 8 deletions src/auth/auth-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ import {
UserRecord as TUserRecord,
} from './user-record';

export function getAuth(app?: App): Auth {
if (typeof app === 'undefined') {
app = getApp();
}

const firebaseApp: FirebaseApp = app as FirebaseApp;
return firebaseApp.getOrInitService('auth', (app) => new Auth(app));
}

/**
* Gets the {@link auth.Auth `Auth`} service for the default app or a
* given app.
Expand All @@ -116,14 +125,7 @@ import {
* ```
*
*/
export function auth(app?: App): Auth {
if (typeof app === 'undefined') {
app = getApp();
}

const firebaseApp: FirebaseApp = app as FirebaseApp;
return firebaseApp.getOrInitService('auth', (app) => new Auth(app));
}
export declare function auth(app?: App): auth.Auth;

/* eslint-disable @typescript-eslint/no-namespace */
export namespace auth {
Expand Down
2 changes: 1 addition & 1 deletion src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ export {
UserRecord,
} from './user-record';

export { auth } from './auth-namespace';
export { getAuth, auth } from './auth-namespace';
18 changes: 10 additions & 8 deletions src/instance-id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ import { InstanceId } from './instance-id';

export { InstanceId };

export function getInstanceId(app?: App): InstanceId {
if (typeof app === 'undefined') {
app = getApp();
}

const firebaseApp: FirebaseApp = app as FirebaseApp;
return firebaseApp.getOrInitService('instanceId', (app) => new InstanceId(app));
}

/**
* Gets the {@link instanceId.InstanceId `InstanceId`} service for the
* default app or a given app.
Expand Down Expand Up @@ -50,14 +59,7 @@ export { InstanceId };
* no app is provided or the `InstanceId` service associated with the
* provided app.
*/
export function instanceId(app?: App): InstanceId {
if (typeof app === 'undefined') {
app = getApp();
}

const firebaseApp: FirebaseApp = app as FirebaseApp;
return firebaseApp.getOrInitService('instanceId', (app) => new InstanceId(app));
}
export declare function instanceId(app?: App): instanceId.InstanceId;

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

Expand Down
75 changes: 75 additions & 0 deletions test/unit/auth/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*!
* @license
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

import * as chai from 'chai';
import * as sinonChai from 'sinon-chai';
import * as chaiAsPromised from 'chai-as-promised';

import * as mocks from '../../resources/mocks';
import { App } from '../../../src/app/index';
import { getAuth, Auth } from '../../../src/auth/index';

chai.should();
chai.use(sinonChai);
chai.use(chaiAsPromised);

const expect = chai.expect;

describe('InstanceId', () => {
let mockApp: App;
let mockCredentialApp: App;

const noProjectIdError = 'Failed to determine project ID for Auth. Initialize the SDK '
+ 'with service account credentials or set project ID as an app option. Alternatively set the '
+ 'GOOGLE_CLOUD_PROJECT environment variable.';

beforeEach(() => {
mockApp = mocks.app();
mockCredentialApp = mocks.mockCredentialApp();
});

describe('getAuth()', () => {
it('should throw when default app is not available', () => {
expect(() => {
return getAuth();
}).to.throw('The default Firebase app does not exist.');
});

it('should reject given an invalid credential without project ID', () => {
// Project ID not set in the environment.
delete process.env.GOOGLE_CLOUD_PROJECT;
delete process.env.GCLOUD_PROJECT;
const auth = getAuth(mockCredentialApp);
return auth.getUser('uid')
.should.eventually.rejectedWith(noProjectIdError);
});

it('should not throw given a valid app', () => {
expect(() => {
return getAuth(mockApp);
}).not.to.throw();
});

it('should return the same instance for a given app instance', () => {
const auth1: Auth = getAuth(mockApp);
const auth2: Auth = getAuth(mockApp);
expect(auth1).to.equal(auth2);
});
});
});
1 change: 1 addition & 0 deletions test/unit/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import './utils/api-request.spec';

// Auth
import './auth/auth.spec';
import './auth/index.spec';
import './auth/user-record.spec';
import './auth/token-generator.spec';
import './auth/token-verifier.spec';
Expand Down
14 changes: 7 additions & 7 deletions test/unit/instance-id/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import * as chaiAsPromised from 'chai-as-promised';

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

chai.should();
chai.use(sinonChai);
Expand All @@ -44,31 +44,31 @@ describe('InstanceId', () => {
mockCredentialApp = mocks.mockCredentialApp();
});

describe('instanceId()', () => {
describe('getInstanceId()', () => {
it('should throw when default app is not available', () => {
expect(() => {
return instanceId();
return getInstanceId();
}).to.throw('The default Firebase app does not exist.');
});

it('should reject given an invalid credential without project ID', () => {
// Project ID not set in the environment.
delete process.env.GOOGLE_CLOUD_PROJECT;
delete process.env.GCLOUD_PROJECT;
const iid = instanceId(mockCredentialApp);
const iid = getInstanceId(mockCredentialApp);
return iid.deleteInstanceId('iid')
.should.eventually.rejectedWith(noProjectIdError);
});

it('should not throw given a valid app', () => {
expect(() => {
return instanceId(mockApp);
return getInstanceId(mockApp);
}).not.to.throw();
});

it('should return the same instance for a given app instance', () => {
const iid1: InstanceId = instanceId(mockApp);
const iid2: InstanceId = instanceId(mockApp);
const iid1: InstanceId = getInstanceId(mockApp);
const iid2: InstanceId = getInstanceId(mockApp);
expect(iid1).to.equal(iid2);
});
});
Expand Down