parent
							
								
									33e4ffd659
								
							
						
					
					
						commit
						5b403fb269
					
				| 
						 | 
					@ -1,8 +1,8 @@
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  "name": "@bytedo/fetch",
 | 
					  "name": "@bytedo/fetch",
 | 
				
			||||||
  "version": "2.1.6",
 | 
					  "version": "2.1.7",
 | 
				
			||||||
  "description": "全新的ajax封装。分2个版本, 一个基于XMLHttpRequest, 一个基于window.fetch",
 | 
					  "description": "全新的ajax封装。分2个版本, 一个基于XMLHttpRequest, 一个基于window.fetch",
 | 
				
			||||||
  "main": "dist/index.js",
 | 
					  "main": "dist/next.js",
 | 
				
			||||||
  "files": [
 | 
					  "files": [
 | 
				
			||||||
    "dist/*"
 | 
					    "dist/*"
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										40
									
								
								src/index.js
								
								
								
								
							
							
						
						
									
										40
									
								
								src/index.js
								
								
								
								
							| 
						 | 
					@ -25,7 +25,7 @@ const ERRORS = {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Promise.defer = function () {
 | 
					Promise.defer = function () {
 | 
				
			||||||
  var _ = {}
 | 
					  let _ = {}
 | 
				
			||||||
  _.promise = new Promise(function (y, n) {
 | 
					  _.promise = new Promise(function (y, n) {
 | 
				
			||||||
    _.resolve = y
 | 
					    _.resolve = y
 | 
				
			||||||
    _.reject = n
 | 
					    _.reject = n
 | 
				
			||||||
| 
						 | 
					@ -66,7 +66,7 @@ class _Request {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!options.signal) {
 | 
					    if (!options.signal) {
 | 
				
			||||||
      var control = new AbortController()
 | 
					      let control = new AbortController()
 | 
				
			||||||
      options.signal = control.signal
 | 
					      options.signal = control.signal
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    this.defer.promise.abort = function () {
 | 
					    this.defer.promise.abort = function () {
 | 
				
			||||||
| 
						 | 
					@ -90,10 +90,10 @@ class _Request {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  __next__() {
 | 
					  __next__() {
 | 
				
			||||||
    var options = this.options
 | 
					    let options = this.options
 | 
				
			||||||
    var params = null
 | 
					    let params = null
 | 
				
			||||||
    var hasAttach = false // 是否有附件
 | 
					    let hasAttach = false // 是否有附件
 | 
				
			||||||
    var noBody = NOBODY_METHODS.includes(options.method)
 | 
					    let noBody = NOBODY_METHODS.includes(options.method)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* ------------------------  1»» 处理signal  ---------------------- */
 | 
					    /* ------------------------  1»» 处理signal  ---------------------- */
 | 
				
			||||||
    options.signal.onabort = _ => {
 | 
					    options.signal.onabort = _ => {
 | 
				
			||||||
| 
						 | 
					@ -103,7 +103,7 @@ class _Request {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* --------------------------  2»» 请求的内容 --------------------- */
 | 
					    /* --------------------------  2»» 请求的内容 --------------------- */
 | 
				
			||||||
    if (options.body) {
 | 
					    if (options.body) {
 | 
				
			||||||
      var type = typeof options.body
 | 
					      let type = typeof options.body
 | 
				
			||||||
      switch (type) {
 | 
					      switch (type) {
 | 
				
			||||||
        case 'number':
 | 
					        case 'number':
 | 
				
			||||||
        case 'string':
 | 
					        case 'string':
 | 
				
			||||||
| 
						 | 
					@ -111,6 +111,11 @@ class _Request {
 | 
				
			||||||
          params = options.body
 | 
					          params = options.body
 | 
				
			||||||
          break
 | 
					          break
 | 
				
			||||||
        case 'object':
 | 
					        case 'object':
 | 
				
			||||||
 | 
					          let _type = getType(options.body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          if (_type === 'ArrayBuffer' || _type === 'Uint8Array') {
 | 
				
			||||||
 | 
					            break
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
          // 解析表单DOM
 | 
					          // 解析表单DOM
 | 
				
			||||||
          if (options.body.nodeName === 'FORM') {
 | 
					          if (options.body.nodeName === 'FORM') {
 | 
				
			||||||
            options.method = options.body.method.toUpperCase() || 'POST'
 | 
					            options.method = options.body.method.toUpperCase() || 'POST'
 | 
				
			||||||
| 
						 | 
					@ -181,16 +186,17 @@ class _Request {
 | 
				
			||||||
    if (noBody) {
 | 
					    if (noBody) {
 | 
				
			||||||
      params = Format.param(params)
 | 
					      params = Format.param(params)
 | 
				
			||||||
      if (params) {
 | 
					      if (params) {
 | 
				
			||||||
        options.url += (~options.url.indexOf('?') ? '&' : '?') + params
 | 
					        options.url += (options.url.includes('?') ? '&' : '?') + params
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      if (options.cache === 'no-store') {
 | 
					      if (options.cache === 'no-store') {
 | 
				
			||||||
        options.url += (~options.url.indexOf('?') ? '&' : '?') + '_t_=' + Date.now()
 | 
					        options.url += (options.url.includes('?') ? '&' : '?') + '_t_=' + Date.now()
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      if (options.body && !hasAttach) {
 | 
					      if (options.body && !hasAttach) {
 | 
				
			||||||
        if (~options.headers['content-type'].indexOf('json')) {
 | 
					        if (options.headers['content-type'].includes('json')) {
 | 
				
			||||||
          params = JSON.stringify(params)
 | 
					          params = JSON.stringify(params)
 | 
				
			||||||
        } else {
 | 
					        }
 | 
				
			||||||
 | 
					        if (options.headers['content-type'].includes('form')) {
 | 
				
			||||||
          params = Format.param(params)
 | 
					          params = Format.param(params)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
| 
						 | 
					@ -288,9 +294,9 @@ class _Request {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  __success__(isSucc, result) {
 | 
					  __success__(isSucc, result) {
 | 
				
			||||||
    var { body, status, statusText, headers } = result
 | 
					    let { body, status, statusText, headers } = result
 | 
				
			||||||
    var response = new Response(body, { status, statusText, headers })
 | 
					    let response = new Response(body, { status, statusText, headers })
 | 
				
			||||||
    var _type
 | 
					    let _type
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (this._owner._inject_res) {
 | 
					    if (this._owner._inject_res) {
 | 
				
			||||||
      response = this._owner._inject_res(response)
 | 
					      response = this._owner._inject_res(response)
 | 
				
			||||||
| 
						 | 
					@ -312,7 +318,7 @@ class _Request {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  __cancel__(result) {
 | 
					  __cancel__(result) {
 | 
				
			||||||
    var response = new Response('', {
 | 
					    let response = new Response('', {
 | 
				
			||||||
      status: 0,
 | 
					      status: 0,
 | 
				
			||||||
      statusText: ERRORS[10100]
 | 
					      statusText: ERRORS[10100]
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
| 
						 | 
					@ -325,7 +331,7 @@ class _Request {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  __timeout__(result) {
 | 
					  __timeout__(result) {
 | 
				
			||||||
    var response = new Response('', {
 | 
					    let response = new Response('', {
 | 
				
			||||||
      status: 504,
 | 
					      status: 504,
 | 
				
			||||||
      statusText: ERRORS[10504]
 | 
					      statusText: ERRORS[10504]
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
| 
						 | 
					@ -353,7 +359,7 @@ const _fetch = function (url, options) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_fetch.create = function () {
 | 
					_fetch.create = function () {
 | 
				
			||||||
  var another = function (url, options) {
 | 
					  let another = function (url, options) {
 | 
				
			||||||
    return new _Request(url, options, another)
 | 
					    return new _Request(url, options, another)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  inject(another)
 | 
					  inject(another)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -15,7 +15,7 @@ export function getType(val) {
 | 
				
			||||||
 * 表单序列化
 | 
					 * 表单序列化
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function serialize(p, obj, query) {
 | 
					function serialize(p, obj, query) {
 | 
				
			||||||
  var k
 | 
					  let k
 | 
				
			||||||
  if (Array.isArray(obj)) {
 | 
					  if (Array.isArray(obj)) {
 | 
				
			||||||
    obj.forEach(function (it, i) {
 | 
					    obj.forEach(function (it, i) {
 | 
				
			||||||
      k = p ? `${p}[${Array.isArray(it) ? i : ''}]` : i
 | 
					      k = p ? `${p}[${Array.isArray(it) ? i : ''}]` : i
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										24
									
								
								src/next.js
								
								
								
								
							
							
						
						
									
										24
									
								
								src/next.js
								
								
								
								
							| 
						 | 
					@ -75,19 +75,25 @@ class _Request {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  __next__() {
 | 
					  __next__() {
 | 
				
			||||||
    var options = this.options
 | 
					    let options = this.options
 | 
				
			||||||
    var hasAttach = false // 是否有附件
 | 
					    let hasAttach = false // 是否有附件
 | 
				
			||||||
    var noBody = NOBODY_METHODS.includes(options.method)
 | 
					    let noBody = NOBODY_METHODS.includes(options.method)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* --------------------------  1»» 请求的内容 --------------------- */
 | 
					    /* --------------------------  1»» 请求的内容 --------------------- */
 | 
				
			||||||
    if (options.body) {
 | 
					    if (options.body) {
 | 
				
			||||||
      var type = typeof options.body
 | 
					      let type = typeof options.body
 | 
				
			||||||
      switch (type) {
 | 
					      switch (type) {
 | 
				
			||||||
        case 'number':
 | 
					        case 'number':
 | 
				
			||||||
        case 'string':
 | 
					        case 'string':
 | 
				
			||||||
          this.__type__('text')
 | 
					          this.__type__('text')
 | 
				
			||||||
          break
 | 
					          break
 | 
				
			||||||
        case 'object':
 | 
					        case 'object':
 | 
				
			||||||
 | 
					          let _type = getType(options.body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          if (_type === 'ArrayBuffer' || _type === 'Uint8Array') {
 | 
				
			||||||
 | 
					            break
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          // 解析表单DOM
 | 
					          // 解析表单DOM
 | 
				
			||||||
          if (options.body.nodeName === 'FORM') {
 | 
					          if (options.body.nodeName === 'FORM') {
 | 
				
			||||||
            options.method = options.body.method.toUpperCase() || 'POST'
 | 
					            options.method = options.body.method.toUpperCase() || 'POST'
 | 
				
			||||||
| 
						 | 
					@ -142,14 +148,14 @@ class _Request {
 | 
				
			||||||
    if (noBody) {
 | 
					    if (noBody) {
 | 
				
			||||||
      let tmp = Format.param(options.body)
 | 
					      let tmp = Format.param(options.body)
 | 
				
			||||||
      if (tmp) {
 | 
					      if (tmp) {
 | 
				
			||||||
        options.url += (~options.url.indexOf('?') ? '&' : '?') + tmp
 | 
					        options.url += (options.url.includes('?') ? '&' : '?') + tmp
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      delete options.body
 | 
					      delete options.body
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      if (options.body && !hasAttach) {
 | 
					      if (options.body && !hasAttach) {
 | 
				
			||||||
        if (~options.headers['content-type'].indexOf('json')) {
 | 
					        if (options.headers['content-type'].includes('json')) {
 | 
				
			||||||
          options.body = JSON.stringify(options.body)
 | 
					          options.body = JSON.stringify(options.body)
 | 
				
			||||||
        } else {
 | 
					        } else if (options.headers['content-type'].includes('form')) {
 | 
				
			||||||
          options.body = Format.param(options.body)
 | 
					          options.body = Format.param(options.body)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
| 
						 | 
					@ -165,7 +171,7 @@ class _Request {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* -----------------    5»» 构造请求    ------------------- */
 | 
					    /* -----------------    5»» 构造请求    ------------------- */
 | 
				
			||||||
    var url = options.url
 | 
					    let url = options.url
 | 
				
			||||||
    delete options.url
 | 
					    delete options.url
 | 
				
			||||||
    for (let k in options) {
 | 
					    for (let k in options) {
 | 
				
			||||||
      if (options[k] === null || options[k] === undefined || options[k] === '') {
 | 
					      if (options[k] === null || options[k] === undefined || options[k] === '') {
 | 
				
			||||||
| 
						 | 
					@ -222,7 +228,7 @@ const _fetch = function (url, options) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_fetch.create = function () {
 | 
					_fetch.create = function () {
 | 
				
			||||||
  var another = function (url, options) {
 | 
					  let another = function (url, options) {
 | 
				
			||||||
    return new _Request(url, options, another)
 | 
					    return new _Request(url, options, another)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  inject(another)
 | 
					  inject(another)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue