· 6 years ago · Mar 19, 2020, 04:12 PM
1const fetch = require('node-fetch');
2const URL = 'https://public-api.tracker.gg/apex/v1/standard/profile';
3
4/**
5 * The main hub for the client, to be instantiated.
6 * @class
7 */
8class Client {
9 /**
10 * @constructor
11 * @param {string} key Your API Key, provided by ApexTracker
12 */
13 constructor(key) {
14 this.headers = { headers: { 'TRN-Api-Key': key } };
15 }
16
17 /**
18 * Gets the stats of a specific user, on a specific platform.
19 * @param {string} username The username of the player to be searched
20 * @param {string} platform The platform of the player to be searched
21 */
22 user(username, platform) {
23 return new Promise((resolve, reject) => {
24 if (!username) return reject(new Error('[APEX-API] You must supply a username'));
25 if (!platform) return reject(new Error('[APEX-API] You must provide a platform [PC | XBOX | PSN]'));
26 if (typeof username !== 'string') return reject(new TypeError(`[APEX-API] Username expects a string, ${typeof username} given`));
27 if (typeof platform !== 'string') return reject(new TypeError(`[APEX-API] Platform expects a string, ${typeof platform} given`));
28 let nbr;
29 if (platform.toUpperCase() === 'PC') nbr = 5;
30 if (platform.toUpperCase() === 'XBOX') nbr = 1;
31 if (platform.toUpperCase() === 'PSN') nbr = 2;
32 if(nbr == undefined) return reject(new TypeError(`[APEX-API] Platforms must be PC, XBOX or PSN`));
33 fetch(`${URL}/${nbr}/${username}`, this.headers)
34 .then(res => res.json()) // FORMATING TO JSON
35 .then(response =>{
36 resolve(response) // REPONSE
37 }).catch(error =>{
38 reject(error.toString()); // REJECT ERROR
39 // A améliorer !
40 });
41 });
42 }
43 weaponInfo(category){
44 return new Promise((resolve, reject) => {
45
46 fetch(`http://apex-api.tk/api/v1/weapons?name=${category}`)
47 .then(res => res.json()) // FORMATING TO JSON
48 .then(response =>{
49 resolve(response) // REPONSE
50 }).catch(error =>{
51 reject(error.toString()); // REJECT ERROR
52 // A améliorer !
53 });
54 })
55 }
56
57 legendInfo(legend){
58 return new Promise((resolve, reject) => {
59
60 fetch(`http://apex-api.tk/api/v1/legends?name=${legend}`)
61 .then(res => res.json()) // FORMATING TO JSON
62 .then(response =>{
63 resolve(response) // REPONSE
64 }).catch(error =>{
65 reject(error.toString()); // REJECT ERROR
66 // A améliorer !
67 });
68 })
69 }
70
71 legendUsage(){
72 return new Promise((resolve, reject) => {
73
74 fetch('http://apex-api.tk/api/v1/legendsUsage')
75 .then(res => res.json()) // FORMATING TO JSON
76 .then(response =>{
77 resolve(response) // REPONSE
78 }).catch(error =>{
79 reject(error.toString()); // REJECT ERROR
80 // A améliorer !
81 });
82 })
83 }
84};
85
86module.exports = Client;