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

Commit

Permalink
fix(plugins): add support for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
nahtnam committed May 6, 2019
1 parent 108d02f commit 0864de1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export {
sendError,
} from 'micro';
export {
createError
createError,
} from 'micro-boom';
12 changes: 7 additions & 5 deletions src/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { run, send } from 'micro';
import { run } from 'micro';
import { IncomingMessage, ServerResponse } from 'http';
import AWSServerlessMicro from 'aws-serverless-micro';
import pino from 'pino-http';
import { handleErrors } from 'micro-boom';

const { NODE_ENV } = process.env;
const DEV = NODE_ENV === 'development';

interface Route {
path?: string;
middleware?: any[];
plugins?: any[];
handler: Handler;
}

Expand All @@ -22,7 +20,7 @@ type AP = Promise<any>;

export default (route: Route): Handler => {
const fn = (Req: IM, Res: SR): AP => {
const exec = async (req: IM, res: SR): AP => {
let exec = async (req: IM, res: SR): AP => {
const middleware: any[] = route.middleware || [];

if (fn.log !== false) {
Expand All @@ -42,6 +40,10 @@ export default (route: Route): Handler => {
return route.handler(req, res);
};

if (route.plugins) {
exec = route.plugins.reverse().reduce((acc, val): any => val(acc), exec);
}

const isAWS: boolean = !!(
(process.env.LAMBDA_TASK_ROOT && process.env.AWS_EXECUTION_ENV) || false
);
Expand Down

0 comments on commit 0864de1

Please sign in to comment.