Skip to content

Commit

Permalink
feat: 请求载荷新增 allData 表示所有数据
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Sep 24, 2020
1 parent 4bedbd9 commit d881969
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
42 changes: 21 additions & 21 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# https://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

# The JSON files contain newlines inconsistently
[*.json]
insert_final_newline = ignore

# Minified JavaScript files shouldn't be changed
[**.min.js]
indent_style = ignore
insert_final_newline = ignore

[*.md]
# https://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

# The JSON files contain newlines inconsistently
[*.json]
insert_final_newline = ignore

# Minified JavaScript files shouldn't be changed
[**.min.js]
indent_style = ignore
insert_final_newline = ignore

[*.md]
trim_trailing_whitespace = false
8 changes: 7 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export function prepare(
): RequestFunctionParams {
let requestPath: string = requestConfig.path
const { data, fileData } = parseRequestData(requestData)
if (data != null && typeof data === 'object' && !Array.isArray(data)) {
const dataIsObject =
data != null && typeof data === 'object' && !Array.isArray(data)
if (dataIsObject) {
// 替换路径参数
if (
Array.isArray(requestConfig.paramNames) &&
Expand Down Expand Up @@ -121,5 +123,9 @@ export function prepare(
data: data,
hasFileData: fileData && Object.keys(fileData).length > 0,
fileData: fileData,
allData: {
...(dataIsObject ? data : {}),
...fileData,
},
}
}
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,13 @@ export interface RequestConfig<
*/
export interface RequestFunctionParams extends RequestConfig {
/** 请求数据,不含文件数据 */
data: any
data: Record<string, any>
/** 是否有文件数据 */
hasFileData: boolean
/** 请求文件数据 */
fileData: Record<string, any>
/** 所有请求数据,包括 data、fileData */
allData: Record<string, any>
}

/** 请求函数的额外参数 */
Expand Down
18 changes: 18 additions & 0 deletions tests/__snapshots__/helpers.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

exports[`prepare 支持解析对象请求体: 对象 1`] = `
Object {
"allData": Object {
"a": 1,
"b": "2",
},
"data": Object {
"a": 1,
"b": "2",
Expand All @@ -14,6 +18,9 @@ Object {

exports[`prepare 支持解析带参路径: 路径参数 1 1`] = `
Object {
"allData": Object {
"b": "2",
},
"data": Object {
"b": "2",
},
Expand All @@ -29,6 +36,9 @@ Object {

exports[`prepare 支持解析带参路径: 路径参数 2 1`] = `
Object {
"allData": Object {
"b": "2",
},
"data": Object {
"b": "2",
},
Expand All @@ -44,6 +54,9 @@ Object {

exports[`prepare 支持解析带参路径: 路径参数 3 - 全部替换 1`] = `
Object {
"allData": Object {
"b": "2",
},
"data": Object {
"b": "2",
},
Expand All @@ -59,6 +72,7 @@ Object {

exports[`prepare 支持解析非对象请求体: 布尔值 1`] = `
Object {
"allData": Object {},
"data": true,
"fileData": Object {},
"hasFileData": false,
Expand All @@ -68,6 +82,7 @@ Object {

exports[`prepare 支持解析非对象请求体: 数组 1`] = `
Object {
"allData": Object {},
"data": Array [
1,
2,
Expand All @@ -84,6 +99,9 @@ Object {

exports[`prepare 支持追加查询参数 1`] = `
Object {
"allData": Object {
"b": "2",
},
"data": Object {
"b": "2",
},
Expand Down

0 comments on commit d881969

Please sign in to comment.