-
Notifications
You must be signed in to change notification settings - Fork 14
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
Allow a form of /page/:id: à la express #3
Comments
Idea:
Basically the idea is to define |
Hey, Colin! Great work here with this module. On this issue, let me know what you think about the direction below. This first part would be called once, as part of a "compilation" step: compile_route <- function(route) {
stopifnot(is.character(route) && length(route) == 1)
params <- stringr::str_match_all(route, "\\{(\\w+)\\}")[[1]][,2]
extractor <- stringr::str_replace_all(route, "\\{\\w+\\}", "([^//]+)")[[1]]
list(
template = route,
parse = function(path) {
parsed <- stringr::str_match_all(path, extractor)[[1]]
if (!nrow(parsed)) return(NULL)
setNames(parsed[1,-1], params)
}
)
}
route <- compile_route("/prefix/{arg1}/{arg2}-argsuffix/suffix") And then in request time: # NULL
route$parse("/blabla")
route$parse("/prefix")
route$parse("/prefix/1")
route$parse("/prefix/1/2-argsuffix")
route$parse("/prefix/1/almost/there-argsuffix/suffix")
# named vectors
route$parse("/prefix/1/2-argsuffix/suffix")
route$parse("/prefix/first/second with spaces-argsuffix/suffix") Some timings, just for reference (8GB RAM, 2.7 GHz i7 CPU):
Maybe this is missing some more validation in order to become user facing. I didn't test more weird inputs |
Hey @abelborges, Thanks a lot for this! Awesome. I love the approach of returning a list with a function and all, smart. |
Ok back to this. Internally, express uses https://www.npmjs.com/package/path-to-regexp, so maybe we should have something in the same spirit, that turns path into regex, then does a grepl on the route table 🤔 |
This is a work in progress. The goal is to allow for route handling via regex. This will allow for more flexibility in the routing of requests. Issue #3
Something that would allow to write :
The text was updated successfully, but these errors were encountered: