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 | var error_1 = require("../utils/error");
|
22 | var api_request_1 = require("../utils/api-request");
|
23 | var utils = require("../utils/index");
|
24 | var validator = require("../utils/validator");
|
25 |
|
26 | var FIREBASE_IID_HOST = 'console.firebase.google.com';
|
27 |
|
28 | var FIREBASE_IID_PATH = '/v1/';
|
29 |
|
30 | var FIREBASE_IID_TIMEOUT = 10000;
|
31 |
|
32 | var 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 | var FirebaseInstallationsRequestHandler = (function () {
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 | function FirebaseInstallationsRequestHandler(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 | FirebaseInstallationsRequestHandler.prototype.deleteInstallation = function (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 | FirebaseInstallationsRequestHandler.prototype.invokeRequestHandler = function (apiSettings) {
|
70 | var _this = this;
|
71 | return this.getPathPrefix()
|
72 | .then(function (path) {
|
73 | var req = {
|
74 | url: "https://" + _this.host + path + apiSettings.getEndpoint(),
|
75 | method: apiSettings.getHttpMethod(),
|
76 | timeout: _this.timeout,
|
77 | };
|
78 | return _this.httpClient.send(req);
|
79 | })
|
80 | .then(function () {
|
81 |
|
82 | })
|
83 | .catch(function (err) {
|
84 | if (err instanceof api_request_1.HttpError) {
|
85 | var response = err.response;
|
86 | var errorMessage = (response.isJson() && 'error' in response.data) ?
|
87 | response.data.error : response.text;
|
88 | var template = ERROR_CODES[response.status];
|
89 | var message = template ?
|
90 | "Installation ID \"" + apiSettings.getEndpoint() + "\": " + template : errorMessage;
|
91 | throw new error_1.FirebaseInstallationsError(error_1.InstallationsClientErrorCode.API_ERROR, message);
|
92 | }
|
93 |
|
94 |
|
95 | throw err;
|
96 | });
|
97 | };
|
98 | FirebaseInstallationsRequestHandler.prototype.getPathPrefix = function () {
|
99 | var _this = this;
|
100 | if (this.path) {
|
101 | return Promise.resolve(this.path);
|
102 | }
|
103 | return utils.findProjectId(this.app)
|
104 | .then(function (projectId) {
|
105 | if (!validator.isNonEmptyString(projectId)) {
|
106 |
|
107 | throw new error_1.FirebaseInstallationsError(error_1.InstallationsClientErrorCode.INVALID_PROJECT_ID, 'Failed to determine project ID for Installations. Initialize the '
|
108 | + 'SDK with service account credentials or set project ID as an app option. '
|
109 | + 'Alternatively set the GOOGLE_CLOUD_PROJECT environment variable.');
|
110 | }
|
111 | _this.path = FIREBASE_IID_PATH + ("project/" + projectId + "/instanceId/");
|
112 | return _this.path;
|
113 | });
|
114 | };
|
115 | return FirebaseInstallationsRequestHandler;
|
116 | }());
|
117 | exports.FirebaseInstallationsRequestHandler = FirebaseInstallationsRequestHandler;
|