@chunpu/http
Advanced tools
Comparing version 1.1.1 to 1.2.0
{ | ||
"name": "@chunpu/http", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Promise Based request / fetch / http For Real Project, Support multiple platforms", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -15,5 +15,3 @@ const _ = require('min-util') | ||
// TODO recognize timeout error type | ||
function HttpClient (opt) { | ||
// has defaults, interceptors two key | ||
this.defaults = { | ||
@@ -120,4 +118,4 @@ baseURL: '', | ||
return Promise.resolve(config) | ||
.then(config => this.interceptors.request.exec(config)) // after get config | ||
var ret = Promise.resolve(config) | ||
ret = this.interceptors.request.intercept(ret) // after get config | ||
.then(config => this.adapter.call(this, config)) | ||
@@ -142,3 +140,4 @@ .then(response => { | ||
}) | ||
.then(response => this.interceptors.response.exec(response)) // after parse data | ||
ret = this.interceptors.response.intercept(ret) // after parse data | ||
return ret | ||
} | ||
@@ -145,0 +144,0 @@ |
@@ -14,7 +14,7 @@ const _ = require('min-util') | ||
}, | ||
exec (value) { | ||
intercept (promise) { | ||
return _.reduce(this.queue, (prev, middleware) => { | ||
return prev.then(...middleware) | ||
}, Promise.resolve(value)) | ||
}, promise) | ||
} | ||
}) |
@@ -114,2 +114,10 @@ const _ = require('min-util') | ||
var wxHttp = httpClient.create({wx}) | ||
wxHttp.interceptors.response.use(response => { | ||
return response | ||
}, err => { | ||
assert(/timeout/.test(err.message), '应该包含 timeout') | ||
err.message = 'Server Timeout!' | ||
return Promise.reject(err) | ||
}) | ||
wxHttp.post('/success', null, { | ||
@@ -120,3 +128,3 @@ timeout: 10 | ||
}).catch(err => { | ||
assert(/timeout/.test(err.message), '应该包含 timeout') | ||
assert.deepEqual(err.message, 'Server Timeout!') | ||
}) | ||
@@ -134,2 +142,27 @@ | ||
function testMultiInterceptors() { | ||
var http = httpClient.create({wx}) | ||
http.interceptors.response.use(response => { | ||
response.x = 1 | ||
return response | ||
}) | ||
http.interceptors.response.use(response => { | ||
assert.deepEqual(response.x, 1) | ||
response.x++ | ||
return response | ||
}) | ||
http.interceptors.response.use(response => { | ||
assert.deepEqual(response.x, 2) | ||
response.x++ | ||
return response | ||
}) | ||
http.post('/success', null, { | ||
timeout: 10000 | ||
}).then(response => { | ||
assert.deepEqual(response.x, 3) | ||
}).catch(() => { | ||
assert(false) | ||
}) | ||
} | ||
function test(opt) { | ||
@@ -221,1 +254,2 @@ var type = _.keys(opt)[0] | ||
testTimeout() | ||
testMultiInterceptors() |
@@ -14,3 +14,2 @@ var webpack = require('webpack') | ||
filename: '[name].js', | ||
// libraryTarget: 'commonjs', | ||
libraryTarget: 'umd', | ||
@@ -23,16 +22,7 @@ library: '[name]' | ||
VERSION: JSON.stringify(pkg.version) | ||
}) | ||
}), | ||
new webpack.optimize.UglifyJsPlugin({minimize: true}) | ||
] | ||
} | ||
// if (DEBUG) { | ||
// config.devtool = 'source-map' | ||
// } | ||
// if (PRODUCTION) { | ||
// config.plugins.push( | ||
// new webpack.optimize.UglifyJsPlugin({minimize: true}) | ||
// ) | ||
// } | ||
module.exports = config |
Sorry, the diff of this file is not supported yet
55424
682