UNPKG

10.6 kBJavaScriptView Raw
1"use strict";
2// Copyright 2019 Google LLC
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.
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.Notification = void 0;
17const index_js_1 = require("./nodejs-common/index.js");
18const promisify_1 = require("@google-cloud/promisify");
19/**
20 * The API-formatted resource description of the notification.
21 *
22 * Note: This is not guaranteed to be up-to-date when accessed. To get the
23 * latest record, call the `getMetadata()` method.
24 *
25 * @name Notification#metadata
26 * @type {object}
27 */
28/**
29 * A Notification object is created from your {@link Bucket} object using
30 * {@link Bucket#notification}. Use it to interact with Cloud Pub/Sub
31 * notifications.
32 *
33 * See {@link https://cloud.google.com/storage/docs/pubsub-notifications| Cloud Pub/Sub Notifications for Google Cloud Storage}
34 *
35 * @class
36 * @hideconstructor
37 *
38 * @param {Bucket} bucket The bucket instance this notification is attached to.
39 * @param {string} id The ID of the notification.
40 *
41 * @example
42 * ```
43 * const {Storage} = require('@google-cloud/storage');
44 * const storage = new Storage();
45 * const myBucket = storage.bucket('my-bucket');
46 *
47 * const notification = myBucket.notification('1');
48 * ```
49 */
50class Notification extends index_js_1.ServiceObject {
51 constructor(bucket, id) {
52 const requestQueryObject = {};
53 const methods = {
54 /**
55 * Creates a notification subscription for the bucket.
56 *
57 * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/insert| Notifications: insert}
58 * @method Notification#create
59 *
60 * @param {Topic|string} topic The Cloud PubSub topic to which this
61 * subscription publishes. If the project ID is omitted, the current
62 * project ID will be used.
63 *
64 * Acceptable formats are:
65 * - `projects/grape-spaceship-123/topics/my-topic`
66 *
67 * - `my-topic`
68 * @param {CreateNotificationRequest} [options] Metadata to set for
69 * the notification.
70 * @param {CreateNotificationCallback} [callback] Callback function.
71 * @returns {Promise<CreateNotificationResponse>}
72 * @throws {Error} If a valid topic is not provided.
73 *
74 * @example
75 * ```
76 * const {Storage} = require('@google-cloud/storage');
77 * const storage = new Storage();
78 * const myBucket = storage.bucket('my-bucket');
79 * const notification = myBucket.notification('1');
80 *
81 * notification.create(function(err, notification, apiResponse) {
82 * if (!err) {
83 * // The notification was created successfully.
84 * }
85 * });
86 *
87 * //-
88 * // If the callback is omitted, we'll return a Promise.
89 * //-
90 * notification.create().then(function(data) {
91 * const notification = data[0];
92 * const apiResponse = data[1];
93 * });
94 * ```
95 */
96 create: true,
97 /**
98 * @typedef {array} DeleteNotificationResponse
99 * @property {object} 0 The full API response.
100 */
101 /**
102 * Permanently deletes a notification subscription.
103 *
104 * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/delete| Notifications: delete API Documentation}
105 *
106 * @param {object} [options] Configuration options.
107 * @param {string} [options.userProject] The ID of the project which will be
108 * billed for the request.
109 * @param {DeleteNotificationCallback} [callback] Callback function.
110 * @returns {Promise<DeleteNotificationResponse>}
111 *
112 * @example
113 * ```
114 * const {Storage} = require('@google-cloud/storage');
115 * const storage = new Storage();
116 * const myBucket = storage.bucket('my-bucket');
117 * const notification = myBucket.notification('1');
118 *
119 * notification.delete(function(err, apiResponse) {});
120 *
121 * //-
122 * // If the callback is omitted, we'll return a Promise.
123 * //-
124 * notification.delete().then(function(data) {
125 * const apiResponse = data[0];
126 * });
127 *
128 * ```
129 * @example <caption>include:samples/deleteNotification.js</caption>
130 * region_tag:storage_delete_bucket_notification
131 * Another example:
132 */
133 delete: {
134 reqOpts: {
135 qs: requestQueryObject,
136 },
137 },
138 /**
139 * Get a notification and its metadata if it exists.
140 *
141 * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/get| Notifications: get API Documentation}
142 *
143 * @param {object} [options] Configuration options.
144 * See {@link Bucket#createNotification} for create options.
145 * @param {boolean} [options.autoCreate] Automatically create the object if
146 * it does not exist. Default: `false`.
147 * @param {string} [options.userProject] The ID of the project which will be
148 * billed for the request.
149 * @param {GetNotificationCallback} [callback] Callback function.
150 * @return {Promise<GetNotificationCallback>}
151 *
152 * @example
153 * ```
154 * const {Storage} = require('@google-cloud/storage');
155 * const storage = new Storage();
156 * const myBucket = storage.bucket('my-bucket');
157 * const notification = myBucket.notification('1');
158 *
159 * notification.get(function(err, notification, apiResponse) {
160 * // `notification.metadata` has been populated.
161 * });
162 *
163 * //-
164 * // If the callback is omitted, we'll return a Promise.
165 * //-
166 * notification.get().then(function(data) {
167 * const notification = data[0];
168 * const apiResponse = data[1];
169 * });
170 * ```
171 */
172 get: {
173 reqOpts: {
174 qs: requestQueryObject,
175 },
176 },
177 /**
178 * Get the notification's metadata.
179 *
180 * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/get| Notifications: get API Documentation}
181 *
182 * @param {object} [options] Configuration options.
183 * @param {string} [options.userProject] The ID of the project which will be
184 * billed for the request.
185 * @param {GetNotificationMetadataCallback} [callback] Callback function.
186 * @returns {Promise<GetNotificationMetadataResponse>}
187 *
188 * @example
189 * ```
190 * const {Storage} = require('@google-cloud/storage');
191 * const storage = new Storage();
192 * const myBucket = storage.bucket('my-bucket');
193 * const notification = myBucket.notification('1');
194 *
195 * notification.getMetadata(function(err, metadata, apiResponse) {});
196 *
197 * //-
198 * // If the callback is omitted, we'll return a Promise.
199 * //-
200 * notification.getMetadata().then(function(data) {
201 * const metadata = data[0];
202 * const apiResponse = data[1];
203 * });
204 *
205 * ```
206 * @example <caption>include:samples/getMetadataNotifications.js</caption>
207 * region_tag:storage_print_pubsub_bucket_notification
208 * Another example:
209 */
210 getMetadata: {
211 reqOpts: {
212 qs: requestQueryObject,
213 },
214 },
215 /**
216 * @typedef {array} NotificationExistsResponse
217 * @property {boolean} 0 Whether the notification exists or not.
218 */
219 /**
220 * @callback NotificationExistsCallback
221 * @param {?Error} err Request error, if any.
222 * @param {boolean} exists Whether the notification exists or not.
223 */
224 /**
225 * Check if the notification exists.
226 *
227 * @method Notification#exists
228 * @param {NotificationExistsCallback} [callback] Callback function.
229 * @returns {Promise<NotificationExistsResponse>}
230 *
231 * @example
232 * ```
233 * const {Storage} = require('@google-cloud/storage');
234 * const storage = new Storage();
235 * const myBucket = storage.bucket('my-bucket');
236 * const notification = myBucket.notification('1');
237 *
238 * notification.exists(function(err, exists) {});
239 *
240 * //-
241 * // If the callback is omitted, we'll return a Promise.
242 * //-
243 * notification.exists().then(function(data) {
244 * const exists = data[0];
245 * });
246 * ```
247 */
248 exists: true,
249 };
250 super({
251 parent: bucket,
252 baseUrl: '/notificationConfigs',
253 id: id.toString(),
254 createMethod: bucket.createNotification.bind(bucket),
255 methods,
256 });
257 }
258}
259exports.Notification = Notification;
260/*! Developer Documentation
261 *
262 * All async methods (except for streams) will return a Promise in the event
263 * that a callback is omitted.
264 */
265(0, promisify_1.promisifyAll)(Notification);