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

Router / Route Prefixing #554

Closed
crookse opened this issue Nov 1, 2021 · 12 comments
Closed

Router / Route Prefixing #554

crookse opened this issue Nov 1, 2021 · 12 comments

Comments

@crookse
Copy link
Member

crookse commented Nov 1, 2021

Discussed in https://github.com/drashland/drash/discussions/539

Originally posted by vosmith September 16, 2021
I happened to come across this project trying to figure out what Deno was and ended up finding Drash as one of the top web frameworks for Deno. I like the simplicity of the current API, but I was wondering if this project was interested in having some deeper support for request routing. Mainly, being able to mount a Resource, and its routes behind a prefix, or establishing a parent-child relationship between Resources. I felt that both of these could be accomplished with a Router or something similar.

Here is a little better of an explanation. If I had 10 routes that all had a similar prefix like /api/users, /api/tasks, etc. I would probably have a UsersResource, TasksResource, etc. If I ever wanted to change the api prefix to api/v1, I would need to update each file and fix the path. However, if I could move that prefix up a level, and declare my resources in a Router that has some parent path then I would only update the Router. Something like:

export interface Router {
  name: string;
  paths: string[];
  resources: Drash.Interfaces.Resource[];
}

Similarly, updating the Resource and ServerConfigs interface to include routers: Drash.Interface.Router[]?

Again, I haven't had much experience with this framework and I don't really know how far the community is planning to take this project, but I thought I'd ask the question.

@Guergeiro
Copy link
Member

Hum, we really don't have a way of "aggregate" endpoints, right?

@crookse
Copy link
Member Author

crookse commented Nov 7, 2021

we don't and i don't even know how we'd put that in without bloating the code just to support a router. any thoughts @Guergeiro @ebebbington ? only way i can think of is pretty much going with the proposed approach in the discussion

@ebebbington
Copy link
Member

ebebbington commented Nov 7, 2021

i dont think we should go naming something a Router because i think it'll just get confusing - it isnt really a router, the server/resources are the 'routers', the 'router' in this context is just a way to prefix multiple resources intto a group

i've played around with it and came up with

const server = new Server({
  resources: [
    {
        prefix: "/api/v1",
        resources: [Res],
    }
  ],
  protocol: "http",
  hostname: "localhost",
  services: [new CORSService({origin: 'google.com'})],
  port: 3000,
});

Still toying with the idea, but another option could be like

new Server({
  resources: [...],
  prefixes: [
    {
      prefix: "/api/v1",
      resources: [HomeResource]
    }
  ]
})

Then when we create the resources and the url pattern, we do something like

this.#options.forEach(resourceClass => {
  const prefixObj = this.#options.prefixes.find(p => p.resources.includes(resourceClass)) // <-- new code
  let prefix= "" // <-- new code
  if (prefixObj) prefix = prefixObj.prefix // <-- new code
  const resource = new resourceClass();
  const patterns: URLPattern[] = [];
  resource.paths.forEach((path) => {
    // Add "{/}?" to match possible trailing slashes too
    patterns.push(new URLPattern({ pathname: prefix + path + "{/}?" })); // <-- added "prefix" part
  });
})

@Guergeiro
Copy link
Member

There's only two ways I think would work without changing a lot of stuff.

  1. The way @ebebbington presented.
  2. Using TS decorators at class level to change the uri to contain the prefix (uhh, we just removed tsconfig...).

@crookse
Copy link
Member Author

crookse commented Nov 7, 2021

i like ed's first way. that's clean.

@Guergeiro
Copy link
Member

And the first approach of @ebebbington seems better. And would also add a global level prefix.

@ebebbington
Copy link
Member

first approach is hackier tho lol (to impl it internally)

@Guergeiro
Copy link
Member

I get it... Is there a way of implementing a decorator pattern so that we don't change the logic of implementation? @crookse @ebebbington

@crookse
Copy link
Member Author

crookse commented Nov 7, 2021

@ebebbington remember when i had the setUp method in Drash.Service? we could still introduce that and use a RouterService to rearrange the resources on setUp

@crookse crookse changed the title Any interest in a Router? Router / Route Prefixing Nov 13, 2021
@ebebbington
Copy link
Member

made a pr for documenting this on how a user can do it, as opposed to us writing the code: drashland/website-v2#47

@crookse do you know if we can contact the person that created the discussion?

@crookse
Copy link
Member Author

crookse commented Nov 26, 2021

made a pr for documenting this on how a user can do it, as opposed to us writing the code: drashland/website-v2#47

@crookse do you know if we can contact the person that created the discussion?

yea. replying now.

@crookse
Copy link
Member Author

crookse commented Nov 26, 2021

hey @vosmith! we looked into this and there's actually nothing to implement since drash can be extended. here's an example of how to implement path prefixing:

https://drash.land/drash/v2.x/tutorials/resources/prefixing-paths

@crookse crookse closed this as completed Nov 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants