11import type { ApiPipeline } from '@genapi/shared'
2- import { codeToAstNode , createComment , createFunction , createImport , createVariable } from 'ts-factory-extra'
2+ import {
3+ genComment ,
4+ genFunction ,
5+ genImport ,
6+ genVariable ,
7+ } from 'knitwork-x'
38
4- import { factory , NodeFlags } from 'typescript'
59import { compilerTsTypingsDeclaration } from './typings'
610
7- const varFlags = {
8- let : NodeFlags . Let ,
9- const : NodeFlags . Const ,
10- var : NodeFlags . None ,
11- }
12- // @ts -check
13- export function compilerTsRequestDeclaration ( configRead : ApiPipeline . ConfigRead ) {
11+ /**
12+ * Compiles configRead graphs to request code string using knitwork-x.
13+ */
14+ export function compilerTsRequestDeclaration ( configRead : ApiPipeline . ConfigRead ) : string {
1415 configRead . graphs . imports = configRead . graphs . imports || [ ]
1516 configRead . graphs . comments = configRead . graphs . comments || [ ]
1617 configRead . graphs . variables = configRead . graphs . variables || [ ]
@@ -19,46 +20,64 @@ export function compilerTsRequestDeclaration(configRead: ApiPipeline.ConfigRead)
1920 const isGenerateType = configRead . outputs . some ( v => v . type === 'typings' )
2021 const isTypescript = configRead . outputs . some ( v => v . type === 'request' && v . path . endsWith ( '.ts' ) )
2122
22- const comments = [
23- createComment ( 'multi' , configRead . graphs . comments ) ,
24- ]
25- const imports = configRead . graphs . imports ?. map ( ( item ) => {
26- return createImport ( item . name , item . names , item . value , item . namespace )
23+ const sections : string [ ] = [ ]
24+
25+ // Comments
26+ if ( configRead . graphs . comments . length > 0 ) {
27+ sections . push ( genComment ( configRead . graphs . comments . join ( '\n' ) , { block : true } ) )
28+ }
29+
30+ // Imports
31+ const importLines = configRead . graphs . imports . map ( ( item ) => {
32+ if ( item . namespace )
33+ return genImport ( item . value , { name : '*' , as : item . name ! } , { type : ! ! item . type } )
34+ if ( item . name && ! item . names )
35+ return genImport ( item . value , item . name , { type : ! ! item . type } )
36+ if ( item . name && item . names ?. length ) {
37+ const imports = [ { name : 'default' , as : item . name } , ...item . names . map ( n => ( { name : n } ) ) ]
38+ return genImport ( item . value , imports , { type : ! ! item . type } )
39+ }
40+ return genImport ( item . value , item . names || [ ] , { type : ! ! item . type } )
2741 } )
28- const variables = configRead . graphs . variables . map ( ( item ) => {
29- // eslint-disable-next-line ts/ban-ts-comment
30- // @ts -expect-error
31- return createVariable ( item . export , varFlags [ item . flag ] , item . name , item . value )
42+ if ( importLines . length > 0 )
43+ sections . push ( importLines . join ( '\n' ) )
44+
45+ // Variables
46+ const variableLines = configRead . graphs . variables . map ( ( item ) => {
47+ return genVariable ( item . name , item . value ?? '' , {
48+ export : ! ! item . export ,
49+ kind : item . flag ,
50+ } )
3251 } )
33- const functions = configRead . graphs . functions . flatMap ( ( item ) => {
34- return createFunction ( {
52+ if ( variableLines . length > 0 )
53+ sections . push ( variableLines . join ( '\n' ) )
54+
55+ // Functions
56+ const functionLines = configRead . graphs . functions . map ( ( item ) => {
57+ return genFunction ( {
3558 export : true ,
36- comment : item . description ,
3759 name : item . name ,
38- parameters : item . parameters ,
39- body : item . body ?. map ( codeToAstNode ) ,
60+ parameters : ( item . parameters || [ ] ) . map ( p => ( {
61+ name : p . name ,
62+ type : p . type ,
63+ optional : ! p . required ,
64+ } ) ) ,
65+ body : item . body || [ ] ,
4066 async : item . async ,
4167 returnType : item . returnType ,
4268 generics : item . generics ,
4369 generator : item . generator ,
70+ jsdoc : item . description ,
4471 } )
4572 } )
73+ if ( functionLines . length > 0 )
74+ sections . push ( functionLines . join ( '\n\n' ) )
4675
47- const nodes = [
48- ...comments ,
49- factory . createIdentifier ( '' ) ,
50- ...imports ,
51- factory . createIdentifier ( '' ) ,
52- ...variables ,
53- factory . createIdentifier ( '' ) ,
54- ...functions ,
55- ]
56-
76+ // Inline typings for TS request files when not generating separate typings
5777 if ( ! isGenerateType && isTypescript ) {
58- nodes . push ( factory . createIdentifier ( '' ) )
59- nodes . push ( factory . createIdentifier ( '' ) )
60- nodes . push ( ...compilerTsTypingsDeclaration ( configRead , false ) )
78+ sections . push ( '' )
79+ sections . push ( compilerTsTypingsDeclaration ( configRead , false ) )
6180 }
6281
63- return nodes
82+ return sections . filter ( Boolean ) . join ( '\n\n' )
6483}
0 commit comments