karl-common-util
Advanced tools
Comparing version 1.3.8 to 1.3.9
@@ -7,4 +7,37 @@ "use strict"; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
require("isomorphic-fetch"); | ||
var _index = require("../index"); | ||
var buildMd5Sign = function buildMd5Sign(data) { | ||
var arr = []; | ||
for (var k in data) { | ||
arr.push({ k: k, v: data[k] }); | ||
} | ||
arr.sort(function (a, b) { | ||
//按小写的key进行排序 | ||
var ak = a.k.toLowerCase(); | ||
var bk = b.k.toLowerCase(); | ||
if (ak < bk) { | ||
return -1; | ||
} | ||
if (ak > bk) { | ||
return 1; | ||
} | ||
return 0; | ||
}); | ||
var m = arr.map(function (d) { | ||
if (_typeof(d.v) === "object") { | ||
return JSON.stringify(d.v); | ||
} else { | ||
return d.v; | ||
} | ||
}).join(""); | ||
m = _index.sign.md5(m); | ||
return m; | ||
}; | ||
var doFetch = function _callee2(_ref) { | ||
@@ -22,3 +55,5 @@ var url = _ref.url, | ||
_ref$isBrowser = _ref.isBrowser, | ||
isBrowser = _ref$isBrowser === undefined ? true : _ref$isBrowser; | ||
isBrowser = _ref$isBrowser === undefined ? true : _ref$isBrowser, | ||
_ref$withMd5Sign = _ref.withMd5Sign, | ||
withMd5Sign = _ref$withMd5Sign === undefined ? false : _ref$withMd5Sign; | ||
var promise; | ||
@@ -43,2 +78,3 @@ return regeneratorRuntime.async(function _callee2$(_context2) { | ||
}, headers); | ||
json = { | ||
@@ -49,2 +85,6 @@ method: method, | ||
if (withMd5Sign === true) { | ||
json.md5 = buildMd5Sign(json); | ||
} | ||
if (method === "GET") { | ||
@@ -72,14 +112,14 @@ paramArr = []; | ||
responese = void 0; | ||
_context.prev = 5; | ||
_context.next = 8; | ||
_context.prev = 6; | ||
_context.next = 9; | ||
return regeneratorRuntime.awrap(fetch(url, json)); | ||
case 8: | ||
case 9: | ||
responese = _context.sent; | ||
_context.next = 15; | ||
_context.next = 16; | ||
break; | ||
case 11: | ||
_context.prev = 11; | ||
_context.t0 = _context["catch"](5); | ||
case 12: | ||
_context.prev = 12; | ||
_context.t0 = _context["catch"](6); | ||
@@ -90,3 +130,3 @@ //url请求失败 | ||
case 15: | ||
case 16: | ||
_responese = responese, status = _responese.status; | ||
@@ -97,3 +137,3 @@ | ||
if (!(status === 204 || status === 205)) { | ||
_context.next = 19; | ||
_context.next = 20; | ||
break; | ||
@@ -105,18 +145,18 @@ } | ||
case 19: | ||
case 20: | ||
//将返回结果解析为json | ||
message = void 0; | ||
_context.prev = 20; | ||
_context.next = 23; | ||
_context.prev = 21; | ||
_context.next = 24; | ||
return regeneratorRuntime.awrap(responese.json()); | ||
case 23: | ||
case 24: | ||
message = _context.sent; | ||
_context.next = 30; | ||
_context.next = 31; | ||
break; | ||
case 26: | ||
_context.prev = 26; | ||
_context.t1 = _context["catch"](20); | ||
case 27: | ||
_context.prev = 27; | ||
_context.t1 = _context["catch"](21); | ||
@@ -127,12 +167,12 @@ //结果类型不为json(例如nginx直接返回404错误) | ||
case 30: | ||
case 31: | ||
_context.t2 = status; | ||
_context.next = _context.t2 === 200 ? 33 : _context.t2 === 201 ? 33 : _context.t2 === 202 ? 33 : _context.t2 === 204 ? 33 : _context.t2 === 401 ? 35 : 38; | ||
_context.next = _context.t2 === 200 ? 34 : _context.t2 === 201 ? 34 : _context.t2 === 202 ? 34 : _context.t2 === 204 ? 34 : _context.t2 === 401 ? 36 : 39; | ||
break; | ||
case 33: | ||
case 34: | ||
resolve({ status: status, message: message }); | ||
return _context.abrupt("break", 40); | ||
return _context.abrupt("break", 41); | ||
case 35: | ||
case 36: | ||
//请求未授权,重新登录 | ||
@@ -143,9 +183,9 @@ if (isBrowser) { | ||
reject({ status: 401, message: "重新登录" }); | ||
return _context.abrupt("break", 40); | ||
return _context.abrupt("break", 41); | ||
case 38: | ||
case 39: | ||
reject({ status: status, message: message }); | ||
return _context.abrupt("break", 40); | ||
return _context.abrupt("break", 41); | ||
case 40: | ||
case 41: | ||
case "end": | ||
@@ -155,3 +195,3 @@ return _context.stop(); | ||
} | ||
}, null, undefined, [[5, 11], [20, 26]]); | ||
}, null, undefined, [[6, 12], [21, 27]]); | ||
}); | ||
@@ -178,3 +218,4 @@ return _context2.abrupt("return", promise); | ||
_ref2$times = _ref2.times, | ||
times = _ref2$times === undefined ? 1 : _ref2$times; | ||
times = _ref2$times === undefined ? 1 : _ref2$times, | ||
withMd5Sign = _ref2.withMd5Sign; | ||
var jwt, response; | ||
@@ -206,3 +247,4 @@ return regeneratorRuntime.async(function _callee3$(_context3) { | ||
contentType: contentType, | ||
isBrowser: isBrowser | ||
isBrowser: isBrowser, | ||
withMd5Sign: withMd5Sign | ||
})); | ||
@@ -235,2 +277,3 @@ | ||
isBrowser: isBrowser, | ||
withMd5Sign: withMd5Sign, | ||
times: times | ||
@@ -237,0 +280,0 @@ })); |
{ | ||
"name": "karl-common-util", | ||
"version": "1.3.8", | ||
"version": "1.3.9", | ||
"description": "karl common lib for client and server", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import "isomorphic-fetch" | ||
let doFetch = async ({url, data = {}, method, timeout = 10000, headers = {}, contentType = "application/json; charset=utf-8", isBrowser = true}) => { | ||
import {sign} from "../index" | ||
let buildMd5Sign = data => { | ||
let arr = [] | ||
for (let k in data) { | ||
arr.push({k: k, v: data[k]}) | ||
} | ||
arr.sort((a, b) => { | ||
//按小写的key进行排序 | ||
let ak = a.k.toLowerCase() | ||
let bk = b.k.toLowerCase() | ||
if (ak < bk) { | ||
return -1 | ||
} | ||
if (ak > bk) { | ||
return 1 | ||
} | ||
return 0 | ||
}) | ||
let m = arr.map(d => { | ||
if (typeof (d.v) === "object") { | ||
return JSON.stringify(d.v) | ||
} else { | ||
return d.v | ||
} | ||
}).join("") | ||
m = sign.md5(m) | ||
return m | ||
} | ||
let doFetch = async ({url, data = {}, method, timeout = 10000, headers = {}, contentType = "application/json; charset=utf-8", isBrowser = true, withMd5Sign = false}) => { | ||
let promise = new Promise(async (resolve, reject) => { | ||
@@ -12,2 +43,3 @@ //超时reject 408,默认为10秒超时 | ||
}, headers) | ||
let json = { | ||
@@ -17,2 +49,6 @@ method: method, | ||
} | ||
if (withMd5Sign === true) { | ||
json.md5 = buildMd5Sign(json) | ||
} | ||
if (method === "GET") { | ||
@@ -87,3 +123,3 @@ let paramArr = [] | ||
let request = async ({url, jwtLocalStorageName, data, timeout, headers, contentType, method, isBrowser, times = 1}) => { | ||
let request = async ({url, jwtLocalStorageName, data, timeout, headers, contentType, method, isBrowser, times = 1, withMd5Sign}) => { | ||
if (jwtLocalStorageName !== undefined) { | ||
@@ -103,3 +139,4 @@ //附加jwt到http header | ||
contentType: contentType, | ||
isBrowser: isBrowser | ||
isBrowser: isBrowser, | ||
withMd5Sign: withMd5Sign | ||
}) | ||
@@ -119,2 +156,3 @@ return response | ||
isBrowser: isBrowser, | ||
withMd5Sign: withMd5Sign, | ||
times: times | ||
@@ -121,0 +159,0 @@ }) |
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
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
57600
18
1396