Skip to content

Commit

Permalink
feat: 优化配置,将更多选项移至 SharedConfig,getRequestFunctionName 现在拥有默认值
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Aug 19, 2019
1 parent c9b5d82 commit bdb9b3c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
10 changes: 6 additions & 4 deletions src/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,12 @@ export class Generator {
...interfaceInfo,
parsedPath: path.parse(interfaceInfo.path),
}
const requestFunctionName = await syntheticalConfig.getRequestFunctionName!(
extendedInterfaceInfo,
changeCase,
)
const requestFunctionName = isFunction(syntheticalConfig.getRequestFunctionName)
? await syntheticalConfig.getRequestFunctionName(
extendedInterfaceInfo,
changeCase,
)
: changeCase.camelCase(interfaceInfo.parsedPath.name)
const requestDataTypeName = isFunction(syntheticalConfig.getRequestDataTypeName)
? await syntheticalConfig.getRequestDataTypeName(
extendedInterfaceInfo,
Expand Down
57 changes: 32 additions & 25 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export interface SharedConfig {
* @default false
*/
typesOnly?: boolean,

/**
* 测试环境名称。
*
Expand All @@ -234,6 +235,7 @@ export interface SharedConfig {
* @example 'dev'
*/
devEnvName?: string,

/**
* 生产环境名称。
*
Expand All @@ -244,6 +246,7 @@ export interface SharedConfig {
* @example 'prod'
*/
prodEnvName?: string,

/**
* 输出文件路径。
*
Expand All @@ -252,28 +255,15 @@ export interface SharedConfig {
* @example 'src/api/index.ts'
*/
outputFilePath?: string,

/**
* 请求函数文件路径。
*
* @default 与 `outputFilePath` 同级目录下的 `request.ts` 文件
* @example 'src/api/request.ts'
*/
requestFunctionFilePath?: string,
/**
* 预处理接口信息,返回新的接口信息。
*
* 譬如你想对接口的 `path` 进行某些处理,就可使用该方法。
*
* @example
*
* ```js
* interfaceInfo => {
* interfaceInfo.path = interfaceInfo.path.replace('v1', 'v2')
* return interfaceInfo
* }
* ```
*/
preproccessInterface?: <T extends Interface>(interfaceInfo: T, changeCase: ChangeCase) => T,

/**
* 如果接口响应的结果是 `JSON` 对象,
* 且我们想要的数据在该对象下,
Expand All @@ -286,29 +276,32 @@ export interface SharedConfig {
* @example 'data'
*/
dataKey?: string,
}

/**
* 分类的配置。
*/
export interface CategoryConfig extends SharedConfig {
/**
* 分类 ID,可以设置多个。设为 `0` 时表示全部分类
* 预处理接口信息,返回新的接口信息
*
* 获取方式:打开项目 --> 点开分类 --> 复制浏览器地址栏 `/api/cat_` 后面的数字
* 譬如你想对接口的 `path` 进行某些处理,就可使用该方法
*
* @example 20
* @example
*
* ```js
* interfaceInfo => {
* interfaceInfo.path = interfaceInfo.path.replace('v1', 'v2')
* return interfaceInfo
* }
* ```
*/
id: number | number[],
preproccessInterface?(interfaceInfo: Interface, changeCase: ChangeCase): Interface,

/**
* 获取请求函数的名称。
*
* @default changeCase.camelCase(interfaceInfo.parsedPath.name)
* @param interfaceInfo 接口信息
* @param changeCase 常用的大小写转换函数集合对象
* @returns 请求函数的名称
*/
getRequestFunctionName(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string,
getRequestFunctionName?(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string,

/**
* 获取请求数据类型的名称。
Expand All @@ -331,6 +324,20 @@ export interface CategoryConfig extends SharedConfig {
getResponseDataTypeName?(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string,
}

/**
* 分类的配置。
*/
export interface CategoryConfig extends SharedConfig {
/**
* 分类 ID,可以设置多个。设为 `0` 时表示全部分类。
*
* 获取方式:打开项目 --> 点开分类 --> 复制浏览器地址栏 `/api/cat_` 后面的数字。
*
* @example 20
*/
id: number | number[],
}

/**
* 项目的配置。
*/
Expand Down

0 comments on commit bdb9b3c

Please sign in to comment.