Skip to content

Commit f3438fc

Browse files
committed
refactored global middleware functionality and invoker functionality
1 parent 7627a86 commit f3438fc

File tree

1 file changed

+66
-39
lines changed

1 file changed

+66
-39
lines changed

index.js

+66-39
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,87 @@
1-
class Connext {
2-
constructor() {
3-
this.routes = [];
4-
this.set = app.set.bind(this);
5-
this.get = this.set.bind(this, 'GET');
6-
this.post = this.set.bind(this, 'POST');
7-
this.put = this.set.bind(this, 'PUT');
8-
this.patch = this.set.bind(this, 'PATCH');
9-
this.delete = this.set.bind(this, 'DELETE');
10-
this.head = this.set.bind(this, 'HEAD');
11-
this.connect = this.set.bind(this, 'CONNECT');
12-
this.options = this.set.bind(this, 'OPTIONS');
13-
this.trace = this.set.bind(this, 'TRACE');
14-
this.find = this.set.bind(this, 'FIND');
15-
this.compareRoutes = app.compareRoutes.bind(this);
16-
this.routeSplitter = app.routeSplitter.bind(this);
17-
this.use = app.use.bind(this);
1+
const Dirext = require('dirext-js');
2+
const fs = require('fs');
3+
4+
const findGlobal = () => {
5+
const globalMiddleware = [];
6+
try {
7+
if (fs.existsSync('./controllers/global.js')) {
8+
globalMiddleware.push(...require('./controllers/global.js'));
9+
}
10+
} catch (err) {
11+
console.log(err);
12+
}
13+
return globalMiddleware;
14+
};
15+
16+
const app = new Dirext();
17+
18+
module.exports = () => {
19+
function connext(req, res) {
20+
connext.invoker(req, res);
1821
}
22+
connext.routes = [];
23+
connext.globalMiddleware = findGlobal();
24+
connext.get = set.bind(connext, 'GET');
25+
connext.post = set.bind(connext, 'POST');
26+
connext.put = set.bind(connext, 'PUT');
27+
connext.patch = set.bind(connext, 'PATCH');
28+
connext.delete = set.bind(connext, 'DELETE');
29+
connext.head = set.bind(connext, 'HEAD');
30+
connext.connect = set.bind(connext, 'CONNECT');
31+
connext.options = set.bind(connext, 'OPTIONS');
32+
connext.trace = set.bind(connext, 'TRACE');
33+
connext.use = app.use.bind(connext);
34+
connext.routeSplitter = app.routeSplitter.bind(connext);
35+
connext.compareRoutes = app.compareRoutes.bind(connext);
1936

20-
21-
invoker(req, res) {
22-
console.log('this.routes is:', this.routes);
37+
function set(...args) {
38+
app.set.apply(connext, args);
39+
return connext;
40+
}
41+
42+
connext.invoker = function (req, res) {
43+
console.log('connext.routes is:', JSON.stringify(connext.routes));
2344
console.log('hitting invoker');
2445
// creating a variable to store user's exported array of global middleware functions
25-
const global = require('../../pages/api/global');
2646
// defining an error handler that can be overwritten if the user creates their own global error handling function
27-
let errorHandler;
47+
let errorHandler = (err, req, res, next) => {
48+
res.status(500).send(`err: ${err}`);
49+
};
2850
// if the global middleware array exists and the last function in the array accepts 4 arguments (catch all error route), set errorHandler to that last function
29-
if (global[0] && global[global.length - 1].length === 4) errorHandler = global.pop();
51+
if (connext.globalMiddleware[connext.globalMiddleware.length - 1].length === 4) {
52+
errorHandler = connext.globalMiddleware.pop();
53+
}
3054
// if the global middleware array is not empty set const middleware to that array, otherwise set const middleware to an empty array
31-
const middleware = global[0] ? global : [];
55+
let middleware = [];
3256
// deconstruct method and url out of req
3357
const { method, url } = req;
34-
console.log('req.url is: ', req.url);
35-
console.log('req.method is: ', req.method);
3658
// invoke this.find with method and url passed in as arguments at the key 'middleware' and set that to const targetMiddleware
37-
const targetMiddleware = this.find(method, url).middleware;
59+
const targetMiddleware = app.find.apply(connext, [method, url]).middleware;
3860
console.log('targetMiddleware is: ', targetMiddleware);
39-
console.log(this.find);
4061
// push the spread array targetMiddleware into middleware array
41-
middleware.push(...targetMiddleware);
62+
middleware.push(...connext.globalMiddleware, ...targetMiddleware);
4263
// counter to keep track of position of current middleware function
4364
console.log('middleware array is: ', middleware);
4465
let i = 0;
4566
// loop through middleware array and invoke each function in order. if an error is passed into next function return that error.
4667
async function next(err) {
47-
if (err) return res.json(err);
68+
if (err) return errorHandler(err, req, res);
4869
// eslint-disable-next-line no-useless-catch
49-
try {
50-
const currentMiddleware = middleware[i];
51-
i += 1;
52-
await currentMiddleware(req, res, next);
53-
return next();
54-
} catch (error) {
55-
return error;
70+
while (i < middleware.length) {
71+
try {
72+
const currentMiddleware = middleware[i];
73+
if (i === middleware.length - 1) {
74+
middleware = [];
75+
}
76+
i += 1;
77+
return currentMiddleware(req, res, next);
78+
} catch (error) {
79+
return error;
80+
}
5681
}
82+
console.log('middleware after splice is:', middleware);
5783
}
5884
return next();
59-
}
60-
}
85+
};
86+
return connext;
87+
};

0 commit comments

Comments
 (0)