fetch-default
Advanced tools
+20
-5
| 'use strict'; | ||
| ~function (win) { | ||
| (function (win) { | ||
@@ -15,2 +15,7 @@ var oldFetch = fetch; | ||
| if (fail) oldFetchPromise = oldFetchPromise.then(function (response) { | ||
| return response; | ||
| }, fail); | ||
| if (dataFilter) oldFetchPromise = oldFetchPromise.then(dataFilter); | ||
| return oldFetchPromise; | ||
@@ -21,2 +26,4 @@ }; | ||
| var opt = {}; | ||
| var dataFilter = void 0; | ||
| var fail = void 0; | ||
@@ -27,7 +34,8 @@ fetch.default = function () { | ||
| if (Object.prototype.toString.call(option) != '[object Object]') throw new Error('option is object!'); | ||
| if (option.uriPrefix && Object.prototype.toString.call(option.uriPrefix) != '[object String]') throw new Error('option is string!'); | ||
| assert(option); | ||
| opt = option; | ||
| uriPrefix = option.uriPrefix || ''; | ||
| opt = option; | ||
| dataFilter = option.dataFilter; | ||
| fail = option.fail; | ||
| }; | ||
@@ -38,2 +46,9 @@ | ||
| } | ||
| }(window); | ||
| function assert(option) { | ||
| if (Object.prototype.toString.call(option) !== '[object Object]') throw new Error('option is object!'); | ||
| if (option.uriPrefix && Object.prototype.toString.call(option.uriPrefix) !== '[object String]') throw new Error('uriPrefix is string!'); | ||
| if (option.dataFilter && typeof option.dataFilter !== 'function') throw new Error('dataFilter is function!'); | ||
| if (option.fail && typeof option.fail !== 'function') throw new Error('fail is function!'); | ||
| } | ||
| })(window); |
+17
-4
@@ -1,2 +0,2 @@ | ||
| ~(function(win) { | ||
| (function(win) { | ||
@@ -10,2 +10,5 @@ let oldFetch = fetch; | ||
| if (fail) oldFetchPromise = oldFetchPromise.then((response) => response, fail); | ||
| if (dataFilter) oldFetchPromise = oldFetchPromise.then(dataFilter); | ||
| return oldFetchPromise; | ||
@@ -17,10 +20,13 @@ | ||
| let opt = {}; | ||
| let dataFilter; | ||
| let fail; | ||
| fetch.default = (option = {}) => { | ||
| if (Object.prototype.toString.call(option) != '[object Object]') throw new Error('option is object!') | ||
| if (option.uriPrefix && Object.prototype.toString.call(option.uriPrefix) != '[object String]') throw new Error('option is string!') | ||
| assert(option); | ||
| opt = option; | ||
| uriPrefix = option.uriPrefix || ''; | ||
| opt = option; | ||
| dataFilter = option.dataFilter; | ||
| fail = option.fail; | ||
@@ -33,2 +39,9 @@ }; | ||
| function assert(option) { | ||
| if (Object.prototype.toString.call(option) !== '[object Object]') throw new Error('option is object!'); | ||
| if (option.uriPrefix && Object.prototype.toString.call(option.uriPrefix) !== '[object String]') throw new Error('uriPrefix is string!'); | ||
| if (option.dataFilter && typeof option.dataFilter !== 'function') throw new Error('dataFilter is function!'); | ||
| if (option.fail && typeof option.fail !== 'function') throw new Error('fail is function!'); | ||
| } | ||
| })(window); |
+1
-1
| { | ||
| "name": "fetch-default", | ||
| "version": "1.0.10", | ||
| "version": "1.0.11", | ||
| "description": "Add global default properties for the web fetch", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+36
-12
| ## fetch-default | ||
| ###### 为web端fetch添加全局默认属性(Add global default properties for the web fetch) | ||
| ###### Add global default properties for the web fetch | ||
| ### 安装(install) | ||
| ### install | ||
| npm i fetch-default --save | ||
| ### 使用(use) | ||
| ### use | ||
@@ -15,3 +15,3 @@ require('fetch-default'); | ||
| ### 通过default为所有请求添加前缀 | ||
| ### default setting uri prefix | ||
@@ -31,6 +31,6 @@ | ||
| ### 通过default设置默认optoin | ||
| ### default setting optoin | ||
| fetch.default({ | ||
| fetch.default({ | ||
| method: 'GET', | ||
@@ -42,6 +42,6 @@ headers: myHeaders, | ||
| }); | ||
| /* | ||
| 默认所有请求都配置 | ||
| method: 'GET', | ||
| headers: myHeaders, | ||
| /* | ||
| //default | ||
| method: 'GET', | ||
| headers: myHeaders, | ||
| mode: 'cors', | ||
@@ -58,4 +58,28 @@ cache: 'default' | ||
| ### default setting dataFilter | ||
| fetch.default({ | ||
| dataFilter(response) { | ||
| if (!response.ok) { | ||
| //if you use fetch-abort (https://www.npmjs.com/package/fetch-abort) | ||
| this.abort() | ||
| message.error(`${response.status}\n${response.statusText}`); | ||
| } | ||
| return response.json(); | ||
| } | ||
| }); | ||
| ### default setting fail | ||
| fetch.default({ | ||
| fail(e) { | ||
| //if you use fetch-abort (https://www.npmjs.com/package/fetch-abort) | ||
| this.abort() | ||
| message.error(e.toString()); | ||
| } | ||
| }); | ||
| ## Community | ||
@@ -62,0 +86,0 @@ |
5464
41.44%66
57.14%84
40%