@chunpu/http
Advanced tools
Comparing version 1.0.2 to 1.1.0
{ | ||
"name": "@chunpu/http", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Promise Based request / fetch / http For Real Project, Support multiple platforms", | ||
@@ -37,3 +37,2 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@std/esm": "^0.26.0", | ||
"body-parser": "^1.18.3", | ||
@@ -40,0 +39,0 @@ "express": "^4.16.3", |
@@ -88,3 +88,3 @@ @chunpu/http | ||
- `params` the url querystring object | ||
- `timeout` request timeout | ||
- `timeout` request timeout, 支持快应用和微信小程序 | ||
- `withCredentials` whether use cors, default `false` | ||
@@ -129,2 +129,4 @@ | ||
支持单个请求超时设置 | ||
> 请通过 npm 安装, 参见 [npm 支持](https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html) | ||
@@ -144,2 +146,4 @@ | ||
支持单个请求超时设置 | ||
记得在 `manifest.json` 文件中加入权限 | ||
@@ -287,2 +291,14 @@ | ||
### Handling Errors | ||
All Platform support timeout error for one request | ||
```js | ||
http.get('/very/slow/api').catch(err => { | ||
if (/timeout/i.test(err.message)) { | ||
// this is timeout error | ||
} | ||
}) | ||
``` | ||
### Other Api | ||
@@ -289,0 +305,0 @@ |
const _ = require('min-util') | ||
const HttpClient = require('./fetch') | ||
const HttpClient = require('./http') | ||
@@ -4,0 +4,0 @@ const instance = new HttpClient() |
@@ -36,2 +36,24 @@ const _ = require('min-util') | ||
function timeout(time) { | ||
return new Promise((resolve, reject) => { | ||
if (timeout) { | ||
setTimeout(() => { | ||
reject(new Error('timeout')) | ||
}, time) | ||
} | ||
}) | ||
} | ||
function clearTimer(timer) { | ||
if (timer) { | ||
clearTimeout(timer) | ||
} | ||
} | ||
function createError(message, obj) { | ||
var err = new Error(message) | ||
_.extend(err, obj) | ||
return err | ||
} | ||
exports.CONTENT_TYPE_KEY = CONTENT_TYPE_KEY | ||
@@ -41,1 +63,4 @@ exports.getContentType = getContentType | ||
exports.isFormData = isFormData | ||
exports.timeout = timeout | ||
exports.clearTimer = clearTimer | ||
exports.createError = createError |
@@ -5,2 +5,4 @@ const _ = require('min-util') | ||
var mockTimeout = 0.2 * 1000 | ||
var wx = { | ||
@@ -27,3 +29,3 @@ request (opt) { | ||
} | ||
}) | ||
}, mockTimeout) | ||
} | ||
@@ -52,3 +54,3 @@ } | ||
} | ||
}) | ||
}, mockTimeout) | ||
} | ||
@@ -84,3 +86,3 @@ } | ||
} | ||
}) | ||
}, mockTimeout) | ||
} | ||
@@ -114,5 +116,25 @@ } | ||
function testTimeout() { | ||
var wxHttp = httpClient.create({wx}) | ||
wxHttp.post('/success', null, { | ||
timeout: 10 | ||
}).then(() => { | ||
assert(false, '应该超时') | ||
}).catch(err => { | ||
assert(/timeout/.test(err.message), '应该包含 timeout') | ||
}) | ||
var quickappHttp = httpClient.create({quickapp}) | ||
quickappHttp.post('/success', null, { | ||
timeout: 10 | ||
}).then(() => { | ||
assert(false, '应该超时') | ||
}).catch(err => { | ||
assert(/timeout/.test(err.message), '应该包含 timeout') | ||
}) | ||
} | ||
function test(opt) { | ||
var type = _.keys(opt)[0] | ||
var timeout = 1000 * 20 | ||
var timeout = mockTimeout * 2 | ||
var http = httpClient.create(_.extend({ | ||
@@ -200,1 +222,2 @@ baseURL: 'http://my.domain', | ||
test({axios}) | ||
testTimeout() |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54452
4
22
659
320
1