1 |
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | Object.defineProperty(exports, "__esModule", { value: true });
|
20 | exports.FirebaseInstallationsRequestHandler = void 0;
|
21 | const error_1 = require("../utils/error");
|
22 | const api_request_1 = require("../utils/api-request");
|
23 | const utils = require("../utils/index");
|
24 | const validator = require("../utils/validator");
|
25 |
|
26 | const FIREBASE_IID_HOST = 'console.firebase.google.com';
|
27 |
|
28 | const FIREBASE_IID_PATH = '/v1/';
|
29 |
|
30 | const FIREBASE_IID_TIMEOUT = 10000;
|
31 |
|
32 | const ERROR_CODES = {
|
33 | 400: 'Malformed installation ID argument.',
|
34 | 401: 'Request not authorized.',
|
35 | 403: 'Project does not match installation ID or the client does not have sufficient privileges.',
|
36 | 404: 'Failed to find the installation ID.',
|
37 | 409: 'Already deleted.',
|
38 | 429: 'Request throttled out by the backend server.',
|
39 | 500: 'Internal server error.',
|
40 | 503: 'Backend servers are over capacity. Try again later.',
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 | class FirebaseInstallationsRequestHandler {
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 | constructor(app) {
|
52 | this.app = app;
|
53 | this.host = FIREBASE_IID_HOST;
|
54 | this.timeout = FIREBASE_IID_TIMEOUT;
|
55 | this.httpClient = new api_request_1.AuthorizedHttpClient(app);
|
56 | }
|
57 | deleteInstallation(fid) {
|
58 | if (!validator.isNonEmptyString(fid)) {
|
59 | return Promise.reject(new error_1.FirebaseInstallationsError(error_1.InstallationsClientErrorCode.INVALID_INSTALLATION_ID, 'Installation ID must be a non-empty string.'));
|
60 | }
|
61 | return this.invokeRequestHandler(new api_request_1.ApiSettings(fid, 'DELETE'));
|
62 | }
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | invokeRequestHandler(apiSettings) {
|
70 | return this.getPathPrefix()
|
71 | .then((path) => {
|
72 | const req = {
|
73 | url: `https://${this.host}${path}${apiSettings.getEndpoint()}`,
|
74 | method: apiSettings.getHttpMethod(),
|
75 | timeout: this.timeout,
|
76 | };
|
77 | return this.httpClient.send(req);
|
78 | })
|
79 | .then(() => {
|
80 |
|
81 | })
|
82 | .catch((err) => {
|
83 | if (err instanceof api_request_1.HttpError) {
|
84 | const response = err.response;
|
85 | const errorMessage = (response.isJson() && 'error' in response.data) ?
|
86 | response.data.error : response.text;
|
87 | const template = ERROR_CODES[response.status];
|
88 | const message = template ?
|
89 | `Installation ID "${apiSettings.getEndpoint()}": ${template}` : errorMessage;
|
90 | throw new error_1.FirebaseInstallationsError(error_1.InstallationsClientErrorCode.API_ERROR, message);
|
91 | }
|
92 |
|
93 |
|
94 | throw err;
|
95 | });
|
96 | }
|
97 | getPathPrefix() {
|
98 | if (this.path) {
|
99 | return Promise.resolve(this.path);
|
100 | }
|
101 | return utils.findProjectId(this.app)
|
102 | .then((projectId) => {
|
103 | if (!validator.isNonEmptyString(projectId)) {
|
104 |
|
105 | throw new error_1.FirebaseInstallationsError(error_1.InstallationsClientErrorCode.INVALID_PROJECT_ID, 'Failed to determine project ID for Installations. Initialize the '
|
106 | + 'SDK with service account credentials or set project ID as an app option. '
|
107 | + 'Alternatively set the GOOGLE_CLOUD_PROJECT environment variable.');
|
108 | }
|
109 | this.path = FIREBASE_IID_PATH + `project/${projectId}/instanceId/`;
|
110 | return this.path;
|
111 | });
|
112 | }
|
113 | }
|
114 | exports.FirebaseInstallationsRequestHandler = FirebaseInstallationsRequestHandler;
|