supercell-coc
Version:
This is an API for Supercell's game [Clash of Clans](https://clashofclans.com) implemented into Node.JS by z3db0y.
29 lines (27 loc) • 942 B
JavaScript
let libs = {
http: require('http'),
https: require('https')
}
module.exports = function(url, opts) {
opts = opts || {};
try {
url = new URL(url);
} catch(err) { throw new Error('Malformed URL: ' + url); }
return new Promise((resolve, reject) => {
let req = libs[url.protocol.slice(0, -1)].request({
method: opts.method || 'GET',
hostname: url.hostname,
port: url.port,
path: url.pathname + url.search,
headers: opts.headers || {}
}, (res) => {
let chunks = [];
res.on('data', (chunk) => { chunks.push(chunk); });
res.on('end', () => { resolve({ body: chunks.join(''), code: res.statusCode }); });
req.on('error', (err) => { reject(err); });
});
if(opts.body) req.write(opts.body);
req.end();
});
}
Object.freeze(module.exports);