Skip to content

Commit 69c268f

Browse files
authored
chore: Make instance-id use new modularization pattern (#977)
1 parent aef6bc4 commit 69c268f

File tree

9 files changed

+73
-106
lines changed

9 files changed

+73
-106
lines changed

src/firebase-app.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import { DatabaseService } from './database/database';
3030
import { Firestore } from '@google-cloud/firestore';
3131
import { FirestoreService } from './firestore/firestore';
3232
import { InstanceId } from './instance-id/instance-id';
33-
import { InstanceIdImpl } from './instance-id/instance-id-internal';
3433

3534
import { ProjectManagement } from './project-management/project-management';
3635
import { SecurityRules } from './security-rules/security-rules';
@@ -353,7 +352,7 @@ export class FirebaseApp {
353352
*/
354353
public instanceId(): InstanceId {
355354
return this.ensureService_('iid', () => {
356-
const iidService: typeof InstanceIdImpl = require('./instance-id/instance-id-internal').InstanceIdImpl;
355+
const iidService: typeof InstanceId = require('./instance-id/instance-id').InstanceId;
357356
return new iidService(this);
358357
});
359358
}

src/firebase-namespace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ export class FirebaseNamespace {
425425
const fn: FirebaseServiceNamespace<InstanceId> = (app?: FirebaseApp) => {
426426
return this.ensureApp(app).instanceId();
427427
};
428-
const instanceId = require('./instance-id/instance-id-internal').InstanceIdImpl;
428+
const instanceId = require('./instance-id/instance-id').InstanceId;
429429
return Object.assign(fn, { InstanceId: instanceId });
430430
}
431431

src/instance-id/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ export namespace admin.instanceId {
3737
// See https://github.com/microsoft/TypeScript/issues/4336
3838
/* eslint-disable @typescript-eslint/no-unused-vars */
3939
// See https://github.com/typescript-eslint/typescript-eslint/issues/363
40-
export import InstanceId = instanceIdApi.InstanceId;
40+
// Allows for exposing classes as interfaces in typings
41+
/* eslint-disable @typescript-eslint/no-empty-interface */
42+
export interface InstanceId extends instanceIdApi.InstanceId {}
4143
}

src/instance-id/instance-id-internal.ts

Lines changed: 0 additions & 85 deletions
This file was deleted.

src/instance-id/instance-id.ts

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@
1515
*/
1616

1717
import { FirebaseApp } from '../firebase-app';
18+
import { FirebaseServiceInterface, FirebaseServiceInternalsInterface } from '../firebase-service';
19+
import { FirebaseInstanceIdError, InstanceIdClientErrorCode } from '../utils/error';
20+
import { FirebaseInstanceIdRequestHandler } from './instance-id-request-internal';
21+
22+
import * as validator from '../utils/validator';
23+
24+
/**
25+
* Internals of an InstanceId service instance.
26+
*/
27+
class InstanceIdInternals implements FirebaseServiceInternalsInterface {
28+
/**
29+
* Deletes the service and its associated resources.
30+
*
31+
* @return {Promise<()>} An empty Promise that will be fulfilled when the service is deleted.
32+
*/
33+
public delete(): Promise<void> {
34+
// There are no resources to clean up
35+
return Promise.resolve(undefined);
36+
}
37+
}
1838

1939
/**
2040
* Gets the {@link InstanceId `InstanceId`} service for the
@@ -30,8 +50,27 @@ import { FirebaseApp } from '../firebase-app';
3050
* @return The `InstanceId` service for the
3151
* current app.
3252
*/
33-
export interface InstanceId {
34-
app: FirebaseApp;
53+
export class InstanceId implements FirebaseServiceInterface {
54+
public INTERNAL: InstanceIdInternals = new InstanceIdInternals();
55+
56+
private app_: FirebaseApp;
57+
private requestHandler: FirebaseInstanceIdRequestHandler;
58+
59+
/**
60+
* @param {FirebaseApp} app The app for this InstanceId service.
61+
* @constructor
62+
*/
63+
constructor(app: FirebaseApp) {
64+
if (!validator.isNonNullObject(app) || !('options' in app)) {
65+
throw new FirebaseInstanceIdError(
66+
InstanceIdClientErrorCode.INVALID_ARGUMENT,
67+
'First argument passed to admin.instanceId() must be a valid Firebase app instance.',
68+
);
69+
}
70+
71+
this.app_ = app;
72+
this.requestHandler = new FirebaseInstanceIdRequestHandler(app);
73+
}
3574

3675
/**
3776
* Deletes the specified instance ID and the associated data from Firebase.
@@ -46,5 +85,19 @@ export interface InstanceId {
4685
*
4786
* @return A promise fulfilled when the instance ID is deleted.
4887
*/
49-
deleteInstanceId(instanceId: string): Promise<void>;
88+
public deleteInstanceId(instanceId: string): Promise<void> {
89+
return this.requestHandler.deleteInstanceId(instanceId)
90+
.then(() => {
91+
// Return nothing on success
92+
});
93+
}
94+
95+
/**
96+
* Returns the app associated with this InstanceId instance.
97+
*
98+
* @return {FirebaseApp} The app associated with this InstanceId instance.
99+
*/
100+
get app(): FirebaseApp {
101+
return this.app_;
102+
}
50103
}

test/unit/firebase-namespace.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ import {
4949
setLogFunction,
5050
} from '@google-cloud/firestore';
5151
import { InstanceId } from '../../src/instance-id/instance-id';
52-
import { InstanceIdImpl } from '../../src/instance-id/instance-id-internal';
5352
import { ProjectManagement } from '../../src/project-management/project-management';
5453
import { SecurityRules } from '../../src/security-rules/security-rules';
5554
import { RemoteConfig } from '../../src/remote-config/remote-config';
@@ -624,7 +623,7 @@ describe('FirebaseNamespace', () => {
624623
});
625624

626625
it('should return a reference to InstanceId type', () => {
627-
expect(firebaseNamespace.instanceId.InstanceId).to.be.deep.equal(InstanceIdImpl);
626+
expect(firebaseNamespace.instanceId.InstanceId).to.be.deep.equal(InstanceId);
628627
});
629628
});
630629

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import * as mocks from '../../resources/mocks';
2727

2828
import { FirebaseApp } from '../../../src/firebase-app';
2929
import { HttpClient } from '../../../src/utils/api-request';
30-
import { FirebaseInstanceIdRequestHandler } from '../../../src/instance-id/instance-id-request';
30+
import { FirebaseInstanceIdRequestHandler } from '../../../src/instance-id/instance-id-request-internal';
3131

3232
chai.should();
3333
chai.use(sinonChai);

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import * as utils from '../utils';
2626
import * as mocks from '../../resources/mocks';
2727

2828
import { InstanceId } from '../../../src/instance-id/instance-id';
29-
import { InstanceIdImpl } from '../../../src/instance-id/instance-id-internal';
30-
import { FirebaseInstanceIdRequestHandler } from '../../../src/instance-id/instance-id-request';
29+
import { FirebaseInstanceIdRequestHandler } from '../../../src/instance-id/instance-id-request-internal';
3130
import { FirebaseApp } from '../../../src/firebase-app';
3231
import { FirebaseInstanceIdError, InstanceIdClientErrorCode } from '../../../src/utils/error';
3332

@@ -58,14 +57,14 @@ describe('InstanceId', () => {
5857
mockApp = mocks.app();
5958
getTokenStub = utils.stubGetAccessToken(undefined, mockApp);
6059
mockCredentialApp = mocks.mockCredentialApp();
61-
iid = new InstanceIdImpl(mockApp);
60+
iid = new InstanceId(mockApp);
6261

6362
googleCloudProject = process.env.GOOGLE_CLOUD_PROJECT;
6463
gcloudProject = process.env.GCLOUD_PROJECT;
6564

66-
nullAccessTokenClient = new InstanceIdImpl(mocks.appReturningNullAccessToken());
67-
malformedAccessTokenClient = new InstanceIdImpl(mocks.appReturningMalformedAccessToken());
68-
rejectedPromiseAccessTokenClient = new InstanceIdImpl(mocks.appRejectedWhileFetchingAccessToken());
65+
nullAccessTokenClient = new InstanceId(mocks.appReturningNullAccessToken());
66+
malformedAccessTokenClient = new InstanceId(mocks.appReturningMalformedAccessToken());
67+
rejectedPromiseAccessTokenClient = new InstanceId(mocks.appRejectedWhileFetchingAccessToken());
6968
});
7069

7170
afterEach(() => {
@@ -81,15 +80,15 @@ describe('InstanceId', () => {
8180
invalidApps.forEach((invalidApp) => {
8281
it('should throw given invalid app: ' + JSON.stringify(invalidApp), () => {
8382
expect(() => {
84-
const iidAny: any = InstanceIdImpl;
83+
const iidAny: any = InstanceId;
8584
return new iidAny(invalidApp);
8685
}).to.throw('First argument passed to admin.instanceId() must be a valid Firebase app instance.');
8786
});
8887
});
8988

9089
it('should throw given no app', () => {
9190
expect(() => {
92-
const iidAny: any = InstanceIdImpl;
91+
const iidAny: any = InstanceId;
9392
return new iidAny();
9493
}).to.throw('First argument passed to admin.instanceId() must be a valid Firebase app instance.');
9594
});
@@ -98,14 +97,14 @@ describe('InstanceId', () => {
9897
// Project ID not set in the environment.
9998
delete process.env.GOOGLE_CLOUD_PROJECT;
10099
delete process.env.GCLOUD_PROJECT;
101-
const instanceId = new InstanceIdImpl(mockCredentialApp);
100+
const instanceId = new InstanceId(mockCredentialApp);
102101
return instanceId.deleteInstanceId('iid')
103102
.should.eventually.rejectedWith(noProjectIdError);
104103
});
105104

106105
it('should not throw given a valid app', () => {
107106
expect(() => {
108-
return new InstanceIdImpl(mockApp);
107+
return new InstanceId(mockApp);
109108
}).not.to.throw();
110109
});
111110
});
@@ -119,7 +118,7 @@ describe('InstanceId', () => {
119118
it('is read-only', () => {
120119
expect(() => {
121120
(iid as any).app = mockApp;
122-
}).to.throw('Cannot set property app of #<InstanceIdImpl> which has only a getter');
121+
}).to.throw('Cannot set property app of #<InstanceId> which has only a getter');
123122
});
124123
});
125124

0 commit comments

Comments
 (0)