Skip to content

Commit

Permalink
feat: 将查询参数序列号进请求路径中 (close: #34, close: #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Aug 5, 2020
1 parent 2787d51 commit ac43e58
Show file tree
Hide file tree
Showing 7 changed files with 651 additions and 56 deletions.
7 changes: 7 additions & 0 deletions src/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,11 @@ export class Generator {
const paramNamesLiteral = JSON.stringify(paramNames)
const paramNameType = paramNames.length === 0 ? 'string' : `'${paramNames.join('\' | \'')}'`

// 支持查询参数
const queryNames = (interfaceInfo.req_query /* istanbul ignore next */ || []).map(item => item.name)
const queryNamesLiteral = JSON.stringify(queryNames)
const queryNameType = queryNames.length === 0 ? 'string' : `'${queryNames.join('\' | \'')}'`

// 转义标题中的 /
const escapedTitle = String(interfaceInfo.title).replace(/\//g, '\\/')

Expand Down Expand Up @@ -669,6 +674,7 @@ export class Generator {
${JSON.stringify(interfaceInfo.path)},
${JSON.stringify(syntheticalConfig.dataKey)},
${paramNameType},
${queryNameType},
${JSON.stringify(isRequestDataOptional)}
>>
Expand All @@ -687,6 +693,7 @@ export class Generator {
responseBodyType: ResponseBodyType.${interfaceInfo.res_body_type},
dataKey: dataKey${categoryUID},
paramNames: ${paramNamesLiteral},
queryNames: ${queryNamesLiteral},
requestDataOptional: ${JSON.stringify(isRequestDataOptional)},
}
Expand Down
41 changes: 30 additions & 11 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,37 @@ export function parseRequestData(requestData?: any): { data: any, fileData: any
* 准备要传给请求函数的参数。
*/
export function prepare(requestConfig: RequestConfig, requestData: any): RequestFunctionParams {
let requestPath = requestConfig.path
let requestPath: string = requestConfig.path
const {data, fileData} = parseRequestData(requestData)
if (Array.isArray(requestConfig.paramNames) && requestConfig.paramNames.length > 0 && data != null && typeof data === 'object' && !Array.isArray(data)) {
Object.keys(data).forEach(key => {
if (requestConfig.paramNames.indexOf(key) >= 0) {
// ref: https://github.com/YMFE/yapi/blob/master/client/containers/Project/Interface/InterfaceList/InterfaceEditForm.js#L465
requestPath = requestPath
.replace(new RegExp(`\\{${key}\\}`, 'g'), data[key])
.replace(new RegExp(`/:${key}(?=/|$)`, 'g'), `/${data[key]}`)
delete data[key]
}
})
if (data != null && typeof data === 'object' && !Array.isArray(data)) {
// 替换路径参数
if (Array.isArray(requestConfig.paramNames) && requestConfig.paramNames.length > 0) {
Object.keys(data).forEach(key => {
if (requestConfig.paramNames.indexOf(key) >= 0) {
// ref: https://github.com/YMFE/yapi/blob/master/client/containers/Project/Interface/InterfaceList/InterfaceEditForm.js#L465
requestPath = requestPath
.replace(new RegExp(`\\{${key}\\}`, 'g'), data[key])
.replace(new RegExp(`/:${key}(?=/|$)`, 'g'), `/${data[key]}`)
delete data[key]
}
})
}

// 追加查询参数到路径上
let queryString = ''
if (Array.isArray(requestConfig.queryNames) && requestConfig.queryNames.length > 0) {
Object.keys(data).forEach(key => {
if (requestConfig.queryNames.indexOf(key) >= 0) {
if (data[key] != null) {
queryString += `${(queryString ? '&' : '')}${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`
}
delete data[key]
}
})
}
if (queryString) {
requestPath += `${(requestPath.indexOf('?') > -1 ? '&' : '?')}${queryString}`
}
}
return {
...requestConfig,
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ export interface RequestConfig<
Path extends string = any,
DataKey extends string | undefined = any,
ParamName extends string = any,
QueryName extends string = any,
RequestDataOptional extends boolean = any,
> {
/** 接口 Mock 地址,结尾无 `/` */
Expand All @@ -486,6 +487,8 @@ export interface RequestConfig<
dataKey: DataKey,
/** 路径参数的名称列表 */
paramNames: ParamName[],
/** 查询参数的名称列表 */
queryNames: QueryName[],
/** 请求数据是否可选 */
requestDataOptional: RequestDataOptional,
}
Expand Down
Loading

0 comments on commit ac43e58

Please sign in to comment.