diff --git a/typescript/translucid/QuickServer.ts b/typescript/translucid/QuickServer.ts new file mode 100644 index 0000000..33b7652 --- /dev/null +++ b/typescript/translucid/QuickServer.ts @@ -0,0 +1,26 @@ +const express = require('express'); +const http = require('http'); + +import {Translucid} from "./Translucid"; + +interface Server{ + app:any, + server:any, + connect:Promise +} + +function QuickServer(port:number):any{ + const app = express(); + const server = http.Server(app); + const translucid = new Translucid(app); + return { + express,app,http,server,translucid, + connect:new Promise((resolve,reject) => { + server.listen(port,() => { + resolve("Connected"); + }); + }) + } +} + +export {QuickServer}; diff --git a/typescript/translucid/Translucid.ts b/typescript/translucid/Translucid.ts new file mode 100644 index 0000000..e6578a4 --- /dev/null +++ b/typescript/translucid/Translucid.ts @@ -0,0 +1,58 @@ +import {read} from "./read"; + +interface Midleware{ + name:string; + run:Function; + keys:Array; +} + +function containsAny(array:Array,keys:Array):boolean{ + for (let i = 0;i < keys.length;i++){ + if (array.indexOf(keys[i]) != -1) return true; + } + return false; +} + +class Translucid{ + midleware:Array = []; + constructor(public app){ + } + use(obj:Midleware):void{ + this.midleware.push(obj); + } + bind(path:string="/",filepath:string="", + text:boolean=false,classes:Array=[]):void{ + this.app.get(path,async (req,res) => { + if (!text){ + res.sendFile(`${__dirname}/${filepath}`); + } + else{ + const readResults = await read(filepath); + + const toRun = []; + for (let i of this.midleware){ + if (containsAny(classes,i.keys)){ + toRun.push(i.run); + } + } + const decorated = []; + + const expressArgs = [req,res]; + + for (let i = 0;i < toRun.length;i++){ + decorated.push((prev) => { + toRun[i](prev,...expressArgs,decorated[i + 1]); + }); + } + + decorated.push((prev) => { + res.send(prev + "
Sent from the last iteration"); + }); + + decorated[0](readResults); + } + }); + } +} + +export {Translucid}; diff --git a/typescript/translucid/app.ts b/typescript/translucid/app.ts new file mode 100644 index 0000000..06a7e9d --- /dev/null +++ b/typescript/translucid/app.ts @@ -0,0 +1,5 @@ +import {read} from "./read"; +import {Translucid} from "./Translucid.js"; +import {QuickServer} from "./QuickServer.js" + +export {read,Translucid,QuickServer}; diff --git a/typescript/translucid/test.ts b/typescript/translucid/test.ts new file mode 100644 index 0000000..8d5b0a0 --- /dev/null +++ b/typescript/translucid/test.ts @@ -0,0 +1,12 @@ +import {QuickServer,Translucid} from "./app"; + +const {app,translucid} = QuickServer(8000); +translucid.bind("/","index.html"); +translucid.bind("/test","read.js",true,["class"]); +translucid.use({ + name:"wow, a midleware", + run:(prev,req,res,next) => { + next("sent by a midleware"); + }, + keys:["class"] +});