pricking-koa
Advanced tools
Comparing version 0.0.2-alpha.3 to 0.0.2-alpha.4
@@ -9,3 +9,3 @@ "use strict"; | ||
constructor(options) { | ||
options.baseUrl = options.baseUrl || process.cwd(); | ||
options.rootPath = options.rootPath || process.cwd(); | ||
options.env = options.env || 'development'; | ||
@@ -12,0 +12,0 @@ this.options = options; |
export interface IOptions { | ||
baseUrl?: string; | ||
rootPath?: string; | ||
port?: number; | ||
env?: string; | ||
debug?: boolean; | ||
} |
@@ -5,16 +5,16 @@ "use strict"; | ||
const tslib_1 = require("tslib"); | ||
const path_1 = tslib_1.__importDefault(require("path")); | ||
const middlewares_1 = tslib_1.__importDefault(require("../middlewares")); | ||
const router_1 = require("./router"); | ||
const middleware_1 = require("./middleware"); | ||
const extends_1 = require("./extends"); | ||
const dependencyLoader = (app, options) => { | ||
const { rootPath } = options; | ||
app.use(middlewares_1.default); | ||
const mwreDir = path_1.default.resolve(options.baseUrl, 'middlewares'); | ||
const ctorDir = path_1.default.resolve(options.baseUrl, 'controllers'); | ||
const extraMiddleware = middleware_1.loadExtraMiddlewares(mwreDir); | ||
const extraMiddleware = middleware_1.loadExtraMiddlewares(rootPath); | ||
if (extraMiddleware) | ||
app.use(extraMiddleware); | ||
const routeMiddleware = router_1.loadRoutes(ctorDir); | ||
const routeMiddleware = router_1.loadRoutes(rootPath, options); | ||
app.use(routeMiddleware); | ||
extends_1.loadContextExtends(app, rootPath, options); | ||
}; | ||
exports.dependencyLoader = dependencyLoader; |
import compose from 'koa-compose'; | ||
export declare const loadExtraMiddlewares: (controllerDir: string) => compose.ComposedMiddleware<unknown>; | ||
export declare const loadExtraMiddlewares: (rootUrl: string) => compose.ComposedMiddleware<unknown>; |
@@ -6,24 +6,11 @@ "use strict"; | ||
const path_1 = tslib_1.__importDefault(require("path")); | ||
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); | ||
const koa_compose_1 = tslib_1.__importDefault(require("koa-compose")); | ||
const loadExtraMiddlewares = (controllerDir) => { | ||
if (!fs_extra_1.default.existsSync(controllerDir)) | ||
return null; | ||
const paths = [controllerDir]; | ||
const files = []; | ||
while (paths.length) { | ||
const pathx = paths.shift(); | ||
const contents = fs_extra_1.default.readdirSync(pathx); | ||
while (contents.length) { | ||
const content = path_1.default.resolve(pathx, contents.shift()); | ||
const stat = fs_extra_1.default.statSync(content); | ||
if (stat.isDirectory()) | ||
paths.push(content); | ||
else if (stat.isFile()) | ||
files.push(content); | ||
} | ||
} | ||
const file_loader_1 = require("../utils/file-loader"); | ||
const loadExtraMiddlewares = (rootUrl) => { | ||
const controllerDir = path_1.default.resolve(rootUrl, 'middlewares'); | ||
const files = file_loader_1.getDirFiles(controllerDir); | ||
const middlewares = []; | ||
while (files.length) { | ||
const filePath = files.shift(); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const mwre = require(filePath); | ||
@@ -30,0 +17,0 @@ if (typeof mwre === 'function') { |
@@ -5,2 +5,3 @@ /// <reference types="koa__router" /> | ||
import compose from 'koa-compose'; | ||
export declare const loadRoutes: (controllerDir: string) => compose.Middleware<import("koa").ParameterizedContext<any, Router.RouterParamContext<any, {}>>>; | ||
import { IOptions } from '../definitions/application'; | ||
export declare const loadRoutes: (rootPath: string, options: IOptions) => compose.Middleware<import("koa").ParameterizedContext<any, Router.RouterParamContext<any, {}>>>; |
@@ -6,8 +6,8 @@ "use strict"; | ||
const router_1 = tslib_1.__importDefault(require("@koa/router")); | ||
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); | ||
const path_1 = tslib_1.__importDefault(require("path")); | ||
const koa_compose_1 = tslib_1.__importDefault(require("koa-compose")); | ||
const decorator_1 = require("../lib/decorator"); | ||
const file_loader_1 = require("../utils/file-loader"); | ||
const methods = ['get', 'put', 'post', 'patch', 'delete', 'del']; | ||
const createRouter = (Controller, router) => { | ||
const createRouter = (Controller, router, debug = false) => { | ||
const ptype = Controller === null || Controller === void 0 ? void 0 : Controller.prototype; | ||
@@ -28,2 +28,4 @@ const basePath = ptype === null || ptype === void 0 ? void 0 : ptype[decorator_1.MainRouteSymbol]; | ||
const mergedPath = (basePath + subPath).replace(/\/{2,}/g, '/'); | ||
if (debug) | ||
console.log(`[${method.toUpperCase()}]`, mergedPath, desc); | ||
router[method](desc, mergedPath, async (ctx) => { | ||
@@ -44,21 +46,6 @@ /** 每次请求都实例化的话可以让每次请求的都保持独立 */ | ||
}; | ||
const loadRoutes = (controllerDir) => { | ||
if (!fs_extra_1.default.existsSync(controllerDir)) { | ||
throw new Error('controllers directory not exist, register failed'); | ||
} | ||
const paths = [controllerDir]; | ||
const files = []; | ||
const loadRoutes = (rootPath, options) => { | ||
const controllerDir = path_1.default.resolve(rootPath, 'controllers'); | ||
const files = file_loader_1.getDirFiles(controllerDir); | ||
const koaRouter = new router_1.default(); | ||
while (paths.length) { | ||
const pathx = paths.shift(); | ||
const contents = fs_extra_1.default.readdirSync(pathx); | ||
while (contents.length) { | ||
const content = path_1.default.resolve(pathx, contents.shift()); | ||
const stat = fs_extra_1.default.statSync(content); | ||
if (stat.isDirectory()) | ||
paths.push(content); | ||
else if (stat.isFile()) | ||
files.push(content); | ||
} | ||
} | ||
while (files.length) { | ||
@@ -68,3 +55,3 @@ const filePath = files.shift(); | ||
const ctor = require(filePath); | ||
createRouter(ctor, koaRouter); | ||
createRouter(ctor, koaRouter, options.debug); | ||
} | ||
@@ -71,0 +58,0 @@ if (koaRouter.stack.length < 1) { |
@@ -0,3 +1,4 @@ | ||
/// <reference types="koa" /> | ||
import compose from 'koa-compose'; | ||
declare const middlewares: compose.Middleware<any>; | ||
declare const middlewares: compose.Middleware<import("koa").ParameterizedContext<unknown, unknown>>; | ||
export = middlewares; |
@@ -6,8 +6,3 @@ "use strict"; | ||
const koa_bodyparser_1 = tslib_1.__importDefault(require("koa-bodyparser")); | ||
const useful_req_1 = tslib_1.__importDefault(require("./useful-req")); | ||
const middlewares = koa_compose_1.default([ | ||
koa_bodyparser_1.default(), | ||
koa_logger_1.default(), | ||
useful_req_1.default(), | ||
]); | ||
const middlewares = koa_compose_1.default([koa_bodyparser_1.default(), koa_logger_1.default()]); | ||
module.exports = middlewares; |
{ | ||
"name": "pricking-koa", | ||
"version": "0.0.2-alpha.3", | ||
"version": "0.0.2-alpha.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "author": "", |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
21289
32
452