Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
slightly improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
nahtnam committed May 23, 2019
1 parent dc5552d commit 5cb5412
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
34 changes: 20 additions & 14 deletions src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ interface Route {
handler: Handler;
}

const youchErrors = (fun: any): any => async (req: IM, res: SR): Promise<void> => {
try {
return await fun(req, res);
} catch (err) {
if (!isProd) {
const youch = new Youch(err, req);
const json = await youch.toJSON();
console.log(forTerminal(json)); // eslint-disable-line
}
throw err;
}
};

export default (route: Route): Handler => {
const fn = async (Req: IM, Res: SR): AP => {
let exec = async (req: IM, res: SR): AP => {
Expand All @@ -95,6 +108,12 @@ export default (route: Route): Handler => {

const plugins = route.plugins || [];

plugins.unshift(handleErrors);

if (!isProd) {
plugins.unshift(youchErrors);
}

if (fn.log !== false) {
plugins.unshift(logger);
fn.log = false;
Expand All @@ -104,20 +123,7 @@ export default (route: Route): Handler => {
exec = plugins.reverse().reduce((acc, val): any => val(acc), exec);
}

const youchErrors = (fun: any): any => async (req: IM, res: SR): Promise<void> => {
try {
return await fun(req, res);
} catch (err) {
if (!isProd) {
const youch = new Youch(err, req);
const json = await youch.toJSON();
console.log(forTerminal(json)); // eslint-disable-line
}
throw err;
}
};

return run(Req, Res, handleErrors(youchErrors(exec)));
return run(Req, Res, exec);
};

Object.keys(route).forEach((key): void => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
/* Basic Options */
"target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
Expand Down

0 comments on commit 5cb5412

Please sign in to comment.