Introduction
JS From Routes is a library to generate JS from routes defined in your Rails application.
Why JS From Routes? ๐ค
Path helpers in Rails make it easy to build URLs, while avoiding typos and mistakes.
Frontend code in Rails typically receives URLs from the server through JSON or HTML templates, or hardcodes paths of any endpoints that need to be accessed. Both approaches are fragile and cumbersome, and don't scale well.
JS From Routes provides path helpers in JS, generating them from your Rails routes, so that you can make requests without manually handling paths, parameter interpolation, or HTTP verbs.
Interested in hearing more? Read the original blog post.
Features โก๏ธ
๐ Path and Request Helpers
Use the controller and action name to obtain paths or make requests. No need to use URLs or manually interpolate parameters, preventing mistakes and saving development time.
๐ Serialization / Deserialization
Consuming JSON APIs works out of the box, but you can easily consume other types of media.
Case conversion between Ruby and JS is handled for you, but you can also opt-out.
โ Safety
Prevents routing mistakes when renaming or removing an action.
Path helpers are fully typed, and client libraries are entirely written in TypeScript.
๐ค Automatic Generation
Path helpers are generated automatically whenever Rails reload is triggered.
Add a route, refresh the page, and start using the path helper!
๐ Customizable Generation
Select a client library that uses fetch
or axios
, or use your own code.
Choose different conventions by customizing how code is generated.