typescript(translucid): typescipited
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
f95c98d2f4
commit
43f997013d
|
@ -12,19 +12,27 @@ function containsAny(array, keys) {
|
|||
class Translucid {
|
||||
constructor(app) {
|
||||
this.app = app;
|
||||
this.midleware = [];
|
||||
this.middleware = [];
|
||||
}
|
||||
use(obj) {
|
||||
this.midleware.push(obj);
|
||||
this.middleware.push(obj);
|
||||
}
|
||||
public(path = "") {
|
||||
this.app.use(`/${path}`, express.static(`${__dirname}/../../${path}`));
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
bind(path = "/", filepath = "", classes = []) {
|
||||
this.app.get(path, async (req, res) => {
|
||||
const readResults = await read_1.read(filepath);
|
||||
const toRun = [];
|
||||
for (let i of this.midleware) {
|
||||
for (let i of this.middleware) {
|
||||
if (containsAny(classes, i.keys)) {
|
||||
toRun.push(i.run);
|
||||
}
|
||||
|
@ -43,4 +51,3 @@ class Translucid {
|
|||
});
|
||||
}
|
||||
}
|
||||
exports.Translucid = Translucid;
|
||||
|
|
62
typescript/translucid/Translucid.ts
Normal file
62
typescript/translucid/Translucid.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
const express = require('express');
|
||||
import {read} from "./read";
|
||||
|
||||
interface Middleware{
|
||||
name:string;
|
||||
keys:Array<string>;
|
||||
run:Function;
|
||||
}
|
||||
|
||||
function containsAny(array:Array<string>, keys:Array<string>):boolean{
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
if (array.indexOf(keys[i]) != -1)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
class Translucid {
|
||||
middleware:Array<Middleware> = [];
|
||||
|
||||
constructor(public app) {}
|
||||
use(obj:Middleware):void{
|
||||
this.middleware.push(obj);
|
||||
}
|
||||
public(path:string = ""):void{
|
||||
this.app.use(`/${path}`, express.static(`${__dirname}/../../${path}`));
|
||||
}
|
||||
async bindJSON(path:string):Promise<void>{
|
||||
const json = await 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);
|
||||
}
|
||||
}
|
||||
bind(path:string = "/", filepath:string = "", classes:Array<string> = []):void{
|
||||
this.app.get(path, async (req, res)=> {
|
||||
const readResults = await read(filepath);
|
||||
const toRun:Array<Function> = [];
|
||||
|
||||
for (let i of this.middleware){
|
||||
if (containsAny(classes, i.keys)) {
|
||||
toRun.push(i.run);
|
||||
}
|
||||
}
|
||||
|
||||
const decorated:Array<Function> = [];
|
||||
const expressArgs = [req, res];
|
||||
|
||||
for (let i = 0; i < toRun.length; i++) {
|
||||
decorated.push((prev:any):void => {
|
||||
toRun[i](prev, ...expressArgs, decorated[i + 1]);
|
||||
});
|
||||
}
|
||||
decorated.push((prev:any):void => {
|
||||
res.send(prev);
|
||||
});
|
||||
|
||||
decorated[0](readResults);
|
||||
});
|
||||
}
|
||||
}
|
9
typescript/translucid/tsconfig.json
Normal file
9
typescript/translucid/tsconfig.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"compilerOptions":{
|
||||
"target":"esnext",
|
||||
"watch":true,
|
||||
"lib":["es2017"],
|
||||
"module": "commonjs"
|
||||
},
|
||||
"exclude": ["node_modules", "**/__tests__/*"]
|
||||
}
|
Loading…
Reference in a new issue