Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix search and add inject arg for controllers #41

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 38 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aster-js/app",
"version": "2.1.6",
"version": "2.1.7",
"description": "Aster core library part of Aster js library",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand All @@ -27,13 +27,13 @@
"license": "ISC",
"dependencies": {
"@aster-js/async": "^1.4.1",
"@aster-js/collections": "^1.0.3",
"@aster-js/core": "^1.3.1",
"@aster-js/collections": "^1.0.5",
"@aster-js/core": "^1.3.2",
"@aster-js/decorators": "^1.1.0",
"@aster-js/dom": "1.1.0",
"@aster-js/events": "^1.3.0",
"@aster-js/ioc": "^1.7.0",
"@aster-js/iterators": "^1.2.2",
"@aster-js/ioc": "^1.7.1",
"@aster-js/iterators": "^1.2.3",
"tslib": "^2.6.3"
},
"devDependencies": {
Expand Down
9 changes: 6 additions & 3 deletions src/controller/controller-routing-handler-tag.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Lookup } from "@aster-js/collections";
import { Constructor, Tag } from "@aster-js/core";
import { IRoutingHandler, RouteData } from "../routing";
import {IApplicationPart} from "../abstraction";

export const ControllerRoutingHandlerTag = Tag.lazy<Constructor<IRoutingHandler>[]>("routes", () => []);

export type ControllerRouteArg = {
export type ControllerArgAccessor = (data: RouteData, app: IApplicationPart) => any;

export type ControllerArg = {
index: number;
accessor: (data: RouteData) => any;
accessor: ControllerArgAccessor;
}

export const ControllerRoutingCallbackArgsTag = Tag.lazy<Lookup<string, ControllerRouteArg>>("route args", () => new Lookup());
export const ControllerCallbackArgsTag = Tag.lazy<Lookup<string, ControllerArg>>("route args", () => new Lookup());
8 changes: 5 additions & 3 deletions src/controller/controller-routing-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Func } from "@aster-js/core"
import { ServiceIdentifier } from "@aster-js/ioc";
import { IRoutingHandler, RouteData, RoutingInvocationContext } from "../routing";
import { ControllerRoutingCallbackArgsTag } from "./controller-routing-handler-tag";
import { ControllerCallbackArgsTag } from "./controller-routing-handler-tag";
import type { IRoutingResult } from "./irouting-result";

/**
Expand All @@ -19,8 +19,10 @@ export class ControllerRoutingHandler implements IRoutingHandler {
const controller = app.services.get(this._target, true);

const proto = Object.getPrototypeOf(controller);
const argsDefinition = ControllerRoutingCallbackArgsTag.get(proto);
const args = [...argsDefinition.get(this._methodName)].reverse().map(x => x.accessor(data));
const argsDefinition = ControllerCallbackArgsTag.get(proto);
const args = [...argsDefinition.get(this._methodName)]
.reverse()
.map(x => x.accessor(data, app));

let result = this._callback.apply(controller, args);

Expand Down
15 changes: 10 additions & 5 deletions src/controller/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { asserts, Constructor } from "@aster-js/core";
import {asserts, Constructor, Func} from "@aster-js/core";
import { resolveServiceId } from "@aster-js/ioc";
import { RouteData, UrlValues } from "../routing";
import { ControllerRoutingHandler } from "./controller-routing-handler";
import { ControllerRoutingCallbackArgsTag, ControllerRoutingHandlerTag } from "./controller-routing-handler-tag";
import { ControllerCallbackArgsTag, ControllerRoutingHandlerTag } from "./controller-routing-handler-tag";
import {IApplicationPart} from "../abstraction";

/** Decorate to enable binding route template to controller method call */
export const RoutePath = (path: string) => {
Expand Down Expand Up @@ -32,7 +33,7 @@ export const RoutePath = (path: string) => {

const accessor = ({ values }: RouteData) => name ? values[name] : structuredClone(values);

ControllerRoutingCallbackArgsTag.get(target).add(propertyKey, { index, accessor });
injectArgument(target, propertyKey, index, accessor);
}
}

