-
Notifications
You must be signed in to change notification settings - Fork 149
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
Add post_processor to customize the output of each route #306
Conversation
Can you give some live examples of how Your solution looks pretty hacky:
I actually think that exposing route spec via something like |
I regret that I did not motivate PR well enough. Consider the following example. I have React Router routes description in routes.ts file: import React from "react";
import ReactDOM from "react-dom/client";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
const router = createBrowserRouter([
{
path: "/settings",
element: <div>...</div>
},
{
path: "/profile",
element: <div>...</div>
},
]);
ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
); In this case type of I can use const router = createBrowserRouter([
{
path: Routes.settings_path.toString().replace(".:format", ""),
element: <div>...</div>
}
]); But in this case the type of |
Yes, the solution looks pretty hacky. I don't like that the post_processor has to deal with an already formed string. Perhaps it should receive not a string, but an array of components [comment, name, body]. |
I am not sure how your solution is going to work. const settings_path = (): string => "/settings";
const path: "/settings" | "/profile" = settings_path(); So, even if you can dynamically define a router using js-routes helpers, you would have to hardcode it later. So, what is the point of avoiding hardcode with js-routes in the first place? Other thing is route parameters like |
Lets look at test one more time Proc.new do |content, route|
if route
# This may be a more complex replacement.
pattern = route.path.spec.to_s.gsub('(.:format)', '').gsub('.:format', '')
content + %Q[,\n#{route.name}_pattern: '#{pattern}']
else
content
end
end It generates the code: /**
* Generates rails route to
* /inboxes/:inbox_id/messages(.:format)
* @param {any} inbox_id
* @param {object | undefined} options
* @returns {string} route path
*/
inbox_messages_path: __jsr.r({"inbox_id":{"r":true},"format":{}}, [2,[7,"/"],[2,[6,"inboxes"],[2,[7,"/"],[2,[3,"inbox_id"],[2,[7,"/"],[2,[6,"messages"],[1,[2,[8,"."],[3,"format"]]]]]]]]]),
inbox_messages_pattern: '/inboxes/:inbox_id/messages', Then I can import and use ...
import { inbox_messages_pattern } from 'routes';
...
const router = createBrowserRouter([
{
path: inbox_messages_pattern,
element: <div>...</div>
}
]); And I don't have to hardcode literal ( |
I think the best way accomplish that is the following:
scope format: false do
resources :inboxes do
resources :messages
end
...
end Any problems with this approach? |
In this case, the type of path will be a string. Ideally, I would like to limit the type to only possible values (existing routes), but this option is quite acceptable. This was the first option I considered before doing PR: And I'll probably stop there for now. For current tasks, this will be enough. While I ask you not to close this PR, suddenly new ideas will arise :) |
So what we can do. First, rename type RouteHelperExtras<T extends string = string> = {
....
spec(): T;
} Make sure we generate The only problem of such approach: it will not strip optional parameters like |
I decided that it is too much of an overhead and isn't applicable to all use cases to accept it to the core. So closing this one until a better solution will be found. |
I agree with you. The idea was underdeveloped |
Sometimes we need to customize the output of each individual route.
I was faced with the task of typing routes when used with react. And I couldn't use dynamic generated routes with toString(). This method returns
string
type for any helper. It would be great for me if it was possible to export patterns as literals