4 Commits

Author SHA1 Message Date
489fbec561 fix(deps): update dependency mongoose to v8.3.2 2024-04-17 00:57:35 +00:00
6a058acd58 fix deps 2023-11-20 16:04:10 +00:00
78c16354d3 fix deps 2023-11-20 15:52:58 +00:00
415b9f8928 add route logging and a generic error handler for routes 2023-11-20 15:42:08 +00:00
5 changed files with 1440 additions and 1059 deletions

View File

@ -5,7 +5,7 @@
"main": "dist/index.js", "main": "dist/index.js",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"build": "tsup src/index.ts --format cjs", "build": "tsup src/index.ts --format cjs --minify terser",
"start": "node dist/index.js", "start": "node dist/index.js",
"dev": "nodemon --exec ts-node src/index.ts" "dev": "nodemon --exec ts-node src/index.ts"
}, },
@ -14,12 +14,13 @@
"express": "^4.18.2", "express": "^4.18.2",
"infisical-node": "^1.5.0", "infisical-node": "^1.5.0",
"mongoose": "^8.0.1", "mongoose": "^8.0.1",
"tsup": "^8.0.0",
"typescript": "^5.2.2" "typescript": "^5.2.2"
}, },
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.21", "@types/express": "^4.17.21",
"nodemon": "^3.0.1", "nodemon": "^3.0.1",
"ts-node": "^10.9.1" "terser": "^5.24.0",
"ts-node": "^10.9.1",
"tsup": "^8.0.0"
} }
} }

2377
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

48
src/route/messages.ts Normal file
View File

@ -0,0 +1,48 @@
/**
* Creates the base message for web responses
*
* @param message the message to send
* @returns the message
*/
function baseMessage(error: boolean, message: string) {
return {
error,
message,
};
}
/**
* Creates an error message for web responses
*
* @param message the message to send
* @returns the message
*/
function errorMessage(message: string) {
return baseMessage(true, message);
}
/**
* Creates a success message for web responses
*
* @param message the message to send
* @returns the message
*/
function successMessage(message: string) {
return baseMessage(false, message);
}
/**
* Creates a message for an internal server error
*
* @returns the message
*/
function internalServerError() {
return errorMessage("Internal Server Error");
}
export const ServerMessages = {
baseMessage,
errorMessage,
successMessage,
internalServerError,
};

View File

@ -24,6 +24,9 @@ export async function initSecrets() {
MONGO_URI = mongoUri; MONGO_URI = mongoUri;
} }
/**
* All of the Infisical secrets
*/
export const Secrets = { export const Secrets = {
get MONGO_URI() { get MONGO_URI() {
return MONGO_URI; return MONGO_URI;

View File

@ -1,4 +1,5 @@
import express, { Express } from "express"; import express, { Express } from "express";
import { ServerMessages } from "../route/messages";
import { Route } from "../route/route"; import { Route } from "../route/route";
type ServerConfig = { type ServerConfig = {
@ -32,13 +33,26 @@ export default class Server {
this.server = express(); this.server = express();
this.preInit(); this.preInit();
// Setup route logging
this.server.use((req, res, next) => {
// TODO: make this look better?
console.log(`[${req.method}] ${req.path}`);
next();
});
// Handle the routes // Handle the routes
for (const route of this.routes) { for (const route of this.routes) {
this.server.all(route.getPath(), (req, res, next) => { this.server.all(route.getPath(), (req, res, next) => {
if (req.method.toUpperCase() !== route.getMethod().toUpperCase()) { if (req.method.toUpperCase() !== route.getMethod().toUpperCase()) {
return next(); // Skip this method return next(); // Skip this method
} }
try {
route.handle(req, res, next); route.handle(req, res, next);
} catch (ex) {
console.error(ex);
// Inform the user that an internal server error occurred
res.status(500).json(ServerMessages.internalServerError());
}
}); });
} }