Comparing version 1.0.9 to 1.1.0
const node_url = require('url'); | ||
const { getType } = require('yu-util'); | ||
const querystring = require('querystring'); | ||
module.exports = (url,options)=>{ | ||
options = getType(options)==='Object' ? options : {}; | ||
options.method = options.method || 'get'; | ||
options.headers = getType(options.headers)==='Object' ? options.headers: {}; | ||
options.method = options.method || 'GET'; | ||
options.data = getType(options.data)==='Object' ? options.data : {}; | ||
let data = querystring.stringify(options.data); | ||
let headers = {}, postData = ''; | ||
if(/post/i.test(options.method)){ | ||
postData = data; | ||
headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Content-Length': Buffer.byteLength(postData) | ||
}; | ||
}else{ | ||
data.length>0 && (url += '?'+data); | ||
} | ||
let obj = node_url.parse(url); | ||
let { protocol,hostname,port,path } = obj; | ||
const { request,Agent } = protocol.includes('https') ? require('https') : require('http'); | ||
@@ -21,3 +31,3 @@ return new Promise(result=>{ | ||
path, | ||
headers: options.headers, | ||
headers, | ||
agent:new Agent({ maxSockets:10 }) | ||
@@ -30,12 +40,8 @@ },res=>{ | ||
res.on('end',()=>{ | ||
result({ statusCode:res.statusCode, headers:res.headers, data:buf }); | ||
result({ statusCode:res.statusCode, headers:res.headers, data:buf.toString() }); | ||
}); | ||
}); | ||
let dataArr = []; | ||
Object.keys(options.data).forEach(key=>{ | ||
dataArr.push(key+'='+data[key]); | ||
}); | ||
req.write(dataArr.join('&')); | ||
req.write(postData); | ||
req.end(); | ||
}); | ||
} |
{ | ||
"name": "yu-node", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"description": "仅用于node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
6834
143