Skip to content

Commit

Permalink
fixed some.
Browse files Browse the repository at this point in the history
  • Loading branch information
darkwaves-ofc committed Jan 2, 2024
1 parent 405cd99 commit 9c5410c
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 112 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodejs-backend-structure",
"version": "1.1.1",
"version": "1.1.2",
"description": "NodeJS Backend Structure.",
"main": "dist/index.js",
"preview": false,
Expand Down
32 changes: 30 additions & 2 deletions src/handlers/initDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,39 @@ import { EventEmitter } from "events";
import { readdir } from "fs";
import dashboardDataDb from "../schema/dashboard";
import userData from "../schema/userData";
import { AppTypes } from "../structures/App";

// interface Schema {
// _id: {
// $oid: string;
// };
// type: string;
// navigationLinks: ({
// title: string;
// icon: string;
// path: string;
// url: string;
// _id: {
// $oid: string;
// };
// subMenu: never[];
// } | {
// title: string;
// icon: string;
// path: string;
// url: string;
// _id: {
// $oid: string;
// };
// subMenu?: undefined;
// })[];
// __v: number;
// }

export = class DatabaseInitializer {
private client: any;
private client: AppTypes;

constructor(client: any) {
constructor(client: AppTypes) {
if (!client) throw new Error(`client is required`);
this.client = client;
}
Expand Down
27 changes: 7 additions & 20 deletions src/handlers/loadincomingEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,20 @@

import { EventEmitter } from "events";
import { readdir } from "fs";
import socketIo, { Server, Socket } from "socket.io";
import { ParsedUrlQuery } from "querystring";

interface Client {
server: any;
config: any;
links: string[];
parseURL: (link: string) => any;
ipport: (link: string, port: number) => string;
port: number;
logger: any;
wspaths: Map<string, any>;
io: Server;
wsevents: EventEmitter;
}
import socketIo, { Socket } from "socket.io";
import { AppTypes } from "../structures/App";

interface WebSocketPath {
default: {
name: string;
run: (client: Client, socket: Socket, request: any) => void;
run: (client: AppTypes, socket: Socket, request: Socket) => void;
};
}

export = class WebSocketInitializer {
private client: Client;
private client: AppTypes;

constructor(client: Client) {
constructor(client: AppTypes) {
if (!client) throw new Error(`client is required`);
this.client = client;
this.client.wspaths = new Map();
Expand All @@ -49,7 +36,7 @@ export = class WebSocketInitializer {
});
this.client.io = io;

io.use((socket: any, next: any) => {
io.use((socket, next) => {
const { host } = socket.request.headers;
let checkHost = false;

Expand Down Expand Up @@ -110,7 +97,7 @@ export = class WebSocketInitializer {

for (const [name, path] of this.client.wspaths) {
const nameSpace = io.of(name);
nameSpace.on("connection", (socket: any) => {
nameSpace.on("connection", (socket: Socket) => {
path.run(this.client, socket, socket.request);
});
}
Expand Down
15 changes: 7 additions & 8 deletions src/handlers/loadroutes.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
"use strict";

import { readdir } from "fs";

interface Client {
routes: Map<string, any>;
logger: any;
}
import { AppTypes } from "../structures/App";

interface Route {
version: string;
}

interface Path {
name: string;
route: any; // Replace 'any' with an appropriate type based on your route structure
route: RouteConstructor;
}
interface RouteConstructor {
new (client: AppTypes): Route; // If Route is a class and takes AppTypes in its constructor
}

export = class RoutesInitializer {
private client: Client;
private client: AppTypes;

constructor(client: Client) {
constructor(client: AppTypes) {
if (!client) throw new Error(`client is required`);
this.client = client;
}
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/sendSystemStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

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

export = class SystemInformation {
private client: Client;
private client: AppTypes;

constructor(client: Client) {
constructor(client: AppTypes) {
if (!client) throw new Error(`client is required`);
this.client = client;
}
Expand Down
15 changes: 0 additions & 15 deletions src/handlers/types.ts

This file was deleted.

28 changes: 13 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import readline from "readline";

const client = new App();

process.on("unhandledRejection", (reason: any, p: any) => {
process.on("unhandledRejection", (reason, p) => {
console.log("\n\n\n\n\n=== unhandled Rejection ===".toUpperCase().yellow.dim);
console.log(reason);
console.log("=== unhandled Rejection ===\n\n\n\n\n".toUpperCase().yellow.dim);
Expand Down Expand Up @@ -37,25 +37,23 @@ process.on("exit", (code: number) => {
console.log("=== exit ===\n\n\n\n\n".toUpperCase().yellow.dim);
});

process.on(
"multipleResolves",
(type: string, promise: Promise<any>, reason: any) => {
// Do something for multipleResolves event
}
);
process.on("multipleResolves", (type, promise, reason) => {
// Do something for multipleResolves event
});

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

Object.keys(networkInterfaces).forEach((interfaceName) => {
const interfaceInfo: any = networkInterfaces[interfaceName];

interfaceInfo.forEach((info: any) => {
if (info.family === "IPv4" && info.internal === false) {
ipv4Address = info.address;
}
});
const interfaceInfo = networkInterfaces[interfaceName];
if (interfaceInfo) {
interfaceInfo.forEach((info) => {
if (info.family === "IPv4" && info.internal === false) {
ipv4Address = info.address;
}
});
}
});

return ipv4Address;
Expand Down Expand Up @@ -103,7 +101,7 @@ new Promise<void>((resolve) => {
}
}

const allips = client.config.website.links.map((val: any) => {
const allips = client.config.website.links.map((val) => {
return (val = `${val}:${client.config.website.port}`);
});

Expand Down
9 changes: 6 additions & 3 deletions src/structures/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import jwt from "jsonwebtoken";
import userDataDb from "../schema/userData";
import cors from "cors";
import { LoggerType, logger } from "../utils/logger";
import EventEmitter from "events";
import socketIo, { Server as SocketServer, Socket } from "socket.io";
import { Server } from "http";
// import * as CircularJSON from "circular-json";
// import * as dayjs from "dayjs";
Expand Down Expand Up @@ -49,10 +51,11 @@ interface App {
findUser: FindUserFunction;
connect: () => Promise<void>;
app: express.Express;
wspaths: Map<string, any>;
io: SocketServer;
wsevents: EventEmitter;
}
export interface AppTypes extends App {

}
export interface AppTypes extends App {}
class App {
constructor() {
this.config = config;
Expand Down
82 changes: 42 additions & 40 deletions src/utils/filterData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dayjs from "dayjs";
import { AppTypes } from "../structures/App";

interface UserData {
profilePicture: string;
Expand All @@ -17,55 +18,56 @@ interface UserData {
};
}

interface DestrucuredData {
profilePicture: string;
bio: string;
userName: string;
id: string;
name: string;
school: string;
owner: boolean;
createdAt: dayjs.Dayjs; // Update to correct type Dayjs
role: string;
editAccessRoles?: {
roleIndex: number;
roleType: string;
}[];
}

class FilterData {
private client: any;
private client: AppTypes;

constructor(client: any) {
constructor(client: AppTypes) {
this.client = client;
}

async user(userType: string, findingUserData: UserData) {
const destrucuredData: any = {};
user(userType: string, findingUserData: UserData): DestrucuredData {
const destrucuredData: DestrucuredData = {
profilePicture: findingUserData.profilePicture || "",
bio: findingUserData.bio || "",
userName: findingUserData.userName || "",
id: String(findingUserData._id) || "",
name: findingUserData.name || "",
school: findingUserData.school || "",
owner: findingUserData.owner || false,
createdAt: dayjs(findingUserData.createdAt), // Initialize with dayjs
role: findingUserData.roles.roleType || "",
};

switch (userType === "@me") {
case true: {
this.populateDestructuredData(destrucuredData, findingUserData);

if (findingUserData.roles.roleIndex === "1") {
destrucuredData.editAcessRoles = [
{ roleIndex: 2, roleType: "admin" },
{ roleIndex: 3, roleType: "staff" },
];
} else if (findingUserData.roles.roleIndex === "2") {
destrucuredData.editAcessRoles = [
{ roleIndex: 3, roleType: "staff" },
];
} else if (findingUserData.roles.roleIndex === "3") {
destrucuredData.editAcessRoles = [];
}
break;
}
case false: {
this.populateDestructuredData(destrucuredData, findingUserData);
break;
if (userType === "@me") {
if (findingUserData.roles.roleIndex === "1") {
destrucuredData.editAccessRoles = [
{ roleIndex: 2, roleType: "admin" },
{ roleIndex: 3, roleType: "staff" },
];
} else if (findingUserData.roles.roleIndex === "2") {
destrucuredData.editAccessRoles = [{ roleIndex: 3, roleType: "staff" }];
} else if (findingUserData.roles.roleIndex === "3") {
destrucuredData.editAccessRoles = [];
}
}
return destrucuredData;
}

private populateDestructuredData(
destrucuredData: any,
findingUserData: UserData
) {
destrucuredData.profilePicture = findingUserData.profilePicture || "";
destrucuredData.bio = findingUserData.bio || "";
destrucuredData.userName = findingUserData.userName || "";
destrucuredData.id = String(findingUserData._id) || "";
destrucuredData.name = findingUserData.name || "";
destrucuredData.school = findingUserData.school || "";
destrucuredData.owner = findingUserData.owner || false;
destrucuredData.createdAt = dayjs(findingUserData.createdAt) || "";
destrucuredData.role = findingUserData.roles.roleType || "";
return destrucuredData;
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/utils/getSystemSpecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ interface DiskUsage {
freeStorage: number;
}

interface SystemSpecs {
id: number;
type: string;
usage: string | number; // Adjust according to actual types for 'usage'
data: SystemData[];
}

interface SystemData {
_id: number;
title: string;
amount: string | number; // Adjust according to actual types for 'amount'
}

function getPrimaryDiskPath(): string {
if (os.platform() === "win32") {
return "C:";
Expand All @@ -33,7 +46,7 @@ function formatBytes(bytes: number): string {
return `${formattedValue} ${sizes[i]}`;
}

async function getCpuUsage(): Promise<any> {
async function getCpuUsage(): Promise<si.Systeminformation.CurrentLoadData | null> {
try {
const sys = await si.currentLoad();
return sys;
Expand Down Expand Up @@ -72,7 +85,7 @@ function getDiskUsage(path: string): Promise<DiskUsage> {
});
}

async function updateSystemSpecs(): Promise<any[]> {
async function updateSystemSpecs(): Promise<SystemSpecs[]> {
try {
const systemUsage = await getSystemUsage();
const diskUsage = await getDiskUsage(getPrimaryDiskPath());
Expand Down
Loading

0 comments on commit 9c5410c

Please sign in to comment.