Skip to content

Commit 9c5410c

Browse files
committed
fixed some.
1 parent 405cd99 commit 9c5410c

13 files changed

+139
-112
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nodejs-backend-structure",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "NodeJS Backend Structure.",
55
"main": "dist/index.js",
66
"preview": false,

src/handlers/initDatabase.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,39 @@ import { EventEmitter } from "events";
44
import { readdir } from "fs";
55
import dashboardDataDb from "../schema/dashboard";
66
import userData from "../schema/userData";
7+
import { AppTypes } from "../structures/App";
8+
9+
// interface Schema {
10+
// _id: {
11+
// $oid: string;
12+
// };
13+
// type: string;
14+
// navigationLinks: ({
15+
// title: string;
16+
// icon: string;
17+
// path: string;
18+
// url: string;
19+
// _id: {
20+
// $oid: string;
21+
// };
22+
// subMenu: never[];
23+
// } | {
24+
// title: string;
25+
// icon: string;
26+
// path: string;
27+
// url: string;
28+
// _id: {
29+
// $oid: string;
30+
// };
31+
// subMenu?: undefined;
32+
// })[];
33+
// __v: number;
34+
// }
735

836
export = class DatabaseInitializer {
9-
private client: any;
37+
private client: AppTypes;
1038

11-
constructor(client: any) {
39+
constructor(client: AppTypes) {
1240
if (!client) throw new Error(`client is required`);
1341
this.client = client;
1442
}

src/handlers/loadincomingEvents.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,20 @@
22

33
import { EventEmitter } from "events";
44
import { readdir } from "fs";
5-
import socketIo, { Server, Socket } from "socket.io";
6-
import { ParsedUrlQuery } from "querystring";
7-
8-
interface Client {
9-
server: any;
10-
config: any;
11-
links: string[];
12-
parseURL: (link: string) => any;
13-
ipport: (link: string, port: number) => string;
14-
port: number;
15-
logger: any;
16-
wspaths: Map<string, any>;
17-
io: Server;
18-
wsevents: EventEmitter;
19-
}
5+
import socketIo, { Socket } from "socket.io";
6+
import { AppTypes } from "../structures/App";
207

218
interface WebSocketPath {
229
default: {
2310
name: string;
24-
run: (client: Client, socket: Socket, request: any) => void;
11+
run: (client: AppTypes, socket: Socket, request: Socket) => void;
2512
};
2613
}
2714

2815
export = class WebSocketInitializer {
29-
private client: Client;
16+
private client: AppTypes;
3017

31-
constructor(client: Client) {
18+
constructor(client: AppTypes) {
3219
if (!client) throw new Error(`client is required`);
3320
this.client = client;
3421
this.client.wspaths = new Map();
@@ -49,7 +36,7 @@ export = class WebSocketInitializer {
4936
});
5037
this.client.io = io;
5138

52-
io.use((socket: any, next: any) => {
39+
io.use((socket, next) => {
5340
const { host } = socket.request.headers;
5441
let checkHost = false;
5542

@@ -110,7 +97,7 @@ export = class WebSocketInitializer {
11097

11198
for (const [name, path] of this.client.wspaths) {
11299
const nameSpace = io.of(name);
113-
nameSpace.on("connection", (socket: any) => {
100+
nameSpace.on("connection", (socket: Socket) => {
114101
path.run(this.client, socket, socket.request);
115102
});
116103
}

src/handlers/loadroutes.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
"use strict";
22

33
import { readdir } from "fs";
4-
5-
interface Client {
6-
routes: Map<string, any>;
7-
logger: any;
8-
}
4+
import { AppTypes } from "../structures/App";
95

106
interface Route {
117
version: string;
128
}
139

1410
interface Path {
1511
name: string;
16-
route: any; // Replace 'any' with an appropriate type based on your route structure
12+
route: RouteConstructor;
13+
}
14+
interface RouteConstructor {
15+
new (client: AppTypes): Route; // If Route is a class and takes AppTypes in its constructor
1716
}
1817

1918
export = class RoutesInitializer {
20-
private client: Client;
19+
private client: AppTypes;
2120

22-
constructor(client: Client) {
21+
constructor(client: AppTypes) {
2322
if (!client) throw new Error(`client is required`);
2423
this.client = client;
2524
}

src/handlers/sendSystemStatus.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import updateSystemSpecs from "../utils/getSystemSpecs";
44
import { readdir } from "fs";
5-
import { Client } from "./types"; // Replace './types' with the path to your types file
5+
import { AppTypes } from "../structures/App";
66

77
export = class SystemInformation {
8-
private client: Client;
8+
private client: AppTypes;
99

10-
constructor(client: Client) {
10+
constructor(client: AppTypes) {
1111
if (!client) throw new Error(`client is required`);
1212
this.client = client;
1313
}

src/handlers/types.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/index.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import readline from "readline";
77

88
const client = new App();
99

10-
process.on("unhandledRejection", (reason: any, p: any) => {
10+
process.on("unhandledRejection", (reason, p) => {
1111
console.log("\n\n\n\n\n=== unhandled Rejection ===".toUpperCase().yellow.dim);
1212
console.log(reason);
1313
console.log("=== unhandled Rejection ===\n\n\n\n\n".toUpperCase().yellow.dim);
@@ -37,25 +37,23 @@ process.on("exit", (code: number) => {
3737
console.log("=== exit ===\n\n\n\n\n".toUpperCase().yellow.dim);
3838
});
3939

40-
process.on(
41-
"multipleResolves",
42-
(type: string, promise: Promise<any>, reason: any) => {
43-
// Do something for multipleResolves event
44-
}
45-
);
40+
process.on("multipleResolves", (type, promise, reason) => {
41+
// Do something for multipleResolves event
42+
});
4643

4744
function getIPv4Addresses(): string | undefined {
4845
const networkInterfaces = os.networkInterfaces();
4946
let ipv4Address: string | undefined;
5047

5148
Object.keys(networkInterfaces).forEach((interfaceName) => {
52-
const interfaceInfo: any = networkInterfaces[interfaceName];
53-
54-
interfaceInfo.forEach((info: any) => {
55-
if (info.family === "IPv4" && info.internal === false) {
56-
ipv4Address = info.address;
57-
}
58-
});
49+
const interfaceInfo = networkInterfaces[interfaceName];
50+
if (interfaceInfo) {
51+
interfaceInfo.forEach((info) => {
52+
if (info.family === "IPv4" && info.internal === false) {
53+
ipv4Address = info.address;
54+
}
55+
});
56+
}
5957
});
6058

6159
return ipv4Address;
@@ -103,7 +101,7 @@ new Promise<void>((resolve) => {
103101
}
104102
}
105103

106-
const allips = client.config.website.links.map((val: any) => {
104+
const allips = client.config.website.links.map((val) => {
107105
return (val = `${val}:${client.config.website.port}`);
108106
});
109107

src/structures/App.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import jwt from "jsonwebtoken";
88
import userDataDb from "../schema/userData";
99
import cors from "cors";
1010
import { LoggerType, logger } from "../utils/logger";
11+
import EventEmitter from "events";
12+
import socketIo, { Server as SocketServer, Socket } from "socket.io";
1113
import { Server } from "http";
1214
// import * as CircularJSON from "circular-json";
1315
// import * as dayjs from "dayjs";
@@ -49,10 +51,11 @@ interface App {
4951
findUser: FindUserFunction;
5052
connect: () => Promise<void>;
5153
app: express.Express;
54+
wspaths: Map<string, any>;
55+
io: SocketServer;
56+
wsevents: EventEmitter;
5257
}
53-
export interface AppTypes extends App {
54-
55-
}
58+
export interface AppTypes extends App {}
5659
class App {
5760
constructor() {
5861
this.config = config;

src/utils/filterData.ts

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dayjs from "dayjs";
2+
import { AppTypes } from "../structures/App";
23

34
interface UserData {
45
profilePicture: string;
@@ -17,55 +18,56 @@ interface UserData {
1718
};
1819
}
1920

21+
interface DestrucuredData {
22+
profilePicture: string;
23+
bio: string;
24+
userName: string;
25+
id: string;
26+
name: string;
27+
school: string;
28+
owner: boolean;
29+
createdAt: dayjs.Dayjs; // Update to correct type Dayjs
30+
role: string;
31+
editAccessRoles?: {
32+
roleIndex: number;
33+
roleType: string;
34+
}[];
35+
}
36+
2037
class FilterData {
21-
private client: any;
38+
private client: AppTypes;
2239

23-
constructor(client: any) {
40+
constructor(client: AppTypes) {
2441
this.client = client;
2542
}
2643

27-
async user(userType: string, findingUserData: UserData) {
28-
const destrucuredData: any = {};
44+
user(userType: string, findingUserData: UserData): DestrucuredData {
45+
const destrucuredData: DestrucuredData = {
46+
profilePicture: findingUserData.profilePicture || "",
47+
bio: findingUserData.bio || "",
48+
userName: findingUserData.userName || "",
49+
id: String(findingUserData._id) || "",
50+
name: findingUserData.name || "",
51+
school: findingUserData.school || "",
52+
owner: findingUserData.owner || false,
53+
createdAt: dayjs(findingUserData.createdAt), // Initialize with dayjs
54+
role: findingUserData.roles.roleType || "",
55+
};
2956

30-
switch (userType === "@me") {
31-
case true: {
32-
this.populateDestructuredData(destrucuredData, findingUserData);
33-
34-
if (findingUserData.roles.roleIndex === "1") {
35-
destrucuredData.editAcessRoles = [
36-
{ roleIndex: 2, roleType: "admin" },
37-
{ roleIndex: 3, roleType: "staff" },
38-
];
39-
} else if (findingUserData.roles.roleIndex === "2") {
40-
destrucuredData.editAcessRoles = [
41-
{ roleIndex: 3, roleType: "staff" },
42-
];
43-
} else if (findingUserData.roles.roleIndex === "3") {
44-
destrucuredData.editAcessRoles = [];
45-
}
46-
break;
47-
}
48-
case false: {
49-
this.populateDestructuredData(destrucuredData, findingUserData);
50-
break;
57+
if (userType === "@me") {
58+
if (findingUserData.roles.roleIndex === "1") {
59+
destrucuredData.editAccessRoles = [
60+
{ roleIndex: 2, roleType: "admin" },
61+
{ roleIndex: 3, roleType: "staff" },
62+
];
63+
} else if (findingUserData.roles.roleIndex === "2") {
64+
destrucuredData.editAccessRoles = [{ roleIndex: 3, roleType: "staff" }];
65+
} else if (findingUserData.roles.roleIndex === "3") {
66+
destrucuredData.editAccessRoles = [];
5167
}
5268
}
53-
return destrucuredData;
54-
}
5569

56-
private populateDestructuredData(
57-
destrucuredData: any,
58-
findingUserData: UserData
59-
) {
60-
destrucuredData.profilePicture = findingUserData.profilePicture || "";
61-
destrucuredData.bio = findingUserData.bio || "";
62-
destrucuredData.userName = findingUserData.userName || "";
63-
destrucuredData.id = String(findingUserData._id) || "";
64-
destrucuredData.name = findingUserData.name || "";
65-
destrucuredData.school = findingUserData.school || "";
66-
destrucuredData.owner = findingUserData.owner || false;
67-
destrucuredData.createdAt = dayjs(findingUserData.createdAt) || "";
68-
destrucuredData.role = findingUserData.roles.roleType || "";
70+
return destrucuredData;
6971
}
7072
}
7173

src/utils/getSystemSpecs.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ interface DiskUsage {
1515
freeStorage: number;
1616
}
1717

18+
interface SystemSpecs {
19+
id: number;
20+
type: string;
21+
usage: string | number; // Adjust according to actual types for 'usage'
22+
data: SystemData[];
23+
}
24+
25+
interface SystemData {
26+
_id: number;
27+
title: string;
28+
amount: string | number; // Adjust according to actual types for 'amount'
29+
}
30+
1831
function getPrimaryDiskPath(): string {
1932
if (os.platform() === "win32") {
2033
return "C:";
@@ -33,7 +46,7 @@ function formatBytes(bytes: number): string {
3346
return `${formattedValue} ${sizes[i]}`;
3447
}
3548

36-
async function getCpuUsage(): Promise<any> {
49+
async function getCpuUsage(): Promise<si.Systeminformation.CurrentLoadData | null> {
3750
try {
3851
const sys = await si.currentLoad();
3952
return sys;
@@ -72,7 +85,7 @@ function getDiskUsage(path: string): Promise<DiskUsage> {
7285
});
7386
}
7487

75-
async function updateSystemSpecs(): Promise<any[]> {
88+
async function updateSystemSpecs(): Promise<SystemSpecs[]> {
7689
try {
7790
const systemUsage = await getSystemUsage();
7891
const diskUsage = await getDiskUsage(getPrimaryDiskPath());

0 commit comments

Comments
 (0)