Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHawkinss committed Oct 18, 2020
1 parent 4bd9e8e commit 357586c
Show file tree
Hide file tree
Showing 6 changed files with 835 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
37 changes: 37 additions & 0 deletions examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const MulticraftAPI = require('./index');

const api = new MulticraftAPI({
url: "http://localhost/api.php",
user: "admin",
key: "5kd9HaPid@mWqK"
});

async function examples() {
try {
const listServers = await api.listServers();
console.log(listServers);
// {
// success: true,
// errors: [],
// data: {
// Servers: { '1': 'test', '2': 'Minecraft Server', '3': 'Minecraft Server' }
// }
// }
} catch (e) {
console.log(e);
}

try {
const listServersByConnection = await api.listServersByConnection();
console.log(listServersByConnection);
// {
// success: true,
// errors: [],
// data: { Servers: { '1': 'test', '2': 'Minecraft Server' } }
// }
} catch (e) {
console.log(e);
}
}

examples()
102 changes: 102 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const API_METHODS = require('./methods.json');
const got = require('got');
const crypto = require('crypto');

class MulticraftAPI {
constructor({ url, user, key }) {
if (typeof url === "undefined") throw new Error('Missing URL')
if (typeof user === "undefined") throw new Error('Missing user')
if (typeof key === "undefined") throw new Error('Missing key')

this.url = url;
this.user = user;
this.key = key;

Object.keys(API_METHODS).forEach((key) => {
this[key] = this._createMethod(key, API_METHODS[key]).bind(this)
});
}

_createMethod(methodName, method) {
return async (data = {}) => {
// check if data is an object
if (typeof data !== 'object') {
throw new Error('Argument should be an object with the required parameters.');
}

// generate calling arguments
const params = method.map((param, i) => {
let out = {};

// if parameter is string, return immediately
if (typeof param == "string") {
out.name = param;

if (typeof data[out.name] == "undefined")
throw new Error(`Missing argument: ${out.name}`);

out.value = data[out.name]
return out;
}

// set returned name
out.name = param.name;

// error if argument is undefined
if (typeof param.default == "undefined" && typeof data[out.name] == "undefined") {
throw new Error(`Missing argument: ${out.name}`);
}

// set default value
if (typeof param.default !== "undefined")
out.value = param.default;

// set passed in value if exists
if (typeof data[out.name] !== "undefined") {
out.value = data[out.name];

// if array type, turn into json string
if (param.type === 'array') {
out.value = JSON.stringify(out.value)
}
}

return out;
});

return await this._call(methodName, params);
}
}

_generateSignature(params) {
const signature = Object.keys(params).reduce((p, key) => (
p + key + params[key]
), "");

return crypto.createHmac('sha256', this.key).update(signature).digest('hex');
}

// TODO cleanup errors
_call(method, params) {
// convert to object
params = params.reduce((p,v) => {
p[v.name] = v.value;
return p;
}, {});

return new Promise(async (resolve, reject) => {
params._MulticraftAPIMethod = method;
params._MulticraftAPIUser = this.user;
params._MulticraftAPIKey = this._generateSignature(params);

const res = await got.post(this.url, {
form: params
});
const data = JSON.parse(res.body);
if (!data.success) return reject(res);
resolve(data);
})
}
}

module.exports = MulticraftAPI;
Loading

0 comments on commit 357586c

Please sign in to comment.