Compare commits
No commits in common. "master" and "2.1.6" have entirely different histories.
|
@ -55,7 +55,7 @@ f1('/get_list', {body: {page: 1}})
|
|||
|
||||
### APIs
|
||||
|
||||
#### 1. fetch(url[, options`<Object>`])
|
||||
#### 1. fetch(url[, options<Object>])
|
||||
> 发起一个网络请求, options的参数如下。 同时支持配置公共域名, 公共参数。
|
||||
|
||||
+ method`<String>` 默认GET, 可选GET/POST/PUT/DELETE...
|
||||
|
|
|
@ -55,7 +55,7 @@ f1('/get_list', {body: {page: 1}})
|
|||
|
||||
### APIs
|
||||
|
||||
#### 1. fetch(url[, options`<Object>`])
|
||||
#### 1. fetch(url[, options<Object>])
|
||||
> 发起一个网络请求, options的参数如下。 同时支持配置公共域名, 公共参数。
|
||||
|
||||
+ method`<String>` 默认GET, 可选GET/POST/PUT/DELETE...
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
{
|
||||
"name": "@bytedo/fetch",
|
||||
"version": "2.1.8",
|
||||
"version": "2.1.6",
|
||||
"description": "全新的ajax封装。分2个版本, 一个基于XMLHttpRequest, 一个基于window.fetch",
|
||||
"main": "dist/next.js",
|
||||
"main": "dist/index.js",
|
||||
"files": [
|
||||
"dist/*"
|
||||
],
|
||||
|
|
40
src/index.js
40
src/index.js
|
@ -25,7 +25,7 @@ const ERRORS = {
|
|||
}
|
||||
|
||||
Promise.defer = function () {
|
||||
let _ = {}
|
||||
var _ = {}
|
||||
_.promise = new Promise(function (y, n) {
|
||||
_.resolve = y
|
||||
_.reject = n
|
||||
|
@ -66,7 +66,7 @@ class _Request {
|
|||
}
|
||||
|
||||
if (!options.signal) {
|
||||
let control = new AbortController()
|
||||
var control = new AbortController()
|
||||
options.signal = control.signal
|
||||
}
|
||||
this.defer.promise.abort = function () {
|
||||
|
@ -90,10 +90,10 @@ class _Request {
|
|||
}
|
||||
|
||||
__next__() {
|
||||
let options = this.options
|
||||
let params = null
|
||||
let hasAttach = false // 是否有附件
|
||||
let noBody = NOBODY_METHODS.includes(options.method)
|
||||
var options = this.options
|
||||
var params = null
|
||||
var hasAttach = false // 是否有附件
|
||||
var noBody = NOBODY_METHODS.includes(options.method)
|
||||
|
||||
/* ------------------------ 1»» 处理signal ---------------------- */
|
||||
options.signal.onabort = _ => {
|
||||
|
@ -103,7 +103,7 @@ class _Request {
|
|||
|
||||
/* -------------------------- 2»» 请求的内容 --------------------- */
|
||||
if (options.body) {
|
||||
let type = typeof options.body
|
||||
var type = typeof options.body
|
||||
switch (type) {
|
||||
case 'number':
|
||||
case 'string':
|
||||
|
@ -111,11 +111,6 @@ class _Request {
|
|||
params = options.body
|
||||
break
|
||||
case 'object':
|
||||
let _type = getType(options.body)
|
||||
|
||||
if (_type === 'ArrayBuffer' || _type === 'Uint8Array') {
|
||||
break
|
||||
}
|
||||
// 解析表单DOM
|
||||
if (options.body.nodeName === 'FORM') {
|
||||
options.method = options.body.method.toUpperCase() || 'POST'
|
||||
|
@ -186,17 +181,16 @@ class _Request {
|
|||
if (noBody) {
|
||||
params = Format.param(params)
|
||||
if (params) {
|
||||
options.url += (options.url.includes('?') ? '&' : '?') + params
|
||||
options.url += (~options.url.indexOf('?') ? '&' : '?') + params
|
||||
}
|
||||
if (options.cache === 'no-store') {
|
||||
options.url += (options.url.includes('?') ? '&' : '?') + '_t_=' + Date.now()
|
||||
options.url += (~options.url.indexOf('?') ? '&' : '?') + '_t_=' + Date.now()
|
||||
}
|
||||
} else {
|
||||
if (options.body && !hasAttach) {
|
||||
if (options.headers['content-type'].includes('json')) {
|
||||
if (~options.headers['content-type'].indexOf('json')) {
|
||||
params = JSON.stringify(params)
|
||||
}
|
||||
if (options.headers['content-type'].includes('form')) {
|
||||
} else {
|
||||
params = Format.param(params)
|
||||
}
|
||||
}
|
||||
|
@ -294,9 +288,9 @@ class _Request {
|
|||
}
|
||||
|
||||
__success__(isSucc, result) {
|
||||
let { body, status, statusText, headers } = result
|
||||
let response = new Response(body, { status, statusText, headers })
|
||||
let _type
|
||||
var { body, status, statusText, headers } = result
|
||||
var response = new Response(body, { status, statusText, headers })
|
||||
var _type
|
||||
|
||||
if (this._owner._inject_res) {
|
||||
response = this._owner._inject_res(response)
|
||||
|
@ -318,7 +312,7 @@ class _Request {
|
|||
}
|
||||
|
||||
__cancel__(result) {
|
||||
let response = new Response('', {
|
||||
var response = new Response('', {
|
||||
status: 0,
|
||||
statusText: ERRORS[10100]
|
||||
})
|
||||
|
@ -331,7 +325,7 @@ class _Request {
|
|||
}
|
||||
|
||||
__timeout__(result) {
|
||||
let response = new Response('', {
|
||||
var response = new Response('', {
|
||||
status: 504,
|
||||
statusText: ERRORS[10504]
|
||||
})
|
||||
|
@ -359,7 +353,7 @@ const _fetch = function (url, options) {
|
|||
}
|
||||
|
||||
_fetch.create = function () {
|
||||
let another = function (url, options) {
|
||||
var another = function (url, options) {
|
||||
return new _Request(url, options, another)
|
||||
}
|
||||
inject(another)
|
||||
|
|
|
@ -15,7 +15,7 @@ export function getType(val) {
|
|||
* 表单序列化
|
||||
*/
|
||||
function serialize(p, obj, query) {
|
||||
let k
|
||||
var k
|
||||
if (Array.isArray(obj)) {
|
||||
obj.forEach(function (it, i) {
|
||||
k = p ? `${p}[${Array.isArray(it) ? i : ''}]` : i
|
||||
|
@ -84,17 +84,13 @@ export const Format = {
|
|||
mkFormData(data) {
|
||||
let form = new FormData()
|
||||
for (let i in data) {
|
||||
let val = data[i]
|
||||
if (val === void 0) {
|
||||
val = ''
|
||||
}
|
||||
|
||||
if (Array.isArray(val)) {
|
||||
val.forEach(function (it) {
|
||||
let el = data[i]
|
||||
if (Array.isArray(el)) {
|
||||
el.forEach(function (it) {
|
||||
form.append(i + '[]', it)
|
||||
})
|
||||
} else {
|
||||
form.append(i, val)
|
||||
form.append(i, data[i])
|
||||
}
|
||||
}
|
||||
return form
|
||||
|
@ -109,11 +105,6 @@ export const Format = {
|
|||
if (/native code/.test(v)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (v === void 0) {
|
||||
v = ''
|
||||
}
|
||||
|
||||
let _type = getType(v)
|
||||
|
||||
v = typeof v === 'function' ? v() : v
|
||||
|
|
24
src/next.js
24
src/next.js
|
@ -75,25 +75,19 @@ class _Request {
|
|||
}
|
||||
|
||||
__next__() {
|
||||
let options = this.options
|
||||
let hasAttach = false // 是否有附件
|
||||
let noBody = NOBODY_METHODS.includes(options.method)
|
||||
var options = this.options
|
||||
var hasAttach = false // 是否有附件
|
||||
var noBody = NOBODY_METHODS.includes(options.method)
|
||||
|
||||
/* -------------------------- 1»» 请求的内容 --------------------- */
|
||||
if (options.body) {
|
||||
let type = typeof options.body
|
||||
var type = typeof options.body
|
||||
switch (type) {
|
||||
case 'number':
|
||||
case 'string':
|
||||
this.__type__('text')
|
||||
break
|
||||
case 'object':
|
||||
let _type = getType(options.body)
|
||||
|
||||
if (_type === 'ArrayBuffer' || _type === 'Uint8Array') {
|
||||
break
|
||||
}
|
||||
|
||||
// 解析表单DOM
|
||||
if (options.body.nodeName === 'FORM') {
|
||||
options.method = options.body.method.toUpperCase() || 'POST'
|
||||
|
@ -148,14 +142,14 @@ class _Request {
|
|||
if (noBody) {
|
||||
let tmp = Format.param(options.body)
|
||||
if (tmp) {
|
||||
options.url += (options.url.includes('?') ? '&' : '?') + tmp
|
||||
options.url += (~options.url.indexOf('?') ? '&' : '?') + tmp
|
||||
}
|
||||
delete options.body
|
||||
} else {
|
||||
if (options.body && !hasAttach) {
|
||||
if (options.headers['content-type'].includes('json')) {
|
||||
if (~options.headers['content-type'].indexOf('json')) {
|
||||
options.body = JSON.stringify(options.body)
|
||||
} else if (options.headers['content-type'].includes('form')) {
|
||||
} else {
|
||||
options.body = Format.param(options.body)
|
||||
}
|
||||
}
|
||||
|
@ -171,7 +165,7 @@ class _Request {
|
|||
}
|
||||
|
||||
/* ----------------- 5»» 构造请求 ------------------- */
|
||||
let url = options.url
|
||||
var url = options.url
|
||||
delete options.url
|
||||
for (let k in options) {
|
||||
if (options[k] === null || options[k] === undefined || options[k] === '') {
|
||||
|
@ -228,7 +222,7 @@ const _fetch = function (url, options) {
|
|||
}
|
||||
|
||||
_fetch.create = function () {
|
||||
let another = function (url, options) {
|
||||
var another = function (url, options) {
|
||||
return new _Request(url, options, another)
|
||||
}
|
||||
inject(another)
|
||||
|
|
Loading…
Reference in New Issue