Mostly just commented out a lot of the extra debugging log noise
All checks were successful
Publish to Private NPM Registry / publish (push) Successful in 39s
All checks were successful
Publish to Private NPM Registry / publish (push) Successful in 39s
This commit is contained in:
parent
b90e7a6346
commit
159599c777
2 changed files with 97 additions and 42 deletions
|
|
@ -79,35 +79,36 @@ export function Controller<T extends { new (...args: any[]): BaseController }>()
|
|||
|
||||
// Bind the method to the class instance
|
||||
app.get(path, async (req, res, next) => {
|
||||
console.log('[Controller.setup.<GET request>] Request:', req);
|
||||
console.log('[Controller.setup.<GET request>] Response:', res);
|
||||
console.log('[Controller.setup.<GET request>] Next:', next);
|
||||
//console.log('[Controller.setup.<GET request>] Request:', req);
|
||||
//console.log('[Controller.setup.<GET request>] Response:', res);
|
||||
//console.log('[Controller.setup.<GET request>] Next:', next);
|
||||
|
||||
try {
|
||||
await Promise.all([fn.bind(controller)(req, res, next)])
|
||||
.catch((error) => {
|
||||
console.log('[Controller.setup.<GET request>] Error in promise:', error);
|
||||
//console.log('[Controller.setup.<GET request>] Error in promise:', error);
|
||||
throw new Error(error);
|
||||
});
|
||||
|
||||
console.log('[Controller.setup.<GET request>] Successfully executed GET method:', method);
|
||||
//console.log('[Controller.setup.<GET request>] Successfully executed GET method:', method);
|
||||
}
|
||||
catch(error) {
|
||||
console.log('[Controller.setup.<GET request>] Error:', error);
|
||||
//console.log('[Controller.setup.<GET request>] Error:', error);
|
||||
if(typeof next !== 'undefined') {
|
||||
console.log('[Controller.setup.<GET request>] Calling next with error:', error);
|
||||
//console.log('[Controller.setup.<GET request>] Calling next with error:', error);
|
||||
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
console.log('[Controller.setup.<GET request>] No next function defined, rejecting promise with error:', error);
|
||||
//console.log('[Controller.setup.<GET request>] No next function defined, rejecting promise with error:', error);
|
||||
|
||||
// Because next is undefined and we still want to have Express handle the error we reject the promise
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[Controller.setup.<GET request>] Finished processing GET request for method:', method);
|
||||
//console.log('[Controller.setup.<GET request>] Finished processing GET request for method:', method);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -125,7 +126,31 @@ export function Controller<T extends { new (...args: any[]): BaseController }>()
|
|||
const middleware: NextFunction[] = postRoute.middleware;
|
||||
|
||||
// Bind the method to the class instance
|
||||
app.post(path, ...middleware, fn.bind(controller));
|
||||
app.post(path, ...middleware, async (req, res, next) => {
|
||||
//console.log('[Controller.setup.<POST request>] Request:', req);
|
||||
//console.log('[Controller.setup.<POST request>] Response:', res);
|
||||
//console.log('[Controller.setup.<POST request>] Next:', next);
|
||||
|
||||
try {
|
||||
await Promise.all([fn.bind(controller)(req, res, next)]);
|
||||
//console.log('[Controller.setup.<POST request>] Successfully executed POST method:', method);
|
||||
}
|
||||
catch(error) {
|
||||
//console.log('[Controller.setup.<POST request>] Error:', error);
|
||||
if(typeof next !== 'undefined') {
|
||||
//console.log('[Controller.setup.<POST request>] Calling next with error:', error);
|
||||
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
//console.log('[Controller.setup.<POST request>] No next function defined, rejecting promise with error:', error);
|
||||
|
||||
// Because next is undefined and we still want to have Express handle the error we reject the promise
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Loop over all the methods in the decorated class looking for methods that use the `@PUT` decorator
|
||||
|
|
@ -142,7 +167,31 @@ export function Controller<T extends { new (...args: any[]): BaseController }>()
|
|||
const middleware = putRoute.middleware;
|
||||
|
||||
// Bind the method to the class instance
|
||||
app.put(path, ...middleware, fn.bind(controller));
|
||||
app.put(path, ...middleware, async (req, res, next) => {
|
||||
//console.log('[Controller.setup.<PUT request>] Request:', req);
|
||||
//console.log('[Controller.setup.<PUT request>] Response:', res);
|
||||
//console.log('[Controller.setup.<PUT request>] Next:', next);
|
||||
|
||||
try {
|
||||
await Promise.all([fn.bind(controller)(req, res, next)]);
|
||||
//console.log('[Controller.setup.<PUT request>] Successfully executed PUT method:', method);
|
||||
}
|
||||
catch(error) {
|
||||
//console.log('[Controller.setup.<PUT request>] Error:', error);
|
||||
if(typeof next !== 'undefined') {
|
||||
//console.log('[Controller.setup.<PUT request>] Calling next with error:', error);
|
||||
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
//console.log('[Controller.setup.<PUT request>] No next function defined, rejecting promise with error:', error);
|
||||
|
||||
// Because next is undefined and we still want to have Express handle the error we reject the promise
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Loop over all the methods in the decorated class looking for methods that use the `@DELETE` decorator
|
||||
|
|
@ -159,7 +208,31 @@ export function Controller<T extends { new (...args: any[]): BaseController }>()
|
|||
const middleware = deleteRoute.middleware;
|
||||
|
||||
// Bind the method to the class instance
|
||||
app.delete(path, ...middleware, fn.bind(controller));
|
||||
app.delete(path, ...middleware, async (req, res, next) => {
|
||||
//console.log('[Controller.setup.<DELETE request>] Request:', req);
|
||||
//console.log('[Controller.setup.<DELETE request>] Response:', res);
|
||||
//console.log('[Controller.setup.<DELETE request>] Next:', next);
|
||||
|
||||
try {
|
||||
await Promise.all([fn.bind(controller)(req, res, next)]);
|
||||
//console.log('[Controller.setup.<DELETE request>] Successfully executed DELETE method:', method);
|
||||
}
|
||||
catch(error) {
|
||||
//console.log('[Controller.setup.<DELETE request>] Error:', error);
|
||||
if(typeof next !== 'undefined') {
|
||||
//console.log('[Controller.setup.<DELETE request>] Calling next with error:', error);
|
||||
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
//console.log('[Controller.setup.<DELETE request>] No next function defined, rejecting promise with error:', error);
|
||||
|
||||
// Because next is undefined and we still want to have Express handle the error we reject the promise
|
||||
return Promise.reject(error);
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,16 +63,14 @@ export function Page(title: string, page: string, extraScripts: (string | { scri
|
|||
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
||||
const original = descriptor.value;
|
||||
|
||||
console.log('[Page Decorator] Original:', original);
|
||||
|
||||
// If the original function has more than 3 parameters, we can assume it is an error handler
|
||||
// An error handler starts with the error object, then the request, response and next functions.
|
||||
if(original.length > 3) {
|
||||
descriptor.value = async function (err: Error, req: Request, res: Response, next: NextFunction, ...args: any[]) {
|
||||
console.log('[Page Decorator] Error:', err);
|
||||
console.log('[Page Decorator] Request:', req);
|
||||
console.log('[Page Decorator] Response:', res);
|
||||
console.log('[Page Decorator] Next:', next);
|
||||
//console.log('[Page Decorator] Error:', err);
|
||||
//console.log('[Page Decorator] Request:', req);
|
||||
//console.log('[Page Decorator] Response:', res);
|
||||
//console.log('[Page Decorator] Next:', next);
|
||||
|
||||
try {
|
||||
// We run the original here so that if the decorated method has specific checks it needs to make (ex. if the ID of whatever actually exists) it can make them before rendering the page
|
||||
|
|
@ -97,7 +95,7 @@ export function Page(title: string, page: string, extraScripts: (string | { scri
|
|||
doRender(propertyKey, output, err, req, res, next, ...args);
|
||||
}
|
||||
catch(error) {
|
||||
console.error('[Page Decorator] Error:', error);
|
||||
//console.error('[Page Decorator] Error:', error);
|
||||
|
||||
if(typeof next !== 'undefined') {
|
||||
next(error);
|
||||
|
|
@ -112,9 +110,9 @@ export function Page(title: string, page: string, extraScripts: (string | { scri
|
|||
}
|
||||
else {
|
||||
descriptor.value = async function (req: Request, res: Response, next: NextFunction) {
|
||||
console.log('[Page Decorator] Request:', req);
|
||||
console.log('[Page Decorator] Response:', res);
|
||||
console.log('[Page Decorator] Next:', next);
|
||||
//console.log('[Page Decorator] Request:', req);
|
||||
//console.log('[Page Decorator] Response:', res);
|
||||
//console.log('[Page Decorator] Next:', next);
|
||||
|
||||
try {
|
||||
// We run the original here so that if the decorated method has specific checks it needs to make (ex. if the ID of whatever actually exists) it can make them before rendering the page
|
||||
|
|
@ -137,34 +135,18 @@ export function Page(title: string, page: string, extraScripts: (string | { scri
|
|||
}
|
||||
|
||||
doRender(propertyKey, output, req, res, next);
|
||||
|
||||
/*const renderParams: { [key: string]: any } = {
|
||||
title: title,
|
||||
page: page,
|
||||
extraStyles: extraStyles,
|
||||
extraScripts: extraScripts,
|
||||
...otherParams
|
||||
};
|
||||
|
||||
// If the decorated method's output is an object, we want to merge it with the renderParams
|
||||
if(typeof output === 'object') {
|
||||
Object.entries(output).forEach((entry) => {
|
||||
renderParams[entry[0]] = entry[1];
|
||||
});
|
||||
}
|
||||
|
||||
args.find(arg => isResponse(arg)).render('base', renderParams);*/
|
||||
}
|
||||
catch(error) {
|
||||
console.error('[Page Decorator] Error:', error);
|
||||
//console.error('[Page Decorator] Error:', error);
|
||||
|
||||
if(typeof next !== 'undefined') {
|
||||
console.log('[Page Decorator] Calling next with error:', error);
|
||||
//console.log('[Page Decorator] Calling next with error:', error);
|
||||
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
console.log('[Page Decorator] No next function defined, rejecting promise with error:', error);
|
||||
//console.log('[Page Decorator] No next function defined, rejecting promise with error:', error);
|
||||
|
||||
// Because next is undefined and we still want to have Express handle the error we reject the promise
|
||||
return Promise.reject(error);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue