enroute 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.eslintrc.json +10 -0
- data/.gitignore +11 -0
- data/.prettierrc.js +1 -0
- data/.rubocop.yml +11 -0
- data/.tool-versions +1 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +107 -0
- data/Rakefile +15 -0
- data/enroute.gemspec +37 -0
- data/exe/enroute +5 -0
- data/jest.config.js +3 -0
- data/lib/enroute.rb +15 -0
- data/lib/enroute/cli.rb +35 -0
- data/lib/enroute/export.rb +79 -0
- data/lib/enroute/routes.rb +60 -0
- data/lib/enroute/template.ts.erb +129 -0
- data/lib/enroute/version.rb +5 -0
- data/package.json +18 -0
- data/tsconfig.json +24 -0
- metadata +168 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 69b21678657d7b4e22392bb0fc93d825146f9af0ff90bcdbc5e356e8492cfb14
|
4
|
+
data.tar.gz: 3cef2464c5fbd9fa7e14a1a321e8a3f054c45ef4f92f9c1dd1814aeeaa0246ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8802edb40c7a1e1ca283bda49f8aa7eb1cc5e68dd13f0234b77a7dcd4f51310fd64fc57a9a6276e4383b4ce78ea691ac4ad53e8b878c7ed9278425936adbf455
|
7
|
+
data.tar.gz: b96ce752c8b775de0b699e3368ef121e98cf66813a052db033084c2def29a54d9fad4d545ceee6cd7c2ae31b8f63393356e68c23b9faaa21d009391b8801b117
|
data/.eslintrc.json
ADDED
data/.gitignore
ADDED
data/.prettierrc.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
module.exports = require("@fnando/codestyle/prettier.json");
|
data/.rubocop.yml
ADDED
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
nodejs 12.16.0
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at me@fnando.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Nando Vieira
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# Enroute
|
2
|
+
|
3
|
+
Generates a TypeScript file that defines all named routes from Rails as helper
|
4
|
+
functions.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem "enroute"
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle install
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install enroute
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
All you have to do is call the `enroute` binary with the main file you want to
|
25
|
+
load and a output path.
|
26
|
+
|
27
|
+
```console
|
28
|
+
$ bundle exec enroute --output ./app/frontend/scripts/config/routes.ts
|
29
|
+
```
|
30
|
+
|
31
|
+
By default, `<pwd>/config/environment.rb` will be loaded. If you want to use a
|
32
|
+
different file, use the `--require` switch.
|
33
|
+
|
34
|
+
```console
|
35
|
+
$ bundle exec enroute --require ./different-file.rb --output ./routes.ts
|
36
|
+
```
|
37
|
+
|
38
|
+
You can then import any route that's been exported. Parameters are positional.
|
39
|
+
|
40
|
+
```typescript
|
41
|
+
import { userUrl } from "./routes";
|
42
|
+
|
43
|
+
userUrl(1234);
|
44
|
+
//=> "/users/1234"
|
45
|
+
|
46
|
+
userUrl(1234, "json");
|
47
|
+
//=> "/users/1234.json"
|
48
|
+
```
|
49
|
+
|
50
|
+
You can also have routes with optional segments. For instance, imagine you have
|
51
|
+
define the route `get "settings/edit(/:section)" => "", as: "edit_settings"`.
|
52
|
+
You could then use the helper `editSettingsUrl()` like this:
|
53
|
+
|
54
|
+
```typescript
|
55
|
+
import { editSettingsUrl } from "./routes";
|
56
|
+
|
57
|
+
editSettingsUrl();
|
58
|
+
//=> "/settings/edit"
|
59
|
+
|
60
|
+
editSettingsUrl("security");
|
61
|
+
//=> "/settings/edit/security"
|
62
|
+
```
|
63
|
+
|
64
|
+
All helpers are typed accordingly to the route definition. For convenience, you
|
65
|
+
may pass any primitives as a url parameter, which will then be converted into
|
66
|
+
string.
|
67
|
+
|
68
|
+
An exception will be raised if you forget to provide a required parameter; empty
|
69
|
+
strings, `null` and `undefined` will be rejected, but `false` is accepted.
|
70
|
+
|
71
|
+
```typescript
|
72
|
+
import { userUrl } from "./routes";
|
73
|
+
|
74
|
+
userUrl();
|
75
|
+
//=> raises `id is required, but received undefined`
|
76
|
+
```
|
77
|
+
|
78
|
+
## Development
|
79
|
+
|
80
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
81
|
+
`rake test` to run the tests. You can also run `bin/console` for an interactive
|
82
|
+
prompt that will allow you to experiment.
|
83
|
+
|
84
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
85
|
+
release a new version, update the version number in `version.rb`, and then run
|
86
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
87
|
+
git commits and tags, and push the `.gem` file to
|
88
|
+
[rubygems.org](https://rubygems.org).
|
89
|
+
|
90
|
+
## Contributing
|
91
|
+
|
92
|
+
Bug reports and pull requests are welcome on GitHub at
|
93
|
+
https://github.com/fnando/enroute. This project is intended to be a safe,
|
94
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
95
|
+
the
|
96
|
+
[code of conduct](https://github.com/fnando/enroute/blob/master/CODE_OF_CONDUCT.md).
|
97
|
+
|
98
|
+
## License
|
99
|
+
|
100
|
+
The gem is available as open source under the terms of the
|
101
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
102
|
+
|
103
|
+
## Code of Conduct
|
104
|
+
|
105
|
+
Everyone interacting in the Enroute project's codebases, issue trackers, chat
|
106
|
+
rooms and mailing lists is expected to follow the
|
107
|
+
[code of conduct](https://github.com/fnando/enroute/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
require "rubocop/rake_task"
|
6
|
+
|
7
|
+
Rake::TestTask.new(:test) do |t|
|
8
|
+
t.libs << "test"
|
9
|
+
t.libs << "lib"
|
10
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
11
|
+
end
|
12
|
+
|
13
|
+
RuboCop::RakeTask.new
|
14
|
+
|
15
|
+
task default: %i[test rubocop]
|
data/enroute.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/enroute/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "enroute"
|
7
|
+
spec.version = Enroute::VERSION
|
8
|
+
spec.authors = ["Nando Vieira"]
|
9
|
+
spec.email = ["me@fnando.com"]
|
10
|
+
|
11
|
+
spec.summary = "Export Rails routes to TypeScript definitions"
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "https://github.com/fnando/enroute"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
19
|
+
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`
|
22
|
+
.split("\x0")
|
23
|
+
.reject {|f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "thor"
|
31
|
+
spec.add_development_dependency "minitest"
|
32
|
+
spec.add_development_dependency "minitest-utils"
|
33
|
+
spec.add_development_dependency "rails"
|
34
|
+
spec.add_development_dependency "rake"
|
35
|
+
spec.add_development_dependency "rubocop"
|
36
|
+
spec.add_development_dependency "rubocop-fnando"
|
37
|
+
end
|
data/exe/enroute
ADDED
data/jest.config.js
ADDED
data/lib/enroute.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "thor"
|
4
|
+
require "fileutils"
|
5
|
+
require "erb"
|
6
|
+
require "json"
|
7
|
+
|
8
|
+
module Enroute
|
9
|
+
require_relative "enroute/version"
|
10
|
+
require_relative "enroute/routes"
|
11
|
+
require_relative "enroute/export"
|
12
|
+
require_relative "enroute/cli"
|
13
|
+
|
14
|
+
class Error < StandardError; end
|
15
|
+
end
|
data/lib/enroute/cli.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Enroute
|
4
|
+
class CLI < Thor
|
5
|
+
def self.exit_on_failure?
|
6
|
+
true
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "version", "Display version"
|
10
|
+
map %w[-v --version] => :version
|
11
|
+
|
12
|
+
def version
|
13
|
+
say "Enroute #{Enroute::VERSION}"
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "export", "Export route definitions"
|
17
|
+
option :require,
|
18
|
+
type: :string,
|
19
|
+
aliases: :r,
|
20
|
+
default: File.join(Dir.pwd, "config/environment.rb")
|
21
|
+
|
22
|
+
option :output,
|
23
|
+
type: :string,
|
24
|
+
required: true,
|
25
|
+
aliases: :o
|
26
|
+
|
27
|
+
def export
|
28
|
+
require_path = File.expand_path(options["require"])
|
29
|
+
output_path = File.expand_path(options["output"])
|
30
|
+
|
31
|
+
require require_path
|
32
|
+
Export.call(output_path)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Enroute
|
4
|
+
module Export
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def call(output_path)
|
8
|
+
FileUtils.mkdir_p(File.dirname(output_path))
|
9
|
+
|
10
|
+
write_template(output_path)
|
11
|
+
end
|
12
|
+
|
13
|
+
def params
|
14
|
+
{
|
15
|
+
routes: routes,
|
16
|
+
types: routes.map {|route| build_ts_definition(route) }.join("\n"),
|
17
|
+
router_type: routes.map {|route| build_ts_route_definition(route) }.join
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def routes
|
22
|
+
Routes.call
|
23
|
+
end
|
24
|
+
|
25
|
+
def write_template(output_path)
|
26
|
+
File.open(output_path, "w+") do |file|
|
27
|
+
file << render_template
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def route_functions
|
32
|
+
routes
|
33
|
+
.each_with_index
|
34
|
+
.map {|route, index| build_ts_function(route, index) }
|
35
|
+
.join("\n\n")
|
36
|
+
end
|
37
|
+
|
38
|
+
def router_type_definitions
|
39
|
+
routes.map {|route| build_ts_route_definition(route) }.join
|
40
|
+
end
|
41
|
+
|
42
|
+
def type_definitions
|
43
|
+
routes.map {|route| build_ts_definition(route) }.join("\n")
|
44
|
+
end
|
45
|
+
|
46
|
+
def render_template
|
47
|
+
ERB.new(File.read("#{__dir__}/template.ts.erb")).result binding
|
48
|
+
end
|
49
|
+
|
50
|
+
def build_ts_definition(route)
|
51
|
+
[
|
52
|
+
"export interface #{route[:typeName]} extends RouteHandler {",
|
53
|
+
" (#{build_ts_args_definition(route)}): string;",
|
54
|
+
"}\n"
|
55
|
+
].join("\n")
|
56
|
+
end
|
57
|
+
|
58
|
+
def build_ts_args_definition(route)
|
59
|
+
route[:segments].map do |segment|
|
60
|
+
optional = route[:requiredSegments].include?(segment) ? "" : "?"
|
61
|
+
"#{segment.camelize(:lower)}#{optional}: any"
|
62
|
+
end.join(", ")
|
63
|
+
end
|
64
|
+
|
65
|
+
def build_ts_function(route, index)
|
66
|
+
args = build_ts_args_definition(route)
|
67
|
+
segments = route[:segments].map {|segment| segment.camelize(:lower) }
|
68
|
+
|
69
|
+
[
|
70
|
+
%[export const #{route[:name]}Url = (#{args}): string =>],
|
71
|
+
%[ routeHandlers[#{index}](#{segments.join(', ')});]
|
72
|
+
].join("\n")
|
73
|
+
end
|
74
|
+
|
75
|
+
def build_ts_route_definition(route)
|
76
|
+
%[\n #{route[:name]}: #{route[:typeName]};]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Enroute
|
4
|
+
module Routes
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def call
|
8
|
+
grouped_routes.each_with_object([]) do |(_pattern, routes), buffer|
|
9
|
+
route = routes.find {|r| r.name.present? }
|
10
|
+
|
11
|
+
next unless route
|
12
|
+
|
13
|
+
buffer << build_payload(route)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def build_payload(route)
|
18
|
+
{
|
19
|
+
name: route.name.camelize(:lower),
|
20
|
+
typeName: "#{route.name.camelize}RouteHandler",
|
21
|
+
incomingPattern: camelize_pattern(route),
|
22
|
+
outgoingPattern: route.ast.to_s,
|
23
|
+
method: reduce_methods(routes),
|
24
|
+
segments: route.segments,
|
25
|
+
requiredSegments: route.path.required_names
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def camelize_pattern(route)
|
30
|
+
route
|
31
|
+
.ast
|
32
|
+
.to_s
|
33
|
+
.gsub(/_(.)/) { Regexp.last_match(1).upcase }
|
34
|
+
end
|
35
|
+
|
36
|
+
def reduce_methods(routes)
|
37
|
+
routes.map(&:verb).flatten.map(&:downcase).uniq
|
38
|
+
end
|
39
|
+
|
40
|
+
def camelize_map(list)
|
41
|
+
list.map {|item| item.camelize(:lower) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def grouped_routes
|
45
|
+
filtered_routes.group_by do |route|
|
46
|
+
route.ast.to_s
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def filtered_routes
|
51
|
+
routes.reject do |route|
|
52
|
+
route.name =~ /rails|script/
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def routes
|
57
|
+
Rails.application.routes.routes
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
/* eslint-disable */
|
2
|
+
// This file has been automatically generated.
|
3
|
+
// Don't edit it manually. Use `enroute export` instead.
|
4
|
+
// Last updated on <%= Time.now.utc %>
|
5
|
+
|
6
|
+
import { zipObject } from "lodash";
|
7
|
+
|
8
|
+
function camelize(text: string): string {
|
9
|
+
return text.replace(
|
10
|
+
/_(.)/g,
|
11
|
+
(_match, group) =>
|
12
|
+
`${group[0].toUpperCase()}${group.substr(1, -1).toLowerCase()}`,
|
13
|
+
);
|
14
|
+
}
|
15
|
+
|
16
|
+
function buildRoute(route: Route): RouteHandler {
|
17
|
+
const incomingPattern = route.incomingPattern.replace("(.:format)", "");
|
18
|
+
const outgoingPattern = route.outgoingPattern.replace("(.:format)", "");
|
19
|
+
|
20
|
+
const handler = generate({
|
21
|
+
...route,
|
22
|
+
segments: route.segments.map(camelize),
|
23
|
+
requiredSegments: route.requiredSegments.map(camelize),
|
24
|
+
pattern: incomingPattern,
|
25
|
+
}) as RouteHandler;
|
26
|
+
|
27
|
+
handler.pattern = incomingPattern;
|
28
|
+
handler.incomingPattern = incomingPattern;
|
29
|
+
handler.outgoingPattern = outgoingPattern;
|
30
|
+
handler.underscore = generate({ ...route, pattern: outgoingPattern });
|
31
|
+
|
32
|
+
return handler;
|
33
|
+
}
|
34
|
+
|
35
|
+
function generate(route: Route): RouteHelper {
|
36
|
+
return (...args: PrimitiveType[]): string => {
|
37
|
+
const { segments } = route;
|
38
|
+
const pattern = route.pattern as string;
|
39
|
+
const actualArgsSize = args.length;
|
40
|
+
const requiredArgsSize = route.requiredSegments.length;
|
41
|
+
const segmentsSize = segments.length;
|
42
|
+
const params = zipObject(route.segments, args);
|
43
|
+
|
44
|
+
if (actualArgsSize < requiredArgsSize) {
|
45
|
+
throw new Error(
|
46
|
+
`Expected ${requiredArgsSize} args; got ${actualArgsSize}`,
|
47
|
+
);
|
48
|
+
}
|
49
|
+
|
50
|
+
if (actualArgsSize > segmentsSize) {
|
51
|
+
throw new Error(
|
52
|
+
`Expected no more than ${segmentsSize} args; got ${actualArgsSize}`,
|
53
|
+
);
|
54
|
+
}
|
55
|
+
|
56
|
+
if (segmentsSize === 0) {
|
57
|
+
return pattern;
|
58
|
+
}
|
59
|
+
|
60
|
+
const ext = params.format ? `.${params.format}` : "";
|
61
|
+
const url = `${pattern}${ext}`;
|
62
|
+
|
63
|
+
return segments.reduce((buffer: string, segment: string) => {
|
64
|
+
const param = String(params[segment]);
|
65
|
+
const encodedParam = encodeURIComponent(param);
|
66
|
+
|
67
|
+
const optionalSegmentRegex = new RegExp(`\\(\\/:${segment}\\)`);
|
68
|
+
const requiredSegmentRegex = new RegExp(`\\(?:${segment}\\)?`);
|
69
|
+
|
70
|
+
if (buffer.match(optionalSegmentRegex)) {
|
71
|
+
const replacement = params[segment] ? `/${encodedParam}` : "";
|
72
|
+
return buffer.replace(optionalSegmentRegex, replacement);
|
73
|
+
}
|
74
|
+
|
75
|
+
const rejectParam =
|
76
|
+
param === "undefined" ||
|
77
|
+
param === "null" ||
|
78
|
+
param.trim() === "";
|
79
|
+
|
80
|
+
if (route.requiredSegments.includes(segment) && rejectParam) {
|
81
|
+
const serializedParam = JSON.stringify(params[segment]);
|
82
|
+
|
83
|
+
throw new Error(
|
84
|
+
`${segment} is required, but received ${serializedParam}.`
|
85
|
+
);
|
86
|
+
}
|
87
|
+
|
88
|
+
return buffer.replace(requiredSegmentRegex, encodedParam);
|
89
|
+
}, url);
|
90
|
+
};
|
91
|
+
}
|
92
|
+
|
93
|
+
export type PrimitiveType = number | string | null | undefined | boolean;
|
94
|
+
export type ArrayType = AnyObject[];
|
95
|
+
export type AnyObject = PrimitiveType | ArrayType | ObjectType | any;
|
96
|
+
|
97
|
+
export interface ObjectType {
|
98
|
+
[key: string]: PrimitiveType | ArrayType | ObjectType;
|
99
|
+
}
|
100
|
+
|
101
|
+
export interface Route {
|
102
|
+
name: string;
|
103
|
+
typeName: string;
|
104
|
+
pattern?: string;
|
105
|
+
method: string[];
|
106
|
+
segments: string[];
|
107
|
+
requiredSegments: string[];
|
108
|
+
incomingPattern: string;
|
109
|
+
outgoingPattern: string;
|
110
|
+
}
|
111
|
+
|
112
|
+
export type RouteHelper = (...args: PrimitiveType[]) => string;
|
113
|
+
|
114
|
+
export type RouteHandler = RouteHelper & {
|
115
|
+
pattern: string;
|
116
|
+
incomingPattern: string;
|
117
|
+
outgoingPattern: string;
|
118
|
+
underscore: RouteHelper;
|
119
|
+
};
|
120
|
+
|
121
|
+
<%= type_definitions %>
|
122
|
+
|
123
|
+
export const routes: Route[] = <%= JSON.pretty_generate(routes) %>;
|
124
|
+
|
125
|
+
export const routeHandlers: RouteHandler[] = [
|
126
|
+
<%= routes.each_with_index.map {|route, index| %[ buildRoute(routes[#{index}]) as #{route[:typeName]},] }.join("\n") %>
|
127
|
+
];
|
128
|
+
|
129
|
+
<%= route_functions %>
|
data/package.json
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
{
|
2
|
+
"private": true,
|
3
|
+
"devDependencies": {
|
4
|
+
"@fnando/codestyle": "*",
|
5
|
+
"@types/jest": "*",
|
6
|
+
"@types/lodash": "*",
|
7
|
+
"@typescript-eslint/eslint-plugin": "*",
|
8
|
+
"@typescript-eslint/parser": "*",
|
9
|
+
"eslint": "*",
|
10
|
+
"eslint-config-prettier": "*",
|
11
|
+
"eslint-plugin-prettier": "*",
|
12
|
+
"jest": "*",
|
13
|
+
"lodash": "*",
|
14
|
+
"prettier": "*",
|
15
|
+
"ts-jest": "*",
|
16
|
+
"typescript": "*"
|
17
|
+
}
|
18
|
+
}
|
data/tsconfig.json
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"allowJs": true,
|
4
|
+
"forceConsistentCasingInFileNames": true,
|
5
|
+
"newLine": "lf",
|
6
|
+
"declaration": false,
|
7
|
+
"allowSyntheticDefaultImports": true,
|
8
|
+
"emitDecoratorMetadata": true,
|
9
|
+
"experimentalDecorators": true,
|
10
|
+
"outDir": "test/enroute/dist/",
|
11
|
+
"lib": ["es7", "dom"],
|
12
|
+
"module": "es6",
|
13
|
+
"moduleResolution": "node",
|
14
|
+
"paths": {
|
15
|
+
"*": ["node_modules/*"]
|
16
|
+
},
|
17
|
+
"baseUrl": "test/enroute/",
|
18
|
+
"sourceMap": true,
|
19
|
+
"target": "es6"
|
20
|
+
},
|
21
|
+
"include": ["test/enroute/**/*.ts"],
|
22
|
+
"exclude": ["node_modules"],
|
23
|
+
"compileOnSave": false
|
24
|
+
}
|
metadata
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enroute
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nando Vieira
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest-utils
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-fnando
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Export Rails routes to TypeScript definitions
|
112
|
+
email:
|
113
|
+
- me@fnando.com
|
114
|
+
executables:
|
115
|
+
- enroute
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".eslintrc.json"
|
120
|
+
- ".gitignore"
|
121
|
+
- ".prettierrc.js"
|
122
|
+
- ".rubocop.yml"
|
123
|
+
- ".tool-versions"
|
124
|
+
- ".travis.yml"
|
125
|
+
- CODE_OF_CONDUCT.md
|
126
|
+
- Gemfile
|
127
|
+
- LICENSE.txt
|
128
|
+
- README.md
|
129
|
+
- Rakefile
|
130
|
+
- enroute.gemspec
|
131
|
+
- exe/enroute
|
132
|
+
- jest.config.js
|
133
|
+
- lib/enroute.rb
|
134
|
+
- lib/enroute/cli.rb
|
135
|
+
- lib/enroute/export.rb
|
136
|
+
- lib/enroute/routes.rb
|
137
|
+
- lib/enroute/template.ts.erb
|
138
|
+
- lib/enroute/version.rb
|
139
|
+
- package.json
|
140
|
+
- tsconfig.json
|
141
|
+
- yarn.lock
|
142
|
+
homepage: https://github.com/fnando/enroute
|
143
|
+
licenses:
|
144
|
+
- MIT
|
145
|
+
metadata:
|
146
|
+
homepage_uri: https://github.com/fnando/enroute
|
147
|
+
source_code_uri: https://github.com/fnando/enroute
|
148
|
+
changelog_uri: https://github.com/fnando/enroute
|
149
|
+
post_install_message:
|
150
|
+
rdoc_options: []
|
151
|
+
require_paths:
|
152
|
+
- lib
|
153
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
requirements: []
|
164
|
+
rubygems_version: 3.1.2
|
165
|
+
signing_key:
|
166
|
+
specification_version: 4
|
167
|
+
summary: Export Rails routes to TypeScript definitions
|
168
|
+
test_files: []
|