diff --git a/components/help.js b/components/help.js index c0d6a86..d43caee 100644 --- a/components/help.js +++ b/components/help.js @@ -1,3 +1,7 @@ +const StdLib = require('@doctormckay/stdlib'); +// eslint-disable-next-line no-unused-vars +const {HttpResponse} = require('@doctormckay/stdlib/http'); + const SteamCommunity = require('../index.js'); const HELP_SITE_DOMAIN = 'https://help.steampowered.com'; @@ -5,18 +9,24 @@ const HELP_SITE_DOMAIN = 'https://help.steampowered.com'; /** * Restore a previously removed steam package from your steam account. * @param {int|string} packageID - * @param {function} callback + * @param {function} [callback] + * @return Promise */ SteamCommunity.prototype.restorePackage = function(packageID, callback) { - this.httpRequestPost({ - uri: HELP_SITE_DOMAIN + '/wizard/AjaxDoPackageRestore', - form: { - packageid: packageID, - sessionid: this.getSessionID(HELP_SITE_DOMAIN), - wizard_ajax: 1 - }, - json: true - }, wizardAjaxHandler(callback)); + return StdLib.Promises.callbackPromise(null, callback, true, async (resolve, reject) => { + let result = await this.httpRequest({ + method: 'POST', + url: `${HELP_SITE_DOMAIN}/wizard/AjaxDoPackageRestore`, + form: { + packageid: packageID, + sessionid: this.getSessionID(HELP_SITE_DOMAIN), + wizard_ajax: 1 + }, + source: 'steamcommunity' + }); + + wizardAjaxHandler(result, resolve, reject); + }); }; /** @@ -25,38 +35,32 @@ SteamCommunity.prototype.restorePackage = function(packageID, callback) { * @param {function} callback */ SteamCommunity.prototype.removePackage = function(packageID, callback) { - this.httpRequestPost({ - uri: HELP_SITE_DOMAIN + '/wizard/AjaxDoPackageRemove', - form: { - packageid: packageID, - sessionid: this.getSessionID(HELP_SITE_DOMAIN), - wizard_ajax: 1 - }, - json: true - }, wizardAjaxHandler(callback)); + return StdLib.Promises.callbackPromise(null, callback, true, async (resolve, reject) => { + let result = await this.httpRequest({ + method: 'POST', + url: `${HELP_SITE_DOMAIN}/wizard/AjaxDoPackageRemove`, + form: { + packageid: packageID, + sessionid: this.getSessionID(HELP_SITE_DOMAIN), + wizard_ajax: 1 + }, + source: 'steamcommunity' + }); + + wizardAjaxHandler(result, resolve, reject); + }); }; /** - * Returns a handler for wizard ajax HTTP requests. - * @param {function} callback - * @returns {(function(*=, *, *): void)|*} + * + * @param {HttpResponse} result + * @param {function} resolve + * @param {function} reject */ -function wizardAjaxHandler(callback) { - return (err, res, body) => { - if (!callback) { - return; - } +function wizardAjaxHandler(result, resolve, reject) { + if (!result.jsonBody || !result.jsonBody.success) { + return reject(new Error((result.jsonBody || {}).errorMsg || 'Unexpected error')); + } - if (err) { - callback(err); - return; - } - - if (!body.success) { - callback(new Error(body.errorMsg || 'Unexpected error')); - return; - } - - callback(null); - }; + resolve(); } diff --git a/index.js b/index.js index dc52723..8008d90 100644 --- a/index.js +++ b/index.js @@ -132,9 +132,9 @@ SteamCommunity.prototype.setCookies = function(cookies) { this._verifyMobileAccessToken(); }; -SteamCommunity.prototype.getSessionID = function() { +SteamCommunity.prototype.getSessionID = function(domain = 'steamcommunity.com') { let sessionIdCookie = this._jar.cookies - .filter(c => c.domain == 'steamcommunity.com') + .filter(c => c.domain == domain) .find(c => c.name == 'sessionid'); if (sessionIdCookie) { return sessionIdCookie.content;