From cea587569a7e42538a6bb7d2cfed15a3b809b8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Tue, 4 Aug 2020 19:29:26 +0800 Subject: [PATCH] update --- src/index.es7 | 152 ++++++++++++++++++++++++++++++ src/{index.js => index222.es7} | 74 +++++++-------- src/lib/{format.js => format.es7} | 0 src/{next.js => next.es7} | 2 +- 4 files changed, 189 insertions(+), 39 deletions(-) create mode 100644 src/index.es7 rename src/{index.js => index222.es7} (91%) rename src/lib/{format.js => format.es7} (100%) rename src/{next.js => next.es7} (71%) diff --git a/src/index.es7 b/src/index.es7 new file mode 100644 index 0000000..13bae05 --- /dev/null +++ b/src/index.es7 @@ -0,0 +1,152 @@ +/** + * 传统版Ajax(基于XMLHttpRequest) + * @author yutent + * @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 diff --git a/src/index.js b/src/index222.es7 similarity index 91% rename from src/index.js rename to src/index222.es7 index fbba382..2566081 100644 --- a/src/index.js +++ b/src/index222.es7 @@ -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 = {}) { - if (typeof method === 'object') { - param = method - method = 'GET' - } - return new _Request(url, method, param) - }, - version: '2.0.0-normal', - init(param = {}) { - this.__INIT__ = param - } +function _fetch(url, method = 'GET', param = {}) { + if (typeof method === 'object') { + param = method + method = 'GET' } + return new _Request(url, method, 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 diff --git a/src/lib/format.js b/src/lib/format.es7 similarity index 100% rename from src/lib/format.js rename to src/lib/format.es7 diff --git a/src/next.js b/src/next.es7 similarity index 71% rename from src/next.js rename to src/next.es7 index 58e3bd2..bf2fc29 100644 --- a/src/next.js +++ b/src/next.es7 @@ -4,4 +4,4 @@ * @date 2020/07/31 18:59:47 */ -import Format from './lib/format' +import Format from './lib/format.es7'