js-routes 2.3.0 → 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8030ebf1b79303bfad35b2003ced97e6ec398b8bab459bf0f867006c9bc5d763
4
- data.tar.gz: 4d84acaa15dacc791f26e059cb620d5c08d90891103fa5d73750d4bae3973e8b
3
+ metadata.gz: 97421ede18a16a1cb38ede32268ea60e79dc3aeee2dbe419cbbc4f117848cebb
4
+ data.tar.gz: d65a56e1026ef6551a7e0c30f90278245dfed8b4ceca460c285b16855586709c
5
5
  SHA512:
6
- metadata.gz: 24e4500fcf1f799633343366f84d619060cd57979edc25a9f83ec17baf6a505e3fbbd838f66b6ec70618bf7d991a0f0614a3c6ad02ce85a90931e4a047f168a6
7
- data.tar.gz: 7cab5ccd03712368ea32d43604381b32491ada2a933bf656b6efa0002132e638653f250e5a5037487e36c6ecfed01454d6df359e2e308b5fc7cd237c7af2fca2
6
+ metadata.gz: 13c8299991c8821aff0aa09f4b90ce3a38a9881b780688a6aabadf4871ab79948a095feb0fc9c76b4cc51c84f00aa758f5276838234e963efa1bb641db526984
7
+ data.tar.gz: 651ae1108a3845a1b948c0b9a1bb9475a2374d6178bd0cf35a7da2dca990081b2bfd40fb2beb92301a4efa3cdef143e83e04a7c39afb7394a685383d9a6b8a91
data/CHANGELOG.md CHANGED
@@ -1,10 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## v2.3.1
4
+
5
+ * Add timestamp on when routes.js was generated into banner.
6
+ * Fix application specified directly without proc. [#323](https://github.com/railsware/js-routes/issues/323)
7
+ * Support `optional_definition_params` option. See [Related Docs](./Readme.md#optional-definition-params).
8
+
3
9
  ## v2.3.0
4
10
 
5
11
  * 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)
12
+ * Fix support of shakapacker [#321](https://github.com/railsware/js-routes/issues/321).
13
+ * Fix support for Rails 8 [#319](https://github.com/railsware/js-routes/issues/319)
8
14
  * Deprecated `rake js:routes:typescript`.
9
15
  `rake js:routes` now automatically detects if types support can be used on not.
10
16
 
@@ -12,7 +18,7 @@
12
18
 
13
19
  * Remove sorbet files from repo
14
20
  * Clearly define files included in gem
15
- * Fix Middleware and Middleware generator bugs [#316](https://github.com/railsware/js-routes/issue/316)
21
+ * Fix Middleware and Middleware generator bugs [#316](https://github.com/railsware/js-routes/issues/316)
16
22
  * Remove empty object linter warning on DTS module
17
23
  * Generators: Add `.ts` extension when searching for main JS file of the project
18
24
 
data/Readme.md CHANGED
@@ -42,8 +42,8 @@ There are several possible ways to setup JsRoutes:
42
42
 
43
43
  ### Quick Start
44
44
 
45
- Setup [Rack Middleware](https://guides.rubyonrails.org/rails_on_rack.html#action-dispatcher-middleware-stack)
46
- to automatically generate and maintain `routes.js` file and corresponding
45
+ Setup [Rack Middleware](https://guides.rubyonrails.org/rails_on_rack.html#action-dispatcher-middleware-stack)
46
+ to automatically generate and maintain `routes.js` file and corresponding
47
47
  [Typescript definitions](https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html) `routes.d.ts`:
48
48
 
49
49
  #### Use a Generator
@@ -70,13 +70,12 @@ import {post_path} from '../routes';
70
70
  alert(post_path(1))
71
71
  ```
72
72
 
73
- Upgrade js building process to update js-routes files in `Rakefile`:
73
+ Upgrade js building process to update js-routes files in `Rakefile`:
74
74
 
75
75
  ``` ruby
76
- task "javascript:build" => "js:routes:typescript"
76
+ task "javascript:build" => "js:routes"
77
77
  # For setups without jsbundling-rails
78
- task "assets:precompile" => "js:routes:typescript"
79
-
78
+ task "assets:precompile" => "js:routes"
80
79
  ```
81
80
 
82
81
  Add js-routes files to `.gitignore`:
@@ -236,7 +235,7 @@ JsRoutes.definitions! # to output to file
236
235
  JsRoutes.definitions # to output to string
237
236
  ```
238
237
 
239
- Even more advanced setups can be achieved by setting `module_type` to `DTS` inside [configuration](#module_type)
238
+ Even more advanced setups can be achieved by setting `module_type` to `DTS` inside [configuration](#module_type)
240
239
  which will cause any `JsRoutes` instance to generate defintions instead of routes themselves.
241
240
 
242
241
  <div id="sprockets"></div>
@@ -325,9 +324,13 @@ Options to configure JavaScript file generator. These options are only available
325
324
  * Sample route call when option is set to true: `users() // => /users`
326
325
  * `application` - a key to specify which rails engine you want to generate routes too.
327
326
  * This option allows to only generate routes for a specific rails engine, that is mounted into routes instead of all Rails app routes
328
- * Default: `Rails.application`
327
+ * It is recommended to wrap the value with `lambda`. This will reduce the reliance on order during initialization your application.
328
+ * Default: `-> { Rails.application }`
329
329
  * `file` - a file location where generated routes are stored
330
330
  * Default: `app/javascript/routes.js` if setup with Webpacker, otherwise `app/assets/javascripts/routes.js` if setup with Sprockets.
331
+ * `optional_definition_params` - make all route paramters in definition optional
332
+ * See [related compatibility issue](#optional-definition-params)
333
+ * Default: `false`
331
334
 
332
335
  <div id="formatter-options"></div>
333
336
 
@@ -359,33 +362,33 @@ import {
359
362
  user_path, user_project_path, company_path
360
363
  } from 'routes';
361
364
 
362
- users_path()
365
+ users_path()
363
366
  // => "/users"
364
367
 
365
- user_path(1)
368
+ user_path(1)
366
369
  // => "/users/1"
367
-
368
- user_path(1, {format: 'json'})
370
+
371
+ user_path(1, {format: 'json'})
369
372
  // => "/users/1.json"
370
373
 
371
- user_path(1, {anchor: 'profile'})
374
+ user_path(1, {anchor: 'profile'})
372
375
  // => "/users/1#profile"
373
376
 
374
- new_user_project_path(1, {format: 'json'})
377
+ new_user_project_path(1, {format: 'json'})
375
378
  // => "/users/1/projects/new.json"
376
379
 
377
- user_project_path(1,2, {q: 'hello', custom: true})
380
+ user_project_path(1,2, {q: 'hello', custom: true})
378
381
  // => "/users/1/projects/2?q=hello&custom=true"
379
382
 
380
- user_project_path(1,2, {hello: ['world', 'mars']})
383
+ user_project_path(1,2, {hello: ['world', 'mars']})
381
384
  // => "/users/1/projects/2?hello%5B%5D=world&hello%5B%5D=mars"
382
385
 
383
386
  var google = {id: 1, name: "Google"};
384
- company_path(google)
387
+ company_path(google)
385
388
  // => "/companies/1"
386
389
 
387
390
  var google = {id: 1, name: "Google", to_param: "google"};
388
- company_path(google)
391
+ company_path(google)
389
392
  // => "/companies/google"
390
393
  ```
391
394
 
@@ -428,14 +431,63 @@ In this case you would need to pass a special key to help:
428
431
  ``` js
429
432
  import {company_project_path} from '../routes'
430
433
 
431
- company_project_path({company_id: 1, id: 2}) // => Not enough parameters
432
- company_project_path({company_id: 1, id: 2, _options: true}) // => "/companies/1/projects/2"
434
+ company_project_path({company_id: 1, id: 2})
435
+ // => Not enough parameters
436
+ company_project_path({company_id: 1, id: 2, _options: true})
437
+ // => "/companies/1/projects/2"
438
+ ```
439
+
440
+ Use `special_options_key` to configure the `_options` parameter name.
441
+
442
+ <div id="optional-definition-params"></div>
443
+
444
+ ### Rails required parameters specified as optional
445
+
446
+ Rails is very flexible on how route parameters can be specified.
447
+ All of the following calls will make the same result:
448
+
449
+ ``` ruby
450
+ # Given route
451
+ # /inboxes/:inbox_id/messages/:message_id/attachments/:id
452
+ # every call below returns:
453
+ # => "/inboxes/1/messages/2/attachments/3"
454
+
455
+ inbox_message_attachment_path(1, 2, 3)
456
+ inbox_message_attachment_path(1, 2, id: 3)
457
+ inbox_message_attachment_path(1, message_id: 2, id: 3)
458
+ inbox_message_attachment_path(inbox_id: 1, message_id: 2, id: 3)
459
+
460
+ # including these mad versions
461
+ inbox_message_attachment_path(2, inbox_id: 1, id: 3)
462
+ inbox_message_attachment_path(1, 3, message_id: 2)
463
+ inbox_message_attachment_path(3, inbox_id: 1, message_id: 2)
464
+ ```
465
+
466
+ While all of these methods are supported by JsRoutes, it is impossible to support them in `DTS` type definitions.
467
+ If you are using routes like this, use the following configuration that will prevent required parameters presence to be validated by definition:
468
+
469
+ ``` ruby
470
+ JsRoutes.configure do |c|
471
+ c.optional_definition_params = true
472
+ end
433
473
  ```
434
474
 
475
+ This will enforce the following route signature:
476
+
477
+ ``` typescript
478
+ export const inbox_message_attachment_path: ((
479
+ inbox_id?: RequiredRouteParameter,
480
+ message_id?: RequiredRouteParameter,
481
+ id?: RequiredRouteParameter,
482
+ options?: RouteOptions
483
+ ) => string) & RouteHelperExtras;
484
+ ```
485
+
486
+ That will make every call above valid.
435
487
 
436
488
  ## What about security?
437
489
 
438
- JsRoutes itself does not have security holes.
490
+ JsRoutes itself does not have security holes.
439
491
  It makes URLs without access protection more reachable by potential attacker.
440
492
  If that is an issue for you, you may use one of the following solutions:
441
493
 
@@ -488,5 +540,3 @@ Advantages of this one are:
488
540
  * Full rails compatibility
489
541
  * Support Rails `#to_param` convention for seo optimized paths
490
542
  * Well tested
491
-
492
-
@@ -37,6 +37,8 @@ module JsRoutes
37
37
  attr_accessor :documentation
38
38
  sig { returns(T.nilable(String)) }
39
39
  attr_accessor :module_type
40
+ sig { returns(T::Boolean) }
41
+ attr_accessor :optional_definition_params
40
42
 
41
43
  sig {params(attributes: T.nilable(Options)).void }
42
44
  def initialize(attributes = nil)
@@ -54,6 +56,7 @@ module JsRoutes
54
56
  @application = T.let(-> { Rails.application }, ApplicationCaller)
55
57
  @module_type = T.let('ESM', T.nilable(String))
56
58
  @documentation = T.let(true, T::Boolean)
59
+ @optional_definition_params = T.let(false, T::Boolean)
57
60
 
58
61
  return unless attributes
59
62
  assign(attributes)
@@ -76,15 +76,14 @@ module JsRoutes
76
76
 
77
77
  sig { returns(T::Hash[String, String]) }
78
78
  def js_variables
79
- version = Rails.version
80
79
  prefix = @configuration.prefix
81
80
  prefix = prefix.call if prefix.is_a?(Proc)
82
81
  {
83
82
  'GEM_VERSION' => JsRoutes::VERSION,
83
+ 'TIMESTAMP' => Time.now.to_s,
84
84
  'ROUTES_OBJECT' => routes_object,
85
85
  'RAILS_VERSION' => ::Rails.version,
86
- 'DEPRECATED_GLOBBING_BEHAVIOR' => version >= '4.0.0' && version < '4.1.0',
87
- 'DEPRECATED_FALSE_PARAMETER_BEHAVIOR' => version < '7.0.0',
86
+ 'DEPRECATED_FALSE_PARAMETER_BEHAVIOR' => Rails.version < '7.0.0',
88
87
  'APP_CLASS' => application.class.to_s,
89
88
  'DEFAULT_URL_OPTIONS' => json(@configuration.default_url_options),
90
89
  'PREFIX' => json(prefix),
@@ -118,7 +117,8 @@ module JsRoutes
118
117
 
119
118
  sig { returns(Application) }
120
119
  def application
121
- @configuration.application.call
120
+ app = @configuration.application
121
+ app.is_a?(Proc) ? app.call : app
122
122
  end
123
123
 
124
124
  sig { params(value: T.untyped).returns(String) }
@@ -66,7 +66,8 @@ module JsRoutes
66
66
  sig { returns(String) }
67
67
  def definition_body
68
68
  options_type = optional_parts_type ? "#{optional_parts_type} & RouteOptions" : "RouteOptions"
69
- args = required_parts.map{|p| "#{apply_case(p)}: RequiredRouteParameter"}
69
+ predicate = @configuration.optional_definition_params ? '?' : ''
70
+ args = required_parts.map{|p| "#{apply_case(p)}#{predicate}: RequiredRouteParameter"}
70
71
  args << "options?: #{options_type}"
71
72
  "((\n#{args.join(",\n").indent(2)}\n) => string) & RouteHelperExtras"
72
73
  end
@@ -17,7 +17,7 @@ module JsRoutes
17
17
  JourneyRoute = T.type_alias{ActionDispatch::Journey::Route}
18
18
  RouteSpec = T.type_alias {T.untyped}
19
19
  Application = T.type_alias { T.any(T::Class[Rails::Engine], Rails::Application) }
20
- ApplicationCaller = T.type_alias { T.proc.returns(Application) }
20
+ ApplicationCaller = T.type_alias { T.any(Application, T.proc.returns(Application)) }
21
21
  Clusivity = T.type_alias { T.any(Regexp, T::Array[Regexp]) }
22
22
  FileName = T.type_alias { T.any(String, Pathname, NilClass) }
23
23
  ConfigurationBlock = T.type_alias { T.proc.params(arg0: JsRoutes::Configuration).void }
@@ -1,4 +1,4 @@
1
1
  # typed: strict
2
2
  module JsRoutes
3
- VERSION = "2.3.0"
3
+ VERSION = "2.3.1"
4
4
  end
data/lib/routes.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * File generated by js-routes RubyVariables.GEM_VERSION
2
+ * File generated by js-routes RubyVariables.GEM_VERSION on RubyVariables.TIMESTAMP
3
3
  * Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4
4
  */
5
5
  declare type Optional<T> = {
@@ -64,7 +64,6 @@ declare type PartsTable = Collection<{
64
64
  declare type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "DTS" | "NIL";
65
65
  declare const RubyVariables: {
66
66
  PREFIX: string;
67
- DEPRECATED_GLOBBING_BEHAVIOR: boolean;
68
67
  DEPRECATED_FALSE_PARAMETER_BEHAVIOR: boolean;
69
68
  SPECIAL_OPTIONS_KEY: string;
70
69
  DEFAULT_URL_OPTIONS: RouteParameters;
data/lib/routes.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * File generated by js-routes RubyVariables.GEM_VERSION
2
+ * File generated by js-routes RubyVariables.GEM_VERSION on RubyVariables.TIMESTAMP
3
3
  * Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4
4
  */
5
5
  // eslint-disable-next-line
@@ -415,9 +415,7 @@ RubyVariables.WRAPPER(
415
415
  value = value.join("/");
416
416
  }
417
417
  const result = this.path_identifier(value);
418
- return RubyVariables.DEPRECATED_GLOBBING_BEHAVIOR
419
- ? result
420
- : encodeURI(result);
418
+ return encodeURI(result);
421
419
  }
422
420
  get_prefix() {
423
421
  const prefix = this.configuration.prefix;
data/lib/routes.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * File generated by js-routes RubyVariables.GEM_VERSION
2
+ * File generated by js-routes RubyVariables.GEM_VERSION on RubyVariables.TIMESTAMP
3
3
  * Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4
4
  */
5
5
 
@@ -82,7 +82,6 @@ type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "DTS" | "NIL";
82
82
 
83
83
  declare const RubyVariables: {
84
84
  PREFIX: string;
85
- DEPRECATED_GLOBBING_BEHAVIOR: boolean;
86
85
  DEPRECATED_FALSE_PARAMETER_BEHAVIOR: boolean;
87
86
  SPECIAL_OPTIONS_KEY: string;
88
87
  DEFAULT_URL_OPTIONS: RouteParameters;
@@ -606,9 +605,7 @@ RubyVariables.WRAPPER(
606
605
  value = value.join("/");
607
606
  }
608
607
  const result = this.path_identifier(value as any);
609
- return RubyVariables.DEPRECATED_GLOBBING_BEHAVIOR
610
- ? result
611
- : encodeURI(result);
608
+ return encodeURI(result);
612
609
  }
613
610
 
614
611
  get_prefix(): string {
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.0
4
+ version: 2.3.1
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-16 00:00:00.000000000 Z
11
+ date: 2024-11-27 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.0/CHANGELOG.md
147
+ changelog_uri: https://github.com/railsware/js-routes/blob/v2.3.1/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.0/activerecord
149
+ source_code_uri: https://github.com/railsware/js-routes/tree/v2.3.1/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.11
167
+ rubygems_version: 3.5.16
168
168
  signing_key:
169
169
  specification_version: 4
170
170
  summary: Brings Rails named routes to javascript