@@ -265,7 +265,7 @@ export function generateOpenApiParams(options: OpenAPIOptions, path: string, pat
265
265
}
266
266
} ) ;
267
267
268
- const typeName = toPascalCase ( getOperationMethodName ( operation , method , path ) ) + 'Request' ;
268
+ const typeName = getOperationTypeName ( options , operation , method , path ) + 'Request' ;
269
269
const paramsInterface = t . tsInterfaceDeclaration (
270
270
t . identifier ( typeName ) ,
271
271
null ,
@@ -361,12 +361,115 @@ export function generateOpenApiTypes(options: OpenAPIOptions, schema: OpenAPISpe
361
361
return interfaces . map ( i => t . exportNamedDeclaration ( i ) ) ;
362
362
}
363
363
364
- const getOperationMethodName = ( operation : Operation , method : string , path : string ) => {
365
- const methodName = operation . operationId || toCamelCase ( method + path . replace ( / \W / g, '_' ) ) ;
364
+ const getOperationMethodName = (
365
+ options : OpenAPIOptions ,
366
+ operation : Operation ,
367
+ method : string ,
368
+ path : string
369
+ ) => {
370
+ const defaultMethodName = toCamelCase ( method + path . replace ( / \W / g, '_' ) ) ;
371
+ const methodName = operation . operationId || defaultMethodName ;
372
+
373
+ if ( options ?. operationNamingStrategy ?. renameMap ) {
374
+ return options . operationNamingStrategy . renameMap [ methodName ] || methodName ;
375
+ }
376
+
366
377
return methodName ;
367
378
} ;
368
379
369
- export function generateMethods ( options : OpenAPIOptions , schema : OpenAPISpec ) : t . ClassMethod [ ] {
380
+ const getOperationTypeName = (
381
+ options : OpenAPIOptions ,
382
+ operation : Operation ,
383
+ method : string ,
384
+ path : string
385
+ ) => {
386
+ const defaultMethodName = toCamelCase ( method + path . replace ( / \W / g, '_' ) ) ;
387
+ const methodName = operation . operationId || defaultMethodName ;
388
+
389
+ if ( ! options ?. operationNamingStrategy ?. renameTypes ) {
390
+ return toPascalCase ( methodName ) ;
391
+ }
392
+
393
+ if ( options ?. operationNamingStrategy ?. renameMap ) {
394
+ return toPascalCase ( options . operationNamingStrategy . renameMap [ methodName ] || methodName ) ;
395
+ }
396
+
397
+ return methodName ;
398
+ } ;
399
+
400
+ export const createOperation = (
401
+ options : OpenAPIOptions ,
402
+ operation : Operation ,
403
+ path : string ,
404
+ method : string ,
405
+ alias ?: string
406
+ ) : t . ClassMethod => {
407
+ const typeName = getOperationTypeName ( options , operation , method , path ) + 'Request' ;
408
+ const id = t . identifier ( 'params' ) ;
409
+ id . typeAnnotation = t . tsTypeAnnotation ( t . tsTypeReference ( t . identifier ( typeName ) ) ) ;
410
+ const params = [ id ] ;
411
+
412
+ const returnType = getOperationReturnType ( options , operation , method ) ;
413
+ const methodName = getOperationMethodName ( options , operation , method , path ) ;
414
+
415
+ const callMethod = t . callExpression (
416
+ t . memberExpression (
417
+ t . thisExpression ( ) ,
418
+ t . identifier ( method )
419
+ ) ,
420
+ [ 'post' , 'put' , 'patch' , 'formData' ] . includes ( method ) ?
421
+ [
422
+ t . identifier ( 'path' ) ,
423
+ t . memberExpression (
424
+ t . identifier ( 'params' ) ,
425
+ t . identifier ( 'body' )
426
+ )
427
+ ] : [
428
+ t . identifier ( 'path' )
429
+ ]
430
+ ) ;
431
+ callMethod . typeParameters = t . tsTypeParameterInstantiation ( [
432
+ returnType
433
+ ] ) ;
434
+
435
+ const methodFunction = t . classMethod (
436
+ 'method' ,
437
+ t . identifier ( alias ? toCamelCase ( alias ) : methodName ) ,
438
+ params ,
439
+ t . blockStatement ( [
440
+ t . variableDeclaration ( 'const' , [
441
+ t . variableDeclarator (
442
+ t . identifier ( 'path' ) ,
443
+ createPathTemplateLiteral ( options , path )
444
+ )
445
+ ] ) ,
446
+ t . returnStatement (
447
+ t . awaitExpression (
448
+ callMethod
449
+ )
450
+ )
451
+ ] ) ,
452
+ false ,
453
+ false ,
454
+ false ,
455
+ true
456
+ ) ;
457
+ methodFunction . returnType = t . tsTypeAnnotation (
458
+ t . tsTypeReference (
459
+ t . identifier ( 'Promise' ) ,
460
+ t . tsTypeParameterInstantiation ( [
461
+ returnType
462
+ ] )
463
+ )
464
+ ) ;
465
+
466
+ return methodFunction ;
467
+ } ;
468
+
469
+ export function generateMethods (
470
+ options : OpenAPIOptions ,
471
+ schema : OpenAPISpec
472
+ ) : t . ClassMethod [ ] {
370
473
const methods : t . ClassMethod [ ] = [ ] ;
371
474
372
475
// Iterate through each path and each method in the path
@@ -380,67 +483,11 @@ export function generateMethods(options: OpenAPIOptions, schema: OpenAPISpec): t
380
483
const operation : Operation = pathItem [ method ] ;
381
484
if ( ! shouldIncludeOperation ( options , pathItem , path , method as any ) ) return ;
382
485
383
-
384
- const typeName = toPascalCase ( getOperationMethodName ( operation , method , path ) ) + 'Request' ;
385
- const id = t . identifier ( 'params' ) ;
386
- id . typeAnnotation = t . tsTypeAnnotation ( t . tsTypeReference ( t . identifier ( typeName ) ) ) ;
387
- const params = [ id ] ;
388
-
389
- const returnType = getOperationReturnType ( options , operation , method ) ;
390
- const methodName = getOperationMethodName ( operation , method , path ) ;
391
-
392
-
393
-
394
- const callMethod = t . callExpression (
395
- t . memberExpression (
396
- t . thisExpression ( ) ,
397
- t . identifier ( method )
398
- ) ,
399
- [ 'post' , 'put' , 'patch' , 'formData' ] . includes ( method ) ?
400
- [
401
- t . identifier ( 'path' ) ,
402
- t . memberExpression (
403
- t . identifier ( 'params' ) ,
404
- t . identifier ( 'body' )
405
- )
406
- ] : [
407
- t . identifier ( 'path' )
408
- ]
409
- ) ;
410
- callMethod . typeParameters = t . tsTypeParameterInstantiation ( [
411
- returnType
412
- ] ) ;
413
- const methodFunction = t . classMethod (
414
- 'method' ,
415
- t . identifier ( methodName ) ,
416
- params ,
417
- t . blockStatement ( [
418
- t . variableDeclaration ( 'const' , [
419
- t . variableDeclarator (
420
- t . identifier ( 'path' ) ,
421
- createPathTemplateLiteral ( options , path )
422
- )
423
- ] ) ,
424
- t . returnStatement (
425
- t . awaitExpression (
426
- callMethod
427
- )
428
- )
429
- ] ) ,
430
- false ,
431
- false ,
432
- false ,
433
- true
434
- ) ;
435
- methodFunction . returnType = t . tsTypeAnnotation (
436
- t . tsTypeReference (
437
- t . identifier ( 'Promise' ) ,
438
- t . tsTypeParameterInstantiation ( [
439
- returnType
440
- ] )
441
- )
442
- ) ;
443
- methods . push ( methodFunction ) ;
486
+ const alias = options . operationNamingStrategy ?. aliases ?. [ operation . operationId ] ;
487
+ if ( alias ) {
488
+ methods . push ( createOperation ( options , operation , path , method , alias ) ) ;
489
+ }
490
+ methods . push ( createOperation ( options , operation , path , method ) ) ;
444
491
}
445
492
446
493
} ) ;
@@ -476,7 +523,10 @@ export const getSwaggerJSONMethod = (): t.ClassMethod => {
476
523
) ;
477
524
} ;
478
525
479
- export function generateOpenApiClient ( options : OpenAPIOptions , schema : OpenAPISpec ) : string {
526
+ export function generateOpenApiClient (
527
+ options : OpenAPIOptions ,
528
+ schema : OpenAPISpec
529
+ ) : string {
480
530
const methods = [ ] ;
481
531
if ( options . includeSwaggerUrl ) {
482
532
methods . push ( getSwaggerJSONMethod ( ) ) ;
@@ -498,21 +548,21 @@ export function generateOpenApiClient(options: OpenAPIOptions, schema: OpenAPISp
498
548
] ) ;
499
549
500
550
const clientClass = t . exportNamedDeclaration ( t . classDeclaration (
501
- t . identifier ( 'KubernetesClient' ) ,
551
+ t . identifier ( options . clientName ) ,
502
552
t . identifier ( 'APIClient' ) ,
503
553
classBody ,
504
554
[ ]
505
555
) ) ;
506
556
507
557
//// INTERFACES
508
- const kubeSchema = {
509
- title : 'Kubernetes' ,
558
+ const apiSchema = {
559
+ title : options . clientName ,
510
560
definitions : schema . definitions
511
561
} ;
512
562
513
- const types = generateTypeScriptTypes ( kubeSchema , {
563
+ const types = generateTypeScriptTypes ( apiSchema , {
514
564
...( options as any ) ,
515
- exclude : [ 'Kubernetes' , ...( options . exclude ?? [ ] ) ]
565
+ exclude : [ options . clientName , ...( options . exclude ?? [ ] ) ]
516
566
} ) ;
517
567
const openApiTypes = generateOpenApiTypes ( options , schema ) ;
518
568
0 commit comments