master
宇天 2020-08-04 19:29:26 +08:00
parent 5c203ad5f5
commit cea587569a
4 changed files with 189 additions and 39 deletions

152
src/index.es7 Normal file
View File

@ -0,0 +1,152 @@
/**
* 传统版Ajax(基于XMLHttpRequest)
* @author yutent<yutent.io@gmail.com>
* @date 2020/08/03 17:05:10
*/
import Format from './lib/format.js'
const log = console.log
const noop = function(e, res) {
this.defer.resolve(res)
}
const NOBODY_METHODS = ['GET', 'HEAD']
const FORM_TYPES = {
form: 'application/x-www-form-urlencoded; charset=UTF-8',
json: 'application/json; charset=UTF-8',
text: 'text/plain; charset=UTF-8'
}
const ERRORS = {
10001: 'Argument url is required',
10012: 'Parse error',
10100: 'Request canceled',
10104: 'Request pending...',
10200: 'Ok',
10204: 'No content',
10304: 'Not modified',
10500: 'Internal Server Error',
10504: 'Connected timeout'
}
const CONVERT = {
text(val) {
return val
},
xml(val, xml) {
return xml !== undefined ? xml : Format.parseXML(val)
},
html(val) {
return Format.parseHTML(val)
},
json(val) {
return JSON.parse(val)
},
script(val) {
return Format.parseJS(val)
}
}
Promise.defer = function() {
var _ = {}
_.promise = new Promise(function(y, n) {
_.resolve = y
_.reject = n
})
return _
}
class _Instance {}
class _Request {
constructor(url = '', method = 'GET', options = {}) {
if (!url) {
throw new Error(ERRORS[10001])
}
// url规范化
url = url.replace(/#.*$/, '')
if (fetch.BASE_URL) {
if (!/^([a-z]+:|\/\/)/.test(url)) {
url = fetch.BASE_URL + url
}
}
method = method.toUpperCase()
this.xhr = new XMLHttpRequest()
this.defer = Promise.defer()
this.options = {
url,
method,
headers: {},
body: null,
dataType: 'blob',
cache: true,
referrer: '',
credentials: false, // 跨域选项,是否验证凭证
signal: null, // 超时信号, 配置该项时, timeout不再生效
timeout: 30000 // 超时时间, 单位毫秒, 默认30秒
}
// 取消网络请求
// this.defer.promise.abort = () => {
// this.cancel = true
// this.xhr.abort()
// }
this.__next__(Object.assign({}, fetch.__INIT__, options))
return this.defer.promise
}
__next__(options) {
var hasAttach = false // 是否有附件
/* -------------------------------------------------------------- */
/* ------------------------ 1»» 配置头信息 ---------------------- */
/* -------------------------------------------------------------- */
if (options.headers) {
Object.assign(this.options.headers, options.headers)
}
}
}
class _Response {
constructor(status = 200, data = null, headers = {}) {
this.status = status
this.statusText = 'OK'
this.ok = true
this.headers = headers
this.__R__ = data
}
text() {
return this.__R__.text()
}
json() {
return this.__R__.text().then(t => {
return JSON.parse(t)
})
}
blob() {
return this.__R__
}
arrayBuffer() {
return this.__R__.arrayBuffer()
}
}
function _fetch(url, method = 'GET', param = {}) {
//
if (typeof method === 'object') {
param = method
method = 'GET'
}
return new _Request(url, method, param)
}
export default _fetch

View File

@ -5,20 +5,20 @@
* @version $Id$
*/
import Format from './lib/format'
import Format from './lib/format.js'
// 本地协议/头 判断正则
const rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/
// const rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/
const log = console.log
const noop = function(e, res) {
this.defer.resolve(res)
}
let isLocal = false
try {
isLocal = rlocalProtocol.test(location.protocol)
} catch (e) {}
// let isLocal = false
// try {
// isLocal = rlocalProtocol.test(location.protocol)
// } catch (e) {}
let originAnchor = document.createElement('a')
originAnchor.href = location.href
@ -69,9 +69,9 @@ class _Request {
// url规范化
url = url.replace(/#.*$/, '')
if (request.BASE_URL) {
if (fetch.BASE_URL) {
if (!/^([a-z]+:|\/\/)/.test(url)) {
url = request.BASE_URL + url
url = fetch.BASE_URL + url
}
}
@ -84,7 +84,7 @@ class _Request {
method,
headers: {},
data: {},
dataType: 'text',
dataType: 'blob',
withCredentials: false // 跨域选项,是否验证凭证
}
@ -93,7 +93,7 @@ class _Request {
this.cancel = true
this.xhr.abort()
}
this.__next__(Object.assign({}, request.__INIT__, param))
this.__next__(Object.assign({}, fetch.__INIT__, param))
return this.defer.promise
}
@ -412,34 +412,32 @@ class _Request {
}
}
if (!window.request) {
window.request = {
get(url, param = {}) {
return new _Request(url, 'GET', param)
},
post(url, param = {}) {
return new _Request(url, 'POST', param)
},
upload(url, param = {}) {
param.formType = 'form-data'
return this.post(url, param)
},
download(url, param = {}) {
param.dataType = 'blob'
return this.get(url, param)
},
open(url, method = 'GET', param = {}) {
function _fetch(url, method = 'GET', param = {}) {
if (typeof method === 'object') {
param = method
method = 'GET'
}
return new _Request(url, method, param)
},
version: '2.0.0-normal',
init(param = {}) {
this.__INIT__ = param
}
}
}
export default request
_fetch.get = function(url, param = {}) {
return new _Request(url, 'GET', param)
}
_fetch.post = function(url, param = {}) {
return new _Request(url, 'POST', param)
}
_fetch.upload = function(url, param = {}) {
param.formType = 'form-data'
return this.post(url, param)
}
_fetch.download = function(url, param = {}) {
param.dataType = 'blob'
return this.get(url, param)
}
_fetch.version = '2.0.0-normal'
_fetch.init = function(param = {}) {
this.__INIT__ = param
}
export default _fetch

View File

@ -4,4 +4,4 @@
* @date 2020/07/31 18:59:47
*/
import Format from './lib/format'
import Format from './lib/format.es7'