|
| 1 | +/*! |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {FirebaseApp} from '../firebase-app'; |
| 18 | +import {FirebaseError, FirebaseInstanceIdError, InstanceIdClientErrorCode} from '../utils/error'; |
| 19 | +import { |
| 20 | + HttpMethod, SignedApiRequestHandler, ApiSettings, |
| 21 | +} from '../utils/api-request'; |
| 22 | + |
| 23 | +import * as validator from '../utils/validator'; |
| 24 | + |
| 25 | +/** Firebase IID backend host. */ |
| 26 | +const FIREBASE_IID_HOST = 'console.firebase.google.com'; |
| 27 | +/** Firebase IID backend port number. */ |
| 28 | +const FIREBASE_IID_PORT = 443; |
| 29 | +/** Firebase IID backend path. */ |
| 30 | +const FIREBASE_IID_PATH = '/v1/'; |
| 31 | +/** Firebase IID request timeout duration in milliseconds. */ |
| 32 | +const FIREBASE_IID_TIMEOUT = 10000; |
| 33 | + |
| 34 | +/** HTTP error codes raised by the backend server. */ |
| 35 | +const ERROR_CODES = { |
| 36 | + 400: 'Malformed instance ID argument.', |
| 37 | + 401: 'Request not authorized.', |
| 38 | + 403: 'Project does not match instance ID or the client does not have sufficient privileges.', |
| 39 | + 404: 'Failed to find the instance ID.', |
| 40 | + 409: 'Already deleted.', |
| 41 | + 429: 'Request throttled out by the backend server.', |
| 42 | + 500: 'Internal server error.', |
| 43 | + 503: 'Backend servers are over capacity. Try again later.', |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * Class that provides mechanism to send requests to the Firebase Instance ID backend endpoints. |
| 48 | + */ |
| 49 | +export class FirebaseInstanceIdRequestHandler { |
| 50 | + |
| 51 | + private host: string = FIREBASE_IID_HOST; |
| 52 | + private port: number = FIREBASE_IID_PORT; |
| 53 | + private timeout: number = FIREBASE_IID_TIMEOUT; |
| 54 | + private signedApiRequestHandler: SignedApiRequestHandler; |
| 55 | + private path: string; |
| 56 | + |
| 57 | + /** |
| 58 | + * @param {FirebaseApp} app The app used to fetch access tokens to sign API requests. |
| 59 | + * @param {string} projectId A Firebase project ID string. |
| 60 | + * |
| 61 | + * @constructor |
| 62 | + */ |
| 63 | + constructor(app: FirebaseApp, projectId: string) { |
| 64 | + this.signedApiRequestHandler = new SignedApiRequestHandler(app); |
| 65 | + this.path = FIREBASE_IID_PATH + `project/${projectId}/instanceId/`; |
| 66 | + } |
| 67 | + |
| 68 | + public deleteInstanceId(instanceId: string): Promise<Object> { |
| 69 | + if (!validator.isNonEmptyString(instanceId)) { |
| 70 | + return Promise.reject(new FirebaseInstanceIdError( |
| 71 | + InstanceIdClientErrorCode.INVALID_INSTANCE_ID, |
| 72 | + 'Instance ID must be a non-empty string.' |
| 73 | + )); |
| 74 | + } |
| 75 | + return this.invokeRequestHandler(new ApiSettings(instanceId, 'DELETE')); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Invokes the request handler based on the API settings object passed. |
| 80 | + * |
| 81 | + * @param {ApiSettings} apiSettings The API endpoint settings to apply to request and response. |
| 82 | + * @param {Object} requestData The request data. |
| 83 | + * @return {Promise<Object>} A promise that resolves with the response. |
| 84 | + */ |
| 85 | + private invokeRequestHandler(apiSettings: ApiSettings): Promise<Object> { |
| 86 | + let path: string = this.path + apiSettings.getEndpoint(); |
| 87 | + let httpMethod: HttpMethod = apiSettings.getHttpMethod(); |
| 88 | + return Promise.resolve() |
| 89 | + .then(() => { |
| 90 | + return this.signedApiRequestHandler.sendRequest( |
| 91 | + this.host, this.port, path, httpMethod, undefined, undefined, this.timeout); |
| 92 | + }) |
| 93 | + .then((response) => { |
| 94 | + return response; |
| 95 | + }) |
| 96 | + .catch((response) => { |
| 97 | + let error; |
| 98 | + if (typeof response === 'object' && 'error' in response) { |
| 99 | + error = response.error; |
| 100 | + } else { |
| 101 | + error = response; |
| 102 | + } |
| 103 | + |
| 104 | + if (error instanceof FirebaseError) { |
| 105 | + // In case of timeouts and other network errors, the API request handler returns a |
| 106 | + // FirebaseError wrapped in the response. Simply throw it here. |
| 107 | + throw error; |
| 108 | + } |
| 109 | + |
| 110 | + let template: string = ERROR_CODES[response.statusCode]; |
| 111 | + let message: string; |
| 112 | + if (template) { |
| 113 | + message = `Instance ID "${apiSettings.getEndpoint()}": ${template}`; |
| 114 | + } else { |
| 115 | + message = JSON.stringify(error); |
| 116 | + } |
| 117 | + throw new FirebaseInstanceIdError(InstanceIdClientErrorCode.API_ERROR, message); |
| 118 | + }); |
| 119 | + } |
| 120 | +} |
0 commit comments