action-code-settings-builder.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*! firebase-admin v12.1.1 */
  2. "use strict";
  3. /*!
  4. * Copyright 2018 Google Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.ActionCodeSettingsBuilder = void 0;
  20. const validator = require("../utils/validator");
  21. const error_1 = require("../utils/error");
  22. /**
  23. * Defines the ActionCodeSettings builder class used to convert the
  24. * ActionCodeSettings object to its corresponding server request.
  25. *
  26. * @internal
  27. */
  28. class ActionCodeSettingsBuilder {
  29. /**
  30. * ActionCodeSettingsBuilder constructor.
  31. *
  32. * @param {ActionCodeSettings} actionCodeSettings The ActionCodeSettings
  33. * object used to initiliaze this server request builder.
  34. * @constructor
  35. */
  36. constructor(actionCodeSettings) {
  37. if (!validator.isNonNullObject(actionCodeSettings)) {
  38. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings" must be a non-null object.');
  39. }
  40. if (typeof actionCodeSettings.url === 'undefined') {
  41. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.MISSING_CONTINUE_URI);
  42. }
  43. else if (!validator.isURL(actionCodeSettings.url)) {
  44. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONTINUE_URI);
  45. }
  46. this.continueUrl = actionCodeSettings.url;
  47. if (typeof actionCodeSettings.handleCodeInApp !== 'undefined' &&
  48. !validator.isBoolean(actionCodeSettings.handleCodeInApp)) {
  49. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.handleCodeInApp" must be a boolean.');
  50. }
  51. this.canHandleCodeInApp = actionCodeSettings.handleCodeInApp || false;
  52. if (typeof actionCodeSettings.dynamicLinkDomain !== 'undefined' &&
  53. !validator.isNonEmptyString(actionCodeSettings.dynamicLinkDomain)) {
  54. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_DYNAMIC_LINK_DOMAIN);
  55. }
  56. this.dynamicLinkDomain = actionCodeSettings.dynamicLinkDomain;
  57. if (typeof actionCodeSettings.iOS !== 'undefined') {
  58. if (!validator.isNonNullObject(actionCodeSettings.iOS)) {
  59. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.iOS" must be a valid non-null object.');
  60. }
  61. else if (typeof actionCodeSettings.iOS.bundleId === 'undefined') {
  62. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.MISSING_IOS_BUNDLE_ID);
  63. }
  64. else if (!validator.isNonEmptyString(actionCodeSettings.iOS.bundleId)) {
  65. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.iOS.bundleId" must be a valid non-empty string.');
  66. }
  67. this.ibi = actionCodeSettings.iOS.bundleId;
  68. }
  69. if (typeof actionCodeSettings.android !== 'undefined') {
  70. if (!validator.isNonNullObject(actionCodeSettings.android)) {
  71. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.android" must be a valid non-null object.');
  72. }
  73. else if (typeof actionCodeSettings.android.packageName === 'undefined') {
  74. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.MISSING_ANDROID_PACKAGE_NAME);
  75. }
  76. else if (!validator.isNonEmptyString(actionCodeSettings.android.packageName)) {
  77. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.android.packageName" must be a valid non-empty string.');
  78. }
  79. else if (typeof actionCodeSettings.android.minimumVersion !== 'undefined' &&
  80. !validator.isNonEmptyString(actionCodeSettings.android.minimumVersion)) {
  81. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.android.minimumVersion" must be a valid non-empty string.');
  82. }
  83. else if (typeof actionCodeSettings.android.installApp !== 'undefined' &&
  84. !validator.isBoolean(actionCodeSettings.android.installApp)) {
  85. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.android.installApp" must be a valid boolean.');
  86. }
  87. this.apn = actionCodeSettings.android.packageName;
  88. this.amv = actionCodeSettings.android.minimumVersion;
  89. this.installApp = actionCodeSettings.android.installApp || false;
  90. }
  91. }
  92. /**
  93. * Returns the corresponding constructed server request corresponding to the
  94. * current ActionCodeSettings.
  95. *
  96. * @returns The constructed EmailActionCodeRequest request.
  97. */
  98. buildRequest() {
  99. const request = {
  100. continueUrl: this.continueUrl,
  101. canHandleCodeInApp: this.canHandleCodeInApp,
  102. dynamicLinkDomain: this.dynamicLinkDomain,
  103. androidPackageName: this.apn,
  104. androidMinimumVersion: this.amv,
  105. androidInstallApp: this.installApp,
  106. iOSBundleId: this.ibi,
  107. };
  108. // Remove all null and undefined fields from request.
  109. for (const key in request) {
  110. if (Object.prototype.hasOwnProperty.call(request, key)) {
  111. if (typeof request[key] === 'undefined' || request[key] === null) {
  112. delete request[key];
  113. }
  114. }
  115. }
  116. return request;
  117. }
  118. }
  119. exports.ActionCodeSettingsBuilder = ActionCodeSettingsBuilder;