js-routes 2.3.1 → 2.3.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97421ede18a16a1cb38ede32268ea60e79dc3aeee2dbe419cbbc4f117848cebb
4
- data.tar.gz: d65a56e1026ef6551a7e0c30f90278245dfed8b4ceca460c285b16855586709c
3
+ metadata.gz: 771e5ddecccbde6b5b78778d456fe9602a4ae4b2aaa89e821cd6bda24be95565
4
+ data.tar.gz: b0ac96fe4273cc21fa616614429e270a393c7bcd11f15b5b79e1b14e56ec579f
5
5
  SHA512:
6
- metadata.gz: 13c8299991c8821aff0aa09f4b90ce3a38a9881b780688a6aabadf4871ab79948a095feb0fc9c76b4cc51c84f00aa758f5276838234e963efa1bb641db526984
7
- data.tar.gz: 651ae1108a3845a1b948c0b9a1bb9475a2374d6178bd0cf35a7da2dca990081b2bfd40fb2beb92301a4efa3cdef143e83e04a7c39afb7394a685383d9a6b8a91
6
+ metadata.gz: 150bccfe350126ca0db67d03a8a7bbc22884edd63ef2e86c1cecd4ac2d0d72b941d7f1133cadbe64d19f99332be22db9891ca42f2f4ccfbec7e77447b741fb71
7
+ data.tar.gz: 2a51b7337560e055a21ff96be1b6f89d40d88bc6b541c56129a5f99823436a7c31027cc6e029f6b33b00a42805cd7986d805a30fc8a8ce5cc4e9bd5be20e2757
data/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## v2.3.2
4
+
5
+ * Add `banner` option that allow to control JSDoc on top of generated file. [#324](https://github.com/bogdan/repo/issues/324).
6
+
7
+ ``` ruby
8
+ JsRoutes.configure do |c|
9
+ c.banner = -> {
10
+ commit_hash = `git rev-parse --short HEAD`.strip
11
+
12
+ <<~DOC
13
+ @file Javascript Route helpers of my magic pony app.
14
+ @author Bogdan Gusiev
15
+ @license MIT
16
+ @version #{commit_hash}
17
+ DOC
18
+ }
19
+ end
20
+ ```
21
+
3
22
  ## v2.3.1
4
23
 
5
24
  * Add timestamp on when routes.js was generated into banner.
data/Readme.md CHANGED
@@ -331,6 +331,19 @@ Options to configure JavaScript file generator. These options are only available
331
331
  * `optional_definition_params` - make all route paramters in definition optional
332
332
  * See [related compatibility issue](#optional-definition-params)
333
333
  * Default: `false`
334
+ * `banner` - specify a JSDoc comment on top of the file.
335
+ * It is not stripped by minifiers by default and helps to originate the content when debugging the build.
336
+ * You may want to control how much information from backend is exposed to potential attacker at the cost of your own comfort.
337
+ * See [JSDoc Guide](https://github.com/shri/JSDoc-Style-Guide/blob/master/README.md#files)
338
+ * Supports a lazy generation with `Proc`.
339
+ * Default: A string that generates the following:
340
+
341
+ ```
342
+ /**
343
+ * File generated by js-routes 2.3.1 on 2024-12-04 09:45:59 +0100
344
+ * Based on Rails 7.2.0 routes of App
345
+ */
346
+ ```
334
347
 
335
348
  <div id="formatter-options"></div>
336
349
 
@@ -421,7 +434,10 @@ user_path.requiredParams() // => ['id']
421
434
 
422
435
  ## Rails Compatibility
423
436
 
424
- JsRoutes tries to replicate the Rails routing API as closely as possible. If you find any incompatibilities (outside of what is described below), please [open an issue](https://github.com/railsware/js-routes/issues/new).
437
+ JsRoutes tries to replicate the Rails routing API as closely as possible.
438
+ There are only 2 known issues with compatibility that happen very rarely and have their workarounds.
439
+
440
+ If you find any incompatibilities outside of ones below, please [open an issue](https://github.com/railsware/js-routes/issues/new).
425
441
 
426
442
  ### Object and Hash distinction issue
427
443
 
@@ -3,6 +3,7 @@
3
3
  require "pathname"
4
4
  require "js_routes/types"
5
5
  require 'js_routes/utils'
6
+ require 'js_routes/version'
6
7
 
7
8
  module JsRoutes
8
9
  class Configuration
@@ -39,6 +40,8 @@ module JsRoutes
39
40
  attr_accessor :module_type
40
41
  sig { returns(T::Boolean) }
41
42
  attr_accessor :optional_definition_params
43
+ sig { returns(BannerCaller) }
44
+ attr_accessor :banner
42
45
 
43
46
  sig {params(attributes: T.nilable(Options)).void }
44
47
  def initialize(attributes = nil)
@@ -53,10 +56,11 @@ module JsRoutes
53
56
  @compact = T.let(false, T::Boolean)
54
57
  @serializer = T.let(nil, T.nilable(String))
55
58
  @special_options_key = T.let("_options", Literal)
56
- @application = T.let(-> { Rails.application }, ApplicationCaller)
59
+ @application = T.let(T.unsafe(-> { Rails.application }), ApplicationCaller)
57
60
  @module_type = T.let('ESM', T.nilable(String))
58
61
  @documentation = T.let(true, T::Boolean)
59
62
  @optional_definition_params = T.let(false, T::Boolean)
63
+ @banner = T.let(default_banner, BannerCaller)
60
64
 
61
65
  return unless attributes
62
66
  assign(attributes)
@@ -158,5 +162,17 @@ module JsRoutes
158
162
  raise "JsRoutes namespace option can only be used if module_type is nil"
159
163
  end
160
164
  end
165
+
166
+ sig { returns(T.proc.returns(String)) }
167
+ def default_banner
168
+ -> () {
169
+ app = application.is_a?(Proc) ? T.unsafe(application).call : application
170
+ <<~TXT
171
+ File generated by js-routes #{JsRoutes::VERSION} on #{Time.now}
172
+ Based on Rails #{Rails.version} routes of #{app.class}
173
+ TXT
174
+
175
+ }
176
+ end
161
177
  end
162
178
  end
@@ -29,9 +29,9 @@ module JsRoutes
29
29
  if named_routes.empty?
30
30
  if application.is_a?(Rails::Application)
31
31
  if Rails.version >= "8.0.0"
32
- application.reload_routes_unless_loaded
32
+ T.unsafe(application).reload_routes_unless_loaded
33
33
  else
34
- application.reload_routes!
34
+ T.unsafe(application).reload_routes!
35
35
  end
36
36
  end
37
37
  end
@@ -43,7 +43,20 @@ module JsRoutes
43
43
  raise("Missing key #{key} in JS template")
44
44
  end
45
45
  end
46
- content + routes_export + prevent_types_export
46
+ banner + content + routes_export + prevent_types_export
47
+ end
48
+
49
+ sig { returns(String) }
50
+ def banner
51
+ banner = @configuration.banner
52
+ banner = banner.call if banner.is_a?(Proc)
53
+ return "" if banner.blank?
54
+ [
55
+ "/**",
56
+ *banner.split("\n").map { |line| " * #{line}" },
57
+ " */",
58
+ "",
59
+ ].join("\n")
47
60
  end
48
61
 
49
62
  sig { void }
@@ -79,12 +92,8 @@ module JsRoutes
79
92
  prefix = @configuration.prefix
80
93
  prefix = prefix.call if prefix.is_a?(Proc)
81
94
  {
82
- 'GEM_VERSION' => JsRoutes::VERSION,
83
- 'TIMESTAMP' => Time.now.to_s,
84
95
  'ROUTES_OBJECT' => routes_object,
85
- 'RAILS_VERSION' => ::Rails.version,
86
96
  'DEPRECATED_FALSE_PARAMETER_BEHAVIOR' => Rails.version < '7.0.0',
87
- 'APP_CLASS' => application.class.to_s,
88
97
  'DEFAULT_URL_OPTIONS' => json(@configuration.default_url_options),
89
98
  'PREFIX' => json(prefix),
90
99
  'SPECIAL_OPTIONS_KEY' => json(@configuration.special_options_key),
@@ -187,7 +196,7 @@ export {};
187
196
  rails_engine_app = T.unsafe(app_from_route(route))
188
197
  if rails_engine_app.is_a?(Class) &&
189
198
  rails_engine_app < Rails::Engine && !route.path.anchored
190
- rails_engine_app.routes.named_routes.flat_map do |_, engine_route|
199
+ T.unsafe(rails_engine_app).routes.named_routes.flat_map do |_, engine_route|
191
200
  route_helpers_if_match(engine_route, route)
192
201
  end
193
202
  else
@@ -200,7 +209,7 @@ export {};
200
209
  app = route.app
201
210
  # Rails Engine can use additional
202
211
  # ActionDispatch::Routing::Mapper::Constraints, which contain app
203
- if app.is_a?(ActionDispatch::Routing::Mapper::Constraints)
212
+ if app.is_a?(T.unsafe(ActionDispatch::Routing::Mapper::Constraints))
204
213
  app.app
205
214
  else
206
215
  app
@@ -6,7 +6,7 @@ module JsRoutes
6
6
  # whenever routes.rb is modified
7
7
  #
8
8
  # Inspired by
9
- # https://github.com/fnando/i18n-js/blob/main/lib/i18n/js/middleware.rb
9
+ # https://github.com/fnando/i18n-js/blob/v3/lib/i18n/js/middleware.rb
10
10
  class Middleware
11
11
  include JsRoutes::Types
12
12
  include RackApp
@@ -16,11 +16,20 @@ module JsRoutes
16
16
  Literal = T.type_alias { T.any(String, Symbol) }
17
17
  JourneyRoute = T.type_alias{ActionDispatch::Journey::Route}
18
18
  RouteSpec = T.type_alias {T.untyped}
19
- Application = T.type_alias { T.any(T::Class[Rails::Engine], Rails::Application) }
20
- ApplicationCaller = T.type_alias { T.any(Application, T.proc.returns(Application)) }
19
+ Application = T.type_alias do
20
+ T.any(T::Class[Rails::Engine], Rails::Application)
21
+ end
22
+ ApplicationCaller = T.type_alias do
23
+ T.any(Application, T.proc.returns(Application))
24
+ end
25
+ BannerCaller = T.type_alias do
26
+ T.any(String, NilClass, T.proc.returns(T.any(String, NilClass)))
27
+ end
21
28
  Clusivity = T.type_alias { T.any(Regexp, T::Array[Regexp]) }
22
29
  FileName = T.type_alias { T.any(String, Pathname, NilClass) }
23
- ConfigurationBlock = T.type_alias { T.proc.params(arg0: JsRoutes::Configuration).void }
30
+ ConfigurationBlock = T.type_alias do
31
+ T.proc.params(arg0: JsRoutes::Configuration).void
32
+ end
24
33
  Prefix = T.type_alias do
25
34
  T.any(T.proc.returns(String), String, NilClass)
26
35
  end
@@ -1,4 +1,4 @@
1
1
  # typed: strict
2
2
  module JsRoutes
3
- VERSION = "2.3.1"
3
+ VERSION = "2.3.2"
4
4
  end
data/lib/routes.d.ts CHANGED
@@ -1,7 +1,3 @@
1
- /**
2
- * File generated by js-routes RubyVariables.GEM_VERSION on RubyVariables.TIMESTAMP
3
- * Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4
- */
5
1
  declare type Optional<T> = {
6
2
  [P in keyof T]?: T[P] | null;
7
3
  };
data/lib/routes.js CHANGED
@@ -1,7 +1,3 @@
1
- /**
2
- * File generated by js-routes RubyVariables.GEM_VERSION on RubyVariables.TIMESTAMP
3
- * Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4
- */
5
1
  // eslint-disable-next-line
6
2
  RubyVariables.WRAPPER(
7
3
  // eslint-disable-next-line
data/lib/routes.ts CHANGED
@@ -1,8 +1,3 @@
1
- /**
2
- * File generated by js-routes RubyVariables.GEM_VERSION on RubyVariables.TIMESTAMP
3
- * Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4
- */
5
-
6
1
  type Optional<T> = { [P in keyof T]?: T[P] | null };
7
2
  type Collection<T> = Record<string, T>;
8
3
 
@@ -1,5 +1,27 @@
1
1
  JsRoutes.setup do |c|
2
2
  # Setup your JS module system:
3
- # ESM, CJS, AMD, UMD or nil
3
+ # ESM, CJS, AMD, UMD or nil.
4
4
  # c.module_type = "ESM"
5
+
6
+ # Legacy setup for no modules system.
7
+ # Sets up a global variable `Routes`
8
+ # that holds route helpers.
9
+ # c.module_type = nil
10
+ # c.namespace = "Routes"
11
+
12
+ # Follow javascript naming convention
13
+ # but lose the ability to match helper name
14
+ # on backend and frontend consistently.
15
+ # c.camel_case = true
16
+
17
+ # Generate only helpers that match specific pattern.
18
+ # c.exclude = /^api_/
19
+ # c.include = /^admin_/
20
+
21
+ # Generate `*_url` helpers besides `*_path`
22
+ # for apps that work on multiple domains.
23
+ # c.url_links = true
24
+
25
+ # More options:
26
+ # @see https://github.com/railsware/js-routes#available-options
5
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js-routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.1
4
+ version: 2.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdan Gusiev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-27 00:00:00.000000000 Z
11
+ date: 2024-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -144,9 +144,9 @@ licenses:
144
144
  - MIT
145
145
  metadata:
146
146
  bug_tracker_uri: https://github.com/railsware/js-routes/issues
147
- changelog_uri: https://github.com/railsware/js-routes/blob/v2.3.1/CHANGELOG.md
147
+ changelog_uri: https://github.com/railsware/js-routes/blob/v2.3.2/CHANGELOG.md
148
148
  documentation_uri: https://github.com/railsware/js-routes
149
- source_code_uri: https://github.com/railsware/js-routes/tree/v2.3.1/activerecord
149
+ source_code_uri: https://github.com/railsware/js-routes/tree/v2.3.2/activerecord
150
150
  rubygems_mfa_required: 'true'
151
151
  github_repo: ssh://github.com/railsware/js-routes
152
152
  post_install_message:
@@ -164,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  - !ruby/object:Gem::Version
165
165
  version: '0'
166
166
  requirements: []
167
- rubygems_version: 3.5.16
167
+ rubygems_version: 3.5.14
168
168
  signing_key:
169
169
  specification_version: 4
170
170
  summary: Brings Rails named routes to javascript