Updated help.js methods to use new http interface

This commit is contained in:
Alex Corn 2023-06-27 03:50:52 -04:00
parent e9bc778e2a
commit 96e3e7ebc0
No known key found for this signature in database
GPG Key ID: E51989A3E7A27FDF
2 changed files with 45 additions and 41 deletions

View File

@ -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<void>
*/
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();
}

View File

@ -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;