eventarc.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*! firebase-admin v12.1.1 */
  2. "use strict";
  3. /*!
  4. * @license
  5. * Copyright 2022 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. */
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. exports.Channel = exports.Eventarc = void 0;
  21. const validator = require("../utils/validator");
  22. const eventarc_utils_1 = require("./eventarc-utils");
  23. const eventarc_client_internal_1 = require("./eventarc-client-internal");
  24. /**
  25. * Eventarc service bound to the provided app.
  26. */
  27. class Eventarc {
  28. /**
  29. * @internal
  30. */
  31. constructor(app) {
  32. if (!validator.isNonNullObject(app) || !('options' in app)) {
  33. throw new eventarc_utils_1.FirebaseEventarcError('invalid-argument', 'First argument passed to Eventarc() must be a valid Firebase app instance.');
  34. }
  35. this.appInternal = app;
  36. }
  37. /**
  38. * The {@link firebase-admin.app#App} associated with the current Eventarc service
  39. * instance.
  40. *
  41. * @example
  42. * ```javascript
  43. * var app = eventarc.app;
  44. * ```
  45. */
  46. get app() {
  47. return this.appInternal;
  48. }
  49. channel(nameOrOptions, options) {
  50. let channel;
  51. let opts;
  52. if (validator.isNonEmptyString(nameOrOptions)) {
  53. channel = nameOrOptions;
  54. }
  55. else {
  56. channel = 'locations/us-central1/channels/firebase';
  57. }
  58. if (validator.isNonNullObject(nameOrOptions)) {
  59. opts = nameOrOptions;
  60. }
  61. else {
  62. opts = options;
  63. }
  64. let allowedEventTypes = undefined;
  65. if (typeof opts?.allowedEventTypes === 'string') {
  66. allowedEventTypes = opts.allowedEventTypes.split(',');
  67. }
  68. else if (validator.isArray(opts?.allowedEventTypes)) {
  69. allowedEventTypes = opts?.allowedEventTypes;
  70. }
  71. else if (typeof opts?.allowedEventTypes !== 'undefined') {
  72. throw new eventarc_utils_1.FirebaseEventarcError('invalid-argument', 'AllowedEventTypes must be either an array of strings or a comma separated string.');
  73. }
  74. return new Channel(this, channel, allowedEventTypes);
  75. }
  76. }
  77. exports.Eventarc = Eventarc;
  78. /**
  79. * Eventarc Channel.
  80. */
  81. class Channel {
  82. /**
  83. * @internal
  84. */
  85. constructor(eventarc, name, allowedEventTypes) {
  86. if (!validator.isNonNullObject(eventarc)) {
  87. throw new eventarc_utils_1.FirebaseEventarcError('invalid-argument', 'First argument passed to Channel() must be a valid Eventarc service instance.');
  88. }
  89. if (!validator.isNonEmptyString(name)) {
  90. throw new eventarc_utils_1.FirebaseEventarcError('invalid-argument', 'name is required.');
  91. }
  92. this.nameInternal = name;
  93. this.eventarcInternal = eventarc;
  94. this.allowedEventTypes = allowedEventTypes;
  95. this.client = new eventarc_client_internal_1.EventarcApiClient(eventarc.app, this);
  96. }
  97. /**
  98. * The {@link firebase-admin.eventarc#Eventarc} service instance associated with the current `Channel`.
  99. *
  100. * @example
  101. * ```javascript
  102. * var app = channel.eventarc;
  103. * ```
  104. */
  105. get eventarc() {
  106. return this.eventarcInternal;
  107. }
  108. /**
  109. * The channel name as provided during channel creation. If it was not specifed, the default channel name is returned
  110. * ('locations/us-central1/channels/firebase').
  111. */
  112. get name() {
  113. return this.nameInternal;
  114. }
  115. /**
  116. * Publishes provided events to this channel. If channel was created with `allowedEventTypes` and event type is not
  117. * on that list, the event is ignored.
  118. *
  119. * @param events - CloudEvent to publish to the channel.
  120. */
  121. publish(events) {
  122. return this.client.publish(events);
  123. }
  124. }
  125. exports.Channel = Channel;