js-routes 2.3.3 → 2.3.4

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: 1acb75b6e26ae17c5b028fb97d400d0fb061ec582298598e91b05f27a5a9a32c
4
- data.tar.gz: 2bc54da6a67463569ad6b239f90392394d666f0b89cde662a09cf35f19b07f85
3
+ metadata.gz: b8057000afc83b5ce111428c7aec30249f52ef15872db97527757c94fc7faf6d
4
+ data.tar.gz: f7d4b47f6248278cb53b5cac80807d37aec7a51a1c47f1c4711e1e6bd0d2d3ea
5
5
  SHA512:
6
- metadata.gz: d4a84398697f1bfb41f9c0dace8d50a805b5aae36de24382bd5b43214e237a776471267046ec01487cb52ab542f0b61cc46f62a32037476af8aa2510d5b87c77
7
- data.tar.gz: db690e312707ad1951b9b9905c62400eb1f70eafbeab95aab94920eab5df57c18d6d18cec1e3bc3bbe133d0a2a98308469fe934b955a43f401357284cf29f9c3
6
+ metadata.gz: 40c3c12d7d4a37089268aebda4b82d2f5d8944d5c16048394c1e38eda270f82f867faed7b129e4c27295bcf8158e998e42b92b552c015303d965c0e2227b0780
7
+ data.tar.gz: 0f075f81e3d5c0b3d6ed9315e3a34d34857c9c9bf34f136c6884e95b4ab6c6f37c0ebb4ee767eceba98bad7377bd1f4e0bc5e68019c9179fef32b4a7f57b96ea
data/CHANGELOG.md CHANGED
@@ -1,6 +1,24 @@
1
1
  # Changelog
2
2
 
