diff --git a/src/decorators/Controller.ts b/src/decorators/Controller.ts index 0fa9a0b..a0989de 100644 --- a/src/decorators/Controller.ts +++ b/src/decorators/Controller.ts @@ -79,35 +79,36 @@ export function Controller() // Bind the method to the class instance app.get(path, async (req, res, next) => { - console.log('[Controller.setup.] Request:', req); - console.log('[Controller.setup.] Response:', res); - console.log('[Controller.setup.] Next:', next); + //console.log('[Controller.setup.] Request:', req); + //console.log('[Controller.setup.] Response:', res); + //console.log('[Controller.setup.] Next:', next); try { await Promise.all([fn.bind(controller)(req, res, next)]) .catch((error) => { - console.log('[Controller.setup.] Error in promise:', error); + //console.log('[Controller.setup.] Error in promise:', error); throw new Error(error); }); - console.log('[Controller.setup.] Successfully executed GET method:', method); + //console.log('[Controller.setup.] Successfully executed GET method:', method); } catch(error) { - console.log('[Controller.setup.] Error:', error); + //console.log('[Controller.setup.] Error:', error); if(typeof next !== 'undefined') { - console.log('[Controller.setup.] Calling next with error:', error); + //console.log('[Controller.setup.] Calling next with error:', error); + next(error); return; } else { - console.log('[Controller.setup.] No next function defined, rejecting promise with error:', error); + //console.log('[Controller.setup.] 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.] Finished processing GET request for method:', method); + //console.log('[Controller.setup.] Finished processing GET request for method:', method); }); }); @@ -125,7 +126,31 @@ export function Controller() 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.] Request:', req); + //console.log('[Controller.setup.] Response:', res); + //console.log('[Controller.setup.] Next:', next); + + try { + await Promise.all([fn.bind(controller)(req, res, next)]); + //console.log('[Controller.setup.] Successfully executed POST method:', method); + } + catch(error) { + //console.log('[Controller.setup.] Error:', error); + if(typeof next !== 'undefined') { + //console.log('[Controller.setup.] Calling next with error:', error); + + next(error); + return; + } + else { + //console.log('[Controller.setup.] 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() 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.] Request:', req); + //console.log('[Controller.setup.] Response:', res); + //console.log('[Controller.setup.] Next:', next); + + try { + await Promise.all([fn.bind(controller)(req, res, next)]); + //console.log('[Controller.setup.] Successfully executed PUT method:', method); + } + catch(error) { + //console.log('[Controller.setup.] Error:', error); + if(typeof next !== 'undefined') { + //console.log('[Controller.setup.] Calling next with error:', error); + + next(error); + return; + } + else { + //console.log('[Controller.setup.] 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() 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.] Request:', req); + //console.log('[Controller.setup.] Response:', res); + //console.log('[Controller.setup.] Next:', next); + + try { + await Promise.all([fn.bind(controller)(req, res, next)]); + //console.log('[Controller.setup.] Successfully executed DELETE method:', method); + } + catch(error) { + //console.log('[Controller.setup.] Error:', error); + if(typeof next !== 'undefined') { + //console.log('[Controller.setup.] Calling next with error:', error); + + next(error); + return; + } + else { + //console.log('[Controller.setup.] 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); + } + }; + }); }); } } diff --git a/src/decorators/Page.ts b/src/decorators/Page.ts index 27f02bf..61714ac 100644 --- a/src/decorators/Page.ts +++ b/src/decorators/Page.ts @@ -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);