@@ -28,9 +28,11 @@ import { serviceFunctionIdentifier } from './plugin-legacy';
28
28
import type { Config } from './types' ;
29
29
30
30
export const operationOptionsType = ( {
31
+ clientOption,
31
32
identifierData,
32
33
throwOnError,
33
34
} : {
35
+ clientOption ?: 'required' | 'omitted' ;
34
36
context : IR . Context ;
35
37
identifierData ?: ReturnType < TypeScriptFile [ 'identifier' ] > ;
36
38
// TODO: refactor this so we don't need to import error type unless it's used here
@@ -39,13 +41,24 @@ export const operationOptionsType = ({
39
41
} ) => {
40
42
const optionsName = clientApi . Options . name ;
41
43
44
+ let optionsType = identifierData
45
+ ? `${ optionsName } <${ identifierData . name } >`
46
+ : optionsName ;
47
+
42
48
// TODO: refactor this to be more generic, works for now
43
49
if ( throwOnError ) {
44
- return `${ optionsName } <${ identifierData ?. name || 'unknown' } , ${ throwOnError } >` ;
50
+ optionsType = `${ optionsName } <${ identifierData ?. name || 'unknown' } , ${ throwOnError } >` ;
45
51
}
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 } , 'client'>` ;
59
+ }
60
+
61
+ return optionsType ;
49
62
} ;
50
63
51
64
const sdkId = 'sdk' ;
@@ -377,6 +390,12 @@ const operationStatements = ({
377
390
value : operation . path ,
378
391
} ) ;
379
392
393
+ let clientCall = '(options?.client ?? client)' ;
394
+
395
+ if ( ! plugin . autoCreateClient ) {
396
+ clientCall = plugin . asClass ? 'this._client' : 'options.client' ;
397
+ }
398
+
380
399
return [
381
400
compiler . returnFunctionCall ( {
382
401
args : [
@@ -385,7 +404,7 @@ const operationStatements = ({
385
404
obj : requestOptions ,
386
405
} ) ,
387
406
] ,
388
- name : `(options?.client ?? client) .${ operation . method } ` ,
407
+ name : `${ clientCall } .${ operation . method } ` ,
389
408
types : [
390
409
identifierResponse . name || 'unknown' ,
391
410
identifierError . name || 'unknown' ,
@@ -417,7 +436,7 @@ const generateClassSdk = ({
417
436
operation . summary && escapeComment ( operation . summary ) ,
418
437
operation . description && escapeComment ( operation . description ) ,
419
438
] ,
420
- isStatic : true ,
439
+ isStatic : ! ! plugin . autoCreateClient , // if client is required, methods are not static
421
440
name : serviceFunctionIdentifier ( {
422
441
config : context . config ,
423
442
handleIllegal : false ,
@@ -429,6 +448,7 @@ const generateClassSdk = ({
429
448
isRequired : hasOperationDataRequired ( operation ) ,
430
449
name : 'options' ,
431
450
type : operationOptionsType ( {
451
+ clientOption : ! plugin . autoCreateClient ? 'omitted' : undefined ,
432
452
context,
433
453
identifierData,
434
454
// identifierError,
@@ -466,9 +486,45 @@ const generateClassSdk = ({
466
486
467
487
context . subscribe ( 'after' , ( ) => {
468
488
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
+
469
525
const node = compiler . classDeclaration ( {
470
526
decorator : undefined ,
471
- members : nodes ,
527
+ members : [ ... extraMembers , ... nodes ] ,
472
528
name : transformServiceName ( {
473
529
config : context . config ,
474
530
name,
@@ -503,9 +559,11 @@ const generateFlatSdk = ({
503
559
expression : compiler . arrowFunction ( {
504
560
parameters : [
505
561
{
506
- isRequired : hasOperationDataRequired ( operation ) ,
562
+ isRequired :
563
+ hasOperationDataRequired ( operation ) || ! plugin . autoCreateClient ,
507
564
name : 'options' ,
508
565
type : operationOptionsType ( {
566
+ clientOption : ! plugin . autoCreateClient ? 'required' : undefined ,
509
567
context,
510
568
identifierData,
511
569
// identifierError,
@@ -550,52 +608,64 @@ export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {
550
608
id : sdkId ,
551
609
path : plugin . output ,
552
610
} ) ;
611
+
553
612
const sdkOutput = file . nameWithoutExtension ( ) ;
554
613
555
614
// import required packages and core files
556
615
const clientModule = clientModulePath ( {
557
616
config : context . config ,
558
617
sourceOutput : sdkOutput ,
559
618
} ) ;
560
- file . import ( {
561
- module : clientModule ,
562
- name : 'createClient' ,
563
- } ) ;
564
- file . import ( {
565
- module : clientModule ,
566
- name : 'createConfig' ,
567
- } ) ;
619
+
568
620
file . import ( {
569
621
...clientApi . Options ,
570
622
module : clientModule ,
571
623
} ) ;
572
624
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 ) ;
625
+ if ( plugin . autoCreateClient ) {
626
+ file . import ( {
627
+ module : clientModule ,
628
+ name : 'createClient' ,
629
+ } ) ;
630
+ file . import ( {
631
+ module : clientModule ,
632
+ name : 'createConfig' ,
633
+ } ) ;
634
+
635
+ // define client first
636
+ const statement = compiler . constVariable ( {
637
+ exportConst : true ,
638
+ expression : compiler . callExpression ( {
639
+ functionName : 'createClient' ,
640
+ parameters : [
641
+ compiler . callExpression ( {
642
+ functionName : 'createConfig' ,
643
+ parameters : [
644
+ plugin . throwOnError
645
+ ? compiler . objectExpression ( {
646
+ obj : [
647
+ {
648
+ key : 'throwOnError' ,
649
+ value : plugin . throwOnError ,
650
+ } ,
651
+ ] ,
652
+ } )
653
+ : undefined ,
654
+ ] ,
655
+ } ) ,
656
+ ] ,
657
+ } ) ,
658
+ name : 'client' ,
659
+ } ) ;
660
+
661
+ file . add ( statement ) ;
662
+ } else {
663
+ // Bring in the client type
664
+ file . import ( {
665
+ ...clientApi . Client ,
666
+ module : clientModule ,
667
+ } ) ;
668
+ }
599
669
600
670
if ( plugin . asClass ) {
601
671
generateClassSdk ( { context, plugin } ) ;
0 commit comments