@@ -365,3 +365,59 @@ AdminClient.prototype.deleteGroups = function (groups, options, cb) {
365365 }
366366 } ) ;
367367}
368+
369+ /**
370+ * List topics.
371+ *
372+ * @param {any? } options
373+ * @param {number? } options.timeout - The request timeout in milliseconds.
374+ * May be unset (default: 5000)
375+ * @param {function } cb - The callback to be executed when finished.
376+ *
377+ * Valid ways to call this function:
378+ * listTopics(cb)
379+ * listTopics(options, cb)
380+ */
381+ AdminClient . prototype . listTopics = function ( options , cb ) {
382+ if ( ! this . _isConnected ) {
383+ throw new Error ( 'Client is disconnected' ) ;
384+ }
385+
386+ if ( typeof options === 'function' ) {
387+ cb = options ;
388+ options = { } ;
389+ }
390+
391+ if ( ! options ) {
392+ options = { } ;
393+ }
394+
395+ // Always set allTopics to true, since we need a list.
396+ options . allTopics = true ;
397+ if ( ! Object . hasOwn ( options , 'timeout' ) ) {
398+ options . timeout = 5000 ;
399+ }
400+
401+ // This definitely isn't the fastest way to list topics as
402+ // this makes a pretty large metadata request. But for the sake
403+ // of AdminAPI, this is okay.
404+ this . _client . getMetadata ( options , function ( err , metadata ) {
405+ if ( err ) {
406+ if ( cb ) {
407+ cb ( LibrdKafkaError . create ( err ) ) ;
408+ }
409+ return ;
410+ }
411+
412+ const topics = [ ]
413+ if ( metadata . topics ) {
414+ for ( const topic of metadata . topics ) {
415+ topics . push ( topic . name ) ;
416+ }
417+ }
418+
419+ if ( cb ) {
420+ cb ( null , topics ) ;
421+ }
422+ } ) ;
423+ }
0 commit comments