js-routes 2.2.9 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -3
  3. data/Readme.md +16 -15
  4. data/lib/js_routes/configuration.rb +7 -5
  5. data/lib/js_routes/generators/base.rb +6 -1
  6. data/lib/js_routes/generators/middleware.rb +2 -3
  7. data/lib/js_routes/generators/webpacker.rb +2 -1
  8. data/lib/js_routes/instance.rb +21 -6
  9. data/lib/js_routes/middleware.rb +5 -4
  10. data/lib/js_routes/route.rb +7 -3
  11. data/lib/js_routes/types.rb +10 -0
  12. data/lib/js_routes/utils.rb +18 -0
  13. data/lib/js_routes/version.rb +1 -1
  14. data/lib/js_routes.rb +12 -4
  15. data/lib/routes.d.ts +7 -6
  16. data/lib/routes.js +19 -18
  17. data/lib/routes.ts +33 -27
  18. data/lib/tasks/js_routes.rake +4 -2
  19. metadata +12 -145
  20. data/.document +0 -5
  21. data/.eslintrc.js +0 -15
  22. data/.github/workflows/ci.yml +0 -36
  23. data/.gitignore +0 -65
  24. data/.nvmrc +0 -1
  25. data/.rspec +0 -1
  26. data/Appraisals +0 -17
  27. data/Gemfile +0 -9
  28. data/Rakefile +0 -37
  29. data/VERSION_2_UPGRADE.md +0 -66
  30. data/bin/tapioca +0 -27
  31. data/gemfiles/rails50_sprockets_3.gemfile +0 -8
  32. data/gemfiles/rails51_sprockets_3.gemfile +0 -8
  33. data/gemfiles/rails52_sprockets_3.gemfile +0 -8
  34. data/gemfiles/rails70_sprockets_4.gemfile +0 -8
  35. data/js-routes.gemspec +0 -42
  36. data/package.json +0 -38
  37. data/sorbet/config +0 -4
  38. data/sorbet/rbi/annotations/.gitattributes +0 -1
  39. data/sorbet/rbi/annotations/actionpack.rbi +0 -428
  40. data/sorbet/rbi/annotations/actionview.rbi +0 -75
  41. data/sorbet/rbi/annotations/activesupport.rbi +0 -421
  42. data/sorbet/rbi/annotations/railties.rbi +0 -61
  43. data/sorbet/rbi/gems/.gitattributes +0 -1
  44. data/sorbet/rbi/gems/actionpack@7.0.4.1.rbi +0 -303
  45. data/sorbet/rbi/gems/actionview@7.0.4.1.rbi +0 -8
  46. data/sorbet/rbi/gems/activesupport@7.0.4.1.rbi +0 -16424
  47. data/sorbet/rbi/gems/appraisal@2.4.1.rbi +0 -584
  48. data/sorbet/rbi/gems/builder@3.2.4.rbi +0 -8
  49. data/sorbet/rbi/gems/bump@0.10.0.rbi +0 -168
  50. data/sorbet/rbi/gems/byebug@11.1.3.rbi +0 -3606
  51. data/sorbet/rbi/gems/coderay@1.1.3.rbi +0 -3426
  52. data/sorbet/rbi/gems/concurrent-ruby@1.2.0.rbi +0 -11570
  53. data/sorbet/rbi/gems/crass@1.0.6.rbi +0 -8
  54. data/sorbet/rbi/gems/diff-lcs@1.5.0.rbi +0 -1083
  55. data/sorbet/rbi/gems/erubi@1.12.0.rbi +0 -145
  56. data/sorbet/rbi/gems/i18n@1.12.0.rbi +0 -2296
  57. data/sorbet/rbi/gems/libv8-node@16.10.0.0.rbi +0 -8
  58. data/sorbet/rbi/gems/loofah@2.19.1.rbi +0 -8
  59. data/sorbet/rbi/gems/method_source@1.0.0.rbi +0 -272
  60. data/sorbet/rbi/gems/mini_racer@0.6.3.rbi +0 -224
  61. data/sorbet/rbi/gems/minitest@5.17.0.rbi +0 -1457
  62. data/sorbet/rbi/gems/netrc@0.11.0.rbi +0 -158
  63. data/sorbet/rbi/gems/nokogiri@1.14.0.rbi +0 -8
  64. data/sorbet/rbi/gems/parallel@1.24.0.rbi +0 -280
  65. data/sorbet/rbi/gems/prettier_print@1.2.1.rbi +0 -951
  66. data/sorbet/rbi/gems/prism@0.24.0.rbi +0 -29744
  67. data/sorbet/rbi/gems/pry-byebug@3.10.1.rbi +0 -1150
  68. data/sorbet/rbi/gems/pry@0.14.2.rbi +0 -10075
  69. data/sorbet/rbi/gems/racc@1.6.2.rbi +0 -150
  70. data/sorbet/rbi/gems/rack-test@2.0.2.rbi +0 -8
  71. data/sorbet/rbi/gems/rack@2.2.6.2.rbi +0 -5585
  72. data/sorbet/rbi/gems/rails-dom-testing@2.0.3.rbi +0 -8
  73. data/sorbet/rbi/gems/rails-html-sanitizer@1.5.0.rbi +0 -8
  74. data/sorbet/rbi/gems/railties@7.0.4.1.rbi +0 -1959
  75. data/sorbet/rbi/gems/rake@13.0.6.rbi +0 -3072
  76. data/sorbet/rbi/gems/rbi@0.1.9.rbi +0 -3006
  77. data/sorbet/rbi/gems/rspec-core@3.12.0.rbi +0 -10868
  78. data/sorbet/rbi/gems/rspec-expectations@3.12.2.rbi +0 -8100
  79. data/sorbet/rbi/gems/rspec-mocks@3.12.3.rbi +0 -5299
  80. data/sorbet/rbi/gems/rspec-support@3.12.0.rbi +0 -1611
  81. data/sorbet/rbi/gems/rspec@3.12.0.rbi +0 -82
  82. data/sorbet/rbi/gems/spoom@1.2.4.rbi +0 -3777
  83. data/sorbet/rbi/gems/sprockets-rails@3.4.2.rbi +0 -14
  84. data/sorbet/rbi/gems/sprockets@4.2.0.rbi +0 -8
  85. data/sorbet/rbi/gems/syntax_tree@6.2.0.rbi +0 -23136
  86. data/sorbet/rbi/gems/tapioca@0.12.0.rbi +0 -3510
  87. data/sorbet/rbi/gems/thor@1.2.1.rbi +0 -3956
  88. data/sorbet/rbi/gems/tzinfo@2.0.5.rbi +0 -5914
  89. data/sorbet/rbi/gems/yard-sorbet@0.8.1.rbi +0 -428
  90. data/sorbet/rbi/gems/yard@0.9.34.rbi +0 -18332
  91. data/sorbet/rbi/gems/zeitwerk@2.6.8.rbi +0 -8
  92. data/sorbet/rbi/todo.rbi +0 -31
  93. data/sorbet/tapioca/config.yml +0 -13
  94. data/sorbet/tapioca/require.rb +0 -4
  95. data/spec/dummy/app/assets/config/manifest.js +0 -2
  96. data/spec/dummy/app/assets/javascripts/.gitkeep +0 -0
  97. data/spec/dummy/config/routes.rb +0 -55
  98. data/spec/js_routes/default_serializer_spec.rb +0 -31
  99. data/spec/js_routes/module_types/amd_spec.rb +0 -35
  100. data/spec/js_routes/module_types/cjs_spec.rb +0 -15
  101. data/spec/js_routes/module_types/dts/routes.spec.d.ts +0 -115
  102. data/spec/js_routes/module_types/dts/test.spec.ts +0 -56
  103. data/spec/js_routes/module_types/dts_spec.rb +0 -126
  104. data/spec/js_routes/module_types/esm_spec.rb +0 -45
  105. data/spec/js_routes/module_types/nil_spec.rb +0 -87
  106. data/spec/js_routes/module_types/umd_spec.rb +0 -85
  107. data/spec/js_routes/options_spec.rb +0 -508
  108. data/spec/js_routes/rails_routes_compatibility_spec.rb +0 -473
  109. data/spec/js_routes/route_specification_spec.rb +0 -37
  110. data/spec/js_routes/zzz_sprockets_spec.rb +0 -152
  111. data/spec/spec_helper.rb +0 -135
  112. data/spec/support/routes.rb +0 -81
  113. data/spec/tsconfig.json +0 -4
  114. data/tsconfig.json +0 -28
  115. data/yarn.lock +0 -2457
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d890e17e32f816311de89a16506fca4b8ff905ea773c7a8d198343e0270eb50a
4
- data.tar.gz: 934c79a8b93e7129778e07789f93ac90d4183bd39ea578551f4ea9ca6cab22d2
3
+ metadata.gz: 8030ebf1b79303bfad35b2003ced97e6ec398b8bab459bf0f867006c9bc5d763
4
+ data.tar.gz: 4d84acaa15dacc791f26e059cb620d5c08d90891103fa5d73750d4bae3973e8b
5
5
  SHA512:
