camelot-unchained
Version:
Camelot Unchained Client Library
98 lines (87 loc) • 4.49 kB
JavaScript
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
require('isomorphic-fetch');
var CoreSettings_1 = require('../core/CoreSettings');
var channelId_1 = require('../core/constants/channelId');
var client_1 = require('../core/client');
var events_1 = require('../events');
var RestUtil = require('./RestUtil');
// TODO remove this when the API's are updated
// TODO: I wanted this to extend CoreSettings but CoreSettings
// won't allow super to access its memebers, or pass anything
// but a default CoreSettings object to its constructor, so
// you can't customise the settings at all (e.g. like define
// the api key or current channel)
var Settings = function () {
function Settings(channel) {
_classCallCheck(this, Settings);
this.core = new CoreSettings_1.default(); // TODO: This class is a bit weird
this.channelId = channel;
this.timeout = 2000; // default timeout
switch (channel) {
case channelId_1.default.HATCHERY:
this.url = 'hatchery.camelotunchained.com';
// BUG: (returns https://) this.url = this.core.hatcheryApiUrl;
this.port = this.core.hatcheryApiPort;
break;
case channelId_1.default.WYRMLING:
this.url = 'wyrmling.camelotunchained.com';
// BUG: (returns https://) this.url = this.core.wyrmlingApiUrl;
this.port = this.core.wyrmlingApiPort;
break;
}
}
_createClass(Settings, [{
key: 'getApiKey',
value: function getApiKey() {
if (!this.apiKey) {
this.apiKey = client_1.default.loginToken; // in fake API will prompt for token
}
return this.apiKey;
}
}]);
return Settings;
}();
// default to Hatchery
var settings = new Settings(4);
if (client_1.hasClientAPI()) {
events_1.default.on('init', function () {
settings = new Settings(client_1.default.patchResourceChannel);
});
}
function makeAPIUrl(endpoint, useHttps) {
if (endpoint.indexOf('://') != -1) return endpoint; // we already have a fully formed url, skip
var protocol = useHttps ? 'https' : 'http';
var port = useHttps ? '4443' : '8000';
return protocol + '://' + settings.url + ':' + port + '/api/' + endpoint.replace(/^\//, '');
}
function getJSON(endpoint) {
var useHttps = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
var query = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
return fetch(RestUtil.makeQueryString(makeAPIUrl(endpoint, useHttps), query)).then(RestUtil.checkStatus).then(RestUtil.parseJSON);
}
exports.getJSON = getJSON;
// old API requires loginToken to be in the data object
function postJSON(endpoint) {
var useHttps = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
var requireAuth = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2];
var data = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
var version = arguments.length <= 4 || arguments[4] === undefined ? 1 : arguments[4];
return fetch(makeAPIUrl(endpoint, useHttps), {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'api-version': '' + version,
'loginToken': client_1.default.loginToken
},
body: JSON.stringify(data)
}).then(RestUtil.checkStatus).then(RestUtil.parseJSON);
}
exports.postJSON = postJSON;