diff --git a/bot.js b/bot.js index f9358f4..8266ed6 100644 --- a/bot.js +++ b/bot.js @@ -1,9 +1,9 @@ // Imports -const { Bot } = require("grammy"); -const { BOT_TOKEN, ADMINS, LIMIT } = require("./config"); -const { isUrl, getRandomId, getProductDetails } = require("./utils"); -const { manageProducts, manageUsers } = require("./db"); -const unshort = require("./unshort"); +import { Bot } from "grammy" +import { BOT_TOKEN, ADMINS, LIMIT } from "./config.js" +import { isUrl, getRandomId, getProductDetails } from "./utils.js" +import { manageProducts, manageUsers } from "./db.js" +import unshort from "./unshort.js" const bot = new Bot(BOT_TOKEN); // Initialize bot @@ -257,4 +257,4 @@ bot.catch((err) => { setInterval(track, 3600000); //Track every hr. -module.exports = bot; +export default bot; diff --git a/config.js b/config.js index cf03711..bead029 100644 --- a/config.js +++ b/config.js @@ -1,4 +1,4 @@ -require('dotenv').config() +import 'dotenv/config' // Check if bot token is there or not if(!process.env.BOT_TOKEN) { @@ -11,11 +11,11 @@ if(!process.env.DB_URL) { process.exit(1); } -module.exports = { - ADMINS: process.env.ADMINS || '', - BOT_TOKEN: process.env.BOT_TOKEN || '', - DB_URL: process.env.DB_URL || '', - WORKER_URL: process.env.WORKER_URL || '', - API_KEY: process.env.API_KEY || '', // Generate any API Key and pass it when accessing the API. - LIMIT: Number(process.env.LIMIT), // Maximum number of products can be added by a user at a time. -} \ No newline at end of file +const ADMINS = process.env.ADMINS || '' +const BOT_TOKEN = process.env.BOT_TOKEN || '' +const DB_URL = process.env.DB_URL || '' +const WORKER_URL = process.env.WORKER_URL || '' +const API_KEY = process.env.API_KEY || '' // Generate any API Key and pass it when accessing the API. +const LIMIT = Number(process.env.LIMIT) // Maximum number of products can be added by a user at a time. + +export { ADMINS, BOT_TOKEN, DB_URL, WORKER_URL, API_KEY, LIMIT } \ No newline at end of file diff --git a/db.js b/db.js index 76919fb..3021c3e 100644 --- a/db.js +++ b/db.js @@ -1,5 +1,5 @@ -const {DB_URL} = require('./config'); -const MongoClient = require('mongodb').MongoClient; +import {DB_URL} from './config.js'; +import {MongoClient} from 'mongodb'; var mongo; // Check if mongodb is connected or not @@ -65,4 +65,4 @@ const manageProducts = async(data, action) => { } } -module.exports = {manageProducts, manageUsers}; \ No newline at end of file +export {manageProducts, manageUsers}; \ No newline at end of file diff --git a/index.js b/index.js index e69e41e..34f7ad6 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,8 @@ -const { getRandomId, getProductDetails } = require("./utils"); -const { manageProducts, manageUsers } = require("./db"); -const {API_KEY} = require('./config'); -const express = require('express'); -const bot = require('./bot'); +import { getRandomId, getProductDetails } from "./utils.js" +import { manageProducts, manageUsers } from "./db.js" +import {API_KEY} from './config.js' +import express from 'express' +import bot from './bot.js' //Globals const port = process.env.PORT || 3000; diff --git a/package.json b/package.json index f1adac1..e4fda66 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "type": "git", "url": "git+https://github.com/AffanTheBest/price-tracker.git" }, + "type": "module", "keywords": [ "price-tracker", "amazon", diff --git a/unshort.js b/unshort.js index bb892e1..ca65765 100644 --- a/unshort.js +++ b/unshort.js @@ -1,4 +1,4 @@ -const axios = require('axios'); +import axios from 'axios'; const unshort = async (url) => { const extractUrl = req => req?.request?.res?.responseUrl || req?.request?._redirectable?._currentUrl || @@ -16,4 +16,4 @@ const unshort = async (url) => { return longUrl; } -module.exports = unshort; \ No newline at end of file +export default unshort; \ No newline at end of file diff --git a/utils.js b/utils.js index d04059a..d234709 100644 --- a/utils.js +++ b/utils.js @@ -1,6 +1,6 @@ -const cheerio = require('cheerio'); -const axios = require('axios'); -const {WORKER_URL} = require('./config'); +import cheerio from 'cheerio' +import axios from 'axios' +import {WORKER_URL} from './config.js' const urlRegex = /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i; @@ -54,4 +54,4 @@ const getProductDetails = async(url, merchant) => { } } -module.exports = { isUrl, getRandomId, getProductDetails }; \ No newline at end of file +export { isUrl, getRandomId, getProductDetails }; \ No newline at end of file