Client.js

  1. const fetch = require('node-fetch');
  2. const Package = require('../package.json');
  3. const Account = require('./Account');
  4. const Match = require('./Match').Match;
  5. const StoreItem = require('./StoreItem');
  6. const Challenges = require('./Challenges');
  7. /**
  8. * The client for interacting with the Fortnite Tracker API
  9. * @class Client
  10. * @param {string} key Fortnite Tracker API token
  11. */
  12. class Client {
  13. constructor(key) {
  14. if (!key) {
  15. throw new Error('No API key passed.');
  16. }
  17. this.key = key;
  18. this.headers = {
  19. 'User-Agent': `fortnite.js v${Package.version} (${Package.homepage})`,
  20. 'TRN-Api-Key': this.key
  21. };
  22. this.rateLimit = {
  23. limit: 30,
  24. remaining: 30
  25. };
  26. }
  27. /**
  28. * Makes the request to the API
  29. * @private
  30. * @param {string} link URL endpoint of API
  31. * @returns {Promise<Object>}
  32. * @memberof Client
  33. */
  34. _request(link) {
  35. return fetch(link, { headers: this.headers })
  36. .then(r => {
  37. this.rateLimit = {
  38. limit: Number(r.headers.get('x-ratelimit-limit-minute')),
  39. remaining: Number(r.headers.get('x-ratelimit-remaining-minute'))
  40. };
  41. if (!r.ok) return Promise.reject(r.statusText);
  42. return r.json();
  43. })
  44. .catch(e => Promise.reject(`HTTP ${e}`));
  45. }
  46. /**
  47. * Get user info
  48. * @param {string} username username of the user to search for
  49. * @param {string=} [platform='pc'] platform to search for user in (pc, xbl, or psn)
  50. * @param {boolean=} [raw=false] whether to return raw response from API
  51. * @returns {(Promise<Account>|Promise<Object>)}
  52. * @memberof Client
  53. */
  54. get(username, platform = 'pc', raw = false) {
  55. return this._request(`https://api.fortnitetracker.com/v1/profile/${platform}/${encodeURI(username)}`)
  56. .then(r => r.error ? Promise.reject(r) : r)
  57. .then(r => raw ? r : new Account(r))
  58. .catch(e => Promise.reject(e));
  59. }
  60. /**
  61. * Get user's matches
  62. * @param {string} accountId user's account ID found in user info
  63. * @param {boolean=} [raw=false] whether to return raw response from API
  64. * @returns {(Promise<Array<Match>>|Promise<Array<Object>>)}
  65. * @memberof Client
  66. */
  67. getMatches(accountId, raw = false) {
  68. return this._request(`https://api.fortnitetracker.com/v1/profile/account/${accountId}/matches`)
  69. .then(r => r.error ? Promise.reject(r.error) : r)
  70. .then(r => raw ? r : r.map(m => new Match(m)))
  71. .catch(e => Promise.reject(e));
  72. }
  73. /**
  74. * Get current store items
  75. * @param {boolean=} [raw=false] whether to return raw response from API
  76. * @returns {Promise<Array<StoreItem>>|Promise<Array<Object>>}
  77. * @memberof Client
  78. */
  79. getStore(raw = false) {
  80. return this._request('https://api.fortnitetracker.com/v1/store')
  81. .then(r => r.error ? Promise.reject(r.error) : r)
  82. .then(r => raw ? r : r.map(item => new StoreItem(item)))
  83. .catch(e => Promise.reject(e));
  84. }
  85. /**
  86. * Get currently weekly challenges
  87. * @param {boolean=} [raw=false] whether to return raw response from API
  88. * @returns {(Promise<Challenges>|Promise<Object>)}
  89. * @memberof Client
  90. */
  91. getChallenges(raw = false) {
  92. return this._request('https://api.fortnitetracker.com/v1/challenges')
  93. .then(r => r.error ? Promise.reject(r.error) : r)
  94. .then(r => raw ? r : new Challenges(r))
  95. .catch(e => Promise.reject(e));
  96. }
  97. }
  98. module.exports = Client;