UNPKG

5.37 kBJavaScriptView Raw
1/*! firebase-admin v10.0.0 */
2"use strict";
3/*!
4 * @license
5 * Copyright 2021 Google Inc.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19Object.defineProperty(exports, "__esModule", { value: true });
20exports.FirebaseInstallationsRequestHandler = void 0;
21var error_1 = require("../utils/error");
22var api_request_1 = require("../utils/api-request");
23var utils = require("../utils/index");
24var validator = require("../utils/validator");
25/** Firebase IID backend host. */
26var FIREBASE_IID_HOST = 'console.firebase.google.com';
27/** Firebase IID backend path. */
28var FIREBASE_IID_PATH = '/v1/';
29/** Firebase IID request timeout duration in milliseconds. */
30var FIREBASE_IID_TIMEOUT = 10000;
31/** HTTP error codes raised by the backend server. */
32var 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 * Class that provides mechanism to send requests to the FIS backend endpoints.
44 */
45var FirebaseInstallationsRequestHandler = /** @class */ (function () {
46 /**
47 * @param app - The app used to fetch access tokens to sign API requests.
48 *
49 * @constructor
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 * Invokes the request handler based on the API settings object passed.
65 *
66 * @param apiSettings - The API endpoint settings to apply to request and response.
67 * @returns A promise that resolves when the request is complete.
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 // return nothing on success
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 // In case of timeouts and other network errors, the HttpClient returns a
94 // FirebaseError wrapped in the response. Simply throw it here.
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 // Assert for an explicit projct ID (either via AppOptions or the cert itself).
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}());
117exports.FirebaseInstallationsRequestHandler = FirebaseInstallationsRequestHandler;