Skip to content

Commit

Permalink
fix: add variable router on controller call
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamensuli committed Feb 26, 2024
1 parent 57d72f1 commit dce3fc1
Show file tree
Hide file tree
Showing 21 changed files with 359 additions and 153 deletions.
4 changes: 3 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Kernel, Module, modules } from "nodefony";

import { controllers } from "@nodefony/framework";
import AppController from "./nodefony/controllers/AppController";
import config from "./nodefony/config/config";
import http from "@nodefony/http";
import security from "@nodefony/security";
Expand All @@ -15,6 +16,7 @@ import sequelize from "@nodefony/sequelize";
"@nodefony/framework",
"@nodefony/sequelize",
])
@controllers([AppController])
class App extends Module {
/**
* Constructs an instance of the App class.
Expand Down
72 changes: 72 additions & 0 deletions nodefony/controllers/AppController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//import { Service, Container, Module } from "nodefony";
import { inject, Fetch } from "nodefony";
import { DefineRoute, DefineController, Controller } from "@nodefony/framework";
import { ContextType } from "@nodefony/http";

class AppController extends Controller {
static override basepath: string = "/app";
constructor(
context: ContextType,
@inject("Fetch") private fetch: Fetch
) {
super("app", context);
}

async initialize() {
//console.log("passs initialize");
//await this.startSession();
return this;
}

@DefineRoute("route1", { path: "/", method: "GET" })
async method1() {
console.log("call method", this.fetch.library);
//await this.startSession();
return this.renderJson({ foo: "bar" });
}

@DefineRoute("route3", { method: "DELETE" })
method3() {
console.log("call method3", this.route);
}

@DefineRoute("route2", { path: "/add", requirements: { methods: "POST" } })
method2() {
console.log("call method2");
return this.renderJson({ foo: "bar" });
}

@DefineRoute("route4", {
path: "/add/{name}",
requirements: { methods: ["GET", "POST"] },
defaults: { name: "cci" },
})
method4(name: string) {
console.log("call method4", name, this.route);
return this.renderJson({ name });
}

@DefineRoute("route6", {
path: "/ele/{metier}/{format}/add",
defaults: { format: "cci" },
})
method6() {
console.log("other route for app");
return this.renderJson({ foo: "bar" });
}
@DefineRoute("route7", {
path: "/ele/{metier}/{format}/{method}/add",
})
method7(metier: string, format: string, method: string) {
return this.renderJson({ metier, format, method });
}

@DefineRoute("route5", {
path: "*",
})
method5() {
console.log("other route for app");
}
}

export default AppController;
2 changes: 1 addition & 1 deletion src/nodefony/src/kernel/Kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export interface FilterInterface {
}

export interface ServiceWithInitialize extends Service {
initialize?(module: Module): Promise<Service>;
initialize?(module?: Module): Promise<Service>;
}

export interface ServiceConstructor {
Expand Down
19 changes: 14 additions & 5 deletions src/packages/@nodefony/framework/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Service, Kernel, Module, services } from "nodefony";
import config from "./nodefony/config/config";
import Router, { TypeController } from "./nodefony/service/router";
import test from "./nodefony/controller/testController";
import Route from "./nodefony/src/Route";
import Controller from "./nodefony/src/Controller";
import Resolver from "./nodefony/src/Resolver";
import { controllers } from "./nodefony/decorators/routerDecorators";
import {
controllers,
DefineRoute,
DefineController,
} from "./nodefony/decorators/routerDecorators";

@controllers([test])
@services([Router])
class Framework extends Module {
constructor(kernel: Kernel) {
Expand All @@ -16,5 +18,12 @@ class Framework extends Module {
}

export default Framework;

export { Route, Controller, Resolver, Router, controllers };
export {
Controller,
Route,
Router,
Resolver,
DefineRoute,
DefineController,
controllers,
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ControllerConstructor } from "../src/Route";

type Constructor<T = {}> = new (...args: any[]) => T;

function controller(name: string, options: Record<string, any>) {
function DefineController(name: string, options: Record<string, any>) {
return function (controller: any) {
const constructor = controller.constructor;
const className = controller.name;
Expand Down Expand Up @@ -58,7 +58,7 @@ function controllers(
* console.log("call method");
* }
*/
function route(name: string, options: RouteOptions) {
function DefineRoute(name: string, options: RouteOptions) {
return function (
target: any,
propertyKey: string,
Expand Down Expand Up @@ -96,4 +96,4 @@ function route(name: string, options: RouteOptions) {
};
}

export { route, controller, controllers };
export { DefineRoute, DefineController, controllers };
27 changes: 2 additions & 25 deletions src/packages/@nodefony/framework/nodefony/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
injectable,
} from "nodefony";
import Route, { RouteOptions } from "../src/Route";
import { HttpKernel, Context } from "@nodefony/http";
import { HttpKernel, Context, ContextType } from "@nodefony/http";
import Resolver from "../src/Resolver";
import Controller from "../src/Controller";
export type TypeController<T> = new (...args: any[]) => T;
Expand All @@ -30,32 +30,9 @@ class Router extends Service {
module.notificationsCenter as Event,
module.options.router
);

// this.kernel?.once("onReady", () => {
// console.log(routes);
// const myRoute: Route = routes[0];
// if (myRoute) {
// if (myRoute.controller && myRoute.classMethod) {
// const inst = new myRoute.controller(module);
// const methodKey = myRoute.classMethod as keyof typeof inst;
// if (typeof inst[methodKey] === "function") {
// (inst[methodKey] as Function)();
// }
// console.log("################################");
// this.matchRoutes("/base/add/ddddd");
// console.log("################################");
// this.matchRoutes("/base/ele/sboob/kdskdkd/add");
// console.log("################################");
// this.matchRoutes("/base/add/");
// console.log("################################");
// this.matchRoutes("/base/ele/sboob/kdskdkd/mymethos/add");
// console.log("################################");
// }
// }
// });
}

resolve(context: Context): Resolver {
resolve(context: ContextType): Resolver {
const resolver = new Resolver(context);
for (let i = 0; i < routes.length; i++) {
try {
Expand Down
Loading

0 comments on commit dce3fc1

Please sign in to comment.