@abyss.ts/express-runner
Advanced tools
Comparing version 0.0.1 to 0.0.2
import { AbyssApplication } from '@abyss.ts/core'; | ||
export { Body, Controller, Get, Param, Query, Request } from '@abyss.ts/core'; | ||
export { Body, Controller, Delete, Get, Inject, Injectable, Param, Patch, Post, Put, Query, Request } from '@abyss.ts/core'; | ||
export { Request as TRequest } from 'express'; | ||
@@ -4,0 +4,0 @@ |
200
lib/index.js
@@ -0,16 +1,19 @@ | ||
var __defProp = Object.defineProperty; | ||
var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); | ||
// src/index.ts | ||
import { Controller, Get, Query, Body, Request, Param } from "@abyss.ts/core"; | ||
import { Get, Put, Body, Post, Patch, Query, Param, Delete, Inject, Request, Controller, Injectable } from "@abyss.ts/core"; | ||
// src/Application.ts | ||
import { nanoid } from "nanoid"; | ||
import bodyParser from "body-parser"; | ||
import detectPort from "detect-port"; | ||
import express, { Router } from "express"; | ||
import express from "express"; | ||
import { AbyssApplication } from "@abyss.ts/core"; | ||
import { createServer as createHttpServer } from "http"; | ||
import { | ||
combine, | ||
AbyssApplication, | ||
getControllerMetadata as getControllerMetadata2, | ||
getControllerActionMetadata | ||
} from "@abyss.ts/core"; | ||
// src/utils/routeUtils.ts | ||
import { Router } from "express"; | ||
import { combine, getFromIoCContainer, getControllerMetadata, getInjectParamMetadata, getControllerActionMetadata } from "@abyss.ts/core"; | ||
// src/utils/actionUtils.ts | ||
@@ -35,7 +38,4 @@ import { getActionParamMetadata } from "@abyss.ts/core"; | ||
} | ||
function mapParameters({ | ||
request, | ||
controller, | ||
propertyKey | ||
}) { | ||
__name(extractData, "extractData"); | ||
function mapParameters({ request, controller, propertyKey }) { | ||
const params = getActionParamMetadata({ | ||
@@ -49,11 +49,135 @@ controller, | ||
} | ||
__name(mapParameters, "mapParameters"); | ||
// src/utils/routeUtils.ts | ||
function mapRoutes(controllers) { | ||
console.log("Initializing routes..."); | ||
const routes = []; | ||
const router = Router(); | ||
for (const controller of controllers) { | ||
const { route } = getControllerMetadata(controller); | ||
const actions = getControllerActionMetadata(controller); | ||
const injects = getInjectParamMetadata({ | ||
target: controller | ||
}); | ||
const injections = injects.map(({ extractor }) => { | ||
return getFromIoCContainer(extractor); | ||
}); | ||
const controllerInstance = new controller(...injections); | ||
for (const action of actions) { | ||
const { exec, httpMethod, route: actionRoute, propertyKey } = action; | ||
const httpRoute = `/${combine({ | ||
joinWith: "/" | ||
}, route, actionRoute)}`; | ||
routes.push(combine({ | ||
joinWith: " " | ||
}, httpMethod.toUpperCase(), httpRoute)); | ||
router[httpMethod](httpRoute, (req, res) => { | ||
const params = mapParameters({ | ||
controller, | ||
propertyKey, | ||
request: req | ||
}); | ||
res.send(exec.bind(controllerInstance)(...params)); | ||
}); | ||
} | ||
} | ||
console.log("Finish initializing routes!\n"); | ||
return [ | ||
routes, | ||
router | ||
]; | ||
} | ||
__name(mapRoutes, "mapRoutes"); | ||
// src/utils/injectionUtils.ts | ||
import { pushToIoCContainer, getFromIoCContainer as getFromIoCContainer2, getInjectionMetadata, getInjectParamMetadata as getInjectParamMetadata2 } from "@abyss.ts/core"; | ||
function mapInjections() { | ||
console.log("Scanning injections..."); | ||
const injections = getInjectionMetadata(); | ||
const groupedInjections = topologicalGroup(injections); | ||
for (const pack of groupedInjections) { | ||
for (const injection of pack) { | ||
const { target, scope } = injection; | ||
if (scope === "singleton") { | ||
const metadata = getInjectParamMetadata2({ | ||
target | ||
}); | ||
const params = metadata.map(({ extractor }) => { | ||
return getFromIoCContainer2(extractor); | ||
}); | ||
const instance = new target(...params); | ||
pushToIoCContainer({ | ||
target, | ||
instance | ||
}); | ||
} | ||
} | ||
} | ||
console.log("Finish scanning injections!\n"); | ||
} | ||
__name(mapInjections, "mapInjections"); | ||
function topologicalGroup(injections) { | ||
const visited = {}; | ||
const result = []; | ||
while (injections.length) { | ||
const removedIndexes = []; | ||
for (let i = 0, n = injections.length; i < n; i++) { | ||
const injection = injections[i]; | ||
if (!injection) { | ||
removedIndexes.push(i); | ||
continue; | ||
} | ||
const { target } = injection; | ||
const params = getInjectParamMetadata2({ | ||
target | ||
}); | ||
if (!params.length) { | ||
result[0] ||= []; | ||
result[0].push(injection); | ||
removedIndexes.push(i); | ||
visited[target] = { | ||
level: 0, | ||
visited: true | ||
}; | ||
continue; | ||
} | ||
let level = 0; | ||
let visitedAllDependencies = true; | ||
params.forEach((injectable) => { | ||
const { extractor } = injectable; | ||
const visitedData = visited[extractor]; | ||
if (visitedData) { | ||
level = Math.max(level, visitedData.level); | ||
} else { | ||
visitedAllDependencies = false; | ||
} | ||
}); | ||
if (visitedAllDependencies) { | ||
result[level + 1] ||= []; | ||
result[level + 1].push(injection); | ||
removedIndexes.push(i); | ||
visited[target] = { | ||
visited: true, | ||
level: level + 1 | ||
}; | ||
} | ||
} | ||
removedIndexes.reverse().forEach((index) => { | ||
injections.splice(index, 1); | ||
}); | ||
} | ||
return result; | ||
} | ||
__name(topologicalGroup, "topologicalGroup"); | ||
// src/utils/controllerUtils.ts | ||
import { getControllerMetadata, loadControllers } from "@abyss.ts/core"; | ||
import { getControllerMetadata as getControllerMetadata2, loadControllers } from "@abyss.ts/core"; | ||
async function mapController() { | ||
console.log("Scanning controllers..."); | ||
const importedControllers = await loadControllers(); | ||
return importedControllers.reduce((result, module) => { | ||
const controllers = importedControllers.reduce((result, module) => { | ||
const classes = Object.values(module); | ||
for (const klass of classes) { | ||
const { type } = getControllerMetadata(klass); | ||
const { type } = getControllerMetadata2(klass); | ||
if (type !== "controller") { | ||
@@ -66,6 +190,12 @@ continue; | ||
}, []); | ||
console.log("Finish scanning controllers!\n"); | ||
return controllers; | ||
} | ||
__name(mapController, "mapController"); | ||
// src/Application.ts | ||
var ExpressApplication = class extends AbyssApplication { | ||
static { | ||
__name(this, "ExpressApplication"); | ||
} | ||
#express; | ||
@@ -86,26 +216,17 @@ static #app; | ||
this.#express.use(bodyParser.json()); | ||
this.#express.use(bodyParser.urlencoded({ extended: true })); | ||
this.#express.use(bodyParser.urlencoded({ | ||
extended: true | ||
})); | ||
return this._instance; | ||
} | ||
async run() { | ||
this.#express.use((req, _res, next) => { | ||
Object.assign(req, { | ||
executionId: nanoid() | ||
}); | ||
next(); | ||
}); | ||
const controllers = await mapController(); | ||
const routes = []; | ||
const router = Router(); | ||
for (const controller of controllers) { | ||
const { route } = getControllerMetadata2(controller); | ||
const actions = getControllerActionMetadata(controller); | ||
for (const action of actions) { | ||
const { exec, httpMethod, route: actionRoute, propertyKey } = action; | ||
const httpRoute = `/${combine({ joinWith: "/" }, route, actionRoute)}`; | ||
routes.push(httpRoute); | ||
router[httpMethod](httpRoute, (req, res) => { | ||
const params = mapParameters({ | ||
controller, | ||
propertyKey, | ||
request: req | ||
}); | ||
res.send(exec(...params)); | ||
}); | ||
} | ||
} | ||
mapInjections(); | ||
const [routes, router] = mapRoutes(controllers); | ||
this.#express.use(router); | ||
@@ -123,3 +244,2 @@ const { port, isStrict } = this._port; | ||
List routes: | ||
${routes.join("\n")}`); | ||
@@ -132,7 +252,13 @@ } | ||
Controller, | ||
Delete, | ||
ExpressApplication, | ||
Get, | ||
Inject, | ||
Injectable, | ||
Param, | ||
Patch, | ||
Post, | ||
Put, | ||
Query, | ||
Request | ||
}; |
{ | ||
"name": "@abyss.ts/express-runner", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"author": "Alpha", | ||
@@ -26,3 +26,4 @@ "repository": { | ||
"express": "^4.19.2", | ||
"@abyss.ts/core": "^0.0.1" | ||
"nanoid": "^5.0.7", | ||
"@abyss.ts/core": "^0.0.2" | ||
}, | ||
@@ -29,0 +30,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8687
260
5
+ Addednanoid@^5.0.7
+ Added@abyss.ts/core@0.0.2(transitive)
+ Addednanoid@5.0.8(transitive)
- Removed@abyss.ts/core@0.0.1(transitive)
Updated@abyss.ts/core@^0.0.2