diff --git a/components/helpers.js b/components/helpers.js index 5c3f705..0dd0e6c 100644 --- a/components/helpers.js +++ b/components/helpers.js @@ -39,15 +39,16 @@ exports.decodeSteamTime = function(time) { /** * Get an Error object for a particular EResult * @param {int} eresult + * @param {string} [message] - If eresult is a failure code and message exists, this message will be used in the Error object instead * @returns {null|Error} */ -exports.eresultError = function(eresult) { - if (eresult == EResult.OK) { +exports.eresultError = function(eresult, message) { + if (!eresult || eresult == EResult.OK) { // no error return null; } - let err = new Error(EResult[eresult] || ("Error " + eresult)); + let err = new Error(message || EResult[eresult] || ("Error " + eresult)); err.eresult = eresult; return err; }; diff --git a/components/market.js b/components/market.js index a56c56e..15b1632 100644 --- a/components/market.js +++ b/components/market.js @@ -8,8 +8,7 @@ const Helpers = require('./helpers.js'); * @param {function} callback - First argument is null|Error, second is an object of appid => name */ SteamCommunity.prototype.getMarketApps = function(callback) { - var self = this; - this.httpRequest('https://steamcommunity.com/market/', function (err, response, body) { + this.httpRequest('https://steamcommunity.com/market/', (err, response, body) => { if (err) { callback(err); return; @@ -19,17 +18,17 @@ SteamCommunity.prototype.getMarketApps = function(callback) { if ($('.market_search_game_button_group')) { let apps = {}; $('.market_search_game_button_group a.game_button').each(function (i, element) { - var e = Cheerio.load(element); - var name = e('.game_button_game_name').text().trim(); - var url = element.attribs.href; - var appid = url.substr(url.indexOf('=') + 1); + let e = Cheerio.load(element); + let name = e('.game_button_game_name').text().trim(); + let url = element.attribs.href; + let appid = url.substr(url.indexOf('=') + 1); apps[appid] = name; }); callback(null, apps); } else { - callback(new Error("Malformed response")); + callback(new Error('Malformed response')); } - }, "steamcommunity"); + }, 'steamcommunity'); }; /** @@ -40,34 +39,32 @@ SteamCommunity.prototype.getMarketApps = function(callback) { */ SteamCommunity.prototype.getGemValue = function(appid, assetid, callback) { this._myProfile({ - "endpoint": "ajaxgetgoovalue/", - "qs": { - "sessionid": this.getSessionID(), - "appid": appid, - "contextid": 6, - "assetid": assetid + endpoint: 'ajaxgetgoovalue/', + qs: { + sessionid: this.getSessionID(), + appid: appid, + contextid: 6, + assetid: assetid }, - "checkHttpError": false, - "json": true + checkHttpError: false, + json: true }, null, (err, res, body) => { if (err) { callback(err); return; } - if (body.success && body.success != SteamCommunity.EResult.OK) { - let err = new Error(body.message || SteamCommunity.EResult[body.success]); - err.eresult = err.code = body.success; - callback(err); - return; + let err2 = Helpers.eresultError(body.success, body.message); + if (err2) { + return callback(err2); } if (!body.goo_value || !body.strTitle) { - callback(new Error("Malformed response")); + callback(new Error('Malformed response')); return; } - callback(null, {"promptTitle": body.strTitle, "gemValue": parseInt(body.goo_value, 10)}); + callback(null, {promptTitle: body.strTitle, gemValue: parseInt(body.goo_value, 10)}); }); }; @@ -80,34 +77,32 @@ SteamCommunity.prototype.getGemValue = function(appid, assetid, callback) { */ SteamCommunity.prototype.turnItemIntoGems = function(appid, assetid, expectedGemsValue, callback) { this._myProfile({ - "endpoint": "ajaxgrindintogoo/", - "json": true, - "checkHttpError": false + endpoint: 'ajaxgrindintogoo/', + json: true, + checkHttpError: false }, { - "appid": appid, - "contextid": 6, - "assetid": assetid, - "goo_value_expected": expectedGemsValue, - "sessionid": this.getSessionID() + appid: appid, + contextid: 6, + assetid: assetid, + goo_value_expected: expectedGemsValue, + sessionid: this.getSessionID() }, (err, res, body) => { if (err) { callback(err); return; } - if (body.success && body.success != SteamCommunity.EResult.OK) { - let err = new Error(body.message || SteamCommunity.EResult[body.success]); - err.eresult = err.code = body.success; - callback(err); + let err2 = Helpers.eresultError(body.success, body.message); + if (err2) { + return callback(err2); + } + + if (!body['goo_value_received '] || !body.goo_value_total) { // lol valve, that trailing space is real + callback(new Error('Malformed response')); return; } - if (!body['goo_value_received '] || !body.goo_value_total) { // lol valve - callback(new Error("Malformed response")); - return; - } - - callback(null, {"gemsReceived": parseInt(body['goo_value_received '], 10), "totalGems": parseInt(body.goo_value_total, 10)}); + callback(null, {gemsReceived: parseInt(body['goo_value_received '], 10), totalGems: parseInt(body.goo_value_total, 10)}); }) }; @@ -119,28 +114,26 @@ SteamCommunity.prototype.turnItemIntoGems = function(appid, assetid, expectedGem */ SteamCommunity.prototype.openBoosterPack = function(appid, assetid, callback) { this._myProfile({ - "endpoint": "ajaxunpackbooster/", - "json": true, - "checkHttpError": false + endpoint: 'ajaxunpackbooster/', + json: true, + checkHttpError: false }, { - "appid": appid, - "communityitemid": assetid, - "sessionid": this.getSessionID() + appid: appid, + communityitemid: assetid, + sessionid: this.getSessionID() }, (err, res, body) => { if (err) { callback(err); return; } - if (body.success && body.success != SteamCommunity.EResult.OK) { - let err = new Error(body.message || SteamCommunity.EResult[body.success]); - err.eresult = err.code = body.success; - callback(err); - return; + let err2 = Helpers.eresultError(body.success, body.message); + if (err2) { + return callback(err2); } if (!body.rgItems) { - callback(new Error("Malformed response")); + callback(new Error('Malformed response')); return; } @@ -155,33 +148,31 @@ SteamCommunity.prototype.openBoosterPack = function(appid, assetid, callback) { */ SteamCommunity.prototype.getGiftDetails = function(giftID, callback) { this.httpRequestPost({ - "uri": "https://steamcommunity.com/gifts/" + giftID + "/validateunpack", - "form": { - "sessionid": this.getSessionID() + uri: `https://steamcommunity.com/gifts/${giftID}/validateunpack`, + form: { + sessionid: this.getSessionID() }, - "json": true + json: true }, (err, res, body) => { if (err) { callback(err); return; } - if (body.success && body.success != SteamCommunity.EResult.OK) { - let err = new Error(body.message || SteamCommunity.EResult[body.success]); - err.eresult = err.code = body.success; - callback(err); - return; + let err2 = Helpers.eresultError(body.success, body.message); + if (err2) { + return callback(err2); } if (!body.packageid || !body.gift_name) { - callback(new Error("Malformed response")); + callback(new Error('Malformed response')); return; } callback(null, { - "giftName": body.gift_name, - "packageID": parseInt(body.packageid, 10), - "owned": body.owned + giftName: body.gift_name, + packageID: parseInt(body.packageid, 10), + owned: body.owned }); }); }; @@ -193,23 +184,20 @@ SteamCommunity.prototype.getGiftDetails = function(giftID, callback) { */ SteamCommunity.prototype.redeemGift = function(giftID, callback) { this.httpRequestPost({ - "uri": "https://steamcommunity.com/gifts/" + giftID + "/unpack", - "form": { - "sessionid": this.getSessionID() + uri: `https://steamcommunity.com/gifts/${giftID}/unpack`, + form: { + sessionid: this.getSessionID() }, - "json": true + json: true }, (err, res, body) => { if (err) { callback(err); return; } - if (body.success && body.success != SteamCommunity.EResult.OK) { - - let err = new Error(body.message || SteamCommunity.EResult[body.success]); - err.eresult = err.code = body.success; - callback(err); - return; + let err2 = Helpers.eresultError(body.success, body.message); + if (err2) { + return callback(err2); } callback(null);