Skip to content

Commit 9b398e9

Browse files
committed
most types are defined.
1 parent 0650ad0 commit 9b398e9

File tree

9 files changed

+114
-77
lines changed

9 files changed

+114
-77
lines changed

src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as dotenv from "dotenv";
44

55
dotenv.config();
66

7-
interface Config {
7+
export interface Config {
88
mongourl: string | undefined;
99
website: {
1010
links: string[];
@@ -52,4 +52,4 @@ const config: Config = {
5252
},
5353
};
5454

55-
export = config;
55+
export default config;

src/handlers/initDatabase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export = class DatabaseInitializer {
275275
let schemaDb = await dashboardDataDb.findOne({ _id: id }).catch(() => {});
276276
if (schemaDb) continue;
277277
schemaDb = await new dashboardDataDb(
278-
this.convertIdsRecursive(schema),
278+
this.convertIdsRecursive(schema)
279279
).save();
280280
}
281281
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function getIPv4Addresses(): string | undefined {
5151
Object.keys(networkInterfaces).forEach((interfaceName) => {
5252
const interfaceInfo: any = networkInterfaces[interfaceName];
5353

54-
interfaceInfo.forEach((info:any) => {
54+
interfaceInfo.forEach((info: any) => {
5555
if (info.family === "IPv4" && info.internal === false) {
5656
ipv4Address = info.address;
5757
}
@@ -103,7 +103,7 @@ new Promise<void>((resolve) => {
103103
}
104104
}
105105

106-
const allips = client.config.website.links.map((val:any) => {
106+
const allips = client.config.website.links.map((val: any) => {
107107
return (val = `${val}:${client.config.website.port}`);
108108
});
109109

src/routes/v1/user/post.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import express, { Request, Response, Router } from "express";
22
import userDataDb from "../../../schema/userData";
33
import bcrypt from "bcrypt";
4+
import { AppTypes } from "../../../structures/App";
45

56
class Route {
6-
constructor(client: any) {
7+
constructor(client: AppTypes) {
78
const router: Router = express.Router();
89

910
router.post("/user/auth", async (req: Request, res: Response) => {
@@ -15,15 +16,15 @@ class Route {
1516
const { loginData = {} } = req.body;
1617
const authHeader = req.headers["authorization"];
1718
const token = authHeader && authHeader.split(" ")[1]; // Bearer TOKEN_VALUE
19+
if (token) {
20+
const tokenUserData = await client
21+
.findUser(token, req.secret)
22+
.catch(() => {});
1823

19-
const tokenUserData = await client
20-
.findUser(token, req.secret)
21-
.catch(() => {});
22-
23-
if (tokenUserData) {
24-
return notFoundError(`You are already logged into the system`, 400);
24+
if (tokenUserData) {
25+
return notFoundError(`You are already logged into the system`, 400);
26+
}
2527
}
26-
2728
const { userName, password } = loginData;
2829
if (!userName) {
2930
return notFoundError(`userName is required`, 400);

src/schema/dashboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const dashboardDataSchema: Schema<IDashboardData> = new Schema({
4242

4343
const DashboardDataModel = model<IDashboardData>(
4444
"DashboardData",
45-
dashboardDataSchema,
45+
dashboardDataSchema
4646
);
4747

4848
export default DashboardDataModel;

src/structures/App.ts

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,63 @@
11
import express from "express";
2-
import config from "../config";
2+
import config, { Config } from "../config";
33
import cookieParser from "cookie-parser";
44
import mongoose from "mongoose";
55
import bodyParser from "body-parser";
66
import amqp from "amqplib";
77
import jwt from "jsonwebtoken";
88
import userDataDb from "../schema/userData";
99
import cors from "cors";
10-
import Logger from "../utils/logger";
10+
import { LoggerType, logger } from "../utils/logger";
11+
import { Server } from "http";
1112
// import * as CircularJSON from "circular-json";
1213
// import * as dayjs from "dayjs";
1314
// import { EventEmitter } from "ws";
1415
// import { v4 as uuidv4 } from "uuid";
1516
// import { CompressionTypes, Partitioners } from "kafkajs";
1617

18+
interface UserData {
19+
_id: string;
20+
tokens: { token: string }[];
21+
// Add other properties as per your actual structure
22+
}
23+
24+
type ErrorMiddlewareFunction = (
25+
req: express.Request,
26+
res: express.Response,
27+
next: express.NextFunction
28+
) => Promise<void>;
29+
30+
interface FindUserFunction {
31+
(token: string, secret?: string): Promise<UserData>; // Adjust the return type as per your actual return structure
32+
}
33+
1734
interface App {
18-
new (someParam: any): App;
19-
config: any;
20-
express: any;
35+
config: Config;
2136
routes: Map<string, any>;
22-
logger: any;
23-
mongoose: any;
37+
logger: LoggerType;
38+
mongoose: mongoose.Mongoose;
2439
fileCache: Map<string, any>;
2540
linkCache: Map<string, any>;
26-
parseURL: any;
27-
jwt: any;
28-
ipport: any;
29-
_errorMiddleware: any;
30-
_requirehandlers: any;
31-
port: any;
32-
links: any;
33-
server: any;
34-
findUser: any;
35-
connect: any;
36-
app: any;
41+
parseURL: (link: string) => URL;
42+
jwt: jwt.JwtPayload;
43+
ipport: (link: string, port: number) => string;
44+
_errorMiddleware: ErrorMiddlewareFunction;
45+
_requirehandlers: () => Promise<void>;
46+
port: number;
47+
links: string[];
48+
server: Server;
49+
findUser: FindUserFunction;
50+
connect: () => Promise<void>;
51+
app: express.Express;
52+
}
53+
export interface AppTypes extends App {
54+
3755
}
38-
3956
class App {
4057
constructor() {
4158
this.config = config;
4259
this.routes = new Map();
43-
this.logger = Logger;
60+
this.logger = logger;
4461
this.mongoose = mongoose;
4562
this.fileCache = new Map();
4663
this.linkCache = new Map();
@@ -74,12 +91,17 @@ class App {
7491
req: express.Request,
7592
res: express.Response,
7693
next: express.NextFunction
77-
) {
78-
return res.status(404).json({ error: true, message: "page not found" });
94+
): Promise<void> {
95+
res.status(404).json({ error: true, message: "page not found" });
96+
return;
7997
};
8098

8199
this._requirehandlers = async function () {
82-
this.port = this.config.website.port;
100+
this.port =
101+
parseInt(
102+
this.config.website.port ? this.config.website.port : "8080",
103+
10
104+
) || 8080;
83105
this.links = this.config.website.links;
84106
this.server = this.app.listen(this.port);
85107
this.app.use(
@@ -204,13 +226,16 @@ class App {
204226
this.connect = async function () {
205227
this.logger.log(`[WebSite] Loading !`, "log");
206228

207-
await this.mongoose.connect(this.config.mongourl, {
208-
useNewUrlParser: true,
209-
autoIndex: false,
210-
connectTimeoutMS: 10000,
211-
family: 4,
212-
useUnifiedTopology: true,
213-
});
229+
await this.mongoose.connect(
230+
this.config.mongourl ? this.config.mongourl : "",
231+
{
232+
// useNewUrlParser: true,
233+
autoIndex: false,
234+
connectTimeoutMS: 10000,
235+
family: 4,
236+
// useUnifiedTopology: true,
237+
}
238+
);
214239

215240
await this._requirehandlers();
216241
this.logger.log(`[WebSite] Website is now Online !`, "ready");

src/utils/filterData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class FilterData {
5555

5656
private populateDestructuredData(
5757
destrucuredData: any,
58-
findingUserData: UserData,
58+
findingUserData: UserData
5959
) {
6060
destrucuredData.profilePicture = findingUserData.profilePicture || "";
6161
destrucuredData.bio = findingUserData.bio || "";

src/utils/logger.ts

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,87 @@
11
import chalk from "chalk";
22
import moment from "moment";
33

4+
type loggerTypes =
5+
| "log"
6+
| "warn"
7+
| "error"
8+
| "debug"
9+
| "cmd"
10+
| "event"
11+
| "ready";
12+
13+
interface LoggerType {
14+
log(message?: any, optionalParams?: loggerTypes): void;
15+
}
416
export default class Logger {
5-
static log(
6-
content: string,
7-
type:
8-
| "log"
9-
| "warn"
10-
| "error"
11-
| "debug"
12-
| "cmd"
13-
| "event"
14-
| "ready" = "log",
15-
) {
17+
log(content: string, type: loggerTypes = "log") {
1618
const date = `${moment().format("DD-MM-YYYY hh:mm:ss")}`;
1719
switch (type) {
1820
case "log": {
1921
return console.log(
2022
`[${chalk.yellow(date)}]: [${chalk.black.bgBlue(
21-
type.toUpperCase(),
22-
)}] ${chalk.blue(content)}`,
23+
type.toUpperCase()
24+
)}] ${chalk.blue(content)}`
2325
);
2426
}
2527
case "warn": {
2628
return console.log(
2729
`[${chalk.yellow(date)}]: [${chalk.black.bgYellow(
28-
type.toUpperCase(),
29-
)}] ${chalk.blue(content)}`,
30+
type.toUpperCase()
31+
)}] ${chalk.blue(content)}`
3032
);
3133
}
3234
case "error": {
3335
return console.log(
3436
`[${chalk.yellow(date)}]: [${chalk.black.bgRed(
35-
type.toUpperCase(),
36-
)}] ${chalk.blue(content)}`,
37+
type.toUpperCase()
38+
)}] ${chalk.blue(content)}`
3739
);
3840
}
3941
case "debug": {
4042
return console.log(
4143
`[${chalk.yellow(date)}]: [${chalk.black.bgGreen(
42-
type.toUpperCase(),
43-
)}] ${chalk.blue(content)}`,
44+
type.toUpperCase()
45+
)}] ${chalk.blue(content)}`
4446
);
4547
}
4648
case "cmd": {
4749
return console.log(
4850
`[${chalk.yellow(date)}]: [${chalk.black.bgWhite(
49-
type.toUpperCase(),
50-
)}] ${chalk.blue(content)}`,
51+
type.toUpperCase()
52+
)}] ${chalk.blue(content)}`
5153
);
5254
}
5355
case "event": {
5456
return console.log(
5557
`[${chalk.yellow(date)}]: [${chalk.black.bgHex("#e1f507")(
56-
type.toUpperCase(),
57-
)}] ${chalk.blue(content)}`,
58+
type.toUpperCase()
59+
)}] ${chalk.blue(content)}`
5860
);
5961
}
6062
case "ready": {
6163
return console.log(
6264
`[${chalk.yellow(date)}]: [${chalk.black.bgHex("#067032")(
63-
type.toUpperCase(),
64-
)}] ${chalk.blue(content)}`,
65+
type.toUpperCase()
66+
)}] ${chalk.blue(content)}`
6567
);
6668
}
6769
default:
6870
throw new TypeError(
69-
"Logger type must be either warn, debug, log, ready, cmd or error.",
71+
"Logger type must be either warn, debug, log, ready, cmd or error."
7072
);
7173
}
7274
}
7375
}
76+
77+
interface CustomLogger {
78+
log: (content: string, type?: loggerTypes) => void;
79+
}
80+
81+
const loggerInstance = new Logger();
82+
83+
const logger: CustomLogger = {
84+
log: loggerInstance.log.bind(loggerInstance),
85+
};
86+
87+
export { logger, LoggerType };

src/websocket/v1/home.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ export default {
4444
socket.removeListener("disconnect", handleClear);
4545
resolve();
4646
};
47-
setTimeout(
48-
() => {
49-
handleClear();
50-
},
51-
1000 * 60 * 2,
52-
);
47+
setTimeout(() => {
48+
handleClear();
49+
}, 1000 * 60 * 2);
5350
socket.on("client-message", handleReceive);
5451
socket.on("disconnect", handleClear);
5552
});

0 commit comments

Comments
 (0)