Expand All @@ -45,7 +46,7 @@ export const FromSearch = (name?: string) => {

const accessor = ({ query }: RouteData) => name ? query[name] : structuredClone(query);

ControllerRoutingCallbackArgsTag.get(target).add(propertyKey, { index, accessor });
injectArgument(target, propertyKey, index, accessor);
}
}

Expand All @@ -65,6 +66,10 @@ export const FromUrl = (name?: string) => {
return values[name];
};

ControllerRoutingCallbackArgsTag.get(target).add(propertyKey, { index, accessor });
injectArgument(target, propertyKey, index, accessor);
}
}

export function injectArgument(target: object, propertyKey: string, index: number, accessor: (data: RouteData, app: IApplicationPart) => any): void {
ControllerCallbackArgsTag.get(target).add(propertyKey, { index, accessor });
}
3 changes: 1 addition & 2 deletions src/routing/default-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { RoutingConstants } from "./routing-constants";
import { Route } from "./route";
import { Path } from "./path";
import { RoutingInvocationContext } from "./routing-invocation-context";
import { ApplicationPartLifecycleHooks } from "../application-part";

@ServiceContract(IRouter)
export class DefaultRouter implements IRouter {
Expand Down Expand Up @@ -47,7 +46,7 @@ export class DefaultRouter implements IRouter {
return await root.eval(url, defaults);
}

// Non relative path (root router context)
// Non-relative path (root router context)
// This url may be relative or not
const finalUrl = new URL(url, location.origin);

Expand Down
4 changes: 2 additions & 2 deletions src/routing/route-data/search-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export namespace SearchValues {

export function toString(query: SearchValues, sort?: boolean): string {
const search = new URLSearchParams();
const entries = Object.entries(query); search
const entries = Object.entries(query);


for (const [key, value] of entries) {
Expand All @@ -47,7 +47,7 @@ export namespace SearchValues {
}
}

if (search) search.sort();
if (sort) search.sort();
return search.toString();
}

Expand Down
53 changes: 50 additions & 3 deletions tests/controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { assert } from "chai";
import { htmlResult, IAppConfigureHandler, IApplicationPart, IRouter, IRoutingResult, FromUrl, UrlValues, FromSearch, SearchValues, RouteData, RoutePath, FromRoute, RouteValues, SinglePageApplication } from "../src";
import {
htmlResult,
IAppConfigureHandler,
IApplicationPart,
IRouter,
IRoutingResult,
FromUrl,
UrlValues,
FromSearch,
SearchValues,
RouteData,
RoutePath,
FromRoute,
RouteValues,
SinglePageApplication,
injectArgument
} from "../src";
import {asserts} from "@aster-js/core";

describe("Controller", () => {

Expand All @@ -9,8 +26,6 @@ describe("Controller", () => {

class CustomerViewController {

constructor(@IApplicationPart private readonly _part: IApplicationPart) { }

@RoutePath("/customer/:+customerId/detail/:+id/:text")
async showDetail(

Expand Down Expand Up @@ -55,6 +70,38 @@ describe("Controller", () => {
assert.equal(root.innerHTML, "<div><i>hello world 33 !!</i></div>");
});

it("Should use custom decorator to inject value", async () => {
const root = document.createElement("div");
root.innerHTML = "<b>Loading...</b>"

const AppName = <ParameterDecorator>function (target: object, propertyKey: string | symbol, index: number) {
asserts.ofType(propertyKey, "string");

const accessor = (_: RouteData, app: IApplicationPart) => app.name;

injectArgument(target, propertyKey, index, accessor);
};

class CustomerViewController {

@RoutePath("/customer/:+customerId")
async showDetail(
@AppName appName: string

): Promise<IRoutingResult> {
assert.equal(appName, "bob", "appName");
return htmlResult(`<h1>${appName}</h1>`, root);
}
}

using app = await SinglePageApplication.start("bob", IAppConfigureHandler.create(builder => {
builder.addController(CustomerViewController);
}));

await app.services.get(IRouter, true).eval("./customer/555");
assert.equal(root.innerHTML, "<div><h1>bob</h1></div>");
});

it("Should use a controller nested in an application part to handle a route", async () => {
const root = document.createElement("div");
root.innerHTML = "<b>Loading...</b>";
Expand Down
Loading