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

Commit

Permalink
fix(middleware): make middleware explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
nahtnam committed Apr 20, 2019
1 parent d009cc3 commit 94e153d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 45 deletions.
10 changes: 5 additions & 5 deletions server/js/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const { route } = require('../../../lib/index'); // eslint-disable-line

module.exports = route({
path: '/',
async handler() {
return { hello: 'hmr' };
middleware: [(req) => {
req.message = 'test';
}],
async handler(req) {
return { hello: 'hmr', message: req.message };
},
}, {
middleware: ['test'],
middlewareRoot: './server/js/middleware',
});
6 changes: 2 additions & 4 deletions src/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ interface Light {
router: any;
}

type handler = (req: IncomingMessage, res: ServerResponse) => any;

const light = ({
routes: routesPath,
log,
log = true,
}: {
routes: string | string[];
log?: boolean | any;
log?: boolean;
}): Light => {
const router = new Router();

Expand Down
62 changes: 26 additions & 36 deletions src/route.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,41 @@
import { send } from 'micro';
import { run } from 'micro';
import { join } from 'path';
import { IncomingMessage, ServerResponse } from 'http';

interface R {
interface Route {
path?: string;
handler: (req: IncomingMessage, res: ServerResponse) => {};
}

interface Opts {
middleware?: string[];
middlewareRoot?: string;
handler: Handler;
}

export default (route: R, opts: Opts = {}): (req: IncomingMessage, res: ServerResponse) => {} => {
const fn = async (req: IncomingMessage, res: ServerResponse): Promise<any> => {
const root = join(fn.module, '../../../', opts.middlewareRoot || 'middleware');
const middlewarePaths: string[] = [];
type Handler = (req: IncomingMessage, res: ServerResponse) => {};
type IM = IncomingMessage;
type SR = ServerResponse;
type AP = Promise<any>;

export default (route: Route): Handler => {
const fn = (Req: IM, Res: SR): AP => run(Req, Res, async (req: IM, res: SR): AP => {
const importMiddleware = (path: string): any => {
let mw = require(path); // eslint-disable-line
if (mw.default) {
mw = mw.default;
}
return mw;
};

const middleware: any[] = route.middleware || [];

if (fn.log !== false) {
middlewarePaths.push(join(fn.module, 'middleware/logger'));
const logger = importMiddleware(join(fn.module, 'middleware/logger'));
middleware.unshift(logger);
}

(opts.middleware || []).forEach((mw): void => {
middlewarePaths.push(join(root, mw));
});

const middleware = middlewarePaths.map((mwPath): any => {
try {
let mw = require(mwPath); // eslint-disable-line
if (mw.default) {
mw = mw.default;
}
return mw;
} catch (err) {
console.log(err); // eslint-disable-line
}
return (): void => {};
});

for (const mw of middleware) { // eslint-disable-line
await mw(req, res); // eslint-disable-line
for (const mw of middleware) { // eslint-disable-line
await mw(req, res); // eslint-disable-line
}
const ret = await route.handler(req, res);
if (ret) {
send(res, 200, ret);
}
};

return route.handler(req, res);
});

fn.path = route.path;
fn.log = true;
Expand Down

0 comments on commit 94e153d

Please sign in to comment.