@@ -169,74 +169,76 @@ PubSub.prototype.createTopic = function(name, callback) {
169
169
* Your provided callback will either be invoked with an error object, if an API
170
170
* error occurred, or a {@linkcode module:pubsub/subscription} object.
171
171
*
172
- * @throws {Error } If a name is not provided.
172
+ * @throws {Error } If a Topic instance or topic name is not provided.
173
+ * @throws {Error } If a subName is not provided.
173
174
*
174
- * @param {string } name - The name of the subscription.
175
+ * @param {module:pubsub/topic|string } - topic - The Topic to create a
176
+ * subscription to.
177
+ * @param {string } subName - The name of the subscription.
175
178
* @param {object= } options - Configuration object.
176
- * @param {number= } options.ackDeadlineSeconds - The maximum time after
177
- * receiving a message that you must ack a message before it is redelivered.
178
- * @param {boolean= } options.autoAck - Automatically acknowledge the message
179
- * once it's pulled. (default: false)
180
- * @param {number= } options.interval - Interval in milliseconds to check for new
179
+ * @param {number } options.ackDeadlineSeconds - The maximum time after receiving
180
+ * a message that you must ack a message before it is redelivered.
181
+ * @param {boolean } options.autoAck - Automatically acknowledge the message once
182
+ * it's pulled. (default: false)
183
+ * @param {number } options.interval - Interval in milliseconds to check for new
181
184
* messages. (default: 10)
182
- * @param {string= } options.projectId - The projectId where the topic resource
183
- * exists.
184
- * @param {boolean= } options.reuseExisting - If the subscription already exists,
185
+ * @param {boolean } options.reuseExisting - If the subscription already exists,
185
186
* reuse it. The options of the existing subscription are not changed. If
186
187
* false, attempting to create a subscription that already exists will fail.
187
188
* (default: false)
188
189
* @param {function } callback - The callback function.
189
190
*
190
191
* @example
191
192
* // Without specifying any options.
192
- * var topic = 'messageTopic' ;
193
- * var sub = 'messageSubscription ';
193
+ * var topic = pubsub.topic('messageCenter') ;
194
+ * var name = 'newMessages ';
194
195
*
195
- * pubsub.subscribe(topic, sub , function(err, subscription, apiResponse) {});
196
+ * pubsub.subscribe(topic, name , function(err, subscription, apiResponse) {});
196
197
*
197
198
* // With options.
198
- * pubsub.subscribe(topic, sub , {
199
+ * pubsub.subscribe(topic, name , {
199
200
* ackDeadlineSeconds: 90,
200
201
* autoAck: true,
201
202
* interval: 30
202
203
* }, function(err, subscription, apiResponse) {});
203
204
*/
204
- PubSub . prototype . subscribe = function ( topicName , subName , options , callback ) {
205
- var self = this ;
206
-
207
- if ( ! topicName ) {
208
- throw new Error ( 'A Topic name is required for a new subscription.' ) ;
205
+ PubSub . prototype . subscribe = function ( topic , subName , options , callback ) {
206
+ if ( ! util . is ( topic , 'string' ) && ! ( topic instanceof Topic ) ) {
207
+ throw new Error ( 'A Topic is required for a new subscription.' ) ;
209
208
}
210
209
211
210
if ( ! subName ) {
212
- throw new Error ( 'A Subscription name is required for a new subscription.' ) ;
211
+ throw new Error ( 'A subscription name is required for a new subscription.' ) ;
213
212
}
214
213
215
214
if ( ! callback ) {
216
215
callback = options ;
217
216
options = { } ;
218
217
}
219
218
219
+ options = options || { } ;
220
+
221
+ if ( util . is ( topic , 'string' ) ) {
222
+ topic = this . topic ( topic ) ;
223
+ }
224
+
220
225
var body = {
221
- topic : topicName
226
+ topic : topic . name
222
227
} ;
223
228
224
229
if ( options . ackDeadlineSeconds ) {
225
230
body . ackDeadlineSeconds = options . ackDeadlineSeconds ;
226
231
}
227
232
228
- var projectId = options . projectId || this . projectId ;
233
+ var subscription = topic . subscription ( subName , options ) ;
229
234
230
- var path = Subscription . formatName_ ( projectId , subName ) ;
231
-
232
- this . makeReq_ ( 'PUT' , path , null , body , function ( err , result ) {
233
- if ( options . reuseExisting && err && err . code === 409 ) {
234
- callback ( null , self . subscription ( subName , options ) , result ) ;
235
- } else if ( err ) {
235
+ this . makeReq_ ( 'PUT' , subscription . name , null , body , function ( err , result ) {
236
+ if ( err && ! ( err . code === 409 && options . reuseExisting ) ) {
236
237
callback ( err , null , result ) ;
237
- } else {
238
- callback ( null , self . subscription ( subName , options ) , result ) ;
238
+ return ;
239
239
}
240
+
241
+ callback ( null , subscription , result ) ;
240
242
} ) ;
241
243
} ;
242
244
@@ -276,7 +278,6 @@ PubSub.prototype.subscription = function(name, options) {
276
278
277
279
options = options || { } ;
278
280
options . name = name ;
279
-
280
281
return new Subscription ( this , options ) ;
281
282
} ;
282
283
@@ -346,8 +347,23 @@ PubSub.prototype.getSubscriptions = function(options, callback) {
346
347
347
348
options = options || { } ;
348
349
350
+ var projectId = this . projectId ;
351
+ var topicName ;
349
352
var query = { } ;
350
353
354
+ if ( util . is ( options . topic , 'string' ) ) {
355
+ topicName = options . topic ;
356
+ } else if ( options . topic instanceof Topic ) {
357
+ // Return Subscriptions to the user under the Topic's PubSub instance.
358
+ self = options . topic . pubsub ;
359
+ projectId = self . projectId ;
360
+ topicName = options . topic . unformattedName ;
361
+ }
362
+
363
+ if ( options . projectId ) {
364
+ projectId = options . projectId ;
365
+ }
366
+
351
367
if ( options . pageSize ) {
352
368
query . pageSize = options . pageSize ;
353
369
}
@@ -356,11 +372,9 @@ PubSub.prototype.getSubscriptions = function(options, callback) {
356
372
query . pageToken = options . pageToken ;
357
373
}
358
374
359
- var projectId = options . projectId || this . projectId ;
360
-
361
375
var apiPath = util . format ( '{projectPath}{topicPath}/subscriptions' , {
362
376
projectPath : 'projects/' + projectId ,
363
- topicPath : options . topic ? '/topics/' + options . topic : ''
377
+ topicPath : topicName ? '/topics/' + topicName : ''
364
378
} ) ;
365
379
366
380
this . makeReq_ ( 'GET' , apiPath , query , null , function ( err , result ) {
@@ -370,14 +384,13 @@ PubSub.prototype.getSubscriptions = function(options, callback) {
370
384
}
371
385
372
386
var subscriptions = ( result . subscriptions || [ ] ) . map ( function ( sub ) {
373
- // Depending on if we're using a subscriptions.list or
374
- // topics.subscriptions.list API endpoint, we will get back a Subscription
375
- // resource or just the name of the subscription.
376
- var subName = sub . name || sub ;
377
-
378
387
return new Subscription ( self , {
379
388
projectId : projectId ,
380
- name : subName
389
+
390
+ // Depending on if we're using a subscriptions.list or
391
+ // topics.subscriptions.list API endpoint, we will get back a
392
+ // Subscription resource or just the name of the subscription.
393
+ name : sub . name || sub
381
394
} ) ;
382
395
} ) ;
383
396
0 commit comments