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

route-based link providers #22

Open
mattbishop opened this issue Feb 28, 2020 · 4 comments
Open

route-based link providers #22

mattbishop opened this issue Feb 28, 2020 · 4 comments

Comments

@mattbishop
Copy link

I'd like a way to register a separate link provider for another resource that is called once the routed request has been fulfilled. This would let me add links to relevant resources without editing that resource.

For instance, if I had a /products/:id route and I wanted to add links to an add-to-cart resource, I would like to register a link provider call-back that would get the representation from the product resource and let it provide additional links to go with it. In this example, it would add a link to add that product to a cart.

@mattbishop
Copy link
Author

It might be easer if the route was named, so I could do this:

app.use(linker('product', new AddToCartLinker())

Then the Linker would get the product representation and URL to construct its own Link objects to add to the representation, but would not change the representation directly.

interface Linker {
  onHead(request): Promise<Link[]>;

  onGet(request, representation): Promise<Link[]>;
}

@evert
Copy link
Member

evert commented Feb 28, 2020

I think this is an interesting idea. The current design definitely doesn't preclude someone writing a middleware that does exactly this, but I am somewhat interested in making this a built-in feature (probably as part of a different middleware, not the router).

Are you considering using curveball and would want this feature?

@mattbishop
Copy link
Author

I don't have an immediate need, but will later this year. The more I think about it, the more I realize this is probably better as a standalone middleware that can plug into curveball, Koa, hapi, etc.

One thing that would help, though, would be if curveball's routes were namable so the developer wouldn't have to match on URI pattern but could instead declare the name of the rout they want to link to.

@evert
Copy link
Member

evert commented Mar 2, 2020

To support named routes, what I would probably do is create a javascript Map object to contain your routes, keyed by name, then when you're building your routes, loop through them.

Then if you want access to a specific controller by name, you can simply find it with routeMap.get('routeName')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants