From 677395372369f4489017362617505b6941261d77 Mon Sep 17 00:00:00 2001 From: King-BR Date: Wed, 3 Jun 2020 00:17:32 -0300 Subject: [PATCH 01/10] Experimental cache system Not tested yet --- .gitignore | 4 ++- index.js | 82 +++++++++++++++++++++++++++++++++++++++++++---- package-lock.json | 5 +++ package.json | 1 + 4 files changed, 84 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index b512c09..1be9ed1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -node_modules \ No newline at end of file +node_modules/ +tests/ +Cache/ \ No newline at end of file diff --git a/index.js b/index.js index 60f9eb3..09f83c8 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ const fetch = require('node-fetch'); +const fs = require('fs') const BASE_URL = "https://api.mozambiquehe.re"; @@ -17,10 +18,14 @@ const DIRECTORY = { * * @constructor * @param {String} apiKey Your mozambiquehe.re Auth Key + * @param {Any} [options] Options for the cache system + * @param {Boolean} [options.useCache=true] Whether or not to use the cache system + * @param {Number} [options.cacheLifetime=7200000] Cache lifetime */ -function MozambiqueAPI(apiKey) { + +function MozambiqueAPI(apiKey, options = { useCache: true, cacheLifetime: 7200000}) { if (!apiKey) { - throw new Error("API Key missing"); + throw new Error("mozampique-api-wrapper: API Key missing"); } let self = this; @@ -30,6 +35,27 @@ function MozambiqueAPI(apiKey) { "User-Agent": "mozambique-api-wrapper", "Content-Type": "application/json" }; + + switch (typeof options.useCache) { + case 'boolean': { break; } + default:{ + console.log(`[WARNING] mozampique-api-wrapper: options.useCache expected to be a Boolean (provided: ${typeof options.useCache}). Using default`); + options.useCache = true; + break; + } + } + + switch (typeof options.cacheLifetime) { + case 'number': { break; } + default: { + console.log(`[WARNING] mozampique-api-wrapper: options.cacheLifetime expected to be a Number (provided: ${typeof options.cacheLifetime}). Using default`); + options.cacheLifetime = 7200000; + break; + } + } + + self.useCache = options.useCache; + self.cacheLifetime = options.cacheLifetime } @@ -45,6 +71,48 @@ function request(self, url) { } +async function requestCache(self, url, type) { + function sleep(ms) { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); + } + + if(self.useCache) { + fs.exists('Cache/mozambiqueCache.json', function(existFile) { + if(!existFile) { + fs.exists('Cache', async function(existDir) { + if(!existDir) { + fs.mkdir('Cache', function(err) {}) + } + fs.writeFile('Cache/mozambiqueCache.json', '{}', { encoding: 'utf8' }, function(err) {}) + }) + } + }) + + await sleep(500) + fs.readFile('Cache/mozambiqueCache.json', async function (err, data) { + if (err) return Promise.reject(err); + + data = JSON.parse(data); + if (typeof data[`${type}Generated`] !== 'number' || Date.now() >= (data[`${type}Generated`] + self.cacheLifetime)) { + data[type] = await request(self, url); + data[`${type}Generated`] = Date.now(); + let json = JSON.stringify(data, null, 2); + fs.writeFile('Cache/mozambiqueCache.json', json, { encoding: 'utf8' }, function (err){}); + } + }); + + const json = require('./Cache/mozambiqueCache.json'); + var name = require.resolve('./Cache/mozambiqueCache.json'); + delete require.cache[name]; + return json[type]; + + } else { + return request(self, url); + } +} + /** * Search a player using player name or UID * @@ -92,7 +160,7 @@ MozambiqueAPI.prototype.news = function (lang = "en-us") { MozambiqueAPI.prototype.server = function() { let url = DIRECTORY.SERVER_STATUS - return request(this, url) + return request(this, url); } @@ -119,7 +187,7 @@ MozambiqueAPI.prototype.history = function(query) { } let url = DIRECTORY.MATCH_HISTORY + type + "&platform" + query.platform + "&auth=" + this.apiKey + "&history=1&action=" + query.action; - return request(this, url) + return request(this, url) ; } /** @@ -131,9 +199,9 @@ MozambiqueAPI.prototype.history = function(query) { * @returns {JSON} Json with requested game data */ -MozambiqueAPI.prototype.gamedata = function(dataType) { +MozambiqueAPI.prototype.gamedata = async function(dataType) { let url = DIRECTORY.GAME_DATA + "type=" + dataType + "&auth=" + this.apiKey - return request(this, url) + return await requestCache(this, url, dataType); } /** @@ -144,7 +212,7 @@ MozambiqueAPI.prototype.gamedata = function(dataType) { MozambiqueAPI.prototype.mapRotation = function() { let url = DIRECTORY.MAP_ROTATION + "auth=" + this.apiKey - return request(this, url) + return request(this, url); } module.exports = MozambiqueAPI; diff --git a/package-lock.json b/package-lock.json index a796afd..a3a0000 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,11 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ=" + }, "node-fetch": { "version": "2.6.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", diff --git a/package.json b/package.json index 5992e0f..6b43231 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "API" ], "dependencies": { + "fs": "0.0.1-security", "node-fetch": "^2.6.0" } } From fdea40722e3d4572f2b53e057c124369b4e0075f Mon Sep 17 00:00:00 2001 From: King-BR Date: Wed, 3 Jun 2020 00:19:08 -0300 Subject: [PATCH 02/10] Update version --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index a3a0000..27ea537 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "mozambique-api-wrapper", - "version": "1.2.1", + "version": "1.3.0-beta.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 6b43231..aae2696 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mozambique-api-wrapper", - "version": "1.2.1", + "version": "1.3.0-beta.0", "description": "Wrapper to make accessing mozambiquehe.re APIs when writing code in Javascript (Node.js) much easier.", "main": "index.js", "scripts": { From d54f3e43926c467dc829a2c7e64acb2e7edcdf53 Mon Sep 17 00:00:00 2001 From: King-BR Date: Wed, 3 Jun 2020 00:19:51 -0300 Subject: [PATCH 03/10] 3.1.0-beta.0 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 27ea537..8bace11 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "mozambique-api-wrapper", - "version": "1.3.0-beta.0", + "version": "3.1.0-beta.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index aae2696..43bdf26 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mozambique-api-wrapper", - "version": "1.3.0-beta.0", + "version": "3.1.0-beta.0", "description": "Wrapper to make accessing mozambiquehe.re APIs when writing code in Javascript (Node.js) much easier.", "main": "index.js", "scripts": { From 38fbc8290d9970f19ff17d914664e4e9826c0e3d Mon Sep 17 00:00:00 2001 From: King-BR Date: Wed, 3 Jun 2020 00:21:25 -0300 Subject: [PATCH 04/10] 1.3.0-beta.0 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8bace11..27ea537 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "mozambique-api-wrapper", - "version": "3.1.0-beta.0", + "version": "1.3.0-beta.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 43bdf26..aae2696 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mozambique-api-wrapper", - "version": "3.1.0-beta.0", + "version": "1.3.0-beta.0", "description": "Wrapper to make accessing mozambiquehe.re APIs when writing code in Javascript (Node.js) much easier.", "main": "index.js", "scripts": { From 9a5632dde35eddd8cb4f875d7f767a5f0c8c946f Mon Sep 17 00:00:00 2001 From: King-BR Date: Wed, 3 Jun 2020 20:03:53 -0300 Subject: [PATCH 05/10] Fix returning undefind Fixed returning undefined when using gamedata method for the first time when using cache --- index.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 09f83c8..7ba4183 100644 --- a/index.js +++ b/index.js @@ -79,19 +79,19 @@ async function requestCache(self, url, type) { } if(self.useCache) { - fs.exists('Cache/mozambiqueCache.json', function(existFile) { + fs.exists('./Cache/mozambiqueCache.json', function(existFile) { if(!existFile) { fs.exists('Cache', async function(existDir) { if(!existDir) { fs.mkdir('Cache', function(err) {}) } - fs.writeFile('Cache/mozambiqueCache.json', '{}', { encoding: 'utf8' }, function(err) {}) + fs.writeFile('./Cache/mozambiqueCache.json', '{}', { encoding: 'utf8' }, function(err) {}) }) } }) await sleep(500) - fs.readFile('Cache/mozambiqueCache.json', async function (err, data) { + fs.readFile('./Cache/mozambiqueCache.json', async function (err, data) { if (err) return Promise.reject(err); data = JSON.parse(data); @@ -99,10 +99,11 @@ async function requestCache(self, url, type) { data[type] = await request(self, url); data[`${type}Generated`] = Date.now(); let json = JSON.stringify(data, null, 2); - fs.writeFile('Cache/mozambiqueCache.json', json, { encoding: 'utf8' }, function (err){}); + fs.writeFile('./Cache/mozambiqueCache.json', json, { encoding: 'utf8' }, function (err){}); } }); + await sleep(1100) const json = require('./Cache/mozambiqueCache.json'); var name = require.resolve('./Cache/mozambiqueCache.json'); delete require.cache[name]; From fc916457df3713467fc300db994a27fc8017aeb0 Mon Sep 17 00:00:00 2001 From: King-BR Date: Wed, 3 Jun 2020 20:06:27 -0300 Subject: [PATCH 06/10] v1.3.0-beta.1 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 27ea537..6a414a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "mozambique-api-wrapper", - "version": "1.3.0-beta.0", + "version": "1.3.0-beta.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index aae2696..c269d18 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mozambique-api-wrapper", - "version": "1.3.0-beta.0", + "version": "1.3.0-beta.1", "description": "Wrapper to make accessing mozambiquehe.re APIs when writing code in Javascript (Node.js) much easier.", "main": "index.js", "scripts": { From 6af8207ec64d7d29e761fca758b16e41e0eff87b Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 10 Nov 2020 20:40:25 -0300 Subject: [PATCH 07/10] wip --- .gitignore | 4 +- README.md | 11 ++- index.js | 261 ++++++++++++++++++++++++++--------------------------- 3 files changed, 140 insertions(+), 136 deletions(-) diff --git a/.gitignore b/.gitignore index 1be9ed1..4300af4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules/ tests/ -Cache/ \ No newline at end of file +Cache/ +gamedata/ +gamedataPrettyPrint/ \ No newline at end of file diff --git a/README.md b/README.md index c59d483..767ffad 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ # Mozambique Here API Wrapper -A simple wrapper to use the [Mozambique Here API](https://github.com/HugoDerave/ApexLegendsAPI/blob/master/README.md). Please refer to the API docs to undestand the inputs and expected results. +A simple wrapper to use the [Mozambique Here API](https://github.com/HugoDerave/ApexLegendsAPI/blob/master/README.md). Please refer to the [API docs](https://apexlegendsapi.com/) to undestand the inputs and expected results. -Please refer to [DOCS.md](https://github.com/King-BR/mozambique-api-wrapper/blob/master/DOCS.md) to see a simple documentation for the NPM package +Please refer to [DOCS.md](https://github.com/arubinofaux/mozambique-api-wrapper/blob/master/DOCS.md) to see a simple documentation for the NPM package -### Installation +## How to get an auth key +Request it at the start of the [documentation](https://apexlegendsapi.com/) + +## Installation Install the package @@ -12,7 +15,7 @@ $ cd /path/to/app $ npm install --save mozambique-api-wrapper ``` -### Usage +## Usage ```js // Require Wrapper Library diff --git a/index.js b/index.js index 7ba4183..654a7c7 100644 --- a/index.js +++ b/index.js @@ -20,45 +20,140 @@ const DIRECTORY = { * @param {String} apiKey Your mozambiquehe.re Auth Key * @param {Any} [options] Options for the cache system * @param {Boolean} [options.useCache=true] Whether or not to use the cache system - * @param {Number} [options.cacheLifetime=7200000] Cache lifetime + * @param {Number} [options.cacheLifetime=1440] Cache lifetime in minutes */ +class MozambiqueAPI { + constructor(apiKey, options = { useCache: true, cacheLifetime: 1440 }) { + if (!apiKey) { + throw new Error("mozampique-api-wrapper: API Key missing"); + } + let self = this; + self.apiKey = apiKey; + self.headers = { + "User-Agent": "mozambique-api-wrapper", + "Content-Type": "application/json" + }; + + switch (typeof options.useCache) { + case 'boolean': { break; } + default: { + console.log(`[WARNING] mozampique-api-wrapper: options.useCache expected to be a Boolean (provided: ${typeof options.useCache}). Using default`); + options.useCache = true; + break; + } + } -function MozambiqueAPI(apiKey, options = { useCache: true, cacheLifetime: 7200000}) { - if (!apiKey) { - throw new Error("mozampique-api-wrapper: API Key missing"); - } - let self = this; + switch (typeof options.cacheLifetime) { + case 'number': { break; } + default: { + console.log(`[WARNING] mozampique-api-wrapper: options.cacheLifetime expected to be a Number (provided: ${typeof options.cacheLifetime}). Using default`); + options.cacheLifetime = 1440; + break; + } + } - self.apiKey = apiKey; + self.useCache = options.useCache; + self.cacheLifetime = options.cacheLifetime * 60000; + } - self.headers = { - "User-Agent": "mozambique-api-wrapper", - "Content-Type": "application/json" - }; + /** + * Search a player using player name or UID + * + * @param {Any} query Query parameters + * @param {String} [query.player] Player name + * @param {String|Number} [query.uid] Player UID + * @param {String} [query.platform] Player platform (PC, PS4, X1) + * @returns {JSON} Json with player info + */ + search(query) { + let type; + + if (query.player) { + type = "player=" + query.player; + } - switch (typeof options.useCache) { - case 'boolean': { break; } - default:{ - console.log(`[WARNING] mozampique-api-wrapper: options.useCache expected to be a Boolean (provided: ${typeof options.useCache}). Using default`); - options.useCache = true; - break; + if (query.uid) { + type = "uid=" + query.uid; } + + let url = DIRECTORY.SEARCH_URL + "&platform=" + query.platform + "&" + type + "&auth=" + this.apiKey; + return request(this, url); } - switch (typeof options.cacheLifetime) { - case 'number': { break; } - default: { - console.log(`[WARNING] mozampique-api-wrapper: options.cacheLifetime expected to be a Number (provided: ${typeof options.cacheLifetime}). Using default`); - options.cacheLifetime = 7200000; - break; + /** + * Get recent news about Apex Legends + * + * @param {String} [lang="en-us"] News language + * @returns {JSON} Json with an array of Apex Legends news + */ + news(lang = "en-us") { + let url = DIRECTORY.NEWS_URL + "&lang=" + lang + "&auth=" + this.apiKey; + return request(this, url); + } + + /** + * Get server status for Origin, EA, Apex Legends and Mozambiquehe.re API + * + * @returns {JSON} Json with status of all servers + */ + server() { + let url = DIRECTORY.SERVER_STATUS; + return request(this, url); + } + + /** + * Avaliable for everyone but with limitations depending on your access type + * + * @param {Any} query Query parameters + * @param {String} [query.player] Player name + * @param {String|Number} [query.uid] Player UID + * @param {String} [query.platform] Player platform (PC, PS4, X1) + * @param {String} [query.action] Action for the Match History API (info, get, delete, add) + * @returns {JSON} Json differs depending on action parameter. Please refer to API documentation for more info (https://mozambiquehe.re/api) + */ + history(query) { + let type; + + if (query.player) { + type = "player=" + query.player; } + + if (query.uid) { + type = "uid=" + query.uid; + } + + let url = DIRECTORY.MATCH_HISTORY + type + "&platform" + query.platform + "&auth=" + this.apiKey + "&history=1&action=" + query.action; + return request(this, url); } - self.useCache = options.useCache; - self.cacheLifetime = options.cacheLifetime + /** + * Get all game data avaliable on [mozambiquehe.re](https://mozambiquehe.re/) separated by data type + * + * Avaliable data types: + * assault_rifles, attachments, consumables, equipment, grenades, legends, light_machine_guns, pistols, shotguns, sniper_rifles, sub_machine_guns + * @param {String} dataType Type of data requested + * @returns {JSON} Json with requested game data + */ + async gamedata(dataType) { + let url = DIRECTORY.GAME_DATA + "type=" + dataType + "&auth=" + this.apiKey; + return await requestCache(this, url, dataType); + } + /** + * Get the map rotation + * + * @returns {JSON} Json with map rotation data + */ + mapRotation() { + let url = DIRECTORY.MAP_ROTATION + "auth=" + this.apiKey; + return request(this, url); + } } - +/** + * @private + * @param {This} self + * @param {String} url + */ function request(self, url) { return fetch(url, { headers: self.headers @@ -70,7 +165,12 @@ function request(self, url) { }); } - +/** + * @private + * @param {This} self + * @param {String} url + * @param {String} type + */ async function requestCache(self, url, type) { function sleep(ms) { return new Promise((resolve) => { @@ -81,9 +181,9 @@ async function requestCache(self, url, type) { if(self.useCache) { fs.exists('./Cache/mozambiqueCache.json', function(existFile) { if(!existFile) { - fs.exists('Cache', async function(existDir) { + fs.exists('./Cache', async function(existDir) { if(!existDir) { - fs.mkdir('Cache', function(err) {}) + fs.mkdir('./Cache', function(err) {}) } fs.writeFile('./Cache/mozambiqueCache.json', '{}', { encoding: 'utf8' }, function(err) {}) }) @@ -114,106 +214,5 @@ async function requestCache(self, url, type) { } } -/** - * Search a player using player name or UID - * - * @param {Any} query Query parameters - * @param {String} [query.player] Player name - * @param {String|Number} [query.uid] Player UID - * @param {String} [query.platform] Player platform (PC, PS4, X1) - * @returns {JSON} Json with player info - */ - -MozambiqueAPI.prototype.search = function (query) { - let type - - if (query.player) { - type = "player=" + query.player; - } - - if (query.uid) { - type = "uid=" + query.uid; - } - - let url = DIRECTORY.SEARCH_URL + "&platform=" + query.platform + "&" + type + "&auth=" + this.apiKey; - return request(this, url); -}; - - -/** - * Get recent news about Apex Legends - * - * @param {String} [lang="en-us"] News language - * @returns {JSON} Json with an array of Apex Legends news - */ - -MozambiqueAPI.prototype.news = function (lang = "en-us") { - let url = DIRECTORY.NEWS_URL + "&lang=" + lang + "&auth=" + this.apiKey; - return request(this, url); -}; - - -/** - * Get server status for Origin, EA, Apex Legends and Mozambiquehe.re API - * - * @returns {JSON} Json with status of all servers - */ - -MozambiqueAPI.prototype.server = function() { - let url = DIRECTORY.SERVER_STATUS - return request(this, url); -} - - -/** - * Avaliable for everyone but with limitations depending on your access type - * - * @param {Any} query Query parameters - * @param {String} [query.player] Player name - * @param {String|Number} [query.uid] Player UID - * @param {String} [query.platform] Player platform (PC, PS4, X1) - * @param {String} [query.action] Action for the Match History API (info, get, delete, add) - * @returns {JSON} Json differs depending on action parameter. Please refer to API documentation for more info (https://mozambiquehe.re/api) - */ - -MozambiqueAPI.prototype.history = function(query) { - let type - - if (query.player) { - type = "player=" + query.player; - } - - if (query.uid) { - type = "uid=" + query.uid; - } - - let url = DIRECTORY.MATCH_HISTORY + type + "&platform" + query.platform + "&auth=" + this.apiKey + "&history=1&action=" + query.action; - return request(this, url) ; -} - -/** - * Get all game data avaliable on [mozambiquehe.re](https://mozambiquehe.re/) separated by data type - * - * Avaliable data types: - * assault_rifles, attachments, consumables, equipment, grenades, legends, light_machine_guns, pistols, shotguns, sniper_rifles, sub_machine_guns - * @param {String} dataType Type of data requested - * @returns {JSON} Json with requested game data - */ - -MozambiqueAPI.prototype.gamedata = async function(dataType) { - let url = DIRECTORY.GAME_DATA + "type=" + dataType + "&auth=" + this.apiKey - return await requestCache(this, url, dataType); -} - -/** - * Get the map rotation - * - * @returns {JSON} Json with map rotation data - */ - -MozambiqueAPI.prototype.mapRotation = function() { - let url = DIRECTORY.MAP_ROTATION + "auth=" + this.apiKey - return request(this, url); -} -module.exports = MozambiqueAPI; +module.exports = MozambiqueAPI; \ No newline at end of file From cd29dbc918381a32b74597a75422928c822b2bf6 Mon Sep 17 00:00:00 2001 From: Fernando Date: Wed, 25 Nov 2020 19:57:38 -0300 Subject: [PATCH 08/10] . --- index.js | 118 ++++++------------------------------------------------- 1 file changed, 12 insertions(+), 106 deletions(-) diff --git a/index.js b/index.js index 654a7c7..67a9c96 100644 --- a/index.js +++ b/index.js @@ -17,13 +17,13 @@ const DIRECTORY = { * Core of mozambique-api-wrapper * * @constructor - * @param {String} apiKey Your mozambiquehe.re Auth Key + * @param {String} apiKey Your apexlegendsapi Auth Key * @param {Any} [options] Options for the cache system * @param {Boolean} [options.useCache=true] Whether or not to use the cache system * @param {Number} [options.cacheLifetime=1440] Cache lifetime in minutes */ class MozambiqueAPI { - constructor(apiKey, options = { useCache: true, cacheLifetime: 1440 }) { + constructor(apiKey) { if (!apiKey) { throw new Error("mozampique-api-wrapper: API Key missing"); } @@ -33,27 +33,6 @@ class MozambiqueAPI { "User-Agent": "mozambique-api-wrapper", "Content-Type": "application/json" }; - - switch (typeof options.useCache) { - case 'boolean': { break; } - default: { - console.log(`[WARNING] mozampique-api-wrapper: options.useCache expected to be a Boolean (provided: ${typeof options.useCache}). Using default`); - options.useCache = true; - break; - } - } - - switch (typeof options.cacheLifetime) { - case 'number': { break; } - default: { - console.log(`[WARNING] mozampique-api-wrapper: options.cacheLifetime expected to be a Number (provided: ${typeof options.cacheLifetime}). Using default`); - options.cacheLifetime = 1440; - break; - } - } - - self.useCache = options.useCache; - self.cacheLifetime = options.cacheLifetime * 60000; } /** @@ -63,7 +42,7 @@ class MozambiqueAPI { * @param {String} [query.player] Player name * @param {String|Number} [query.uid] Player UID * @param {String} [query.platform] Player platform (PC, PS4, X1) - * @returns {JSON} Json with player info + * @returns {Player} Json with player info */ search(query) { let type; @@ -84,7 +63,7 @@ class MozambiqueAPI { * Get recent news about Apex Legends * * @param {String} [lang="en-us"] News language - * @returns {JSON} Json with an array of Apex Legends news + * @returns {News} Json with an array of Apex Legends news */ news(lang = "en-us") { let url = DIRECTORY.NEWS_URL + "&lang=" + lang + "&auth=" + this.apiKey; @@ -92,9 +71,9 @@ class MozambiqueAPI { } /** - * Get server status for Origin, EA, Apex Legends and Mozambiquehe.re API + * Get server status for Origin, EA, Apex Legends and apexlegendsapi API * - * @returns {JSON} Json with status of all servers + * @returns {ServerStatus} Json with status of all servers */ server() { let url = DIRECTORY.SERVER_STATUS; @@ -109,7 +88,7 @@ class MozambiqueAPI { * @param {String|Number} [query.uid] Player UID * @param {String} [query.platform] Player platform (PC, PS4, X1) * @param {String} [query.action] Action for the Match History API (info, get, delete, add) - * @returns {JSON} Json differs depending on action parameter. Please refer to API documentation for more info (https://mozambiquehe.re/api) + * @returns {Object} Json differs depending on action parameter. Please refer to API documentation for more info (https://apexlegendsapi/api) */ history(query) { let type; @@ -127,92 +106,19 @@ class MozambiqueAPI { } /** - * Get all game data avaliable on [mozambiquehe.re](https://mozambiquehe.re/) separated by data type + * WARNING: endpoint data not updated anymore + * + * Get all game data avaliable on [apexlegendsapi](https://apexlegendsapi/) separated by data type * * Avaliable data types: * assault_rifles, attachments, consumables, equipment, grenades, legends, light_machine_guns, pistols, shotguns, sniper_rifles, sub_machine_guns * @param {String} dataType Type of data requested - * @returns {JSON} Json with requested game data + * @returns {Object} Json with requested game data */ - async gamedata(dataType) { + gamedata(dataType) { let url = DIRECTORY.GAME_DATA + "type=" + dataType + "&auth=" + this.apiKey; return await requestCache(this, url, dataType); } - /** - * Get the map rotation - * - * @returns {JSON} Json with map rotation data - */ - mapRotation() { - let url = DIRECTORY.MAP_ROTATION + "auth=" + this.apiKey; - return request(this, url); - } -} - -/** - * @private - * @param {This} self - * @param {String} url - */ -function request(self, url) { - return fetch(url, { - headers: self.headers - }) - .then(function (res) { - return res.json(); - }).catch(function (err) { - return Promise.reject(err); - }); -} - -/** - * @private - * @param {This} self - * @param {String} url - * @param {String} type - */ -async function requestCache(self, url, type) { - function sleep(ms) { - return new Promise((resolve) => { - setTimeout(resolve, ms); - }); - } - - if(self.useCache) { - fs.exists('./Cache/mozambiqueCache.json', function(existFile) { - if(!existFile) { - fs.exists('./Cache', async function(existDir) { - if(!existDir) { - fs.mkdir('./Cache', function(err) {}) - } - fs.writeFile('./Cache/mozambiqueCache.json', '{}', { encoding: 'utf8' }, function(err) {}) - }) - } - }) - - await sleep(500) - fs.readFile('./Cache/mozambiqueCache.json', async function (err, data) { - if (err) return Promise.reject(err); - - data = JSON.parse(data); - if (typeof data[`${type}Generated`] !== 'number' || Date.now() >= (data[`${type}Generated`] + self.cacheLifetime)) { - data[type] = await request(self, url); - data[`${type}Generated`] = Date.now(); - let json = JSON.stringify(data, null, 2); - fs.writeFile('./Cache/mozambiqueCache.json', json, { encoding: 'utf8' }, function (err){}); - } - }); - - await sleep(1100) - const json = require('./Cache/mozambiqueCache.json'); - var name = require.resolve('./Cache/mozambiqueCache.json'); - delete require.cache[name]; - return json[type]; - - } else { - return request(self, url); - } } - module.exports = MozambiqueAPI; \ No newline at end of file From e4a0f1ecc83ce7558c59440e77e5d53ccbc76dce Mon Sep 17 00:00:00 2001 From: Fernando Date: Wed, 25 Nov 2020 20:05:27 -0300 Subject: [PATCH 09/10] . --- index.js | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 67a9c96..5ae8022 100644 --- a/index.js +++ b/index.js @@ -5,22 +5,31 @@ const BASE_URL = "https://api.mozambiquehe.re"; const DIRECTORY = { SEARCH_URL: BASE_URL + "/bridge?version=4", - NEWS_URL: BASE_URL + "/news?version=4", - SERVER_STATUS: "https://apexlegendsstatus.com/servers.json", + NEWS_URL: BASE_URL + "/news?", + SERVER_STATUS: BASE_URL + "/status?", MATCH_HISTORY: BASE_URL + "/bridge?", - GAME_DATA: BASE_URL + "/gamedata?", - MAP_ROTATION: BASE_URL + "/maprotation?" + GAME_DATA: BASE_URL + "/gamedata?" }; +/** + * @private + */ +function request(self, url) { + return fetch(url, { + headers: self.headers + }) + .then(function (res) { + return res.json(); + }).catch(function (err) { + return Promise.reject(err); + }); +} /** * Core of mozambique-api-wrapper * * @constructor * @param {String} apiKey Your apexlegendsapi Auth Key - * @param {Any} [options] Options for the cache system - * @param {Boolean} [options.useCache=true] Whether or not to use the cache system - * @param {Number} [options.cacheLifetime=1440] Cache lifetime in minutes */ class MozambiqueAPI { constructor(apiKey) { @@ -42,7 +51,7 @@ class MozambiqueAPI { * @param {String} [query.player] Player name * @param {String|Number} [query.uid] Player UID * @param {String} [query.platform] Player platform (PC, PS4, X1) - * @returns {Player} Json with player info + * @returns {Player} Object with player info */ search(query) { let type; @@ -63,7 +72,7 @@ class MozambiqueAPI { * Get recent news about Apex Legends * * @param {String} [lang="en-us"] News language - * @returns {News} Json with an array of Apex Legends news + * @returns {News} Object with an array of Apex Legends news */ news(lang = "en-us") { let url = DIRECTORY.NEWS_URL + "&lang=" + lang + "&auth=" + this.apiKey; @@ -73,7 +82,7 @@ class MozambiqueAPI { /** * Get server status for Origin, EA, Apex Legends and apexlegendsapi API * - * @returns {ServerStatus} Json with status of all servers + * @returns {ServerStatus} Object with status of all servers */ server() { let url = DIRECTORY.SERVER_STATUS; @@ -88,7 +97,7 @@ class MozambiqueAPI { * @param {String|Number} [query.uid] Player UID * @param {String} [query.platform] Player platform (PC, PS4, X1) * @param {String} [query.action] Action for the Match History API (info, get, delete, add) - * @returns {Object} Json differs depending on action parameter. Please refer to API documentation for more info (https://apexlegendsapi/api) + * @returns {Object} Object differs depending on action parameter. Please refer to API documentation for more info (https://apexlegendsapi/api) */ history(query) { let type; @@ -113,11 +122,11 @@ class MozambiqueAPI { * Avaliable data types: * assault_rifles, attachments, consumables, equipment, grenades, legends, light_machine_guns, pistols, shotguns, sniper_rifles, sub_machine_guns * @param {String} dataType Type of data requested - * @returns {Object} Json with requested game data + * @returns {Object} Object with requested game data */ gamedata(dataType) { let url = DIRECTORY.GAME_DATA + "type=" + dataType + "&auth=" + this.apiKey; - return await requestCache(this, url, dataType); + return request(this, url); } } From 05dc4c8cb12fea47ac2e6960e21ff1baa8f69018 Mon Sep 17 00:00:00 2001 From: Fernando Date: Wed, 25 Nov 2020 21:35:03 -0300 Subject: [PATCH 10/10] 1.3.0 --- DOCS.md | Bin 7724 -> 7252 bytes index.js | 71 ++++++++++++++++++---------------------------- package-lock.json | 13 +++------ package.json | 8 +++--- 4 files changed, 36 insertions(+), 56 deletions(-) diff --git a/DOCS.md b/DOCS.md index 0b951a374c9a5b933399a1fc137a61daa8cd30eb..a569234c457fbb7153270041164e88d35df44477 100644 GIT binary patch delta 1094 zcmZ2ubH!pp+GGt@myKyjjNJYVNeo#GsSL>sC6fc0lqY{;jNo)+C<4MnhDxAFxp**8 zwuqq&Oa?QQ0L4ofN*RhFs+p3|Ops@;W2l>4$y_LlWHN&dgFeuPe1;UDDRvAX6%$z^ z`E7u97XkUa3|t^Q`8A8}WHnx`$!j<~Cf{TA2#*HZRRGjc!H@$a(}6e-XhJbqG?O8g zL4zR!=#~kjtPs*_kaD!|si2foMJv-Yml|!KjVf8ww1%33*47H|+79XG$`@nux^ z0s1x%7%HVe8Wc7P!0=52k|n@U0rB#HWH~6F_@c4cuRghgiD&WyJ`p_e3vyAKG@dw^ zJdyvRIga#!)#=EA)hf`B#a+siEd&iv0x^(Dcyg0q9diLg=H%ak4@ruU|3Za~(UbFq zl`Y9nEfa<1C)aRHL2(l(VrPlGV3I^j!VJnlr~ppj2Zco^8whetUM6l)kK}E%U;$Zch~0sSUIC~Elqm{;k)H`< yl>kLjz3!J?A{{^StkKzKK#wWGWkus7o0G?Fi%qd~)4!8vJ5v9|T;7{3Nz=`7Po9!gpm};hCCo~+hS_r( zU?JUdU%AHnil+hPwWpt7NpTV8GimZ*Dcqpq$5+PmTzG zm2cMrnB8`-2v>E|1jXnXIHYkfP6iF)ouLhj70+Ee>AVkiJEqzdwN=o4A)P_F%0iYlF=oMkM4sYzj_Z}Ik1jHzEq)&D zd&)MYUhDh5v$g~sJKt1QY^%d{m>Qp|yFBaq*%r!zw$7(oh(r1fbpVn|cnZ)kj#9?B zEW5SKE+AsHdyS&3_TJ@!p1v&T4`F`n4~Z%tmv^j=k3Wz^J#wf@7U%P*k%0zTc1c>L zhHh@_GTCswf0lGmc+F{{ycl1&>e^Gs= zPs~AoL~vXlYGL3J)qSqArTH5z?hjfq|uFF9=z%N~gPyW|=E#Q!-R$PN1 GpZ)^N=j2uZ diff --git a/index.js b/index.js index 5ae8022..30a2421 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,11 @@ const fetch = require('node-fetch'); -const fs = require('fs') const BASE_URL = "https://api.mozambiquehe.re"; const DIRECTORY = { SEARCH_URL: BASE_URL + "/bridge?version=4", NEWS_URL: BASE_URL + "/news?", - SERVER_STATUS: BASE_URL + "/status?", + SERVER_STATUS: BASE_URL + "/servers?", MATCH_HISTORY: BASE_URL + "/bridge?", GAME_DATA: BASE_URL + "/gamedata?" }; @@ -18,64 +17,58 @@ function request(self, url) { return fetch(url, { headers: self.headers }) - .then(function (res) { - return res.json(); - }).catch(function (err) { - return Promise.reject(err); - }); + .then(function (res) { + return res.json(); + }) + .catch(function (err) { + return Promise.reject(err); + }); } /** * Core of mozambique-api-wrapper * * @constructor - * @param {String} apiKey Your apexlegendsapi Auth Key + * @param {String} apiKey Your [apexlegendsapi](https://apexlegendsapi.com) Auth Key */ class MozambiqueAPI { constructor(apiKey) { - if (!apiKey) { - throw new Error("mozampique-api-wrapper: API Key missing"); - } + if (!apiKey) throw new Error("mozampique-api-wrapper: API Key missing"); + let self = this; self.apiKey = apiKey; self.headers = { "User-Agent": "mozambique-api-wrapper", - "Content-Type": "application/json" + "Content-Type": "application/json", + "Authorization": self.apiKey }; } /** * Search a player using player name or UID * - * @param {Any} query Query parameters + * @param {Object} query Query parameters * @param {String} [query.player] Player name * @param {String|Number} [query.uid] Player UID * @param {String} [query.platform] Player platform (PC, PS4, X1) - * @returns {Player} Object with player info + * @returns {Object} Object with player info */ search(query) { let type; - - if (query.player) { - type = "player=" + query.player; - } - - if (query.uid) { - type = "uid=" + query.uid; - } - - let url = DIRECTORY.SEARCH_URL + "&platform=" + query.platform + "&" + type + "&auth=" + this.apiKey; + if (query.player) type = "player=" + query.player; + if (query.uid) type = "uid=" + query.uid; + let url = DIRECTORY.SEARCH_URL + "&platform=" + query.platform + "&" + type; return request(this, url); } /** * Get recent news about Apex Legends * - * @param {String} [lang="en-us"] News language - * @returns {News} Object with an array of Apex Legends news + * @param {String} [lang="en-us"] Language of the news + * @returns {Array} Array of Apex Legends news */ news(lang = "en-us") { - let url = DIRECTORY.NEWS_URL + "&lang=" + lang + "&auth=" + this.apiKey; + let url = DIRECTORY.NEWS_URL + "lang=" + lang; return request(this, url); } @@ -90,42 +83,34 @@ class MozambiqueAPI { } /** - * Avaliable for everyone but with limitations depending on your access type + * Avaliable for everyone but with limitations depending on your api access type * - * @param {Any} query Query parameters + * @param {Object} query Query parameters * @param {String} [query.player] Player name * @param {String|Number} [query.uid] Player UID * @param {String} [query.platform] Player platform (PC, PS4, X1) * @param {String} [query.action] Action for the Match History API (info, get, delete, add) - * @returns {Object} Object differs depending on action parameter. Please refer to API documentation for more info (https://apexlegendsapi/api) + * @returns {Object} Object differs depending on action parameter. Please refer to [API documentation](https://apexlegendsapi.com) for more info */ history(query) { let type; - - if (query.player) { - type = "player=" + query.player; - } - - if (query.uid) { - type = "uid=" + query.uid; - } - - let url = DIRECTORY.MATCH_HISTORY + type + "&platform" + query.platform + "&auth=" + this.apiKey + "&history=1&action=" + query.action; + if (query.player) type = "player=" + query.player; + if (query.uid) type = "uid=" + query.uid; + let url = DIRECTORY.MATCH_HISTORY + type + "&platform" + query.platform + "&history=1&action=" + query.action; return request(this, url); } /** * WARNING: endpoint data not updated anymore * - * Get all game data avaliable on [apexlegendsapi](https://apexlegendsapi/) separated by data type - * * Avaliable data types: * assault_rifles, attachments, consumables, equipment, grenades, legends, light_machine_guns, pistols, shotguns, sniper_rifles, sub_machine_guns + * @deprecated data not update anymore * @param {String} dataType Type of data requested * @returns {Object} Object with requested game data */ gamedata(dataType) { - let url = DIRECTORY.GAME_DATA + "type=" + dataType + "&auth=" + this.apiKey; + let url = DIRECTORY.GAME_DATA + "type=" + dataType; return request(this, url); } } diff --git a/package-lock.json b/package-lock.json index 6a414a4..6e1078b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,13 @@ { "name": "mozambique-api-wrapper", - "version": "1.3.0-beta.1", + "version": "1.3.0", "lockfileVersion": 1, "requires": true, "dependencies": { - "fs": { - "version": "0.0.1-security", - "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", - "integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ=" - }, "node-fetch": { - "version": "2.6.0", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", - "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", + "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" } } } diff --git a/package.json b/package.json index c269d18..5ecc99d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mozambique-api-wrapper", - "version": "1.3.0-beta.1", + "version": "1.3.0", "description": "Wrapper to make accessing mozambiquehe.re APIs when writing code in Javascript (Node.js) much easier.", "main": "index.js", "scripts": { @@ -22,7 +22,7 @@ "API" ], "dependencies": { - "fs": "0.0.1-security", - "node-fetch": "^2.6.0" - } + "node-fetch": "^2.6.1" + }, + "devDependencies": {} }