From 43f997013de4859c1810f6b03677c9aeaa9f87a5 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Tue, 22 Jan 2019 20:26:18 +0200 Subject: [PATCH] typescript(translucid): typescipited Signed-off-by: prescientmoon --- typescript/translucid/Translucid.js | 15 +++++-- typescript/translucid/Translucid.ts | 62 +++++++++++++++++++++++++++++ typescript/translucid/tsconfig.json | 9 +++++ 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 typescript/translucid/Translucid.ts create mode 100644 typescript/translucid/tsconfig.json diff --git a/typescript/translucid/Translucid.js b/typescript/translucid/Translucid.js index 188ca5e..1e6eb78 100644 --- a/typescript/translucid/Translucid.js +++ b/typescript/translucid/Translucid.js @@ -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; diff --git a/typescript/translucid/Translucid.ts b/typescript/translucid/Translucid.ts new file mode 100644 index 0000000..2bc3ec6 --- /dev/null +++ b/typescript/translucid/Translucid.ts @@ -0,0 +1,62 @@ +const express = require('express'); +import {read} from "./read"; + +interface Middleware{ + name:string; + keys:Array; + run:Function; +} + +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 { + middleware:Array = []; + + 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{ + 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 = []):void{ + this.app.get(path, async (req, res)=> { + const readResults = await read(filepath); + const toRun:Array = []; + + for (let i of this.middleware){ + if (containsAny(classes, i.keys)) { + toRun.push(i.run); + } + } + + const decorated:Array = []; + 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); + }); + } +} diff --git a/typescript/translucid/tsconfig.json b/typescript/translucid/tsconfig.json new file mode 100644 index 0000000..4a38e41 --- /dev/null +++ b/typescript/translucid/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions":{ + "target":"esnext", + "watch":true, + "lib":["es2017"], + "module": "commonjs" + }, + "exclude": ["node_modules", "**/__tests__/*"] +}