1
+ import type { Ora } from 'ora' ;
2
+ import process from 'node:process' ;
1
3
import { fileHeader } from '@openapi-qraft/plugin/lib/fileHeader' ;
2
4
import {
3
5
createPredefinedParametersGlobs ,
@@ -7,6 +9,7 @@ import { QraftCommand } from '@openapi-qraft/plugin/lib/QraftCommand';
7
9
import { QraftCommandPlugin } from '@openapi-qraft/plugin/lib/QraftCommandPlugin' ;
8
10
import { Option } from 'commander' ;
9
11
import { generateCode } from './generateCode.js' ;
12
+ import { getAllAvailableCallbackNames } from './ts-factory/getCallbackNames.js' ;
10
13
11
14
export const plugin : QraftCommandPlugin = {
12
15
setupCommand ( command : QraftCommand ) {
@@ -31,7 +34,17 @@ export const plugin: QraftCommandPlugin = {
31
34
return arg ?. toLowerCase ( ) !== 'false' ;
32
35
}
33
36
)
37
+ . addOption (
38
+ new Option (
39
+ '--default-client-callbacks <callbacks...>' ,
40
+ 'List of default API client methods and hooks that will be available by default. These can be overridden at runtime if needed..'
41
+ )
42
+ . choices ( [ 'all' , 'none' , ...getAllAvailableCallbackNames ( ) ] )
43
+ . default ( [ 'all' ] )
44
+ )
34
45
. action ( async ( { spinner, output, args, services, schema } , resolve ) => {
46
+ validateDefaultCallbacks ( args . defaultClientCallbacks , spinner ) ;
47
+
35
48
return void ( await generateCode ( {
36
49
spinner,
37
50
services,
@@ -44,6 +57,9 @@ export const plugin: QraftCommandPlugin = {
44
57
explicitImportExtensions : args . explicitImportExtensions ,
45
58
servicesDirName : 'services' ,
46
59
exportSchemaTypes : args . exportOpenapiTypes ,
60
+ defaultClientCallbacks : args . defaultClientCallbacks
61
+ . map ( ( callbackName : string ) => callbackName . trim ( ) )
62
+ . filter ( Boolean ) ,
47
63
operationPredefinedParameters : args . operationPredefinedParameters
48
64
? createPredefinedParametersGlobs (
49
65
schema ,
@@ -57,3 +73,20 @@ export const plugin: QraftCommandPlugin = {
57
73
} ) ;
58
74
} ,
59
75
} ;
76
+
77
+ function validateDefaultCallbacks (
78
+ defaultCallbacks : string [ ] ,
79
+ spinner : Ora
80
+ ) : asserts defaultCallbacks is string [ ] | [ 'all' ] | [ 'none' ] {
81
+ if ( ! defaultCallbacks || defaultCallbacks . length === 0 ) return ;
82
+
83
+ const hasSpecialValue = defaultCallbacks . some (
84
+ ( cb ) => cb === 'all' || cb === 'none'
85
+ ) ;
86
+ if ( hasSpecialValue && defaultCallbacks . length > 1 ) {
87
+ spinner . fail (
88
+ `When using "all" or "none" as a callback value, no other callbacks should be specified.`
89
+ ) ;
90
+ process . exit ( 1 ) ;
91
+ }
92
+ }
0 commit comments