I have created a service class and made it singleton:
class DiscordService {
async getToken(code) {
const creds = btoa(`${process.env.DISCORD_CLIENT_ID}:${process.env.DISCORD_SECRET}`);
const headers = {
Authorization: `Basic ${creds}`,
};
const baseUrl = 'https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=';
const url = `${baseUrl}${code}&redirect_uri=${process.env.DISCORD_CALLBACK_URL}`;
const response = await axios.post(url, {}, {headers});
return await response.data;
}
}
export default new DiscordService();
I have imported that class in my routes file discord.js
import DiscordService from '../services/discord';
and I am using it like:
const response = await DiscordService.getToken(code);
When I run my server I get the following error:
import DiscordService from '../services/discord';
^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
Is it because its only allowed in typescript? or do I need babel for it ?