@koa-boot/server
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -1,28 +0,23 @@ | ||
import { ComponentClass } from "./ioc"; | ||
import { MethodDecorator, Prototype } from "./types"; | ||
interface ControllerClass extends ComponentClass { | ||
} | ||
declare type ControllerDecorator = (Class: ControllerClass) => void; | ||
import { ControllerClass, ControllerDecorator, ControllerPrototype, MethodDecorator } from "./types"; | ||
export declare function Controller(Class: ControllerClass): void; | ||
export declare function Controller(prefix: string): ControllerDecorator; | ||
export declare const Get: { | ||
(url: string): MethodDecorator; | ||
(prototype: Prototype, methodName: string): void; | ||
(url: string): MethodDecorator<ControllerClass>; | ||
(prototype: ControllerPrototype, methodName: string): void; | ||
}; | ||
export declare const Post: { | ||
(url: string): MethodDecorator; | ||
(prototype: Prototype, methodName: string): void; | ||
(url: string): MethodDecorator<ControllerClass>; | ||
(prototype: ControllerPrototype, methodName: string): void; | ||
}; | ||
export declare const Delete: { | ||
(url: string): MethodDecorator; | ||
(prototype: Prototype, methodName: string): void; | ||
(url: string): MethodDecorator<ControllerClass>; | ||
(prototype: ControllerPrototype, methodName: string): void; | ||
}; | ||
export declare const Put: { | ||
(url: string): MethodDecorator; | ||
(prototype: Prototype, methodName: string): void; | ||
(url: string): MethodDecorator<ControllerClass>; | ||
(prototype: ControllerPrototype, methodName: string): void; | ||
}; | ||
export declare const Patch: { | ||
(url: string): MethodDecorator; | ||
(prototype: Prototype, methodName: string): void; | ||
(url: string): MethodDecorator<ControllerClass>; | ||
(prototype: ControllerPrototype, methodName: string): void; | ||
}; | ||
export {}; |
@@ -7,22 +7,23 @@ "use strict"; | ||
exports.Patch = exports.Put = exports.Delete = exports.Post = exports.Get = exports.Controller = void 0; | ||
const ioc_1 = require("./ioc"); | ||
const router_1 = __importDefault(require("@koa/router")); | ||
const application_1 = require("./application"); | ||
const CLASS_ROUTE_METHODS = Symbol("CLASS_ROUTE_METHODS"); | ||
const ioc_1 = require("./ioc"); | ||
const types_1 = require("./types"); | ||
let currentBuildingRouter; | ||
function controllerDecorator(Class) { | ||
var _a, _b, _c; | ||
ioc_1.Component(Class); | ||
const instance = ioc_1.getInstance(Class); | ||
const prototype = Class.prototype; | ||
const classRouteMethods = prototype[CLASS_ROUTE_METHODS]; | ||
const classRouteMethods = prototype[types_1.CLASS_ROUTE_METHODS]; | ||
if (!classRouteMethods) { | ||
return; | ||
} | ||
if (!currentBuildingRouter) { | ||
currentBuildingRouter = new router_1.default(); | ||
} | ||
const allRouteMiddlewares = (_a = prototype[types_1.ROUTE_MIDDLEWARES]) !== null && _a !== void 0 ? _a : []; | ||
currentBuildingRouter = currentBuildingRouter !== null && currentBuildingRouter !== void 0 ? currentBuildingRouter : new router_1.default(); | ||
for (const classRouteMethod of classRouteMethods) { | ||
const { method, url, classMethodName } = classRouteMethod; | ||
const handlerFunc = prototype[classMethodName].bind(instance); | ||
currentBuildingRouter[method](url, handlerFunc); | ||
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); | ||
} | ||
@@ -40,11 +41,15 @@ application_1.Application.registerRouter(currentBuildingRouter); | ||
exports.Controller = Controller; | ||
function RequestMethod(method) { | ||
function requestMethodFactory(method) { | ||
function decorator(...args) { | ||
let url = "/"; | ||
function _decorator(prototype, methodName) { | ||
let routeMethods = prototype[CLASS_ROUTE_METHODS]; | ||
if (!routeMethods) { | ||
routeMethods = prototype[CLASS_ROUTE_METHODS] = []; | ||
let classRouteMethods = prototype[types_1.CLASS_ROUTE_METHODS]; | ||
if (!classRouteMethods) { | ||
classRouteMethods = prototype[types_1.CLASS_ROUTE_METHODS] = []; | ||
} | ||
routeMethods.push({ method, url, classMethodName: methodName }); | ||
classRouteMethods.push({ | ||
method, | ||
url, | ||
classMethodName: methodName, | ||
}); | ||
} | ||
@@ -59,7 +64,7 @@ if (typeof args[0] === "string") { | ||
} | ||
exports.Get = RequestMethod("get"); | ||
exports.Post = RequestMethod("post"); | ||
exports.Delete = RequestMethod("delete"); | ||
exports.Put = RequestMethod("put"); | ||
exports.Patch = RequestMethod("patch"); | ||
exports.Get = requestMethodFactory("get"); | ||
exports.Post = requestMethodFactory("post"); | ||
exports.Delete = requestMethodFactory("delete"); | ||
exports.Put = requestMethodFactory("put"); | ||
exports.Patch = requestMethodFactory("patch"); | ||
//# sourceMappingURL=controller.js.map |
export * from "./application"; | ||
export * from "./controller"; | ||
export * from "./ioc"; | ||
export * from "./middleware"; | ||
export * from "./service"; | ||
export * from "./ioc"; |
@@ -15,4 +15,5 @@ "use strict"; | ||
__exportStar(require("./controller"), exports); | ||
__exportStar(require("./ioc"), exports); | ||
__exportStar(require("./middleware"), exports); | ||
__exportStar(require("./service"), exports); | ||
__exportStar(require("./ioc"), exports); | ||
//# sourceMappingURL=index.js.map |
import "reflect-metadata"; | ||
import { Class, Prototype } from "./types"; | ||
export interface ComponentClass extends Class { | ||
} | ||
import { ComponentClass, Prototype } from "./types"; | ||
export declare function Component(Class: ComponentClass): void; | ||
export declare function Inject(prototype: Prototype, fieldName: string): void; | ||
export declare function getInstance<T extends ComponentClass>(Class: T): InstanceType<T>; |
@@ -1,4 +0,2 @@ | ||
import { ComponentClass } from "./ioc"; | ||
export interface ServiceClass extends ComponentClass { | ||
} | ||
import { ServiceClass } from "./types"; | ||
export declare function Service(Class: ServiceClass): void; |
@@ -1,5 +0,8 @@ | ||
export interface Class { | ||
/// <reference types="koa__router" /> | ||
import { Middleware } from "@koa/router"; | ||
export interface ParameterlessClass { | ||
new (): void; | ||
prototype: Prototype; | ||
} | ||
export declare type MethodDecorator = (prototype: Prototype, methodName: string) => void; | ||
export declare type MethodDecorator<T extends ParameterlessClass = ParameterlessClass> = (prototype: T["prototype"], methodName: string) => void; | ||
export interface Prototype { | ||
@@ -9,1 +12,25 @@ constructor: Function; | ||
} | ||
export interface ComponentClass extends ParameterlessClass { | ||
} | ||
export interface ControllerClass extends ComponentClass { | ||
prototype: ControllerPrototype; | ||
} | ||
export declare type ControllerDecorator = (Class: ControllerClass) => void; | ||
export declare type HttpMethod = "get" | "post" | "put" | "delete" | "patch"; | ||
export declare const CLASS_ROUTE_METHODS: unique symbol; | ||
export declare const ROUTE_MIDDLEWARES: unique symbol; | ||
export declare type ClassRouteMethod = { | ||
method: HttpMethod; | ||
url: string; | ||
classMethodName: string; | ||
}; | ||
export declare type RouteMiddlewares = { | ||
classMethodName: string; | ||
middlewares: Middleware[]; | ||
}; | ||
export interface ControllerPrototype extends Prototype { | ||
[CLASS_ROUTE_METHODS]?: ClassRouteMethod[]; | ||
[ROUTE_MIDDLEWARES]?: RouteMiddlewares[]; | ||
} | ||
export interface ServiceClass extends ComponentClass { | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ROUTE_MIDDLEWARES = exports.CLASS_ROUTE_METHODS = void 0; | ||
exports.CLASS_ROUTE_METHODS = Symbol("CLASS_ROUTE_METHODS"); | ||
exports.ROUTE_MIDDLEWARES = Symbol("ROUTE_MIDDLEWARES"); | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "@koa-boot/server", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A SpringBoot-like lightweight web framework based on Koa.", | ||
@@ -25,5 +25,11 @@ "main": "./dist/index.js", | ||
"prettier": "^2.0.5", | ||
"prettier-plugin-organize-imports": "^1.1.1", | ||
"pretty-quick": "^2.0.1", | ||
"typescript": "^3.9.7" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "pretty-quick --staged" | ||
} | ||
} | ||
} |
# koa-boot-server | ||
[![Actions Status](https://github.com/SUCHMOKUO/node-worker-threads-pool/workflows/Workflow/badge.svg)](https://github.com/SUCHMOKUO/koa-boot-server/actions) | ||
[![](https://img.shields.io/npm/v/@koa-boot/server.svg)](https://www.npmjs.com/package/@koa-boot/server) | ||
A SpringBoot-like lightweight web framework based on Koa. | ||
_TODO_ | ||
- [ ] Add document |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
25629
30
371
11
8