From 97ffdcb685795f17d947a5357e330822e87a7f6a Mon Sep 17 00:00:00 2001 From: Mehrshad Date: Thu, 31 Aug 2023 17:32:51 +0330 Subject: [PATCH] WIP1 The entire project needs to be converted to typescript due to a conflict between typescript and nodejs & at the moment, the project cannot run and work properly. --- app.js => app.ts | 16 +++++++++------- db_connect.js => db_connect.ts | 7 ++++--- lnurl/{lnurl-pay.js => lnurl-pay.ts} | 13 +++++-------- logger.js => logger.ts | 10 +++++----- 4 files changed, 23 insertions(+), 23 deletions(-) rename app.js => app.ts (71%) rename db_connect.js => db_connect.ts (76%) rename lnurl/{lnurl-pay.js => lnurl-pay.ts} (80%) rename logger.js => logger.ts (82%) diff --git a/app.js b/app.ts similarity index 71% rename from app.js rename to app.ts index 2fbc6604..99e5609a 100644 --- a/app.js +++ b/app.ts @@ -1,7 +1,8 @@ -require('dotenv').config(); -const { SocksProxyAgent } = require('socks-proxy-agent'); -const { start } = require('./bot'); -const mongoConnect = require('./db_connect'); +import * as dotenv from "dotenv"; +dotenv.config() +const SocksProxyAgent = require('socks-proxy-agent') +import { start } from "./bot/start"; +import mongoConnect from './db_connect' const { resubscribeInvoices } = require('./ln'); const logger = require('./logger'); const { delay } = require('./util'); @@ -30,12 +31,13 @@ const { delay } = require('./util'); telegram: { agent, }, - }; + } as any; } - const bot = start(process.env.BOT_TOKEN, options); + const bot = start(String(process.env.BOT_TOKEN), options); // Wait 1 seconds before try to resubscribe hold invoices await delay(1000); await resubscribeInvoices(bot); }) - .on('error', error => logger.error(`Error connecting to Mongo: ${error}`)); + .on('error', (error: Error) => logger.error(`Error connecting to Mongo: ${error}`)); })(); + diff --git a/db_connect.js b/db_connect.ts similarity index 76% rename from db_connect.js rename to db_connect.ts index e38fbd10..30e43ef7 100644 --- a/db_connect.js +++ b/db_connect.ts @@ -1,4 +1,4 @@ -const mongoose = require('mongoose'); +import mongoose from "mongoose"; const logger = require('./logger'); // connect to database @@ -12,12 +12,13 @@ if (!MONGO_URI) { throw new Error('You must provide a MongoDB URI'); } logger.info(`Connecting to: ${MONGO_URI}`); +// TODO: Update mongoose to latest version const connect = () => { mongoose.connect(MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true, - }); + } as any); // older version of mongoose that does not have the ConnectOptions type defined. return mongoose; }; -module.exports = connect; +export default connect; diff --git a/lnurl/lnurl-pay.js b/lnurl/lnurl-pay.ts similarity index 80% rename from lnurl/lnurl-pay.js rename to lnurl/lnurl-pay.ts index 45235898..e49d593b 100644 --- a/lnurl/lnurl-pay.js +++ b/lnurl/lnurl-pay.ts @@ -1,11 +1,11 @@ -const axios = require('axios').default; +import axios from 'axios'; const logger = require('../logger'); // { // pr: String, // bech32-serialized lightning invoice // routes: [], // an empty array // } -const resolvLightningAddress = async (address, amountMsat) => { +const resolvLightningAddress = async (address: string, amountMsat: number) => { const [user, domain] = address.split('@'); const lnAddressQuery = `https://${domain}/.well-known/lnurlp/${user}`; @@ -17,7 +17,7 @@ const resolvLightningAddress = async (address, amountMsat) => { } if ( - (lnAddressRes.minSendable > amountMsat) | + (lnAddressRes.minSendable > amountMsat) || (lnAddressRes.maxSendable < amountMsat) ) { logger.info('lnAddress invalid amount'); @@ -31,7 +31,7 @@ const resolvLightningAddress = async (address, amountMsat) => { return res; }; -const existLightningAddress = async address => { +const existLightningAddress = async (address: string) => { const [user, domain] = address.split('@'); const lnAddressQuery = `https://${domain}/.well-known/lnurlp/${user}`; @@ -49,7 +49,4 @@ const existLightningAddress = async address => { } }; -module.exports = { - resolvLightningAddress, - existLightningAddress, -}; +export { resolvLightningAddress, existLightningAddress } diff --git a/logger.js b/logger.ts similarity index 82% rename from logger.js rename to logger.ts index ada7ed8e..d5fe6e2a 100644 --- a/logger.js +++ b/logger.ts @@ -1,4 +1,4 @@ -const winston = require('winston'); +import * as winston from 'winston'; const level = process.env.LOG_LEVEL || 'notice'; @@ -9,9 +9,8 @@ const logger = winston.createLogger({ }), winston.format.colorize(), winston.format.printf(info => { - return `[${info.timestamp}] ${info.level}: ${info.message} ${ - info.stack ? info.stack : '' - }`; + return `[${info.timestamp}] ${info.level}: ${info.message} ${info.stack ? info.stack : '' + }`; }) ), levels: winston.config.syslog.levels, @@ -24,4 +23,5 @@ const logger = winston.createLogger({ exitOnError: false, }); -module.exports = logger; + +export default logger;