@@ -28,9 +28,11 @@ import { serviceFunctionIdentifier } from './plugin-legacy';
2828import  type  {  Config  }  from  './types' ; 
2929
3030export  const  operationOptionsType  =  ( { 
31+   clientOption, 
3132  identifierData, 
3233  throwOnError, 
3334} : { 
35+   clientOption ?: 'required'  |  'omitted' ; 
3436  context : IR . Context ; 
3537  identifierData ?: ReturnType < TypeScriptFile [ 'identifier' ] > ; 
3638  // TODO: refactor this so we don't need to import error type unless it's used here 
@@ -39,13 +41,24 @@ export const operationOptionsType = ({
3941} )  =>  { 
4042  const  optionsName  =  clientApi . Options . name ; 
4143
44+   let  optionsType  =  identifierData 
45+     ? `${ optionsName } ${ identifierData . name }  
46+     : optionsName ; 
47+ 
4248  // TODO: refactor this to be more generic, works for now 
4349  if  ( throwOnError )  { 
44-     return  `${ optionsName } ${ identifierData ?. name  ||  'unknown' } ${ throwOnError }  ; 
50+     optionsType   =  `${ optionsName } ${ identifierData ?. name  ||  'unknown' } ${ throwOnError }  ; 
4551  } 
46-   return  identifierData 
47-     ? `${ optionsName } ${ identifierData . name }  
48-     : optionsName ; 
52+ 
53+   if  ( clientOption  ===  'required' )  { 
54+     optionsType  +=  ` & { client: Client }` ; 
55+   } 
56+ 
57+   if  ( clientOption  ===  'omitted' )  { 
58+     optionsType  =  `Omit<${ optionsType }  ; 
59+   } 
60+ 
61+   return  optionsType ; 
4962} ; 
5063
5164const  sdkId  =  'sdk' ; 
@@ -377,6 +390,12 @@ const operationStatements = ({
377390    value : operation . path , 
378391  } ) ; 
379392
393+   let  clientCall  =  '(options?.client ?? client)' ; 
394+ 
395+   if  ( ! plugin . autoCreateClient )  { 
396+     clientCall  =  plugin . asClass  ? 'this._client'  : 'options.client' ; 
397+   } 
398+ 
380399  return  [ 
381400    compiler . returnFunctionCall ( { 
382401      args : [ 
@@ -385,7 +404,7 @@ const operationStatements = ({
385404          obj : requestOptions , 
386405        } ) , 
387406      ] , 
388-       name : `(options?.client ?? client) .${ operation . method }  , 
407+       name : `${ clientCall } ${ operation . method }  , 
389408      types : [ 
390409        identifierResponse . name  ||  'unknown' , 
391410        identifierError . name  ||  'unknown' , 
@@ -417,7 +436,7 @@ const generateClassSdk = ({
417436        operation . summary  &&  escapeComment ( operation . summary ) , 
418437        operation . description  &&  escapeComment ( operation . description ) , 
419438      ] , 
420-       isStatic : true , 
439+       isStatic : ! ! plugin . autoCreateClient ,   // if client is required, methods are not static 
421440      name : serviceFunctionIdentifier ( { 
422441        config : context . config , 
423442        handleIllegal : false , 
@@ -429,6 +448,7 @@ const generateClassSdk = ({
429448          isRequired : hasOperationDataRequired ( operation ) , 
430449          name : 'options' , 
431450          type : operationOptionsType ( { 
451+             clientOption : ! plugin . autoCreateClient  ? 'omitted'  : undefined , 
432452            context, 
433453            identifierData, 
434454            // identifierError, 
@@ -466,9 +486,45 @@ const generateClassSdk = ({
466486
467487  context . subscribe ( 'after' ,  ( )  =>  { 
468488    for  ( const  [ name ,  nodes ]  of  sdks )  { 
489+       const  extraMembers : ts . ClassElement [ ]  =  [ ] ; 
490+ 
491+       // Add client property and constructor if autoCreateClient is false 
492+       if  ( ! plugin . autoCreateClient )  { 
493+         const  clientType  =  'Client' ; 
494+ 
495+         const  clientProperty  =  compiler . propertyDeclaration ( { 
496+           accessLevel : 'private' , 
497+           comment : [ 'Client Instance' ] , 
498+           name : '_client' , 
499+           type : compiler . typeReferenceNode ( {  typeName : clientType  } ) , 
500+         } ) ; 
501+ 
502+         const  constructor  =  compiler . constructorDeclaration ( { 
503+           comment : [ '@param client - Client Instance' ] , 
504+           parameters : [ 
505+             { 
506+               isRequired : true , 
507+               name : 'client' , 
508+               type : clientType , 
509+             } , 
510+           ] , 
511+           statements : [ 
512+             compiler . expressionToStatement ( { 
513+               expression : compiler . binaryExpression ( { 
514+                 left : compiler . identifier ( {  text : 'this._client '  } ) , 
515+                 operator : '=' , 
516+                 right : compiler . identifier ( {  text : 'client'  } ) , 
517+               } ) , 
518+             } ) , 
519+           ] , 
520+         } ) ; 
521+ 
522+         extraMembers . push ( clientProperty ,  constructor ) ; 
523+       } 
524+ 
469525      const  node  =  compiler . classDeclaration ( { 
470526        decorator : undefined , 
471-         members : nodes , 
527+         members : [ ... extraMembers ,  ... nodes ] , 
472528        name : transformServiceName ( { 
473529          config : context . config , 
474530          name, 
@@ -503,9 +559,11 @@ const generateFlatSdk = ({
503559      expression : compiler . arrowFunction ( { 
504560        parameters : [ 
505561          { 
506-             isRequired : hasOperationDataRequired ( operation ) , 
562+             isRequired :
563+               hasOperationDataRequired ( operation )  ||  ! plugin . autoCreateClient , 
507564            name : 'options' , 
508565            type : operationOptionsType ( { 
566+               clientOption : ! plugin . autoCreateClient  ? 'required'  : undefined , 
509567              context, 
510568              identifierData, 
511569              // identifierError, 
@@ -550,53 +608,65 @@ export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {
550608    id : sdkId , 
551609    path : plugin . output , 
552610  } ) ; 
611+ 
553612  const  sdkOutput  =  file . nameWithoutExtension ( ) ; 
554613
555614  // import required packages and core files 
556615  const  clientModule  =  clientModulePath ( { 
557616    config : context . config , 
558617    sourceOutput : sdkOutput , 
559618  } ) ; 
560-   file . import ( { 
561-     module : clientModule , 
562-     name : 'createClient' , 
563-   } ) ; 
564-   file . import ( { 
565-     module : clientModule , 
566-     name : 'createConfig' , 
567-   } ) ; 
619+ 
620+   if  ( plugin . autoCreateClient )  { 
621+     file . import ( { 
622+       module : clientModule , 
623+       name : 'createClient' , 
624+     } ) ; 
625+     file . import ( { 
626+       module : clientModule , 
627+       name : 'createConfig' , 
628+     } ) ; 
629+ 
630+     // define client first 
631+     const  statement  =  compiler . constVariable ( { 
632+       exportConst : true , 
633+       expression : compiler . callExpression ( { 
634+         functionName : 'createClient' , 
635+         parameters : [ 
636+           compiler . callExpression ( { 
637+             functionName : 'createConfig' , 
638+             parameters : [ 
639+               plugin . throwOnError 
640+                 ? compiler . objectExpression ( { 
641+                     obj : [ 
642+                       { 
643+                         key : 'throwOnError' , 
644+                         value : plugin . throwOnError , 
645+                       } , 
646+                     ] , 
647+                   } ) 
648+                 : undefined , 
649+             ] , 
650+           } ) , 
651+         ] , 
652+       } ) , 
653+       name : 'client' , 
654+     } ) ; 
655+ 
656+     file . add ( statement ) ; 
657+   }  else  { 
658+     // Bring in the client type 
659+     file . import ( { 
660+       ...clientApi . Client , 
661+       module : clientModule , 
662+     } ) ; 
663+   } 
664+ 
568665  file . import ( { 
569666    ...clientApi . Options , 
570667    module : clientModule , 
571668  } ) ; 
572669
573-   // define client first 
574-   const  statement  =  compiler . constVariable ( { 
575-     exportConst : true , 
576-     expression : compiler . callExpression ( { 
577-       functionName : 'createClient' , 
578-       parameters : [ 
579-         compiler . callExpression ( { 
580-           functionName : 'createConfig' , 
581-           parameters : [ 
582-             plugin . throwOnError 
583-               ? compiler . objectExpression ( { 
584-                   obj : [ 
585-                     { 
586-                       key : 'throwOnError' , 
587-                       value : plugin . throwOnError , 
588-                     } , 
589-                   ] , 
590-                 } ) 
591-               : undefined , 
592-           ] , 
593-         } ) , 
594-       ] , 
595-     } ) , 
596-     name : 'client' , 
597-   } ) ; 
598-   file . add ( statement ) ; 
599- 
600670  if  ( plugin . asClass )  { 
601671    generateClassSdk ( {  context,  plugin } ) ; 
602672  }  else  { 
0 commit comments