Skip to content

chore(iid): Exposed admin.instanceId namespace #1046

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

Merged
merged 2 commits into from
Sep 29, 2020
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
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ gulp.task('compile', function() {
'lib/**/*.js',
'lib/credential/index.d.ts',
'lib/firebase-namespace-api.d.ts',
'lib/instance-id/index.d.ts',
'lib/messaging/index.d.ts',
'lib/remote-config/index.d.ts',
];
Expand Down
2 changes: 2 additions & 0 deletions src/firebase-namespace-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { Agent } from 'http';
import { credential } from './credential/index';
import { instanceId } from './instance-id/index';
import { messaging } from './messaging/index';
import { remoteConfig } from './remote-config/index';

Expand Down Expand Up @@ -214,6 +215,7 @@ export namespace app {
*/
options: AppOptions;

instanceId(): instanceId.InstanceId;
messaging(): messaging.Messaging;
remoteConfig(): remoteConfig.RemoteConfig;

Expand Down
1 change: 1 addition & 0 deletions src/firebase-namespace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@

export * from './credential/index';
export * from './firebase-namespace-api';
export * from './instance-id/index';
export * from './messaging/index';
export * from './remote-config/index';
80 changes: 64 additions & 16 deletions src/instance-id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,72 @@
* limitations under the License.
*/

import { FirebaseApp } from '../firebase-app';
import * as instanceIdApi from './instance-id';

export declare function instanceId(app?: FirebaseApp): instanceIdApi.InstanceId;
import { app } from '../firebase-namespace-api';

/**
* We must define a namespace to make the typings work correctly. Otherwise
* `admin.instanceId()` cannot be called like a function. Temporarily,
* admin.instanceId is used as the namespace name because we cannot barrel
* re-export the contents from instance-id, and we want it to
* match the namespacing in the re-export inside src/index.d.ts
* Gets the {@link admin.instanceId.InstanceId `InstanceId`} service for the
* default app or a given app.
*
* `admin.instanceId()` can be called with no arguments to access the default
* app's {@link admin.instanceId.InstanceId `InstanceId`} service or as
* `admin.instanceId(app)` to access the
* {@link admin.instanceId.InstanceId `InstanceId`} service associated with a
* specific app.
*
* @example
* ```javascript
* // Get the Instance ID service for the default app
* var defaultInstanceId = admin.instanceId();
* ```
*
* @example
* ```javascript
* // Get the Instance ID service for a given app
* var otherInstanceId = admin.instanceId(otherApp);
*```
*
* @param app Optional app whose `InstanceId` service to
* return. If not provided, the default `InstanceId` service will be
* returned.
*
* @return The default `InstanceId` service if
* no app is provided or the `InstanceId` service associated with the
* provided app.
*/
export declare function instanceId(app?: app.App): instanceId.InstanceId;

/* eslint-disable @typescript-eslint/no-namespace */
export namespace admin.instanceId {
// See https://github.com/microsoft/TypeScript/issues/4336
/* eslint-disable @typescript-eslint/no-unused-vars */
// See https://github.com/typescript-eslint/typescript-eslint/issues/363
// Allows for exposing classes as interfaces in typings
/* eslint-disable @typescript-eslint/no-empty-interface */
export interface InstanceId extends instanceIdApi.InstanceId {}
export namespace instanceId {
/**
* Gets the {@link InstanceId `InstanceId`} service for the
* current app.
*
* @example
* ```javascript
* var instanceId = app.instanceId();
* // The above is shorthand for:
* // var instanceId = admin.instanceId(app);
* ```
*
* @return The `InstanceId` service for the
* current app.
*/
export interface InstanceId {
app: app.App;

/**
* Deletes the specified instance ID and the associated data from Firebase.
*
* Note that Google Analytics for Firebase uses its own form of Instance ID to
* keep track of analytics data. Therefore deleting a Firebase Instance ID does
* not delete Analytics data. See
* [Delete an Instance ID](/support/privacy/manage-iids#delete_an_instance_id)
* for more information.
*
* @param instanceId The instance ID to be deleted.
*
* @return A promise fulfilled when the instance ID is deleted.
*/
deleteInstanceId(instanceId: string): Promise<void>;
}
}
6 changes: 4 additions & 2 deletions src/instance-id/instance-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import { FirebaseApp } from '../firebase-app';
import { FirebaseServiceInterface, FirebaseServiceInternalsInterface } from '../firebase-service';
import { FirebaseInstanceIdError, InstanceIdClientErrorCode } from '../utils/error';
import { FirebaseInstanceIdRequestHandler } from './instance-id-request-internal';

import { instanceId } from './index';
import * as validator from '../utils/validator';

import InstanceIdInterface = instanceId.InstanceId;

/**
* Internals of an InstanceId service instance.
*/
Expand Down Expand Up @@ -50,7 +52,7 @@ class InstanceIdInternals implements FirebaseServiceInternalsInterface {
* @return The `InstanceId` service for the
* current app.
*/
export class InstanceId implements FirebaseServiceInterface {
export class InstanceId implements FirebaseServiceInterface, InstanceIdInterface {
public INTERNAL: InstanceIdInternals = new InstanceIdInternals();

private app_: FirebaseApp;
Expand Down