Giter VIP home page Giter VIP logo

enroute's Introduction

Enroute

Generates a TypeScript file that defines all named routes from Rails as helper functions.

Tests Gem Gem

Installation

Add this line to your application's Gemfile:

gem "enroute"

And then execute:

$ bundle install

Or install it yourself as:

$ gem install enroute

Usage

All you have to do is call the enroute binary with the main file you want to load and a output path.

$ bundle exec enroute export --output ./app/frontend/scripts/config/routes.ts

By default, <pwd>/config/environment.rb will be loaded. If you want to use a different file, use the --require switch.

$ bundle exec enroute export --require ./different-file.rb --output ./routes.ts

You can also ignore routes by using a config file.

$ bundle exec enroute export --output ./app/frontend/scripts/config/routes.ts --config ./config/enroute.yml

The config file must look like this:

---
ignore:
  - route_name

There's also a :only option that will include only the matching named routes.

---
only:
  - route_name

By default, route params will be typed as any. To add a custom typing annotation, you can use the typings key on the configuration file. Imagine you have the route get "settings/edit(/:section)" => "", as: "edit_settings"; you can have a config file like this:

---
typings:
  _default:
    format: '"html" | "json"'

  edit_settings:
    section: string

Importing helpers on TypeScript

You can then import any route that's been exported. Parameters are positional.

import { userUrl } from "./routes";

userUrl(1234);
//=> "/users/1234"

userUrl(1234, "json");
//=> "/users/1234.json"

You can also have routes with optional segments. For instance, imagine you have define the route get "settings/edit(/:section)" => "", as: "edit_settings". You could then use the helper editSettingsUrl() like this:

import { editSettingsUrl } from "./routes";

editSettingsUrl();
//=> "/settings/edit"

editSettingsUrl("security");
//=> "/settings/edit/security"

All helpers are typed accordingly to the route definition. For convenience, you may pass any primitives as a url parameter, which will then be converted into string.

An exception will be raised if you forget to provide a required parameter; empty strings, null and undefined will be rejected, but false is accepted.

import { userUrl } from "./routes";

userUrl();
//=> raises `id is required, but received undefined`

Maintainer

Contributors

Contributing

For more details about how to contribute, please read https://github.com/fnando/enroute/blob/main/CONTRIBUTING.md.

License

The gem is available as open source under the terms of the MIT License. A copy of the license can be found at https://github.com/fnando/enroute/blob/main/LICENSE.md.

Code of Conduct

Everyone interacting in the enroute project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

enroute's People

Contributors

dependabot[bot] avatar fnando avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.