@@ -12,6 +12,11 @@ import { getStorage, setStorage } from '../../storage';
1212import { getPkgInfo , loadModule , parseConfig , parseOptions } from '../../utils' ;
1313import { API_METHOD_SYMBOL , API_TYPE_SYMBOL , template } from './template' ;
1414
15+ /**
16+ * 过滤无效的路由
17+ *
18+ * TODO: 暂时没有无效的定义, 留个口子
19+ */
1520function filterValidRouter ( routerInfoMap : Record < string , RouterInfo > ) {
1621 const validRouterInfoMap : Record < string , RouterInfo > = { } ;
1722
@@ -31,6 +36,9 @@ export type MethodTypeInfo = {
3136 params : string [ ] ;
3237} & Partial < Record < RequestMethod , TypeInfo > > ;
3338
39+ /**
40+ * 获取 dataKey 对应的字段的名称
41+ */
3442function getDataKeyName ( key : string ) : DataKey {
3543 switch ( key ) {
3644 case 'params' :
@@ -45,6 +53,9 @@ function getDataKeyName(key: string): DataKey {
4553 }
4654}
4755
56+ /**
57+ * 通过字符串路径获取 params
58+ */
4859function getRouterParams ( path : string ) {
4960 const splitPath = path . split ( '/' ) ;
5061 const params : string [ ] = [ ] ;
@@ -56,13 +67,19 @@ function getRouterParams(path: string) {
5667 return params ;
5768}
5869
70+ /**
71+ * 获取 dataKey 对应的 schema
72+ */
5973function getRouterDataSchema ( dataKey : string , schema : RouterDataSchema ) {
6074 if ( dataKey === 'response' ) {
6175 return schema . response ?. [ '200' ] || schema . response ?. default || { } ;
6276 }
6377 return schema [ dataKey ] ;
6478}
6579
80+ /**
81+ * 解析 methodSchema 中对应 dataKey 的类型
82+ */
6683function parseRouterDataSchema ( methodSchema : RouterDataSchema ) {
6784 const typeInfo : TypeInfo = { } ;
6885 for ( const dataKey in methodSchema ) {
@@ -73,6 +90,9 @@ function parseRouterDataSchema(methodSchema: RouterDataSchema) {
7390 return typeInfo ;
7491}
7592
93+ /**
94+ * 解析接口 methodSchema
95+ */
7696function parseMethodSchema ( schemaMap : RouterSchema ) {
7797 const methodTypeInfo : Partial < Record < RequestMethod , TypeInfo > > = { } ;
7898 for ( const method in schemaMap ) {
@@ -82,6 +102,9 @@ function parseMethodSchema(schemaMap: RouterSchema) {
82102 return methodTypeInfo ;
83103}
84104
105+ /**
106+ * 将 routerInfoMap 转换为 typeInfoList
107+ */
85108function parseTypeInfo ( routerInfoMap : Record < string , RouterInfo > ) {
86109 const typeInfoList : MethodTypeInfo [ ] = [ ] ;
87110 for ( const stringPath in routerInfoMap ) {
@@ -96,11 +119,17 @@ function parseTypeInfo(routerInfoMap: Record<string, RouterInfo>) {
96119 return typeInfoList ;
97120}
98121
122+ /**
123+ * 获取请求函数的函数名
124+ */
99125function getMethodName ( method : string , path : string ) {
100126 const splitPath = path . split ( '/' ) . filter ( item => item && ! item . startsWith ( ':' ) ) ;
101127 return camelCase ( `${ method } /${ splitPath . join ( '/' ) } ` ) ;
102128}
103129
130+ /**
131+ * 获取请求函数的入参类型
132+ */
104133function getMethodOptionsType ( typeInfo : TypeInfo ) {
105134 const optionsType : string [ ] = [ ] ;
106135 for ( const dataKey in typeInfo ) {
@@ -115,6 +144,11 @@ interface APIInfo {
115144 request : string ;
116145}
117146
147+ /**
148+ * 将 typeInfo 解析为 api 信息
149+ *
150+ * 包含类型和请求方法
151+ */
118152function parseAPIInfo ( typeInfoList : MethodTypeInfo [ ] ) {
119153 const apiInfoList : APIInfo [ ] = [ ] ;
120154 typeInfoList . forEach ( ( item ) => {
@@ -132,6 +166,9 @@ function parseAPIInfo(typeInfoList: MethodTypeInfo[]) {
132166 return apiInfoList ;
133167}
134168
169+ /**
170+ * 解析 api 信息为实际代码内容
171+ */
135172function parseAPIContentInfo ( apiInfo : APIInfo [ ] ) {
136173 const { type, method } = apiInfo . reduce < { type : string [ ] ; method : string [ ] } > ( ( prev , cur ) => {
137174 prev . type . push ( cur . type ) ;
@@ -144,14 +181,22 @@ function parseAPIContentInfo(apiInfo: APIInfo[]) {
144181 } ;
145182}
146183
184+ /**
185+ * 生成前端请求接口的代码文件
186+ */
147187function generateAPIFile ( contentInfo : { type : string ; method : string } ) {
148188 return template . replace ( API_TYPE_SYMBOL , contentInfo . type ) . replace ( API_METHOD_SYMBOL , contentInfo . method ) ;
149189}
150190
191+ /**
192+ * 根据 router-schema 生成前端请求接口的代码文件
193+ */
151194export async function generateRequestScriptCli ( ) {
195+ // 禁用日志输出, 因为会载入代码收集依赖, 所以需要将 console 也进行代理, 屏蔽常用输出
152196 setStorage ( 'disabledConsola' , true ) ;
153197 globalThis . console = new Proxy ( { } , { get : ( ) => noop } ) as Console ;
154198
199+ // 初始化应用
155200 const { port, sourceDir } = await parseConfig ( ) ;
156201 const { pkgPath } = await getPkgInfo ( ) ;
157202 const devOptions = { pkgPath, sourceDir, port, isCli : true } ;
@@ -165,6 +210,7 @@ export async function generateRequestScriptCli() {
165210
166211 await loadModule ( app , router , options ) ;
167212
213+ // 处理文件依赖信息, 生成前端请求文件
168214 const routerInfoMap = getStorage ( 'routerInfoMap' ) ;
169215 const validRouterInfoMap = filterValidRouter ( routerInfoMap ) ;
170216
0 commit comments