UNPKG

11.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 nodejs_common_1 = require("./nodejs-common");
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 nodejs_common_1.ServiceObject {
51 constructor(bucket, id) {
52 const methods = {
53 /**
54 * Creates a notification subscription for the bucket.
55 *
56 * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/insert| Notifications: insert}
57 * @method Notification#create
58 *
59 * @param {Topic|string} topic The Cloud PubSub topic to which this
60 * subscription publishes. If the project ID is omitted, the current
61 * project ID will be used.
62 *
63 * Acceptable formats are:
64 * - `projects/grape-spaceship-123/topics/my-topic`
65 *
66 * - `my-topic`
67 * @param {CreateNotificationRequest} [options] Metadata to set for
68 * the notification.
69 * @param {CreateNotificationCallback} [callback] Callback function.
70 * @returns {Promise<CreateNotificationResponse>}
71 * @throws {Error} If a valid topic is not provided.
72 *
73 * @example
74 * ```
75 * const {Storage} = require('@google-cloud/storage');
76 * const storage = new Storage();
77 * const myBucket = storage.bucket('my-bucket');
78 * const notification = myBucket.notification('1');
79 *
80 * notification.create(function(err, notification, apiResponse) {
81 * if (!err) {
82 * // The notification was created successfully.
83 * }
84 * });
85 *
86 * //-
87 * // If the callback is omitted, we'll return a Promise.
88 * //-
89 * notification.create().then(function(data) {
90 * const notification = data[0];
91 * const apiResponse = data[1];
92 * });
93 * ```
94 */
95 create: true,
96 /**
97 * @typedef {array} NotificationExistsResponse
98 * @property {boolean} 0 Whether the notification exists or not.
99 */
100 /**
101 * @callback NotificationExistsCallback
102 * @param {?Error} err Request error, if any.
103 * @param {boolean} exists Whether the notification exists or not.
104 */
105 /**
106 * Check if the notification exists.
107 *
108 * @method Notification#exists
109 * @param {NotificationExistsCallback} [callback] Callback function.
110 * @returns {Promise<NotificationExistsResponse>}
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.exists(function(err, exists) {});
120 *
121 * //-
122 * // If the callback is omitted, we'll return a Promise.
123 * //-
124 * notification.exists().then(function(data) {
125 * const exists = data[0];
126 * });
127 * ```
128 */
129 exists: true,
130 };
131 super({
132 parent: bucket,
133 baseUrl: '/notificationConfigs',
134 id: id.toString(),
135 createMethod: bucket.createNotification.bind(bucket),
136 methods,
137 });
138 }
139 /**
140 * @typedef {array} DeleteNotificationResponse
141 * @property {object} 0 The full API response.
142 */
143 /**
144 * Permanently deletes a notification subscription.
145 *
146 * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/delete| Notifications: delete API Documentation}
147 *
148 * @param {object} [options] Configuration options.
149 * @param {string} [options.userProject] The ID of the project which will be
150 * billed for the request.
151 * @param {DeleteNotificationCallback} [callback] Callback function.
152 * @returns {Promise<DeleteNotificationResponse>}
153 *
154 * @example
155 * ```
156 * const {Storage} = require('@google-cloud/storage');
157 * const storage = new Storage();
158 * const myBucket = storage.bucket('my-bucket');
159 * const notification = myBucket.notification('1');
160 *
161 * notification.delete(function(err, apiResponse) {});
162 *
163 * //-
164 * // If the callback is omitted, we'll return a Promise.
165 * //-
166 * notification.delete().then(function(data) {
167 * const apiResponse = data[0];
168 * });
169 *
170 * ```
171 * @example <caption>include:samples/deleteNotification.js</caption>
172 * region_tag:storage_delete_bucket_notification
173 * Another example:
174 */
175 delete(optionsOrCallback, callback) {
176 const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
177 callback =
178 typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
179 this.request({
180 method: 'DELETE',
181 uri: '',
182 qs: options,
183 }, callback || nodejs_common_1.util.noop);
184 }
185 /**
186 * Get a notification and its metadata if it exists.
187 *
188 * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/get| Notifications: get API Documentation}
189 *
190 * @param {object} [options] Configuration options.
191 * See {@link Bucket#createNotification} for create options.
192 * @param {boolean} [options.autoCreate] Automatically create the object if
193 * it does not exist. Default: `false`.
194 * @param {string} [options.userProject] The ID of the project which will be
195 * billed for the request.
196 * @param {GetNotificationCallback} [callback] Callback function.
197 * @return {Promise<GetNotificationCallback>}
198 *
199 * @example
200 * ```
201 * const {Storage} = require('@google-cloud/storage');
202 * const storage = new Storage();
203 * const myBucket = storage.bucket('my-bucket');
204 * const notification = myBucket.notification('1');
205 *
206 * notification.get(function(err, notification, apiResponse) {
207 * // `notification.metadata` has been populated.
208 * });
209 *
210 * //-
211 * // If the callback is omitted, we'll return a Promise.
212 * //-
213 * notification.get().then(function(data) {
214 * const notification = data[0];
215 * const apiResponse = data[1];
216 * });
217 * ```
218 */
219 get(optionsOrCallback, callback) {
220 const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
221 callback =
222 typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
223 const autoCreate = options.autoCreate;
224 delete options.autoCreate;
225 const onCreate = (err, notification, apiResponse) => {
226 if (err) {
227 if (err.code === 409) {
228 this.get(options, callback);
229 return;
230 }
231 callback(err, null, apiResponse);
232 return;
233 }
234 callback(null, notification, apiResponse);
235 };
236 this.getMetadata(options, (err, metadata) => {
237 if (err) {
238 if (err.code === 404 && autoCreate) {
239 const args = [];
240 if (Object.keys(options).length > 0) {
241 args.push(options);
242 }
243 args.push(onCreate);
244 // eslint-disable-next-line
245 this.create.apply(this, args);
246 return;
247 }
248 callback(err, null, metadata);
249 return;
250 }
251 callback(null, this, metadata);
252 });
253 }
254 /**
255 * Get the notification's metadata.
256 *
257 * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/get| Notifications: get API Documentation}
258 *
259 * @param {object} [options] Configuration options.
260 * @param {string} [options.userProject] The ID of the project which will be
261 * billed for the request.
262 * @param {GetNotificationMetadataCallback} [callback] Callback function.
263 * @returns {Promise<GetNotificationMetadataResponse>}
264 *
265 * @example
266 * ```
267 * const {Storage} = require('@google-cloud/storage');
268 * const storage = new Storage();
269 * const myBucket = storage.bucket('my-bucket');
270 * const notification = myBucket.notification('1');
271 *
272 * notification.getMetadata(function(err, metadata, apiResponse) {});
273 *
274 * //-
275 * // If the callback is omitted, we'll return a Promise.
276 * //-
277 * notification.getMetadata().then(function(data) {
278 * const metadata = data[0];
279 * const apiResponse = data[1];
280 * });
281 *
282 * ```
283 * @example <caption>include:samples/getMetadataNotifications.js</caption>
284 * region_tag:storage_print_pubsub_bucket_notification
285 * Another example:
286 */
287 getMetadata(optionsOrCallback, callback) {
288 const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
289 callback =
290 typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
291 this.request({
292 uri: '',
293 qs: options,
294 }, (err, resp) => {
295 if (err) {
296 callback(err, null, resp);
297 return;
298 }
299 this.metadata = resp;
300 callback(null, this.metadata, resp);
301 });
302 }
303}
304exports.Notification = Notification;
305/*! Developer Documentation
306 *
307 * All async methods (except for streams) will return a Promise in the event
308 * that a callback is omitted.
309 */
310(0, promisify_1.promisifyAll)(Notification);
311//# sourceMappingURL=notification.js.map
\No newline at end of file