Skip to content

Commit

Permalink
feat: 新增 queryStringArrayFormat 配置项支持配置查询字符串数组格式化方式 (close: #71)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Mar 23, 2022
1 parent 8b0cde8 commit 485d6ed
Show file tree
Hide file tree
Showing 8 changed files with 1,127 additions and 41 deletions.
26 changes: 26 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,32 @@ ytt 内置了一个映射表,此配置会进行追加(相同覆盖):
}
```

### queryStringArrayFormat <Badge>3.34.0+</Badge>

- 类型: `'brackets' | 'indices' | 'repeat' | 'comma' | 'json'`
- 默认值: `'brackets'`
- 说明:

查询字符串数组格式化方式。见 [path](./request.html#path) 的说明,查询参数会被序列化进接口路径,可通过此选项配置如何格式化数组值。

| 格式化方式 | 说明 | 示例 |
| ---------- | -------------- | --------------- |
| `brackets` | 索引(不带数字) | `a[]=b&a[]=c` |
| `indices` | 索引(带数字) | `a[0]=b&a[1]=c` |
| `repeat` | 重复 | `a=b&a=c` |
| `comma` | 逗号分隔 | `a=b,c` |
| `json` | JSON 字符串 | `a=["b","c"]` |

用法:

```typescript
import { QueryStringArrayFormat } from 'yapi-to-typescript'

export default defineConfig({
queryStringArrayFormat: QueryStringArrayFormat.brackets,
})
```

### setRequestFunctionExtraInfo <Badge>3.27.0+</Badge>

- 类型: `(interfaceInfo: Interface, changeCase: ChangeCase): Record<string, any>`
Expand Down
7 changes: 6 additions & 1 deletion src/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
Method,
Project,
ProjectConfig,
QueryStringArrayFormat,
RequestBodyType,
ServerConfig,
SyntheticalConfig,
Expand Down Expand Up @@ -480,7 +481,7 @@ export class Generator {
: dedent`
// @ts-ignore
// prettier-ignore
import { Method, RequestBodyType, ResponseBodyType, FileData, prepare } from 'yapi-to-typescript'
import { QueryStringArrayFormat, Method, RequestBodyType, ResponseBodyType, FileData, prepare } from 'yapi-to-typescript'
// @ts-ignore
// prettier-ignore
import type { RequestConfig, RequestFunctionRestArgs } from 'yapi-to-typescript'
Expand Down Expand Up @@ -938,6 +939,10 @@ export class Generator {
: {},
)},
requestFunctionName: ${JSON.stringify(requestFunctionName)},
queryStringArrayFormat: QueryStringArrayFormat.${
syntheticalConfig.queryStringArrayFormat ||
QueryStringArrayFormat.brackets
},
extraInfo: ${JSON.stringify(requestFunctionExtraInfo)},
}
Expand Down
46 changes: 42 additions & 4 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { AppendOptions } from 'form-data'
import type {
import {
CliHooks,
Config,
ConfigWithHooks,
QueryStringArrayFormat,
RequestConfig,
RequestFunctionParams,
} from './types'
import type { AppendOptions } from 'form-data'

/**
* 定义配置。
Expand Down Expand Up @@ -99,6 +100,41 @@ export function parseRequestData(requestData?: any): {
return result
}

const queryStringify = (
key: string,
value: any,
arrayFormat: QueryStringArrayFormat,
): string => {
let str = ''
if (value != null) {
if (!Array.isArray(value)) {
str = `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
} else if (arrayFormat === QueryStringArrayFormat.indices) {
str = value
.map(
(v, i) =>
`${encodeURIComponent(`${key}[${i}]`)}=${encodeURIComponent(v)}`,
)
.join('&')
} else if (arrayFormat === QueryStringArrayFormat.repeat) {
str = value
.map(v => `${encodeURIComponent(key)}=${encodeURIComponent(v)}`)
.join('&')
} else if (arrayFormat === QueryStringArrayFormat.comma) {
str = `${encodeURIComponent(key)}=${encodeURIComponent(value.join(','))}`
} else if (arrayFormat === QueryStringArrayFormat.json) {
str = `${encodeURIComponent(key)}=${encodeURIComponent(
JSON.stringify(value),
)}`
} else {
str = value
.map(v => `${encodeURIComponent(`${key}[]`)}=${encodeURIComponent(v)}`)
.join('&')
}
}
return str
}

/**
* 准备要传给请求函数的参数。
*/
Expand Down Expand Up @@ -136,9 +172,11 @@ export function prepare(
Object.keys(data).forEach(key => {
if (requestConfig.queryNames.indexOf(key) >= 0) {
if (data[key] != null) {
queryString += `${queryString ? '&' : ''}${encodeURIComponent(
queryString += `${queryString ? '&' : ''}${queryStringify(
key,
)}=${encodeURIComponent(data[key])}`
data[key],
requestConfig.queryStringArrayFormat,
)}`
}
delete data[key]
}
Expand Down
23 changes: 23 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ export enum ResponseBodyType {
// jsonSchema = 'json-schema',
}

/** 查询字符串数组格式化方式 */
export enum QueryStringArrayFormat {
/** 示例: `a[]=b&a[]=c` */
'brackets' = 'brackets',
/** 示例: `a[0]=b&a[1]=c` */
'indices' = 'indices',
/** 示例: `a=b&a=c` */
'repeat' = 'repeat',
/** 示例: `a=b,c` */
'comma' = 'comma',
/** 示例: `a=["b","c"]` */
'json' = 'json',
}

/** 接口定义 */
export interface Interface {
/** 接口 ID */
Expand Down Expand Up @@ -524,6 +538,13 @@ export interface SharedConfig {
*/
customTypeMapping?: Record<string, JSONSchema4TypeName>

/**
* 如何格式化查询字符串中的数组值。
*
* @default QueryStringArrayFormat.brackets
*/
queryStringArrayFormat?: QueryStringArrayFormat

/**
* 设置传给请求函数的参数中的 extraInfo 的值。
*
Expand Down Expand Up @@ -715,6 +736,8 @@ export interface RequestConfig<
responseDataJsonSchema: JSONSchema4
/** 请求函数名称 */
requestFunctionName: string
/** 如何格式化查询字符串中的数组值 */
queryStringArrayFormat: QueryStringArrayFormat
/** 额外信息 */
extraInfo: Record<string, any>
}
Expand Down
Loading

0 comments on commit 485d6ed

Please sign in to comment.