优化表单解析, 支持多文件数组上传

master 2.1.6
yutent 2023-06-25 19:10:25 +08:00
parent 4d97503ee0
commit c286cdf2f2
5 changed files with 36 additions and 21 deletions

3
.gitignore vendored
View File

@ -1,6 +1,9 @@
*.min.js
*.min.css
demo.html
.httpserver
node_modules/
dist/

View File

@ -1,6 +1,6 @@
{
"name": "@bytedo/fetch",
"version": "2.1.5",
"version": "2.1.6",
"description": "全新的ajax封装。分2个版本, 一个基于XMLHttpRequest, 一个基于window.fetch",
"main": "dist/index.js",
"files": [

View File

@ -4,7 +4,7 @@
* @date 2020/08/03 17:05:10
*/
import { Format, toS } from './lib/format.js'
import { Format, getType } from './lib/format.js'
const NOBODY_METHODS = ['GET', 'HEAD']
const FORM_TYPES = {
@ -127,14 +127,19 @@ class _Request {
params = options.body
} else {
for (let k in options.body) {
if (
toS.call(options.body[k]) === '[object File]' ||
toS.call(options.body[k]) === '[object Blob]'
) {
hasAttach = true
break
if (Array.isArray(options.body[k]) || getType(options.body[k]) === 'FileList') {
options.body[k] = Array.from(options.body[k])
hasAttach = options.body[k].some(
it => getType(it) === 'File' || getType(it) === 'Blob'
)
} else {
if (getType(options.body[k]) === 'File' || getType(options.body[k]) === 'Blob') {
hasAttach = true
break
}
}
}
// 有附件,则改为FormData
if (hasAttach) {
if (noBody) {
@ -289,13 +294,13 @@ class _Request {
if (this._owner._inject_res) {
response = this._owner._inject_res(response)
_type = toS.call(response)
_type = getType(it)(response)
}
if (isSucc) {
this.defer.resolve(response)
} else {
if (_type === '[object Promise]') {
if (_type === 'Promise') {
return response.then(_ => this.defer.reject(_)).catch(_ => this.defer.reject(_))
} else {
this.defer.reject(response)

View File

@ -5,10 +5,12 @@
*
*/
export const toS = Object.prototype.toString
export const encode = encodeURIComponent
export const decode = decodeURIComponent
export function getType(val) {
return Object.prototype.toString.call(val).slice(8, -1)
}
/**
* 表单序列化
*/
@ -103,9 +105,10 @@ export const Format = {
if (/native code/.test(v)) {
return
}
let _type = getType(v)
v = typeof v === 'function' ? v() : v
v = toS.call(v) === '[object File]' || toS.call(v) === '[object Blob]' ? v : encode(v)
v = _type === 'File' || _type === 'Blob' ? v : encode(v)
arr.push(encode(k) + '=' + v)
}

View File

@ -4,7 +4,7 @@
* @date 2020/07/31 18:59:47
*/
import { Format, toS } from './lib/format.js'
import { Format, getType } from './lib/format.js'
const nativeFetch = window.fetch
const NOBODY_METHODS = ['GET', 'HEAD']
@ -104,12 +104,16 @@ class _Request {
}
} else {
for (let k in options.body) {
if (
toS.call(options.body[k]) === '[object File]' ||
toS.call(options.body[k]) === '[object Blob]'
) {
hasAttach = true
break
if (Array.isArray(options.body[k]) || getType(options.body[k]) === 'FileList') {
options.body[k] = Array.from(options.body[k])
hasAttach = options.body[k].some(
it => getType(it) === 'File' || getType(it) === 'Blob'
)
} else {
if (getType(options.body[k]) === 'File' || getType(options.body[k]) === 'Blob') {
hasAttach = true
break
}
}
}
// 有附件,则改为FormData
@ -175,12 +179,12 @@ class _Request {
let _type
if (this._owner._inject_res) {
r = this._owner._inject_res(r)
_type = toS.call(r)
_type = getType(r)
}
if (isSucc) {
return r
} else {
if (_type === '[object Promise]') {
if (_type === 'Promise') {
return r.then(_ => Promise.reject(_))
} else {
return Promise.reject(r)