Routes - Type-safe Phoenix routes in your JavaScript/TypeScript!

Hello Elixir community! I’m excited to announce the first release of Routes, a library that automatically generates JavaScript and TypeScript route helpers from your Phoenix router. It aims to bring the convenience and type safety of Phoenix route helpers to your frontend code.

Why Routes?

Working with Phoenix and modern JavaScript frameworks, I often found myself manually maintaining route paths in the frontend or creating custom solutions to keep frontend routing in sync with Phoenix routes. Routes solves this by:

  • :arrows_counterclockwise: Automatically generating route helpers from your Phoenix router
  • :memo: Providing full TypeScript support with type definitions
  • :mag: Supporting live reloading during development
  • :dart: Handling path parameters and query strings with type safety
  • :muscle: Ensuring route parameters are correctly typed and validated

Quick Example

# In your Phoenix router
defmodule MyAppWeb.Router do
  use Phoenix.Router
  use Routes

  scope "/" do
    get "/users/:id", UserController, :show, as: :user
  end
end
// In your JavaScript/TypeScript code
import Routes from './routes';

// Type-safe route generation
const url = Routes.path('user.show', { id: 123 });
// => "/users/123"

// With query parameters
const searchUrl = Routes.path('user.index', {
  _query: { search: 'john', filter: 'active' }
});
// => "/users?search=john&filter=active"

Inertia.js Integration

If you’re using Inertia.js, Routes provides a clean way to create type-safe navigation:

<Link
  to="/users/:id"
  params={{ id: 123, _query: { tab: "profile" } }}
>
  View Profile
</Link>

Getting Started

Add to your mix.exs:

def deps do
  [
    {:routes, "~> 0.1.0"}
  ]
end

Configure in config.exs:

config :routes,
  router: MyAppWeb.Router,
  typescript: true,
  routes_path: "assets/js/routes"

Links

Feedback Welcome!

This is the initial release, and I’d love to hear your thoughts, suggestions, and use cases. Feel free to open issues on GitHub or contribute to the project.

Happy coding! :tada:

9 Likes

Hi @RxAssim,

Always nice to see people invest time in the eco system! As for the feedback:

  1. Pardon my question but I fail to see the use case. Where does this fit in? When does a server side path need a sync with client side, which can not be (and afaik is) handled by Phoenix LiveView?*

Edit: answer myself: Inertiajs of course :slight_smile:

—-

  1. The name of the lib is a bit too close to my lib RouteX for my taste to be honest and also in the same valley as libs called ‘framework’ or ‘xml’. I would suggest a more distrinct name.

—-

  1. Maybe this can be a Routex extension? This would solve the naming issue, you can focus on the Typescript part (as processing and live regeneration is taken care of) and all features of Routex will be supported by the Typescript files (alternative routes for instance).

Kind regard,
Author of Routex

Hi @BartOtten,

  1. Yes, you could use it with Inertia, Live_svelte, Live_vue…
  2. It’s just the simplest name I could think of + this lib is mostly code that I wrote for my own projects and decided to open source it mostly as is.
  3. Seems like a good idea, but Routes is supposed to handle one thing and one thing alone which is easy routes access for frontend frameworks that use Phoenix as a backend.

Thank you for this package @RxAssim. Do you know if there’s any package that generates props types?

1 Like