2019-01-19 21:41:54 +01:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2019-01-20 12:06:55 +01:00
|
|
|
const express = require('express');
|
2019-01-19 21:41:54 +01:00
|
|
|
const read_1 = require("./read");
|
|
|
|
function containsAny(array, keys) {
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
if (array.indexOf(keys[i]) != -1)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
class Translucid {
|
|
|
|
constructor(app) {
|
|
|
|
this.app = app;
|
2019-01-22 19:26:18 +01:00
|
|
|
this.middleware = [];
|
2019-01-19 21:41:54 +01:00
|
|
|
}
|
|
|
|
use(obj) {
|
2019-01-22 19:26:18 +01:00
|
|
|
this.middleware.push(obj);
|
2019-01-19 21:41:54 +01:00
|
|
|
}
|
2019-01-20 12:02:48 +01:00
|
|
|
public(path = "") {
|
2019-01-20 12:13:08 +01:00
|
|
|
this.app.use(`/${path}`, express.static(`${__dirname}/../../${path}`));
|
2019-01-20 12:02:48 +01:00
|
|
|
}
|
2019-01-22 19:26:18 +01:00
|
|
|
async bindJSON(path) {
|
|
|
|
const json = await read_1.read(path);
|
|
|
|
const object = JSON.parse(json);
|
|
|
|
for (let i in object) {
|
|
|
|
const classes = object.classes || [];
|
|
|
|
this.bind(object[i].path, object[i].file, classes);
|
|
|
|
}
|
|
|
|
}
|
2019-01-20 11:57:11 +01:00
|
|
|
bind(path = "/", filepath = "", classes = []) {
|
2019-01-19 21:41:54 +01:00
|
|
|
this.app.get(path, async (req, res) => {
|
2019-01-20 11:57:11 +01:00
|
|
|
const readResults = await read_1.read(filepath);
|
|
|
|
const toRun = [];
|
2019-01-22 19:26:18 +01:00
|
|
|
for (let i of this.middleware) {
|
2019-01-20 11:57:11 +01:00
|
|
|
if (containsAny(classes, i.keys)) {
|
|
|
|
toRun.push(i.run);
|
2019-01-19 21:41:54 +01:00
|
|
|
}
|
2019-01-20 11:57:11 +01:00
|
|
|
}
|
|
|
|
const decorated = [];
|
|
|
|
const expressArgs = [req, res];
|
|
|
|
for (let i = 0; i < toRun.length; i++) {
|
2019-01-19 21:41:54 +01:00
|
|
|
decorated.push((prev) => {
|
2019-01-20 11:57:11 +01:00
|
|
|
toRun[i](prev, ...expressArgs, decorated[i + 1]);
|
2019-01-19 21:41:54 +01:00
|
|
|
});
|
|
|
|
}
|
2019-01-20 11:57:11 +01:00
|
|
|
decorated.push((prev) => {
|
|
|
|
res.send(prev);
|
|
|
|
});
|
|
|
|
decorated[0](readResults);
|
2019-01-19 21:41:54 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|