From 5f9c35b111442e4b222ec1c0fdab7c0fdb4d35ae Mon Sep 17 00:00:00 2001 From: Pradyuman Vig Date: Tue, 5 Jul 2016 00:30:54 -0700 Subject: [PATCH] v2.0.0 --- .travis.yml | 3 + README.md | 23 +++-- index.js | 19 ++-- lib/champions.js | 162 ++++++++++++++++++++++++++++++++++ lib/champs.js | 202 ------------------------------------------ lib/statistics.js | 202 ++++++++++++++++++++++++++++++++++++++++++ lib/stats.js | 219 ---------------------------------------------- lib/utils.js | 127 +++++++++++++++++++-------- package.json | 15 +--- test/test.js | 33 +++---- 10 files changed, 493 insertions(+), 512 deletions(-) create mode 100644 .travis.yml create mode 100644 lib/champions.js delete mode 100644 lib/champs.js create mode 100644 lib/statistics.js delete mode 100644 lib/stats.js diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..88a8dbe --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - 6 diff --git a/README.md b/README.md index 5877143..fe73064 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,29 @@ -# championgg-api-node -A node module wrapper for the Champion.GG API +# Node.js Client for the Champion.GG API + +[![NPM](https://nodei.co/npm/@solomid/node-gg.png?mini=true)](https://www.npmjs.com/package/@solomid/node-gg) + +[![Build Status via Travis CI](https://travis-ci.org/solomidnet/championgg-api-node.svg?branch=master)](https://travis-ci.org/solomidnet/championgg-api-node) + +The official Champion.GG API Node.js Client. # installation ``` npm i @solomid/node-gg ``` + + If you really need an ES5 version: + ``` + npm i @solomid/node-gg@1.1.0 + ``` # usage First, instantiate the wrapper with your api key ``` - var GG = require('node-gg'); - var gg = GG.init('YOUR_KEY'); + const GG = require('node-gg'); + const gg = GG.init('YOUR_KEY'); ``` -Then you gain access to all current endpoints of the API! All but initialization require a callback function and some also require either `role` or `champion_name`, along with an optional object with ...options. These options are passed as query string params and are `page` and `limit` +Then you gain access to all current endpoints of the API! All but initialization require a callback function and some also require either `role` or `champion_name`, along with an optional object with ...options. These options are passed as query string params and are `page` and `limit`. The methods are: @@ -58,6 +68,3 @@ Method | Parameters | ## contributing Feel free to contribute, let's just try to keep it readable :) - -## Release History - * 1.0.0 Initial release diff --git a/index.js b/index.js index e09e5c5..745ebaf 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,15 @@ -var stats = require('./lib/stats'); -var champs = require('./lib/champs'); +const champions = require('./lib/champions'); +const statistics = require('./lib/statistics'); -var key = null; +function init(key) { + if (!key) throw new Error('API Key Required'); + + statistics.init(key); + champions.init(key); -function init(_key) { - if (!_key) throw new Error('API Key required'); - key = _key; - stats.init(key); - champs.init(key); return { - statistics: stats, - champions: champs + statistics: statistics, + champions: champions }; } diff --git a/lib/champions.js b/lib/champions.js new file mode 100644 index 0000000..add9208 --- /dev/null +++ b/lib/champions.js @@ -0,0 +1,162 @@ +const { request, resolve, error } = require('./utils'); + +const base = '/champion'; + +function all (cb) { + request(base, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function specific(name, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + + request(`${base}/${name}`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function general(name, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + + resolve(`${base}/${name}/general`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function generalMatchup (name, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + + request(`${base}/${name}/matchup`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function specificMatchup (name, enemy, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + if (!enemy || typeof enemy !== 'string') throw new Error(error.enemy); + + request(`${base}/${name}/matchup/${enemy}`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function mostPopularFinishedItems(name, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + + request(`${base}/${name}/items/finished/mostPopular`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function mostWinsFinishedItems(name, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + + request(`${base}/${name}/items/finished/mostWins`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function mostWinsStartingItems(name, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + + request(`${base}/${name}/items/starters/mostWins`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function mostPopularStartingItems(name, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + + request(`${base}/${name}/items/starters/mostPopular`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function skillsInfo(name, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + + request(`${base}/${name}/skills`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function mostWinsSkills(name, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + + request(`${base}/${name}/skills/mostWins`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function mostPopularSkills(name, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + + request(`${base}/${name}/skills/mostPopular`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function mostPopularRunes (name, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + + request(`${base}/${name}/runes/mostPopular`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function mostWinningRunes (name, cb) { + if (!name || typeof name !== 'string') throw new Error(error.champ); + + request(`${base}/${name}/runes/mostWins`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +module.exports = { + init: key => module.key = key, + all: all, + data: { + general: general, + specific: specific + }, + skills: { + info: skillsInfo, + order: { + popular: mostPopularSkills, + winning: mostWinsSkills + } + }, + items: { + starting: { + popular: mostPopularStartingItems, + winning: mostWinsStartingItems + }, + finished: { + popular: mostPopularFinishedItems, + winning: mostWinsFinishedItems + } + }, + runes: { + popular: mostPopularRunes, + winning: mostWinningRunes + }, + matchups: { + general: generalMatchup, + specific: specificMatchup + } +} diff --git a/lib/champs.js b/lib/champs.js deleted file mode 100644 index a317e0f..0000000 --- a/lib/champs.js +++ /dev/null @@ -1,202 +0,0 @@ -var needle = require('needle'); -var printf = require('sprintf-js').sprintf; -var utils = require('./utils'); - -var _key = null; -var url = 'http://api.champion.gg/champion'; - -function init(key) { - _key = key; -} - -function all (cb) { - if (!_key) throw new Error('No API key'); - needle.get(printf("%s?api_key=%s", url, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -function specific(name, cb) { - if (!name) throw new Error('Champion name missing'); - needle.get(printf("%s/%s?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -function general(name, cb) { - if (!name) throw new Error('Champion name missing'); - needle.get(printf("%s/%s/general?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -var data = { - specific: specific, - general: general -}; - -var matchups = { - general: generalMatchup, - specific: specificMatchup -}; - -function generalMatchup (name, cb) { - if (!name) throw new Error('Champion name missing'); - needle.get(printf("%s/%s/matchup?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -function specificMatchup (name, enemy, cb) { - if (!name) throw new Error('Champion name missing'); - if (!enemy) throw new Error('Enemy champion name missing'); - needle.get(printf("%s/%s/matchup/%s?api_key=%s", url, name, enemy, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -function matchups() { - if (!_key) throw new Error('No API key'); - return { - general: generalMatchup, - specific: specificMatchup - }; -} - -var items = { - starting: { - popular: mostPopularStartingItems, - winning: mostWinsStartingItems - }, - finished: { - popular: mostPopularFinishedItems, - winning: mostWinsFinishedItems - } -}; - -function mostPopularFinishedItems(name, cb) { - if (!name) throw new Error('Champion name missing'); - needle.get(printf("%s/%s/items/finished/mostPopular?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -function mostWinsFinishedItems(name, cb) { - if (!name) throw new Error('Champion name missing'); - needle.get(printf("%s/%s/items/finished/mostWins?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -function mostWinsStartingItems(name, cb) { - if (!name) throw new Error('Champion name missing'); - needle.get(printf("%s/%s/items/starters/mostWins?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -function mostPopularStartingItems(name, cb) { - if (!name) throw new Error('Champion name missing'); - needle.get(printf("%s/%s/items/starters/mostPopular?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -var skills = { - info: skillsInfo, - order: { - popular: mostPopularSkills, - winning: mostWinsSkills - } -}; - -function skillsInfo(name, cb) { - if (!name) throw new Error('Champion name missing'); - needle.get(printf("%s/%s/skills?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -function mostWinsSkills(name, cb) { - if (!name) throw new Error('Champion name missing'); - needle.get(printf("%s/%s/skills/mostWins?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - - -function mostPopularSkills(name, cb) { - if (!name) throw new Error('Champion name missing'); - needle.get(printf("%s/%s/skills/mostPopular?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -var runes = { - popular: mostPopularRunes, - winning: mostWinningRunes -}; - -function mostPopularRunes (name, cb) { - if (!name) throw new Error('Champion name missing'); - needle.get(printf("%s/%s/runes/mostPopular?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -function mostWinningRunes (name, cb) { - if (!name) throw new Error('Champion name missing'); - needle.get(printf("%s/%s/runes/mostWins?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -module.exports.init = init; -module.exports.all = all; -module.exports.data = data; -module.exports.skills = skills; -module.exports.items = items; -module.exports.runes = runes; -module.exports.matchups = matchups; diff --git a/lib/statistics.js b/lib/statistics.js new file mode 100644 index 0000000..004d3d3 --- /dev/null +++ b/lib/statistics.js @@ -0,0 +1,202 @@ +const { request, resolve, sanitize, error } = require('./utils'); + +const base = '/stats'; +const dq = { + page: 1, + limit: 10 +} + + +function general (cb) { + request(base, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function all(cb) { + request(`${base}/role`, module.key, (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function roleLeastImproved(role, query, cb) { + if (!role || typeof role !== 'string') throw new Error(error.role); + + ({ query, cb } = sanitize(query, cb)); + + request(`${base}/role/${role}/leastImproved`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function roleMostImproved(role, query, cb) { + if (!role || typeof role !== 'string') throw new Error(error.role); + + ({ query, cb } = sanitize(query, cb)); + + request(`${base}/role/${role}/mostImproved`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function roleMostWins(role, query, cb) { + if (!role || typeof role !== 'string') throw new Error(error.role); + + ({ query, cb } = sanitize(query, cb)); + + request(`${base}/role/${role}/mostWinning`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function roleLeastWins(role, query, cb) { + if (!role || typeof role !== 'string') throw new Error(error.role); + + ({ query, cb } = sanitize(query, cb)); + + request(`${base}/role/${role}/leastWinning`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function roleBestPerformance(role, query, cb) { + if (!role || typeof role !== 'string') throw new Error(error.role); + + ({ query, cb } = sanitize(query, cb)); + + request(`${base}/role/${role}/bestPerformance`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function roleWorstPerformance(role, query, cb) { + if (!role || typeof role !== 'string') throw new Error(error.role); + + ({ query, cb } = sanitize(query, cb)); + + request(`${base}/role/${role}/worstPerformance`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function champData(name, cb) { + if (!name || typeof name != 'string') throw new Error(error.champ); + + request(`${base}/champs/${name}`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function champMostWins(query, cb) { + ({ query, cb } = sanitize(query, cb)); + + request(`${base}/champs/mostWinning`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function champLeastWins(query, cb) { + ({ query, cb } = sanitize(query, cb)); + + request(`${base}/champs/leastWinning`, module.key, Object,assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function champMostPlayed(query, cb) { + ({ query, cb } = sanitize(query, cb)); + + request(`${base}%s/champs/mostPlayed`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function champLeastPlayed(query, cb) { + ({ query, cb } = sanitize(query, cb)); + + request(`${base}/champs/leastPlayed`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function champMostBanned(query, cb) { + ({ query, cb } = sanitize(query, cb)); + + request(`${base}/champs/mostBanned`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function champBestRated(query, cb) { + ({ query, cb } = sanitize(query, cb)); + + request(`${base}/champs/bestRated`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +function champWorstRated(query, cb) { + ({ query, cb } = sanitize(query, cb)); + + request(`${base}/champs/worstRated`, module.key, Object.assign(dq, query), (err, res) => { + if (err) throw err; + resolve(res, cb); + }); +} + +module.exports = { + init: key => module.key = key, + all: general, + role: { + all: all, + specific: { + improvement: { + least: roleLeastImproved, + most: roleMostImproved + }, + winning: { + least: roleLeastWins, + most: roleMostWins + }, + performance: { + best: roleBestPerformance, + worst: roleWorstPerformance + } + } + }, + champs: { + specific: champData, + general: { + winning: { + most: champMostWins, + least: champLeastWins + }, + played: { + most: champMostPlayed, + least: champLeastPlayed + }, + banned: { + most: champMostBanned + }, + rating: { + best: champBestRated, + worst: champWorstRated + } + } + } +} diff --git a/lib/stats.js b/lib/stats.js deleted file mode 100644 index 3e322dc..0000000 --- a/lib/stats.js +++ /dev/null @@ -1,219 +0,0 @@ -var needle = require('needle'); -var printf = require('sprintf-js').sprintf; -var utils = require('./utils'); - -var _key = null; -var url = 'http://api.champion.gg/stats'; - -function init(key) { - _key = key; -} - -var role = { - all: all, - specific: { - improvement: { - least: roleLeastImproved, - most: roleMostImproved - }, - winning: { - least: roleLeastWins, - most: roleMostWins - }, - performance: { - best: roleBestPerformance, - worst: roleWorstPerformance - } - } -}; - -function general (cb) { - needle.get(printf("%s?api_key=%s", url, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -function all(cb) { - needle.get(printf("%s/role?api_key=%s", url, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -function roleLeastImproved(role, options, cb) { - var params = utils.cleanParams(role, options, cb); - needle.get(printf("%s/role/%s/leastImproved?api_key=%s&page=%i&limit=%i", url, params.role, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -function roleMostImproved(role, options, cb) { - var params = utils.cleanParams(role, options, cb); - needle.get(printf("%s/role/%s/mostImproved?api_key=%s&page=%i&limit=%i", url, params.role, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -function roleMostWins(role, options, cb) { - var params = utils.cleanParams(role, options, cb); - needle.get(printf("%s/role/%s/mostWinning?api_key=%s&page=%i&limit=%i", url, params.role, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -function roleLeastWins(role, options, cb) { - var params = utils.cleanParams(role, options, cb); - needle.get(printf("%s/role/%s/leastWinning?api_key=%s&page=%i&limit=%i", url, params.role, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -function roleBestPerformance(role, options, cb) { - var params = utils.cleanParams(role, options, cb); - needle.get(printf("%s/role/%s/bestPerformance?api_key=%s&page=%i&limit=%i", url, params.role, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -function roleWorstPerformance(role, options, cb) { - var params = utils.cleanParams(role, options, cb); - needle.get(printf("%s/role/%s/worstPerformance?api_key=%s&page=%i&limit=%i", url, params.role, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -var champs = { - specific: champData, - general: { - winning: { - most: champMostWins, - least: champLeastWins - }, - played: { - most: champMostPlayed, - least: champLeastPlayed - }, - banned: { - most: champMostBanned - }, - rating: { - best: champBestRated, - worst: champWorstRated - } - } -}; - -function champData(name, cb) { - if (!name) throw new Error('Champion name required'); - needle.get(printf("%s/champs/%s?api_key=%s", url, name, _key), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, cb); - }); -} - -function champMostWins(options, cb) { - var params = utils.cleanParams('pass', options, cb); - - needle.get(printf("%s/champs/mostWinning?api_key=%s&page=%i&limit=%i", url, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -function champLeastWins(options, cb) { - var params = utils.cleanParams('pass', options, cb); - - needle.get(printf("%s/champs/leastWinning?api_key=%s&page=%i&limit=%i", url, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -function champMostPlayed(options, cb) { - var params = utils.cleanParams('pass', options, cb); - - needle.get(printf("%s/champs/mostPlayed?api_key=%s&page=%i&limit=%i", url, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -function champLeastPlayed(options, cb) { - var params = utils.cleanParams('pass', options, cb); - - needle.get(printf("%s/champs/leastPlayed?api_key=%s&page=%i&limit=%i", url, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -function champMostBanned(options, cb) { - var params = utils.cleanParams('pass', options, cb); - - needle.get(printf("%s/champs/mostBanned?api_key=%s&page=%i&limit=%i", url, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -function champBestRated(options, cb) { - var params = utils.cleanParams('pass', options, cb); - - needle.get(printf("%s/champs/bestRated?api_key=%s&page=%i&limit=%i", url, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -function champWorstRated(options, cb) { - var params = utils.cleanParams('pass', options, cb); - - needle.get(printf("%s/champs/worstRated?api_key=%s&page=%i&limit=%i", url, _key, params.options.page, params.options.limit), function (err, response) { - if (err) { - throw err; - } - utils.manageResponse(response, params.cb); - }); -} - -module.exports.init = init; -module.exports.all = general; -module.exports.role = role; -module.exports.champs = champs; diff --git a/lib/utils.js b/lib/utils.js index 6a8f365..0d9f9ff 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,44 +1,95 @@ -var _ = require('underscore'); - -function manageResponse(resp, cb) { - if (!resp) cb(new Error('No response')); - if (resp.statusCode === 200) { - return cb(null, resp.body); - } else if (resp.statusCode === 403) { - return cb({ - error: 'AuthError' +const http = require('http'); +const qs = require('querystring'); + +const hostname = 'api.champion.gg'; +const port = 80; + +function request(route, key, query, cb) { + if (!key) throw 'API Key Required'; + + ({ query, cb } = sanitize(query, cb)); + + const path = `${route}?api_key=${key}&${qs.stringify(query)}`; + const req = http.request({ + hostname: hostname, + port: port, + path: path, + method: 'GET', + headers: { 'Content-Type': 'application/json' } + }, (res) => { + let data = ''; + res.on('data', chunk => data += chunk); + + res.on('end', () => { + if (cb) cb(null, { + body: data, + headers: res.headers, + hostname: hostname, + method: 'GET', + port: port, + route: route, + status: res.statusCode, + url: `${hostname}${path}` + }); + }); + + res.on('error', (e) => { + if (cb) cb(e); + }); }); - } else if (resp.statusCode === 400 || resp.statusCode === 404 || resp.statusCode === 500) { - return cb(resp.body); - } else { - console.trace(); - return cb({ - error: 'Unknown error' + + req.end(); +} + +function resolve(res, cb) { + if (!res) cb({ + error: 'API Failure - No Response' }); - } + + switch(res.status) { + case 200: + return cb(null, JSON.parse(res.body)); + case 403: + return cb({ + status: res.status, + error: 'Authentication Error' + }); + case 400: + case 404: + case 500: + console.log(res.url) + return cb({ + status: res.status, + error: res.body + }); + default: + console.trace(); + return cb({ + status: res.status, + error: 'Unknown Error' + }); + } } -function cleanParams (role, options, cb) { - if (role != 'pass' | !role || typeof(role) !== 'string') { - throw new Error('Champion role needed'); - } - - if (typeof(options) === 'function') { - cb = options; - options = {}; - } - - options = _.extend({ - page: 1, - limit: 10 - }, options); - - return { - role: role, - options: options, - cb: cb - }; +function sanitize(query, cb) { + if (query instanceof Function) { + cb = query; + query = {}; + } + + return { + cb: cb, + query: query + } } -module.exports.manageResponse = manageResponse; -module.exports.cleanParams = cleanParams; +module.exports = { + request: request, + resolve: resolve, + sanitize: sanitize, + error: { + champ: 'Champion name missing (must be a string)', + enemy: 'Enemy champion name missing (must be a string)', + role: 'Role missing (must be a string)' + } +} diff --git a/package.json b/package.json index ffd8432..1d0a0a1 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,8 @@ { "name": "@solomid/node-gg", - "version": "1.1.0", - "description": "Node wrapper for champion.gg API", + "version": "2.0.0", + "description": "Node.js Client for champion.gg API", "main": "index.js", - "directories": { - "test": "test" - }, "scripts": { "test": "mocha" }, @@ -15,15 +12,9 @@ "http", "api" ], - "author": "Alejandro Baltra", + "author": "Pradyuman Vig", "license": "MIT", - "dependencies": { - "needle": "^0.11.0", - "sprintf-js": "^1.0.3", - "underscore": "^1.8.3" - }, "devDependencies": { - "assert": "^1.3.0", "chai": "^3.4.1", "mocha": "^2.3.4" }, diff --git a/test/test.js b/test/test.js index c33f2d6..7ff3b34 100644 --- a/test/test.js +++ b/test/test.js @@ -1,27 +1,14 @@ -var assert = require('assert'); -var GG = require('../index.js'); -var chai = require('chai'); -var expect = chai.expect; +const assert = require('chai').assert; +const GG = require('../index.js'); -describe('Failed calls', function () { - it('Missing key', function (done) { - expect(GG.init).to.throw('API Key required'); - done(); - }); - - it('Empty key', function (done) { - expect(GG.init.bind(GG, '')).to.throw('API Key required'); - done(); - }); +context('API Key Testing', function () { + specify('Missing key', function (done) { + assert.throws(GG.init, Error, 'API Key Required'); + done(); + }); - it('Wrong key', function (done) { - var gg = GG.init('12345'); - gg.statistics.all(function (err, res) { - expect(err).to.exist; - expect(err.error).to.equal('AuthError'); - done(); + specify('Empty key', function (done) { + assert.throws(GG.init.bind(GG, ''), Error, 'API Key Required'); + done(); }); - }); }); - -//All working calls are simple http gets, no need to throttle servers on test