humblebundle
Version:
A simple API for reading your Humble Bundle purchases
94 lines (76 loc) • 2.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.login = login;
exports.order = order;
exports.claimed = claimed;
var _qs = require('qs');
var _qs2 = _interopRequireDefault(_qs);
var _fetchCookie = require('fetch-cookie');
var _fetchCookie2 = _interopRequireDefault(_fetchCookie);
var _nodeFetch = require('node-fetch');
var _nodeFetch2 = _interopRequireDefault(_nodeFetch);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// emulate browser fetch
var fetch = (0, _fetchCookie2.default)(_nodeFetch2.default);
var headers = {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
'Keep-Alive': 'true',
'X-Requested-By': 'hb_android_app',
'User-Agent': 'Apache-HttpClient/UNAVAILABLE (java 1.4)'
};
// internal conveneience: GET with proper headers
function get(url) {
var options = {
method: 'GET',
credentials: 'include',
headers: headers
};
return fetch(url, options);
}
// internal conveneience: POST with proper headers
function post(url, params) {
var options = {
method: 'POST',
credentials: 'include',
headers: headers
};
if (params) {
options.body = _qs2.default.stringify(params);
}
return fetch(url, options);
}
// login, set coookies
function login(email, password) {
return post('https://www.humblebundle.com/login', { username: email, password: password }).then(function (res) {
if (res.status !== 200) {
throw new Error('Could not login.');
}
return res;
});
}
// get gamekeys from your library
function order(id) {
var url = id ? 'https://www.humblebundle.com/api/v1/order/' + id + '?unused_tpkds=true' : 'https://www.humblebundle.com/api/v1/user/order';
return get(url).then(function (res) {
return res.json();
}).then(function (body) {
if (id) {
return body;
} else {
return body.map(function (g) {
return g.gamekey;
});
}
});
}
// get list of claimed entities
function claimed() {
return get('https://www.humblebundle.com/api/v1/user/claimed/entities').then(function (res) {
return res.json();
});
}
var api = { login: login, order: order, claimed: claimed };
exports.default = api;