Skip to content

Proposal: Type Checked Links #196

@henkkuli

Description

@henkkuli

Links form the backbone of hypertext documents and nothing is more annoying than dead links, especially those trying to point to other parts of a web application. That's why I propose type checked links.

Type Checked Links

Type checked links allow creating links to individual actions in controllers by their name in a type checked manner. They also allows passing parameters to those actions. This makes possible refactoring paths of the controllers and actions without needing to worry about broken links as links are automatically created by the proposed function. Changing the internal name of an action is also safe as type checked links enforce that the action really exists on the controller and raises a build time error otherwise.

Proposed function

I propose a function with the following signature to be added to the library:

function addressOf<Controller>(controller: new(...args: any[]) => Controller, action: keyof Controller, params: object = {})

The implementation of this function can be as simple as:

  1. Find the path of the action
  2. Substitute params to the path
    This can be done using path-to-regexp.

Examples

Here is a small example of using the proposed function.

@Controller("/user")
class UserController {
	@Get("/")
	public userList() {
		const users = [ /* ... */ ];
		const result = new Array<string>();
		
		for (const user of users) {
			const userAddress = addressOf(UserController, "showUser", { id: user.id });
			result.push(`<a href="${htmlspecialchars(userAddress)}>${user.name}</a>"`);
		}
		
		return result.join("<br />");
	}
	
	@Get("/:id(\\d+)")
	public showUser() {
		const userListAddress = addressOf(UserController, "index");
		return `<a href="${htmlspecialchars(userListAddress)}>Back to user list</a>"`;
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: awaiting answerAwaiting answer from the author of issue or PR.type: discussionIssues discussing any topic.type: featureIssues related to new features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions