supercell-api-scraper
Version:
A supercell games scraper that has additional calculations and info, easy to use methods. [BETA]
50 lines (49 loc) • 1.78 kB
JavaScript
export default class BaseGame {
constructor(client) {
this.client = client;
}
async fetch(game, domain, options) {
const token = BaseGame.token(game, this.client.options);
if (!token)
throw new Error(`There is no token for specified game!`);
const url = `https://api.${game.toLowerCase().replace(/ +/gim, '')}.com/v1/`;
let request = url, response = null;
request += domain;
if (options?.predomain)
request += `/${options.predomain}`;
if (options?.tag)
request += `/%23${options.tag.replace(/#/gim, '')}`;
if (options?.name)
request += `?name=${options.name.replace(/ /gim, '%20')}`;
if (options?.subdomain)
request += `/${options.subdomain}`;
if (this.client.options.config?.debug)
console.info(`Request URL: ${request}`);
try {
response = await fetch(request, {
method: 'GET',
headers: {
Accept: 'application/json',
Authorization: 'Bearer ' + token,
},
});
if (this.client.options.config?.debug)
console.info(`Response Status: ${response.status}`);
response = await response.json();
}
catch (error) {
console.log(error);
}
return response;
}
static token(game, options) {
switch (game) {
case 'Brawl Stars':
return options.BrawlStars?.token || null;
case 'Clash of Clans':
return options.ClashOfClans?.token || null;
case 'Clash Royale':
return options.ClashRoyale?.token || null;
}
}
}