channel.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import { ServiceObject, util } from './nodejs-common/index.js';
  15. import { promisifyAll } from '@google-cloud/promisify';
  16. /**
  17. * Create a channel object to interact with a Cloud Storage channel.
  18. *
  19. * See {@link https://cloud.google.com/storage/docs/object-change-notification| Object Change Notification}
  20. *
  21. * @class
  22. *
  23. * @param {string} id The ID of the channel.
  24. * @param {string} resourceId The resource ID of the channel.
  25. *
  26. * @example
  27. * ```
  28. * const {Storage} = require('@google-cloud/storage');
  29. * const storage = new Storage();
  30. * const channel = storage.channel('id', 'resource-id');
  31. * ```
  32. */
  33. class Channel extends ServiceObject {
  34. constructor(storage, id, resourceId) {
  35. const config = {
  36. parent: storage,
  37. baseUrl: '/channels',
  38. // An ID shouldn't be included in the API requests.
  39. // RE:
  40. // https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1145
  41. id: '',
  42. methods: {
  43. // Only need `request`.
  44. },
  45. };
  46. super(config);
  47. this.metadata.id = id;
  48. this.metadata.resourceId = resourceId;
  49. }
  50. /**
  51. * @typedef {array} StopResponse
  52. * @property {object} 0 The full API response.
  53. */
  54. /**
  55. * @callback StopCallback
  56. * @param {?Error} err Request error, if any.
  57. * @param {object} apiResponse The full API response.
  58. */
  59. /**
  60. * Stop this channel.
  61. *
  62. * @param {StopCallback} [callback] Callback function.
  63. * @returns {Promise<StopResponse>}
  64. *
  65. * @example
  66. * ```
  67. * const {Storage} = require('@google-cloud/storage');
  68. * const storage = new Storage();
  69. * const channel = storage.channel('id', 'resource-id');
  70. * channel.stop(function(err, apiResponse) {
  71. * if (!err) {
  72. * // Channel stopped successfully.
  73. * }
  74. * });
  75. *
  76. * //-
  77. * // If the callback is omitted, we'll return a Promise.
  78. * //-
  79. * channel.stop().then(function(data) {
  80. * const apiResponse = data[0];
  81. * });
  82. * ```
  83. */
  84. stop(callback) {
  85. callback = callback || util.noop;
  86. this.request({
  87. method: 'POST',
  88. uri: '/stop',
  89. json: this.metadata,
  90. }, (err, apiResponse) => {
  91. callback(err, apiResponse);
  92. });
  93. }
  94. }
  95. /*! Developer Documentation
  96. *
  97. * All async methods (except for streams) will return a Promise in the event
  98. * that a callback is omitted.
  99. */
  100. promisifyAll(Channel);
  101. /**
  102. * Reference to the {@link Channel} class.
  103. * @name module:@google-cloud/storage.Channel
  104. * @see Channel
  105. */
  106. export { Channel };