@koa-boot/server
Advanced tools
Comparing version 0.1.2 to 0.1.3
/// <reference types="koa__router" /> | ||
import Koa from "koa"; | ||
import Router from "@koa/router"; | ||
import Koa, { Middleware } from "koa"; | ||
export interface ApplicationConfig { | ||
@@ -18,4 +18,6 @@ port: number; | ||
static getKoa(): Koa; | ||
static use(middleware: Middleware): void; | ||
static registerRouter(router: Router): void; | ||
private static loadAllComponents; | ||
private static getProjectEntryDir; | ||
private static registerMiddlewares; | ||
@@ -22,0 +24,0 @@ private static loadComponent; |
@@ -7,6 +7,6 @@ "use strict"; | ||
exports.Application = void 0; | ||
const router_1 = __importDefault(require("@koa/router")); | ||
const fs_1 = require("fs"); | ||
const koa_1 = __importDefault(require("koa")); | ||
const path_1 = __importDefault(require("path")); | ||
const fs_1 = require("fs"); | ||
const router_1 = __importDefault(require("@koa/router")); | ||
const utils_1 = require("./utils"); | ||
@@ -35,2 +35,5 @@ class Application { | ||
} | ||
static use(middleware) { | ||
Application.koa.use(middleware); | ||
} | ||
static registerRouter(router) { | ||
@@ -40,7 +43,3 @@ Application.mainRouter.use(router.routes(), router.allowedMethods()); | ||
static loadAllComponents() { | ||
var _a; | ||
const entryDir = (_a = require.main) === null || _a === void 0 ? void 0 : _a.path; | ||
if (!entryDir) { | ||
throw new Error("cannot get project entry directory"); | ||
} | ||
const entryDir = Application.getProjectEntryDir(); | ||
Application.components.forEach((relativePath) => { | ||
@@ -50,2 +49,9 @@ Application.loadComponent(path_1.default.resolve(entryDir, relativePath)); | ||
} | ||
static getProjectEntryDir() { | ||
var _a; | ||
if (!require.main) { | ||
throw new Error("cannot get project entry directory"); | ||
} | ||
return (_a = require.main.path) !== null && _a !== void 0 ? _a : path_1.default.dirname(require.main.filename); | ||
} | ||
static registerMiddlewares() { | ||
@@ -72,3 +78,4 @@ const { koa, mainRouter } = Application; | ||
else if (stat.isFile() && Application.validateSourceFile(sourcePath)) { | ||
if (!(sourcePath in require.cache)) { | ||
if (!require.cache[sourcePath] && | ||
require.extensions[path_1.default.extname(sourcePath)]) { | ||
utils_1.logger.info("Load source:", sourcePath); | ||
@@ -75,0 +82,0 @@ require(sourcePath); |
@@ -27,3 +27,3 @@ "use strict"; | ||
const middlewares = (_c = (_b = allRouteMiddlewares.find((routeMiddlewares) => routeMiddlewares.classMethodName === classMethodName)) === null || _b === void 0 ? void 0 : _b.middlewares) !== null && _c !== void 0 ? _c : []; | ||
currentBuildingRouter[method](url, ...middlewares, handlerFunc); | ||
currentBuildingRouter[method](url, ...middlewares.reverse(), handlerFunc); | ||
} | ||
@@ -30,0 +30,0 @@ application_1.Application.registerRouter(currentBuildingRouter); |
@@ -19,3 +19,3 @@ "use strict"; | ||
else { | ||
routeMiddlewares.middlewares.unshift(middleware); | ||
routeMiddlewares.middlewares.push(middleware); | ||
} | ||
@@ -22,0 +22,0 @@ }; |
{ | ||
"name": "@koa-boot/server", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "A SpringBoot-like lightweight web framework based on Koa.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26312
380