Comparing version 4.1.1 to 4.2.1
{ | ||
"name": "fortnite", | ||
"version": "4.1.1", | ||
"description": "A Fortnite wrapper around the FortniteTracker API", | ||
"version": "4.2.1", | ||
"description": "An API wrapper, for Fortnite, around the FortniteTracker API.", | ||
"main": "index.js", | ||
@@ -17,4 +17,4 @@ "repository": { | ||
"dependencies": { | ||
"node-fetch": "^2.1.2" | ||
"node-fetch": "^2.3.0" | ||
} | ||
} |
@@ -7,3 +7,3 @@ # Fortnite [![npm downloads](https://img.shields.io/npm/dt/fortnite.svg?maxAge=3600)](https://www.npmjs.com/package/fortnite) [![Dependencies](https://img.shields.io/david/jake-ruston/fortnite.svg?maxAge=3600)](https://david-dm.org/jake-ruston/fortnite) [![RunKit](https://badge.runkitcdn.com/fortnite.svg)](https://npm.runkit.com/fortnite) | ||
A simple, easy to use module for interacting with the [FortniteTracker](https://fortnitetracker.com/) [API](https://fortnitetracker.com/site-api) | ||
A simple, easy to use module for interacting with the [FortniteTracker](https://fortnitetracker.com/) [API.](https://fortnitetracker.com/site-api) | ||
@@ -25,4 +25,6 @@ ## Setup and Installation | ||
// Get the stats of an Xbox player by the name of Number1Swifty | ||
fortnite.user('Number1Swifty', 'xbl').then(console.log); | ||
// All methods | ||
fortnite.user('username', 'platform [pc, xbl, psn]').then(console.log); | ||
fortnite.store().then(console.log); | ||
fortnite.challenges().then(console.log); | ||
``` |
const fetch = require('node-fetch'); | ||
const User = require('./User'); | ||
const Profile = require('./Profile'); | ||
const Store = require('./Store'); | ||
const Challenge = require('./Challenge'); | ||
const URL = 'https://api.fortnitetracker.com/v1'; | ||
/** | ||
* The main hub for interacting with the FortniteTracker API | ||
* The main hub for the client, to be instantiated. | ||
* | ||
* @author Jake Ruston | ||
* @class | ||
*/ | ||
class Client { | ||
module.exports = class Client { | ||
/** | ||
* @param {string} key The API Key from FortniteTracker | ||
* @param {string} key Your API Key, provided by FortniteTracker | ||
*/ | ||
constructor(key) { | ||
this.url = 'https://api.fortnitetracker.com/v1'; | ||
this.headers = { headers: { 'TRN-Api-Key': key } }; | ||
/** | ||
* The available platforms | ||
* @type {Object} | ||
*/ | ||
this.platforms = { | ||
xbl: ['xbox', 'xb1', 'xbox1', 'xbox one'], | ||
psn: ['playstation', 'ps4', 'ps', 'playstation 4'], | ||
pc: ['computer'] | ||
}; | ||
} | ||
/** | ||
* Gets the stats of a certain user | ||
* Gets the stats of a specific user, on a specific platform. | ||
* | ||
* @param {string} username The username to search for | ||
* @param {string} platform The platform that the user plays on | ||
* @returns {Promise<Object>} | ||
* @param {string} username The username of the player to be searched | ||
* @param {string} [platform = pc] The platform of the player to be searched | ||
* | ||
* @returns {Profile} | ||
*/ | ||
user(username, platform) { | ||
return new Promise((resolve, reject) => { | ||
// No values given | ||
if (!username) return reject(new Error('You must supply a username')); | ||
if (!platform) return reject(new Error('You must supply a platform')); | ||
async user(username, platform = 'pc') { | ||
if (!username) throw new Error('You must supply a username'); | ||
// Invalid value type | ||
if (typeof username !== 'string') return reject(new TypeError('Username must be a string')); | ||
if (typeof platform !== 'string') return reject(new TypeError('Platform must be a string')); | ||
if (typeof username !== 'string') throw new TypeError(`Username expects a string, ${typeof username} given`); | ||
if (typeof platform !== 'string') throw new TypeError(`Platform expects a string, ${typeof platform} given`); | ||
let result; | ||
let data; | ||
const result = await fetch(`${URL}/profile/${platform}/${username}`, this.headers); | ||
const data = await result.json(); | ||
(async () => { | ||
try { | ||
username = encodeURI(username); | ||
platform = this.getPlatform(platform); | ||
// Invalid API Key | ||
if (data.message === 'Invalid authentication credentials') throw new Error(data.message); | ||
result = await fetch(`${this.url}/profile/${platform}/${username}`, this.headers); | ||
data = await result.json(); | ||
} catch (err) { | ||
return reject(err); | ||
} | ||
// Handling Player Not Found error | ||
if (data.error === 'Player Not Found') return { code: 404, error: data.error }; | ||
// Handling any other error | ||
else if (data.error) return data; | ||
if (data.error === 'Player Not Found') return reject(new Error('User not found')); | ||
return resolve(new User(data)); | ||
})(); | ||
return new Profile(data); | ||
} | ||
return undefined; | ||
}); | ||
/** | ||
* Gets the current store information. | ||
*/ | ||
async store() { | ||
const result = await fetch(`${URL}/store`, this.headers); | ||
const data = await result.json(); | ||
return data.map(item => new Store(item)); | ||
} | ||
/** | ||
* Gets the correct platform from a hash map | ||
* | ||
* @param {string} platform The platform to get | ||
* @returns {void} | ||
* Gets the current active challenges. | ||
*/ | ||
getPlatform(platform) { | ||
if (platform in this.platforms) { | ||
return platform; | ||
} else { | ||
for (const plat in this.platforms) { | ||
if (this.platforms[plat].includes(platform)) { | ||
return Object.keys(this.platforms).find(key => this.platforms[key] === this.platforms[plat]); | ||
} | ||
} | ||
} | ||
async challenges() { | ||
const result = await fetch(`${URL}/challenges`, this.headers); | ||
const data = await result.json(); | ||
return undefined; | ||
return data.items.map(item => new Challenge(item)); | ||
} | ||
} | ||
module.exports = Client; | ||
}; |
@@ -1,22 +0,24 @@ | ||
/** Class representing a game mode */ | ||
class Mode { | ||
/** | ||
* Represents a game mode. | ||
* | ||
* @class | ||
*/ | ||
module.exports = class Mode { | ||
/** | ||
* @param {Object} data All of the type data resolved from the API | ||
* @param {Object} stat Each individual stat | ||
*/ | ||
constructor(data) { | ||
this.score = data.score.valueInt; | ||
this.kd = data.kd.valueDec; | ||
this.matches = data.matches.valueInt; | ||
this.kills = data.kills.valueInt; | ||
this.kills_per_match = data.kpg.valueDec; | ||
this.score_per_match = data.scorePerMatch.valueDec; | ||
this.wins = data.top1.valueInt; | ||
this.top_3 = data.top3.valueInt + this.wins; | ||
this.top_5 = data.top5.valueInt + this.top_3 + this.wins; | ||
this.top_6 = data.top6.valueInt + this.top_5 + this.top_3 + this.wins; | ||
this.top_12 = data.top12.valueInt + this.top_6 + this.top_5 + this.top_3 + this.wins; | ||
this.top_25 = data.top25.valueInt + this.top_12 + this.top_6 + this.top_5 + this.top_3 + this.wins; | ||
constructor(stat) { | ||
this.score = stat.score.valueInt; | ||
this.kd = stat.kd.valueDec; | ||
this.matches = stat.matches.valueInt; | ||
this.kills = stat.kills.valueInt; | ||
this.kills_per_match = stat.kpg.valueDec; | ||
this.score_per_match = stat.scorePerMatch.valueDec; | ||
this.wins = stat.top1.valueInt; | ||
this.top_3 = stat.top3.valueInt + this.wins; | ||
this.top_5 = stat.top5.valueInt + this.top_3 + this.wins; | ||
this.top_6 = stat.top6.valueInt + this.top_5 + this.top_3 + this.wins; | ||
this.top_12 = stat.top12.valueInt + this.top_6 + this.top_5 + this.top_3 + this.wins; | ||
this.top_25 = stat.top25.valueInt + this.top_12 + this.top_6 + this.top_5 + this.top_3 + this.wins; | ||
} | ||
} | ||
module.exports = Mode; | ||
}; |
@@ -1,11 +0,13 @@ | ||
/** Class representing a single stat */ | ||
class Stat { | ||
/** | ||
* Represents a single stat. | ||
* | ||
* @class | ||
*/ | ||
module.exports = class Stat { | ||
/** | ||
* @param {Object} data All of the stat data resolved from the API | ||
* @param {Object} stat Each individual stat | ||
*/ | ||
constructor(data) { | ||
this[data.key] = data.value; | ||
constructor(stat) { | ||
this[stat.key.split(' ').join('_')] = stat.value; | ||
} | ||
} | ||
module.exports = Stat; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7769
11
198
29
4
Updatednode-fetch@^2.3.0