diff --git a/package.json b/package.json index 3c17ff0..7acd85f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bytedo/fetch", - "version": "1.0.2", + "version": "1.1.0", "description": "全新的ajax封装。分2个版本, 一个基于XMLHttpRequest, 一个基于window.fetch", "main": "dist/index.js", "directories": { diff --git a/src/index.es7 b/src/index.es7 index 58d823a..82a10fa 100644 --- a/src/index.es7 +++ b/src/index.es7 @@ -6,10 +6,6 @@ import { Format, toS } from './lib/format.js' -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', @@ -286,12 +282,8 @@ class _Request { } __success__(isSucc, result) { - var response = new _Response( - result.status, - result.statusText, - result.body, - result.headers - ) + var { body, status, statusText, headers } = result + var response = new Response(body, { status, statusText, headers }) if (isSucc) { this.defer.resolve(response) @@ -304,7 +296,10 @@ class _Request { } __cancel__(result) { - var response = new _Response(0, ERRORS[10100], Object.create(null)) + var response = new Response('', { + status: 0, + statusText: ERRORS[10100] + }) this.defer.reject(response) @@ -314,7 +309,10 @@ class _Request { } __timeout__(result) { - var response = new _Response(504, ERRORS[10504], Object.create(null)) + var response = new Response('', { + status: 504, + statusText: ERRORS[10504] + }) this.defer.reject(response) @@ -324,40 +322,6 @@ class _Request { } } -class _Response { - constructor(status = 200, statusText = 'OK', data = null, headers = {}) { - this.status = status - this.statusText = statusText - this.ok = status >= 200 && status < 400 - this.headers = headers - - Object.defineProperty(this, '__R__', { - value: data, - writable: true, - enumerable: false, - configurable: true - }) - } - - 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() - } -} - const _fetch = function(url, options) { return new _Request(url, options, { BASE_URL: _fetch.BASE_URL, diff --git a/src/next.es7 b/src/next.es7 index bc1c860..5d0b217 100644 --- a/src/next.es7 +++ b/src/next.es7 @@ -6,21 +6,29 @@ import { Format, toS } from './lib/format.js' -const noop = function(e, res) { - this.defer.resolve(res) -} - +const NATIVE_FETCH = window.fetch 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' +} class _Request { constructor(url = '', options = {}, { BASE_URL, __INIT__ }) { if (!url) { - throw new Error('Argument url is required') + throw new Error(ERRORS[10001]) } // url规范化 @@ -176,8 +184,7 @@ class _Request { delete options[k] } } - return window - .fetch(url, options) + return NATIVE_FETCH(url, options) .then(r => { clearTimeout(this.timer) var isSucc = r.status >= 200 && r.status < 400