From c286cdf2f251957c1b0edab176c92251358ae80f Mon Sep 17 00:00:00 2001 From: yutent Date: Sun, 25 Jun 2023 19:10:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=A1=A8=E5=8D=95=E8=A7=A3?= =?UTF-8?q?=E6=9E=90,=20=E6=94=AF=E6=8C=81=E5=A4=9A=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +++ package.json | 2 +- src/index.js | 23 ++++++++++++++--------- src/lib/format.js | 7 +++++-- src/next.js | 22 +++++++++++++--------- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 01c3f24..a2dbca9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ *.min.js *.min.css +demo.html +.httpserver + node_modules/ dist/ diff --git a/package.json b/package.json index c70d33c..ea08754 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/index.js b/src/index.js index bb24e2b..20d2eee 100644 --- a/src/index.js +++ b/src/index.js @@ -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) diff --git a/src/lib/format.js b/src/lib/format.js index a17536c..7a5d862 100644 --- a/src/lib/format.js +++ b/src/lib/format.js @@ -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) } diff --git a/src/next.js b/src/next.js index 2d89735..fa9edf0 100644 --- a/src/next.js +++ b/src/next.js @@ -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)