3
- ## v2.3.3
3
+ ## [2.3.4]
4
+
5
+ * Fix deprecator usage in `rake js:routes:typescript` [#327](https://github.com/railsware/js-routes/issues/327)
6
+ * Migrate to yarn 4
7
+ * Deprecated `prefix` option in favor of `default_url_options.script_name`.
8
+ * Add support for `script_name` [Rails helper option](https://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for).
9
+
10
+ ``` javascript
11
+ Routes.post_path(1, { script_name: "/myapp" })
12
+ // => /myapp/post/1
13
+
14
+ Routes.configure({
15
+ default_url_options: { script_name: "/myapp" }
16
+ })
17
+
18
+ Routes.post_path(1) // => /myapp/post/1
19
+ ```
20
+
21
+ ## [2.3.3]
4
22
 
5
23
  * Rework default banner to use `routes.rb` digest instead of timestamp to ensure
6
24
  consistent routes.js version accross environments.
@@ -17,7 +35,7 @@ The new default banner:
17
35
  */
18
36
  ```
19
37
 
20
- ## v2.3.2
38
+ ## [2.3.2]
21
39
 
22
40
  * Add `banner` option that allow to control JSDoc on top of generated file. [#324](https://github.com/bogdan/repo/issues/324).
23
41
 
@@ -34,13 +52,13 @@ JsRoutes.setup do |c|
34
52
  end
35
53
  ```
36
54
 
37
- ## v2.3.1
55
+ ## [2.3.1]
38
56
 
39
57
  * Add timestamp on when routes.js was generated into banner.
40
58
  * Fix application specified directly without proc. [#323](https://github.com/railsware/js-routes/issues/323)
41
59
  * Support `optional_definition_params` option. See [Related Docs](./Readme.md#optional-definition-params).
42
60
 
43
- ## v2.3.0
61
+ ## [2.3.0]
44
62
 
45
63
  * Drop support of Rails 4.x
46
64
  * Fix support of shakapacker [#321](https://github.com/railsware/js-routes/issues/321).
@@ -48,7 +66,7 @@ end
48
66
  * Deprecated `rake js:routes:typescript`.
49
67
  `rake js:routes` now automatically detects if types support can be used on not.
50
68
 
51
- ## v2.2.10
69
+ ## [2.2.10]
52
70
 
53
71
  * Remove sorbet files from repo
54
72
  * Clearly define files included in gem
@@ -56,109 +74,109 @@ end
56
74
  * Remove empty object linter warning on DTS module
57
75
  * Generators: Add `.ts` extension when searching for main JS file of the project
58
76
 
59
- ## v2.2.9
77
+ ## [2.2.9]
60
78
 
61
79
  * Fix middleware error for non-modern setup.
62
80
  * Added [Sorbet](https://sorbet.org/) method signatures.
63
81
  * Always use DTS module type when calling JsRoutes.definitions or .definitions!.
64
82
  [#313](https://github.com/railsware/js-routes/issues/313)
65
83
 
66
- ## v2.2.8
84
+ ## [2.2.8]
67
85
 
68
86
  * Leave emoji symbols intact when encoding URI fragment [#312](https://github.com/railsware/js-routes/issues/312)
69
87
  * Use webpacker config variable instead of hardcode [#309](https://github.com/railsware/js-routes/issues/309)
70
88
  * Use `File.exist?` to be compatible with all versions of ruby [#310](https://github.com/railsware/js-routes/issues/310)
71
89
 
72
- ## v2.2.7
90
+ ## [2.2.7]
73
91
 
74
92
  * Fix ESM Tree Shaking [#306](https://github.com/railsware/js-routes/issues/306)
75
93
 
76
- ## v2.2.6
94
+ ## [2.2.6]
77
95
 
78
96
  * Prefer to extend `javascript:build` instead of `assets:precompile`. [#305](https://github.com/railsware/js-routes/issues/305)
79
97
  * Add stimulus framework application.js location to generators
80
98
 
81
- ## v2.2.5
99
+ ## [2.2.5]
82
100
 
83
101
  * Upgraded eslint and prettier versions [#304](https://github.com/railsware/js-routes/issues/304)
84
102
  * Fix middleware generator [#300](https://github.com/railsware/js-routes/issues/300)
85
103
  * Support `params` special parameter
86
104
 
87
- ## v2.2.4
105
+ ## [2.2.4]
88
106
 
89
107
  * Fix rails engine loading if sprockets is not in Gemfile. Fixes [#294](https://github.com/railsware/js-routes/issues/294)
90
108
 
91
- ## v2.2.3
109
+ ## [2.2.3]
92
110
 
93
111
  * Fixed NIL module type namespace defintion [#297](https://github.com/railsware/js-routes/issues/297).
94
112
  * The patch may cause a problem with nested `namespace` option.
95
113
  * Ex. Value like `MyProject.Routes` requires to define `window.MyProject` before importing the routes file
96
114
 
97
- ## v2.2.2
115
+ ## [2.2.2]
98
116
 
99
117
  * Fix custom file path [#295](https://github.com/railsware/js-routes/issues/295)
100
118
 
101
- ## v2.2.1
119
+ ## [2.2.1]
102
120
 
103
121
  * Improve generator to update route files on `assets:precompile` and add them to `.gitignore by default` [#288](https://github.com/railsware/js-routes/issues/288#issuecomment-1012182815)
104
122
 
105
- ## v2.2.0
123
+ ## [2.2.0]
106
124
 
107
125
  * Use Rack Middleware to automatically update routes file in development [#288](https://github.com/railsware/js-routes/issues/288)
108
126
  * This setup is now a default recommended due to lack of any downside comparing to [ERB Loader](./Readme.md#webpacker) and [Manual Setup](./Readme.md#advanced-setup)
109
127
 
110
- ## v2.1.3
128
+ ## [2.1.3]
111
129
 
112
130
  * Fix `default_url_options` bug. [#290](https://github.com/railsware/js-routes/issues/290)
113
131
 
114
- ## v2.1.2
132
+ ## [2.1.2]
115
133
 
116
134
  * Improve browser window object detection. [#287](https://github.com/railsware/js-routes/issues/287)
117
135
 
118
- ## v2.1.1
136
+ ## [2.1.1]
119
137
 
120
138
  * Added webpacker generator `./bin/rails generate js_routes:webpacker`
121
139
  * Reorganized Readme to describe different setups with their pros and cons more clearly
122
140
 
123
- ## v2.1.0
141
+ ## [2.1.0]
124
142
 
125
143
  * Support typescript defintions file aka `routes.d.ts`. See [Readme.md](./Readme.md#definitions) for more information.
126
144
 
127
- ## v2.0.8
145
+ ## [2.0.8]
128
146
 
129
147
  * Forbid usage of `namespace` option if `module_type` is not `nil`. [#281](https://github.com/railsware/js-routes/issues/281).
130
148
 
131
- ## v2.0.7
149
+ ## [2.0.7]
132
150
 
133
151
  * Remove source map annotation from JS file. Fixes [#277](https://github.com/railsware/js-routes/issues/277)
134
152
  * Generated file is not minified, so it is better to use app side bundler/compressor for source maps
135
153
 
136
154
 
137
- ## v2.0.6
155
+ ## [2.0.6]
138
156
 
139
157
  * Disable `namespace` option default for all envs [#278](https://github.com/railsware/js-routes/issues/278)
140
158
 
141
- ## v2.0.5
159
+ ## [2.0.5]
142
160
 
143
161
  * Fixed backward compatibility issue [#276](https://github.com/railsware/js-routes/issues/276)
144
162
 
145
- ## v2.0.4
163
+ ## [2.0.4]
146
164
 
147
165
  * Fixed backward compatibility issue [#275](https://github.com/railsware/js-routes/issues/275)
148
166
 
149
- ## v2.0.3
167
+ ## [2.0.3]
150
168
 
151
169
  * Fixed backward compatibility issue [#275](https://github.com/railsware/js-routes/issues/275)
152
170
 
153
- ## v2.0.2
171
+ ## [2.0.2]
154
172
 
155
173
  * Fixed backward compatibility issue [#274](https://github.com/railsware/js-routes/issues/274)
156
174
 
157
- ## v2.0.1
175
+ ## [2.0.1]
158
176
 
159
177
  * Fixed backward compatibility issue [#272](https://github.com/railsware/js-routes/issues/272)
160
178
 
161
- ## v2.0.0
179
+ ## [2.0.0]
162
180
 
163
181
  Version 2.0 has some breaking changes.
164
182
  See [UPGRADE TO 2.0](./VERSION_2_UPGRADE.md) for guidance.
data/Readme.md CHANGED
@@ -353,10 +353,8 @@ Options to configure routes formatting. These options are available both in Ruby
353
353
 
354
354
  * `default_url_options` - default parameters used when generating URLs
355
355
  * Example: `{format: "json", trailing_slash: true, protocol: "https", subdomain: "api", host: "example.com", port: 3000}`
356
+ * See [`url_for` doc](https://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for) for list of supported options
356
357
  * Default: `{}`
357
- * `prefix` - string that will prepend any generated URL. Usually used when app URL root includes a path component.
358
- * Example: `/rails-app`
359
- * Default: `Rails.application.config.relative_url_root`
360
358
  * `serializer` - a JS function that serializes a Javascript Hash object into URL paramters like `{a: 1, b: 2} => "a=1&b=2"`.
361
359
  * Default: `nil`. Uses built-in serializer compatible with Rails
362
360
  * Example: `jQuery.param` - use jQuery's serializer algorithm. You can attach serialize function from your favorite AJAX framework.
@@ -19,7 +19,7 @@ module JsRoutes
19
19
  sig { returns(FileName) }
20
20
  attr_accessor :file
21
21
  sig { returns(Prefix) }
22
- attr_accessor :prefix
22
+ attr_reader :prefix
23
23
  sig { returns(T::Boolean) }
24
24
  attr_accessor :url_links
25
25
  sig { returns(T::Boolean) }
@@ -91,6 +91,11 @@ module JsRoutes
91
91
  public_send(attribute)
92
92
  end
93
93
 
94
+ def prefix=(value)
95
+ JsRoutes::Utils.deprecator.warn("JsRoutes configuration prefix is deprecated in favor of default_url_options.script_name.")
96
+ @prefix = value
97
+ end
98
+
94
99
  sig { params(attributes: Options).returns(JsRoutes::Configuration) }
95
100
  def merge(attributes)
96
101
  clone.assign(attributes)
@@ -26,7 +26,7 @@ module JsRoutes
26
26
  # Ensure routes are loaded. If they're not, load them.
27
27
 
28
28
  application = T.unsafe(self.application)
29
- if named_routes.empty?
29
+ if routes_from(application).empty?
30
30
  if application.is_a?(Rails::Application)
31
31
  if Rails.version >= "8.0.0"
32
32
  T.unsafe(application).reload_routes_unless_loaded
@@ -135,9 +135,9 @@ module JsRoutes
135
135
  JsRoutes.json(value)
136
136
  end
137
137
 
138
- sig { returns(T::Hash[Symbol, JourneyRoute]) }
139
- def named_routes
140
- T.unsafe(application).routes.named_routes.to_h
138
+ sig {params(application: Application).returns(T::Array[JourneyRoute])}
139
+ def routes_from(application)
140
+ T.unsafe(application).routes.named_routes.to_h.values.sort_by(&:name)
141
141
  end
142
142
 
143
143
  sig { returns(String) }
@@ -186,7 +186,7 @@ export {};
186
186
 
187
187
  sig { returns(T::Array[StringArray]) }
188
188
  def routes_list
189
- named_routes.sort_by(&:first).flat_map do |_, route|
189
+ routes_from(application).flat_map do |route|
190
190
  route_helpers_if_match(route) + mounted_app_routes(route)
191
191
  end
192
192
  end
@@ -196,7 +196,7 @@ export {};
196
196
  rails_engine_app = T.unsafe(app_from_route(route))
197
197
  if rails_engine_app.is_a?(Class) &&
198
198
  rails_engine_app < Rails::Engine && !route.path.anchored
199
- T.unsafe(rails_engine_app).routes.named_routes.flat_map do |_, engine_route|
199
+ routes_from(rails_engine_app).flat_map do |engine_route|
200
200
  route_helpers_if_match(engine_route, route)
201
201
  end
202
202
  else
@@ -13,6 +13,15 @@ module JsRoutes
13
13
  nil
14
14
  end
15
15
  end
16
+
17
+ sig { returns(T.untyped) }
18
+ def self.deprecator
19
+ if defined?(Rails) && Rails.version >= "7.1.0"
20
+ Rails.deprecator
21
+ else
22
+ ActiveSupport::Deprecation
23
+ end
24
+ end
16
25
  end
17
26
 
18
27
  end
@@ -1,4 +1,4 @@
1
1
  # typed: strict
2
2
  module JsRoutes
3
- VERSION = "2.3.3"
3
+ VERSION = "2.3.4"
4
4
  end
data/lib/routes.d.ts CHANGED
@@ -50,6 +50,7 @@ declare type KeywordUrlOptions = Optional<{
50
50
  port: string | number;
51
51
  anchor: string;
52
52
  trailing_slash: boolean;
53
+ script_name: string;
53
54
  params: RouteParameters;
54
55
  }>;
55
56
  declare type RouteOptions = KeywordUrlOptions & RouteParameters;
data/lib/routes.js CHANGED
@@ -110,6 +110,7 @@ RubyVariables.WRAPPER(
110
110
  "host",
111
111
  "port",
112
112
  "protocol",
113
+ "script_name",
113
114
  ];
114
115
  class UtilsClass {
115
116
  constructor() {
@@ -275,21 +276,29 @@ RubyVariables.WRAPPER(
275
276
  }
276
277
  build_route(parts, required_params, default_options, route, absolute, args) {
277
278
  const { keyword_parameters, query_parameters } = this.partition_parameters(parts, required_params, default_options, args);
279
+ let { trailing_slash, anchor, script_name } = keyword_parameters;
278
280
  const missing_params = required_params.filter((param) => !hasProp(query_parameters, param) ||
279
281
  this.is_nullable(query_parameters[param]));
280
282
  if (missing_params.length) {
281
283
  throw new ParametersMissing(...missing_params);
282
284
  }
283
285
  let result = this.get_prefix() + this.visit(route, query_parameters);
284
- if (keyword_parameters.trailing_slash) {
286
+ if (trailing_slash) {
285
287
  result = result.replace(/(.*?)[/]?$/, "$1/");
286
288
  }
287
289
  const url_params = this.serialize(query_parameters);
288
290
  if (url_params.length) {
289
291
  result += "?" + url_params;
290
292
  }
291
- if (keyword_parameters.anchor) {
292
- result += "#" + keyword_parameters.anchor;
293
+ if (anchor) {
294
+ result += "#" + anchor;
295
+ }
296
+ if (script_name) {
297
+ const last_index = script_name.length - 1;
298
+ if (script_name[last_index] == "/" && result[0] == "/") {
299
+ script_name = script_name.slice(0, last_index);
300
+ }
301
+ result = script_name + result;
293
302
  }
294
303
  if (absolute) {
295
304
  result = this.route_url(keyword_parameters) + result;
@@ -481,6 +490,9 @@ RubyVariables.WRAPPER(
481
490
  return ReservedOptions.includes(key);
482
491
  }
483
492
  configure(new_config) {
493
+ if (new_config.prefix) {
494
+ console.warn("JsRoutes configuration prefix option is deprecated in favor of default_url_options.script_name.");
495
+ }
484
496
  this.configuration = { ...this.configuration, ...new_config };
485
497
  return this.configuration;
486
498
  }
data/lib/routes.ts CHANGED
@@ -66,6 +66,7 @@ type KeywordUrlOptions = Optional<{
66
66
  port: string | number;
67
67
  anchor: string;
68
68
  trailing_slash: boolean;
69
+ script_name: string;
69
70
  params: RouteParameters;
70
71
  }>;
71
72
 
@@ -230,7 +231,9 @@ RubyVariables.WRAPPER(
230
231
  "host",
231
232
  "port",
232
233
  "protocol",
234
+ "script_name",
233
235
  ] as const;
236
+
234
237
  type ReservedOption = (typeof ReservedOptions)[any];
235
238
 
236
239
  class UtilsClass {
@@ -433,6 +436,8 @@ RubyVariables.WRAPPER(
433
436
  default_options,
434
437
  args
435
438
  );
439
+
440
+ let { trailing_slash, anchor, script_name } = keyword_parameters;
436
441
  const missing_params = required_params.filter(
437
442
  (param) =>
438
443
  !hasProp(query_parameters, param) ||
@@ -442,15 +447,22 @@ RubyVariables.WRAPPER(
442
447
  throw new ParametersMissing(...missing_params);
443
448
  }
444
449
  let result = this.get_prefix() + this.visit(route, query_parameters);
445
- if (keyword_parameters.trailing_slash) {
450
+ if (trailing_slash) {
446
451
  result = result.replace(/(.*?)[/]?$/, "$1/");
447
452
  }
448
453
  const url_params = this.serialize(query_parameters);
449
454
  if (url_params.length) {
450
455
  result += "?" + url_params;
451
456
  }
452
- if (keyword_parameters.anchor) {
453
- result += "#" + keyword_parameters.anchor;
457
+ if (anchor) {
458
+ result += "#" + anchor;
459
+ }
460
+ if (script_name) {
461
+ const last_index = script_name.length - 1;
462
+ if (script_name[last_index] == "/" && result[0] == "/") {
463
+ script_name = script_name.slice(0, last_index);
464
+ }
465
+ result = script_name + result;
454
466
  }
455
467
  if (absolute) {
456
468
  result = this.route_url(keyword_parameters) + result;
@@ -696,6 +708,11 @@ RubyVariables.WRAPPER(
696
708
  }
697
709
 
698
710
  configure(new_config: Partial<Configuration>): Configuration {
711
+ if (new_config.prefix) {
712
+ console.warn(
713
+ "JsRoutes configuration prefix option is deprecated in favor of default_url_options.script_name."
714
+ );
715
+ }
699
716
  this.configuration = { ...this.configuration, ...new_config };
700
717
  return this.configuration;
701
718
  }
@@ -8,7 +8,7 @@ namespace :js do
8
8
  namespace :routes do
9
9
  desc "Make a js file with all rails route URL helpers and typescript definitions for them"
10
10
  task typescript: "js:routes" do
11
- ActiveSupport::Deprecation.warn(
11
+ JsRoutes::Utils.deprecator.warn(
12
12
  "`js:routes:typescript` task is deprecated. Please use `js:routes` instead."
13
13
  )
14
14
  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.3
4
+ version: 2.3.4
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-12-04 00:00:00.000000000 Z
11
+ date: 2024-12-17 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.3/CHANGELOG.md
147
+ changelog_uri: https://github.com/railsware/js-routes/blob/v2.3.4/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.3/activerecord
149
+ source_code_uri: https://github.com/railsware/js-routes/tree/v2.3.4/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.14
167
+ rubygems_version: 3.5.11
168
168
  signing_key:
169
169
  specification_version: 4
170
170
  summary: Brings Rails named routes to javascript