Updating Koa decorators

This commit is contained in:
2026-04-14 12:20:28 +01:00
parent 8e5a8d073f
commit bee509b4b6
3 changed files with 24 additions and 34 deletions
+21 -32
View File
@@ -9,30 +9,15 @@ type Route = {
fn: any;
};
export class BaseRouter {
public router!: Router;
processRoutes(routes: Route[]) {
for (const route of routes) switch (route.method) {
case "get": this.router.get(route.path, route.fn); break;
case "post": this.router.post(route.path, route.fn); break;
case 'use': this.router.use(route.path, route.fn); break;
}
}
injectInto(app: Koa) {
app.use(this.router.routes()).use(this.router.allowedMethods());
}
}
const routeSymbol = Symbol("routeSymbol");
export function get(path: string) {
return function getDecorator (originalMethod: any, context: ClassMethodDecoratorContext) {
if (context.metadata) {
if (!context.metadata.routes) {
context.metadata.routes = [];
if (!context.metadata[routeSymbol]) {
context.metadata[routeSymbol] = [] as Route[];
}
// @ts-ignore
context.metadata.routes.push({method: 'get', path, fn: originalMethod});
context.metadata[routeSymbol].push({method: 'get', path, fn: originalMethod});
}
return originalMethod;
}
@@ -41,11 +26,10 @@ export function get(path: string) {
export function post(path: string) {
return function getDecorator (originalMethod: any, context: ClassMethodDecoratorContext) {
if (context.metadata) {
if (!context.metadata.routes) {
context.metadata.routes = [];
if (!context.metadata[routeSymbol]) {
context.metadata[routeSymbol] = [] as Route;
}
// @ts-ignore
context.metadata.routes.push({method: 'post', path, fn: originalMethod});
context.metadata[routeSymbol].push({method: 'post', path, fn: originalMethod});
}
return originalMethod;
}
@@ -54,11 +38,10 @@ export function post(path: string) {
export function middleware(fn: any, path: string = '') {
return function middlewareDecorator (constructor: any, context: ClassDecoratorContext) {
if (context.metadata) {
if (!context.metadata.routes) {
context.metadata.routes = [];
if (!context.metadata[routeSymbol]) {
context.metadata[routeSymbol] = [];
}
// @ts-ignore
context.metadata.routes.unshift({method: 'use', path, fn});
context.metadata[routeSymbol].unshift({method: 'use', path, fn});
}
return constructor;
}
@@ -66,15 +49,21 @@ export function middleware(fn: any, path: string = '') {
export function router(prefix: string) {
return function routerDecorator<T extends Constructor>(constructor: T, context: ClassDecoratorContext<T>) {
return class extends constructor {
return class KoaRouter extends constructor {
public router: Router;
constructor(...args: any[]) {
super(...args);
if (this instanceof BaseRouter) {
this.router = new Router({prefix});
// @ts-ignore
this.processRoutes(context.metadata?.routes || [] satisfies Route[]);
this.router = new Router({prefix});
for (const route of context.metadata?.[routeSymbol] || [] satisfies Route[]) switch (route.method) {
case "get": this.router.get(route.path, route.fn); break;
case "post": this.router.post(route.path, route.fn); break;
case 'use': this.router.use(route.fn); break;
}
}
public injectInto(app: Koa) {
app.use(this.router.routes()).use(this.router.allowedMethods());
}
}
}
}