-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpressmid.js
59 lines (56 loc) · 1.71 KB
/
expressmid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const {AltSource} = require('./classes')
/**
* Express Middleware for AltSource
* @param {{}|AltSource} Conf
* @param {{path: String?}} Ext
* @returns {any}
*/
module.exports = function (Conf, Ext={}) {
//logthat(Conf)
let Alt;
if (Conf.constructor.name === "AltSource") {
Alt = Conf
} else {
Alt = new AltSource(Conf)
}
//logthat("Alt @ mid:", Alt)
/**
* Middleware function
* @param {Request} req
* @param {Response} res
* @param {Next} next
* @returns {void}
*
* If the path is `Ext.path?` or `/altstore.json` then the middleware will respond with the json of the AltSource
*
* Else it will call `next()` and it will add `Altsource` to the `req` property
*/
return (req, res, next) => {
//logthat("Passed AltSource",Conf)
//logthat("mid:", req, res, next)
if (Ext.hasOwnProperty('path')) {
if (String(req.baseUrl + req.path) === Conf.path) {
logthat('path: custom', String(req.baseUrl + req.path));
res.json(Alt.toJSON())
} else {
logthat('outside of the scope with path')
res.AltSource = Alt;
next();
}
} else if (!Ext.hasOwnProperty('path') && String(req.baseUrl + req.path) === '/altstore.json') {
logthat('path: default', '/altstore.json');
res.json(Alt.toJSON())
} else {
logthat('outside of the scope')
req.AltSource = Alt;
next();
}
}
}
function logthat(where, what) {
if (process.env.DEBUGING === 'y') {
return console.log(where, what)
} else {
return `${where} ${what}`
}
}