@@ -534,27 +534,7 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
534534 }
535535
536536 await cluster . addAuthIfNeeded ( ) ;
537-
538- // Set up requireApiVersion if requested.
539- if ( options . requireApiVersion !== undefined ) {
540- if ( options . topology === 'replset' ) {
541- throw new Error (
542- 'requireApiVersion is not supported for replica sets, see SERVER-97010' ,
543- ) ;
544- }
545- await Promise . all (
546- [ ...cluster . servers ] . map (
547- async ( child ) =>
548- await child . withClient ( async ( client ) => {
549- const admin = client . db ( 'admin' ) ;
550- await admin . command ( { setParameter : 1 , requireApiVersion : true } ) ;
551- } ) ,
552- ) ,
553- ) ;
554- await cluster . updateDefaultConnectionOptions ( {
555- serverApi : String ( options . requireApiVersion ) as '1' ,
556- } ) ;
557- }
537+ await cluster . addRequireApiVersionIfNeeded ( options ) ;
558538 return cluster ;
559539 }
560540
@@ -563,6 +543,32 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
563543 yield * this . shards ;
564544 }
565545
546+ async addRequireApiVersionIfNeeded ( {
547+ ...options
548+ } : MongoClusterOptions ) : Promise < void > {
549+ // Set up requireApiVersion if requested.
550+ if ( options . requireApiVersion !== undefined ) {
551+ return ;
552+ }
553+ if ( options . topology === 'replset' ) {
554+ throw new Error (
555+ 'requireApiVersion is not supported for replica sets, see SERVER-97010' ,
556+ ) ;
557+ }
558+ await Promise . all (
559+ [ ...this . servers ] . map (
560+ async ( child ) =>
561+ await child . withClient ( async ( client ) => {
562+ const admin = client . db ( 'admin' ) ;
563+ await admin . command ( { setParameter : 1 , requireApiVersion : true } ) ;
564+ } ) ,
565+ ) ,
566+ ) ;
567+ await this . updateDefaultConnectionOptions ( {
568+ serverApi : String ( options . requireApiVersion ) as '1' ,
569+ } ) ;
570+ }
571+
566572 async addAuthIfNeeded ( ) : Promise < void > {
567573 if ( ! this . users ?. length ) return ;
568574 // Sleep to give time for a possible replset election to settle.
0 commit comments