forked from firebase/firebase-admin-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance-id.ts
More file actions
96 lines (86 loc) · 3.39 KB
/
instance-id.ts
File metadata and controls
96 lines (86 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*!
* Copyright 2017 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.
*/
import {FirebaseApp} from '../firebase-app';
import {FirebaseInstanceIdError, InstanceIdClientErrorCode} from '../utils/error';
import {FirebaseServiceInterface, FirebaseServiceInternalsInterface} from '../firebase-service';
import {FirebaseInstanceIdRequestHandler} from './instance-id-request';
import * as utils from '../utils/index';
import * as validator from '../utils/validator';
/**
* Internals of an InstanceId service instance.
*/
class InstanceIdInternals implements FirebaseServiceInternalsInterface {
/**
* Deletes the service and its associated resources.
*
* @return {Promise<()>} An empty Promise that will be fulfilled when the service is deleted.
*/
public delete(): Promise<void> {
// There are no resources to clean up
return Promise.resolve(undefined);
}
}
export class InstanceId implements FirebaseServiceInterface {
public INTERNAL: InstanceIdInternals = new InstanceIdInternals();
private app_: FirebaseApp;
private requestHandler: FirebaseInstanceIdRequestHandler;
/**
* @param {FirebaseApp} app The app for this InstanceId service.
* @constructor
*/
constructor(app: FirebaseApp) {
if (!validator.isNonNullObject(app) || !('options' in app)) {
throw new FirebaseInstanceIdError(
InstanceIdClientErrorCode.INVALID_ARGUMENT,
'First argument passed to admin.instanceId() must be a valid Firebase app instance.',
);
}
const projectId: string = utils.getProjectId(app);
if (!validator.isNonEmptyString(projectId)) {
// Assert for an explicit projct ID (either via AppOptions or the cert itself).
throw new FirebaseInstanceIdError(
InstanceIdClientErrorCode.INVALID_PROJECT_ID,
'Failed to determine project ID for InstanceId. Initialize the '
+ 'SDK with service account credentials or set project ID as an app option. '
+ 'Alternatively set the GOOGLE_CLOUD_PROJECT environment variable.',
);
}
this.app_ = app;
this.requestHandler = new FirebaseInstanceIdRequestHandler(app, projectId);
}
/**
* Deletes the specified instance ID from Firebase. This can be used to delete an instance ID
* and associated user data from a Firebase project, pursuant to the General Data Protection
* Regulation (GDPR).
*
* @param {string} instanceId The instance ID to be deleted
* @return {Promise<void>} A promise that resolves when the instance ID is successfully deleted.
*/
public deleteInstanceId(instanceId: string): Promise<void> {
return this.requestHandler.deleteInstanceId(instanceId)
.then((result) => {
// Return nothing on success
});
}
/**
* Returns the app associated with this InstanceId instance.
*
* @return {FirebaseApp} The app associated with this InstanceId instance.
*/
get app(): FirebaseApp {
return this.app_;
}
}