6
- metadata.gz: 6adcfbfefbbf13c8f46198f9bf2ca47efdac35aa8d28a8b17be7b8af65d321a3ba0da30dd26e40c4a329c359f1a02b865adb74a1d5e3d024b61bae7fd6f8d9e0
7
- data.tar.gz: 9cc86f32f79975aace5a264ef94d7bbe1afb41a7a141c738969c762479789fa74b1fb752139320f8ddd7c25b2eb164fe14cbcc3c6eb1a40046549538c3a6eb33
6
+ metadata.gz: 24e4500fcf1f799633343366f84d619060cd57979edc25a9f83ec17baf6a505e3fbbd838f66b6ec70618bf7d991a0f0614a3c6ad02ce85a90931e4a047f168a6
7
+ data.tar.gz: 7cab5ccd03712368ea32d43604381b32491ada2a933bf656b6efa0002132e638653f250e5a5037487e36c6ecfed01454d6df359e2e308b5fc7cd237c7af2fca2
data/CHANGELOG.md CHANGED
@@ -1,15 +1,27 @@
1
1
  # Changelog
2
2
 
3
- ## master
3
+ ## v2.3.0
4
+
5
+ * Drop support of Rails 4.x
6
+ * Fix support of shakapacker [#321](https://github.com/railsware/js-routes/issue/321).
7
+ * Fix support for Rails 8 [#319](https://github.com/railsware/js-routes/issue/319)
8
+ * Deprecated `rake js:routes:typescript`.
9
+ `rake js:routes` now automatically detects if types support can be used on not.
4
10
 
5
11
  ## v2.2.10
6
12
 
7
- * Fix middleware error for non-modern setup.
13
+ * Remove sorbet files from repo
14
+ * Clearly define files included in gem
15
+ * Fix Middleware and Middleware generator bugs [#316](https://github.com/railsware/js-routes/issue/316)
16
+ * Remove empty object linter warning on DTS module
17
+ * Generators: Add `.ts` extension when searching for main JS file of the project
8
18
 
9
19
  ## v2.2.9
10
20
 
21
+ * Fix middleware error for non-modern setup.
11
22
  * Added [Sorbet](https://sorbet.org/) method signatures.
12
- * Always use DTS module type when calling JsRoutes.definitions or .definitions!. [#313](https://github.com/railsware/js-routes/issues/313)
23
+ * Always use DTS module type when calling JsRoutes.definitions or .definitions!.
24
+ [#313](https://github.com/railsware/js-routes/issues/313)
13
25
 
14
26
  ## v2.2.8
15
27
 
data/Readme.md CHANGED
@@ -2,7 +2,16 @@
2
2
 
3
3
  [![CI](https://github.com/railsware/js-routes/actions/workflows/ci.yml/badge.svg)](https://github.com/railsware/js-routes/actions/workflows/ci.yml)
4
4
 
5
- Generates javascript file that defines all Rails named routes as javascript helpers
5
+ Generates javascript file that defines all Rails named routes as javascript helpers:
6
+
7
+ ``` js
8
+ import { root_path, api_user_path } from './routes';
9
+
10
+ root_path() # => /
11
+ api_user_path(25, include_profile: true, format: 'json') // => /api/users/25.json?include_profile=true
12
+ ```
13
+
14
+ [More Examples](#usage)
6
15
 
7
16
  ## Intallation
8
17
 
@@ -348,7 +357,7 @@ Configuration above will create a nice javascript file with `Routes` object that
348
357
  ``` js
349
358
  import {
350
359
  user_path, user_project_path, company_path
351
- } as Routes from 'routes';
360
+ } from 'routes';
352
361
 
353
362
  users_path()
354
363
  // => "/users"
@@ -459,15 +468,13 @@ console.log(routes.inbox_path); // OK, only `inbox_path` is included in the bund
459
468
  console.log(Object.keys(routes)); // forces bundler to include all exports, breaking tree shaking
460
469
  ```
461
470
 
462
- ### Exclude option
471
+ ### Exclude/Include options
463
472
 
464
473
  Split your routes into multiple files related to each section of your website like:
465
474
 
466
- ``` javascript
467
- // admin-routes.js.erb
468
- <%= JsRoutes.generate(include: /^admin_/) %>
469
- // app-routes.js.erb
470
- <%= JsRoutes.generate(exclude: /^admin_/) %>
475
+ ``` ruby
476
+ JsRoutes.generate!('app/javascript/admin-routes.js', include: /^admin_/) %>
477
+ JsRoutes.generate!('app/javascript/app-routes.js', exclude: /^admin_/) %>
471
478
  ```
472
479
 
473
480
  ## Advantages over alternatives
@@ -475,17 +482,11 @@ Split your routes into multiple files related to each section of your website li
475
482
  There are some alternatives available. Most of them has only basic feature and don't reach the level of quality I accept.
476
483
  Advantages of this one are:
477
484
 
478
- * Rails 4,5,6 support
485
+ * Actively maintained
479
486
  * [ESM Tree shaking](https://webpack.js.org/guides/tree-shaking/) support
480
487
  * Rich options set
481
488
  * Full rails compatibility
482
489
  * Support Rails `#to_param` convention for seo optimized paths
483
490
  * Well tested
484
491
 
485
- #### Thanks to [contributors](https://github.com/railsware/js-routes/contributors)
486
-
487
- #### Have fun
488
-
489
492
 
490
- ## License
491
- [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Frailsware%2Fjs-routes.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Frailsware%2Fjs-routes?ref=badge_large)
@@ -1,6 +1,8 @@
1
1
  # typed: strict
2
+
2
3
  require "pathname"
3
4
  require "js_routes/types"
5
+ require 'js_routes/utils'
4
6
 
5
7
  module JsRoutes
6
8
  class Configuration
@@ -114,14 +116,14 @@ module JsRoutes
114
116
 
115
117
  sig { returns(Pathname) }
116
118
  def output_file
117
- webpacker_dir = defined?(::Webpacker) ?
118
- T.unsafe(::Webpacker).config.source_path :
119
- pathname('app', 'javascript')
119
+ shakapacker = JsRoutes::Utils.shakapacker
120
+ shakapacker_dir = shakapacker ?
121
+ shakapacker.config.source_path : pathname('app', 'javascript')
120
122
  sprockets_dir = pathname('app','assets','javascripts')
121
123
  file_name = file || default_file_name
122
124
  sprockets_file = sprockets_dir.join(file_name)
123
- webpacker_file = webpacker_dir.join(file_name)
124
- !Dir.exist?(webpacker_dir) && defined?(::Sprockets) ? sprockets_file : webpacker_file
125
+ webpacker_file = shakapacker_dir.join(file_name)
126
+ !Dir.exist?(shakapacker_dir) && defined?(::Sprockets) ? sprockets_file : webpacker_file
125
127
  end
126
128
 
127
129
  protected
@@ -3,13 +3,18 @@ require "rails/generators"
3
3
 
4
4
  class JsRoutes::Generators::Base < Rails::Generators::Base
5
5
 
6
- source_root File.expand_path(__FILE__ + "/../../../templates")
6
+ def self.inherited(subclass)
7
+ super
8
+ subclass.source_root(File.expand_path(__FILE__ + "/../../../templates"))
9
+ end
7
10
 
8
11
  protected
9
12
 
10
13
  def application_js_path
11
14
  [
15
+ "app/javascript/packs/application.ts",
12
16
  "app/javascript/packs/application.js",
17
+ "app/javascript/controllers/application.ts",
13
18
  "app/javascript/controllers/application.js",
14
19
  ].find do |path|
15
20
  File.exist?(Rails.root.join(path))
@@ -10,8 +10,7 @@ class JsRoutes::Generators::Middleware < JsRoutes::Generators::Base
10
10
  if path = application_js_path
11
11
  inject_into_file path, pack_content
12
12
  end
13
- JsRoutes.generate!
14
- JsRoutes.definitions!
13
+ JsRoutes.generate!(typed: true)
15
14
  end
16
15
 
17
16
  protected
@@ -36,7 +35,7 @@ alert(`JsRoutes installed.\\nYour root path is ${root_path()}`)
36
35
  enhanced_task = depends_on_js_bundling? ? "javascript:build" : "assets:precompile"
37
36
  <<-RB
38
37
  # Update js-routes file before javascript build
39
- task "#{enhanced_task}" => "js:routes:typescript"
38
+ task "#{enhanced_task}" => "js:routes"
40
39
  RB
41
40
  end
42
41
 
@@ -1,11 +1,12 @@
1
1
  require "rails/generators"
2
+ require 'js_routes/utils'
2
3
 
3
4
  class JsRoutes::Generators::Webpacker < Rails::Generators::Base
4
5
 
5
6
  def create_webpack
6
7
  copy_file "initializer.rb", "config/initializers/js_routes.rb"
7
8
  copy_file "erb.js", "config/webpack/loaders/erb.js"
8
- copy_file "routes.js.erb", "#{Webpacker.config.source_path}/routes.js.erb"
9
+ copy_file "routes.js.erb", "#{JsRoutes::Utils.shakapacker.config.source_path}/routes.js.erb"
9
10
  inject_into_file "config/webpack/environment.js", loader_content
10
11
  if path = application_js_path
11
12
  inject_into_file path, pack_content
@@ -2,6 +2,7 @@
2
2
  require "js_routes/configuration"
3
3
  require "js_routes/route"
4
4
  require "js_routes/types"
5
+ require 'fileutils'
5
6
 
6
7
  module JsRoutes
7
8
  class Instance # :nodoc:
@@ -23,9 +24,16 @@ module JsRoutes
23
24
  sig {returns(String)}
24
25
  def generate
25
26
  # Ensure routes are loaded. If they're not, load them.
27
+
26
28
  application = T.unsafe(self.application)
27
- if named_routes.empty? && application.respond_to?(:reload_routes!, true)
28
- application.reload_routes!
29
+ if named_routes.empty?
30
+ if application.is_a?(Rails::Application)
31
+ if Rails.version >= "8.0.0"
32
+ application.reload_routes_unless_loaded
33
+ else
34
+ application.reload_routes!
35
+ end
36
+ end
29
37
  end
30
38
  content = File.read(@configuration.source_file)
31
39
 
@@ -57,6 +65,13 @@ module JsRoutes
57
65
  end
58
66
  end
59
67
 
68
+ sig { void }
69
+ def remove!
70
+ path = Rails.root.join(@configuration.output_file)
71
+ FileUtils.rm_rf(path)
72
+ FileUtils.rm_rf(path.sub(%r{\.js\z}, '.d.ts'))
73
+ end
74
+
60
75
  protected
61
76
 
62
77
  sig { returns(T::Hash[String, String]) }
@@ -170,8 +185,8 @@ export {};
170
185
  sig { params(route: JourneyRoute).returns(T::Array[StringArray]) }
171
186
  def mounted_app_routes(route)
172
187
  rails_engine_app = T.unsafe(app_from_route(route))
173
- if rails_engine_app.respond_to?(:superclass) &&
174
- rails_engine_app.superclass == Rails::Engine && !route.path.anchored
188
+ if rails_engine_app.is_a?(Class) &&
189
+ rails_engine_app < Rails::Engine && !route.path.anchored
175
190
  rails_engine_app.routes.named_routes.flat_map do |_, engine_route|
176
191
  route_helpers_if_match(engine_route, route)
177
192
  end
@@ -183,9 +198,9 @@ export {};
183
198
  sig { params(route: JourneyRoute).returns(T.untyped) }
184
199
  def app_from_route(route)
185
200
  app = route.app
186
- # rails engine in Rails 4.2 use additional
201
+ # Rails Engine can use additional
187
202
  # ActionDispatch::Routing::Mapper::Constraints, which contain app
188
- if app.respond_to?(:app) && app.respond_to?(:constraints)
203
+ if app.is_a?(ActionDispatch::Routing::Mapper::Constraints)
189
204
  app.app
190
205
  else
191
206
  app
@@ -9,16 +9,18 @@ module JsRoutes
9
9
  # https://github.com/fnando/i18n-js/blob/main/lib/i18n/js/middleware.rb
10
10
  class Middleware
11
11
  include JsRoutes::Types
12
+ include RackApp
13
+
12
14
  extend T::Sig
13
15
 
14
- sig { params(app: T.proc.params(a0: StringHash).returns(UntypedArray)).void }
16
+ sig { params(app: T.untyped).void }
15
17
  def initialize(app)
16
18
  @app = app
17
19
  @routes_file = T.let(Rails.root.join("config/routes.rb"), Pathname)
18
20
  @mtime = T.let(nil, T.nilable(Time))
19
21
  end
20
22
 
21
- sig { params(env: StringHash).returns(UntypedArray) }
23
+ sig { override.params(env: StringHash).returns(UntypedArray) }
22
24
  def call(env)
23
25
  update_js_routes
24
26
  @app.call(env)
@@ -37,8 +39,7 @@ module JsRoutes
37
39
 
38
40
  sig { void }
39
41
  def regenerate
40
- JsRoutes.generate!
41
- JsRoutes.definitions! if JsRoutes.configuration.modern?
42
+ JsRoutes.generate!(typed: true)
42
43
  end
43
44
 
44
45
  sig { returns(T.nilable(Time)) }
@@ -65,13 +65,15 @@ module JsRoutes
65
65
 
66
66
  sig { returns(String) }
67
67
  def definition_body
68
+ options_type = optional_parts_type ? "#{optional_parts_type} & RouteOptions" : "RouteOptions"
68
69
  args = required_parts.map{|p| "#{apply_case(p)}: RequiredRouteParameter"}
69
- args << "options?: #{optional_parts_type} & RouteOptions"
70
+ args << "options?: #{options_type}"
70
71
  "((\n#{args.join(",\n").indent(2)}\n) => string) & RouteHelperExtras"
71
72
  end
72
73
 
73
- sig { returns(String) }
74
+ sig { returns(T.nilable(String)) }
74
75
  def optional_parts_type
76
+ return nil if optional_parts.empty?
75
77
  @optional_parts_type ||= T.let(
76
78
  "{" + optional_parts.map {|p| "#{p}?: OptionalRouteParameter"}.join(', ') + "}",
77
79
  T.nilable(String)
@@ -192,7 +194,9 @@ JS
192
194
  sig { params(spec: SpecNode, parent_spec: T.nilable(RouteSpec)).returns(T.nilable(T.any(UntypedArray, String))) }
193
195
  def serialize(spec, parent_spec=nil)
194
196
  return nil unless spec
195
- # Rails 4 globbing requires * removal
197
+ # Removing special character prefix from route variable name
198
+ # * for globbing
199
+ # : for common parameter
196
200
  return spec.tr(':*', '') if spec.is_a?(String)
197
201
 
198
202
  result = serialize_spec(spec, parent_spec)
@@ -24,5 +24,15 @@ module JsRoutes
24
24
  Prefix = T.type_alias do
25
25
  T.any(T.proc.returns(String), String, NilClass)
26
26
  end
27
+
28
+ module RackApp
29
+ extend T::Sig
30
+ extend T::Helpers
31
+
32
+ interface!
33
+
34
+ sig { abstract.params(input: StringHash).returns(UntypedArray) }
35
+ def call(input); end
36
+ end
27
37
  end
28
38
  end
@@ -0,0 +1,18 @@
1
+ # typed: strict
2
+
3
+ module JsRoutes
4
+ module Utils
5
+ extend T::Sig
6
+ sig {returns(T.untyped)}
7
+ def self.shakapacker
8
+ if defined?(::Shakapacker)
9
+ ::Shakapacker
10
+ elsif defined?(::Webpacker)
11
+ ::Webpacker
12
+ else
13
+ nil
14
+ end
15
+ end
16
+ end
17
+
18
+ end
@@ -1,4 +1,4 @@
1
1
  # typed: strict
2
2
  module JsRoutes
3
- VERSION = "2.2.9"
3
+ VERSION = "2.3.0"
4
4
  end
data/lib/js_routes.rb CHANGED
@@ -34,19 +34,27 @@ module JsRoutes
34
34
  Instance.new(**opts).generate
35
35
  end
36
36
 
37
- sig { params(file_name: FileName, opts: T.untyped).void }
38
- def generate!(file_name = configuration.file, **opts)
37
+ sig { params(file_name: FileName, typed: T::Boolean, opts: T.untyped).void }
38
+ def generate!(file_name = configuration.file, typed: false, **opts)
39
39
  Instance.new(file: file_name, **opts).generate!
40
+ definitions!(file_name, **opts) if typed
41
+ end
42
+
43
+ sig { params(file_name: FileName, opts: T.untyped).void }
44
+ def remove!(file_name = configuration.file, **opts)
45
+ Instance.new(file: file_name, **opts).remove!
40
46
  end
41
47
 
42
48
  sig { params(opts: T.untyped).returns(String) }
43
49
  def definitions(**opts)
44
- generate(**opts, module_type: 'DTS',)
50
+ generate(**opts, module_type: 'DTS')
45
51
  end
46
52
 
47
53
  sig { params(file_name: FileName, opts: T.untyped).void }
48
54
  def definitions!(file_name = nil, **opts)
49
- file_name ||= configuration.file&.sub(%r{(\.d)?\.(j|t)s\Z}, ".d.ts")
55
+ file_name ||= configuration.file
56
+
57
+ file_name = file_name&.sub(%r{(\.d)?\.(j|t)s\Z}, ".d.ts")
50
58
  generate!(file_name, **opts, module_type: 'DTS')
51
59
  end
52
60
 
data/lib/routes.d.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  declare type Optional<T> = {
6
6
  [P in keyof T]?: T[P] | null;
7
7
  };
8
+ declare type Collection<T> = Record<string, T>;
8
9
  declare type BaseRouteParameter = string | boolean | Date | number;
9
10
  declare type MethodRouteParameter = BaseRouteParameter | (() => BaseRouteParameter);
10
11
  declare type ModelRouteParameter = {
@@ -19,8 +20,8 @@ declare type OptionalRouteParameter = undefined | null | RequiredRouteParameter;
19
20
  declare type QueryRouteParameter = OptionalRouteParameter | QueryRouteParameter[] | {
20
21
  [k: string]: QueryRouteParameter;
21
22
  };
22
- declare type RouteParameters = Record<string, QueryRouteParameter>;
23
- declare type Serializable = Record<string, unknown>;
23
+ declare type RouteParameters = Collection<QueryRouteParameter>;
24
+ declare type Serializable = Collection<unknown>;
24
25
  declare type Serializer = (value: Serializable) => string;
25
26
  declare type RouteHelperExtras = {
26
27
  requiredParams(): string[];
@@ -32,9 +33,9 @@ declare type RequiredParameters<T extends number> = T extends 1 ? [RequiredRoute
32
33
  RequiredRouteParameter,
33
34
  RequiredRouteParameter
34
35
  ] : RequiredRouteParameter[];
35
- declare type RouteHelperOptions = RouteOptions & Record<string, OptionalRouteParameter>;
36
+ declare type RouteHelperOptions = RouteOptions & Collection<OptionalRouteParameter>;
36
37
  declare type RouteHelper<T extends number = number> = ((...args: [...RequiredParameters<T>, RouteHelperOptions]) => string) & RouteHelperExtras;
37
- declare type RouteHelpers = Record<string, RouteHelper>;
38
+ declare type RouteHelpers = Collection<RouteHelper>;
38
39
  declare type Configuration = {
39
40
  prefix: string;
40
41
  default_url_options: RouteParameters;
@@ -56,7 +57,7 @@ declare type KeywordUrlOptions = Optional<{
56
57
  params: RouteParameters;
57
58
  }>;
58
59
  declare type RouteOptions = KeywordUrlOptions & RouteParameters;
59
- declare type PartsTable = Record<string, {
60
+ declare type PartsTable = Collection<{
60
61
  r?: boolean;
61
62
  d?: OptionalRouteParameter;
62
63
  }>;
@@ -76,5 +77,5 @@ declare const define: undefined | (((arg: unknown[], callback: () => unknown) =>
76
77
  amd?: unknown;
77
78
  });
78
79
  declare const module: {
79
- exports: any;
80
+ exports: unknown;
80
81
  } | undefined;
data/lib/routes.js CHANGED
@@ -30,8 +30,9 @@ RubyVariables.WRAPPER(
30
30
  const ModuleReferences = {
31
31
  CJS: {
32
32
  define(routes) {
33
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
34
- module.exports = routes;
33
+ if (module) {
34
+ module.exports = routes;
35
+ }
35
36
  },
36
37
  isSupported() {
37
38
  return typeof module === "object";
@@ -39,10 +40,11 @@ RubyVariables.WRAPPER(
39
40
  },
40
41
  AMD: {
41
42
  define(routes) {
42
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
43
- define([], function () {
44
- return routes;
45
- });
43
+ if (define) {
44
+ define([], function () {
45
+ return routes;
46
+ });
47
+ }
46
48
  },
47
49
  isSupported() {
48
50
  return typeof define === "function" && !!define.amd;
@@ -290,9 +292,9 @@ RubyVariables.WRAPPER(
290
292
  if (url_params.length) {
291
293
  result += "?" + url_params;
292
294
  }
293
- result += keyword_parameters.anchor
294
- ? "#" + keyword_parameters.anchor
295
- : "";
295
+ if (keyword_parameters.anchor) {
296
+ result += "#" + keyword_parameters.anchor;
297
+ }
296
298
  if (absolute) {
297
299
  result = this.route_url(keyword_parameters) + result;
298
300
  }
@@ -502,28 +504,27 @@ RubyVariables.WRAPPER(
502
504
  define_module(name, module) {
503
505
  this.ensure_module_supported(name);
504
506
  ModuleReferences[name].define(module);
507
+ return module;
505
508
  }
506
509
  }
507
- const Utils = new UtilsClass();
510
+ const utils = new UtilsClass();
508
511
  // We want this helper name to be short
509
512
  const __jsr = {
510
513
  r(parts_table, route_spec, absolute) {
511
- return Utils.route(parts_table, route_spec, absolute);
514
+ return utils.route(parts_table, route_spec, absolute);
512
515
  },
513
516
  };
514
- const result = {
517
+ return utils.define_module(RubyVariables.MODULE_TYPE, {
515
518
  ...__jsr,
516
519
  configure: (config) => {
517
- return Utils.configure(config);
520
+ return utils.configure(config);
518
521
  },
519
522
  config: () => {
520
- return Utils.config();
523
+ return utils.config();
521
524
  },
522
525
  serialize: (object) => {
523
- return Utils.serialize(object);
526
+ return utils.serialize(object);
524
527
  },
525
528
  ...RubyVariables.ROUTES_OBJECT,
526
- };
527
- Utils.define_module(RubyVariables.MODULE_TYPE, result);
528
- return result;
529
+ });
529
530
  })();