Package breakdown - initial commit 1.0.0
This commit is contained in:
13
lib/middleware/authenication.ts
Normal file
13
lib/middleware/authenication.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Middleware } from 'koa';
|
||||
|
||||
import { LOGIN_ROUTE } from '../constants/env';
|
||||
|
||||
export const authenticated = (): Middleware => {
|
||||
return (ctx, next) => {
|
||||
if (ctx.isAuthenticated()) {
|
||||
return next();
|
||||
} else {
|
||||
ctx.redirect(process.env.LOGIN_ROUTE || LOGIN_ROUTE);
|
||||
}
|
||||
};
|
||||
};
|
||||
13
lib/middleware/errorHandler.ts
Normal file
13
lib/middleware/errorHandler.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { StatusCodes } from 'http-status-codes';
|
||||
import { Context, Next } from 'koa';
|
||||
|
||||
export const errorHandler = async (ctx: Context, next: Next) => {
|
||||
try {
|
||||
await next();
|
||||
} catch (error: any) {
|
||||
ctx.status = error.statusCode || error.status || StatusCodes.INTERNAL_SERVER_ERROR;
|
||||
error.status = ctx.status;
|
||||
ctx.body = { error };
|
||||
ctx.app.emit('error', error, ctx);
|
||||
}
|
||||
};
|
||||
14
lib/middleware/performance.ts
Normal file
14
lib/middleware/performance.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Context, Next } from 'koa';
|
||||
|
||||
export const performanceLogger = async (ctx: Context, next: Next) => {
|
||||
await next();
|
||||
const rt = ctx.response.get('X-Response-Time');
|
||||
console.log(`${ctx.method} ${ctx.url} - ${rt}`);
|
||||
};
|
||||
|
||||
export const performanceTimer = async (ctx: Context, next: Next) => {
|
||||
const start = Date.now();
|
||||
await next();
|
||||
const ms = Date.now() - start;
|
||||
ctx.set('X-Response-Time', `${ms}ms`);
|
||||
};
|
||||
Reference in New Issue
Block a user