js-routes 2.2.3 → 2.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +1 -1
  3. data/.github/workflows/ci.yml +36 -0
  4. data/Appraisals +2 -4
  5. data/CHANGELOG.md +7 -0
  6. data/Readme.md +50 -43
  7. data/gemfiles/rails50_sprockets_3.gemfile +1 -1
  8. data/gemfiles/rails51_sprockets_3.gemfile +1 -1
  9. data/gemfiles/rails52_sprockets_3.gemfile +1 -1
  10. data/gemfiles/rails70_sprockets_4.gemfile +8 -0
  11. data/lib/js_routes/engine.rb +6 -1
  12. data/lib/js_routes/generators/middleware.rb +1 -1
  13. data/lib/js_routes/instance.rb +2 -2
  14. data/lib/js_routes/middleware.rb +6 -2
  15. data/lib/js_routes/version.rb +1 -1
  16. data/lib/routes.d.ts +2 -0
  17. data/lib/routes.js +23 -5
  18. data/lib/routes.ts +34 -17
  19. data/package.json +7 -6
  20. data/spec/js_routes/default_serializer_spec.rb +5 -5
  21. data/spec/js_routes/module_types/amd_spec.rb +3 -3
  22. data/spec/js_routes/module_types/cjs_spec.rb +1 -1
  23. data/spec/js_routes/module_types/dts/routes.spec.d.ts +2 -0
  24. data/spec/js_routes/module_types/dts_spec.rb +1 -1
  25. data/spec/js_routes/module_types/esm_spec.rb +2 -2
  26. data/spec/js_routes/module_types/nil_spec.rb +7 -7
  27. data/spec/js_routes/module_types/umd_spec.rb +1 -1
  28. data/spec/js_routes/options_spec.rb +81 -81
  29. data/spec/js_routes/rails_routes_compatibility_spec.rb +127 -138
  30. data/spec/js_routes/route_specification_spec.rb +40 -0
  31. data/spec/js_routes/zzz_last_post_rails_init_spec.rb +8 -2
  32. data/spec/spec_helper.rb +5 -5
  33. data/yarn.lock +760 -448
  34. metadata +6 -10
  35. data/.travis.yml +0 -67
  36. data/gemfiles/rails40_sprockets_2.gemfile +0 -8
  37. data/gemfiles/rails40_sprockets_3.gemfile +0 -8
  38. data/gemfiles/rails41_sprockets_2.gemfile +0 -8
  39. data/gemfiles/rails41_sprockets_3.gemfile +0 -8
  40. data/gemfiles/rails42_sprockets_2.gemfile +0 -8
  41. data/gemfiles/rails42_sprockets_3.gemfile +0 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b706c51040d12aaa69182dad9d25304ba947fe1e80cc5171775864a27d6f4ed8
4
- data.tar.gz: 1a105e8a4dbc9cfde5a787a0714b6e0bf0758b6291f6945f0a16ca9c1e7a47a9
3
+ metadata.gz: d0fd81f6f8575bb26353c7e485f0551cc62420a266e22d5503739332b5762037
4
+ data.tar.gz: b84d4ad0f70f8e1f454bdd2c6fb8b77a316376b3407a3fd0b6c681d87b87ada9
5
5
  SHA512:
6
- metadata.gz: f053d1f48cb8a4f9f7ac7f1e68d37f4816d09e42e0a91660fb84c1e9f4ad36d439019f6f402f1e2fce30467e6b0d5b799fd08bbe70bfc2aa8423c63f053f0bdb
7
- data.tar.gz: bdcf72700542350da423e9cf62b4abcc0ba326400c4077c70c5f6174edb75a7e61b6b6a765a0454b4cd1a2fc5ddbf91f3a16294ce995d9ab31d9984153c29b4c
6
+ metadata.gz: 23db2ecfc872d470727b60d7c63148aa15d9d5b76d6eaedbf7bbe5ee964ae5ca5abfb09f69199dd813fcd0b09b12d0920fcb0abf9b09a1bb2a6dbfbaf7884e75
7
+ data.tar.gz: faf0d713243b705277604493bdf2f5d9f61c5cd953ef9bcfbcd1f2d88fdfd850d27ed48e0740f0fa4c0f4492a310bec13d6a76fc070113703ab7e7750e812b0c
data/.eslintrc.js CHANGED
@@ -4,7 +4,7 @@
4
4
  'eslint:recommended',
5
5
  'plugin:@typescript-eslint/eslint-recommended',
6
6
  'plugin:@typescript-eslint/recommended',
7
- 'prettier/@typescript-eslint',
7
+ 'prettier',
8
8
  ],
9
9
  parser: '@typescript-eslint/parser',
10
10
  plugins: ['@typescript-eslint'],
@@ -0,0 +1,36 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request:
8
+
9
+ jobs:
10
+ build:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ ruby: ["2.7"]
15
+ gemfile:
16
+ - gemfiles/rails50_sprockets_3.gemfile
17
+ - gemfiles/rails51_sprockets_3.gemfile
18
+ - gemfiles/rails52_sprockets_3.gemfile
19
+ - gemfiles/rails70_sprockets_4.gemfile
20
+
21
+ env:
22
+ BUNDLE_GEMFILE: ${{ matrix.gemfile }}
23
+ steps:
24
+ - uses: actions/checkout@v2
25
+ - uses: ruby/setup-ruby@v1
26
+ with:
27
+ ruby-version: ${{ matrix.ruby }}
28
+ - uses: actions/setup-node@v3
29
+ with:
30
+ node-version: "16"
31
+ - name: install dependencies
32
+ run: |
33
+ bundle install --jobs 3 --retry 3
34
+ yarn install
35
+ - name: test
36
+ run: bundle exec rake spec
data/Appraisals CHANGED
@@ -8,12 +8,10 @@ def define_appraisal(rails, version, sprockets)
8
8
  end
9
9
 
10
10
  [
11
- [:rails40, '4.0.13', [2, 3]],
12
- [:rails41, '4.1.16', [2, 3]],
13
- [:rails42, '4.2.9', [2, 3]],
14
11
  [:rails50, '5.0.5', [3]],
15
12
  [:rails51, '5.1.3', [3]],
16
- [:rails52, '5.2.3', [3]]
13
+ [:rails52, '5.2.3', [3]],
14
+ [:rails70, '7.0.3.1', [4]]
17
15
  ].each do |name, version, sprockets|
18
16
  define_appraisal(name, version, sprockets)
19
17
  end
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  ## master
2
2
 
3
+
4
+ ## v2.2.5
5
+
6
+ * Upgraded eslint and prettier versions [#304](https://github.com/railsware/js-routes/issues/304)
7
+ * Fix middleware generator [#300](https://github.com/railsware/js-routes/issues/300)
8
+ * Support `params` special parameter
9
+
3
10
  ## v2.2.4
4
11
 
5
12
  * Fix rails engine loading if sprockets is not in Gemfile. Fixes [#294](https://github.com/railsware/js-routes/issues/294)
data/Readme.md CHANGED
@@ -1,10 +1,8 @@
1
1
  # JsRoutes
2
- [![Build Status](https://travis-ci.org/railsware/js-routes.svg?branch=master)](https://travis-ci.org/railsware/js-routes)
3
- [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Frailsware%2Fjs-routes.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Frailsware%2Fjs-routes?ref=badge_shield)
4
2
 
5
- Generates javascript file that defines all Rails named routes as javascript helpers
3
+ [![CI](https://github.com/railsware/js-routes/actions/workflows/ci.yml/badge.svg)](https://github.com/railsware/js-routes/actions/workflows/ci.yml)
6
4
 
7
- [UPGRADE TO 2.0](./VERSION_2_UPGRADE.md)
5
+ Generates javascript file that defines all Rails named routes as javascript helpers
8
6
 
9
7
  ## Intallation
10
8
 
@@ -54,15 +52,15 @@ Add the following to `config/environments/development.rb`:
54
52
  config.middleware.use(JsRoutes::Middleware)
55
53
  ```
56
54
 
57
- Use it in `app/javascript/packs/application.js`:
55
+ Use it in any JS file:
58
56
 
59
57
  ``` javascript
60
- import * as Routes from '../routes';
61
- // window.Routes = Routes;
62
- alert(Routes.post_path(1))
58
+ import {post_path} from '../routes';
59
+
60
+ alert(post_path(1))
63
61
  ```
64
62
 
65
- Upgrade `rake assets:precompile` to update js-routes files:
63
+ Upgrade `rake assets:precompile` to update js-routes files in `Rakefile`:
66
64
 
67
65
  ``` ruby
68
66
  namespace :assets do
@@ -133,11 +131,12 @@ Create routes file `app/javascript/routes.js.erb`:
133
131
  <%= JsRoutes.generate() %>
134
132
  ```
135
133
 
136
- Use routes wherever you need them `app/javascript/packs/application.js`:
134
+ Use routes wherever you need them:
137
135
 
138
136
  ``` javascript
139
- import * as Routes from 'routes.js.erb';
140
- window.Routes = Routes;
137
+ import {post_path} from 'routes.js.erb';
138
+
139
+ alert(post_path(2));
141
140
  ```
142
141
 
143
142
  <div id='advanced-setup'></div>
@@ -175,14 +174,20 @@ You can manipulate the generated helper manually by injecting ruby into javascri
175
174
  export const routes = <%= JsRoutes.generate(module_type: nil, namespace: nil) %>
176
175
  ```
177
176
 
178
- If you want to generate the routes files outside of the asset pipeline, you can use `JsRoutes.generate!`:
177
+ If you want to generate the routes files manually with custom options, you can use `JsRoutes.generate!`:
179
178
 
180
179
  ``` ruby
181
180
  path = Rails.root.join("app/javascript")
182
181
 
183
- JsRoutes.generate!("#{path}/app_routes.js", exclude: [/^admin_/, /^api_/])
184
- JsRoutes.generate!("#{path}/adm_routes.js", include: /^admin_/)
185
- JsRoutes.generate!("#{path}/api_routes.js", include: /^api_/, default_url_options: {format: "json"})
182
+ JsRoutes.generate!(
183
+ "#{path}/app_routes.js", exclude: [/^admin_/, /^api_/]
184
+ )
185
+ JsRoutes.generate!(
186
+ "#{path}/adm_routes.js", include: /^admin_/
187
+ )
188
+ JsRoutes.generate!(
189
+ "#{path}/api_routes.js", include: /^api_/, default_url_options: {format: "json"}
190
+ )
186
191
  ```
187
192
 
188
193
  <div id='definitions'></div>
@@ -252,11 +257,12 @@ end
252
257
  Or dynamically in JavaScript, although only [Formatter Options](#formatter-options) are supported:
253
258
 
254
259
  ``` js
255
- import * as Routes from 'routes'
256
- Routes.configure({
260
+ import {configure, config} from 'routes'
261
+
262
+ configure({
257
263
  option: value
258
264
  });
259
- Routes.config(); // current config
265
+ config(); // current config
260
266
  ```
261
267
 
262
268
  ### Available Options
@@ -290,7 +296,7 @@ Options to configure JavaScript file generator. These options are only available
290
296
  * Note: generated URLs will first use the protocol, host, and port options specified in the route definition. Otherwise, the URL will be based on the option specified in the `default_url_options` config. If no default option has been set, then the URL will fallback to the current URL based on `window.location`.
291
297
  * `compact` - Remove `_path` suffix in path routes(`*_url` routes stay untouched if they were enabled)
292
298
  * Default: `false`
293
- * Sample route call when option is set to true: Routes.users() => `/users`
299
+ * Sample route call when option is set to true: `users() // => /users`
294
300
  * `application` - a key to specify which rails engine you want to generate routes too.
295
301
  * This option allows to only generate routes for a specific rails engine, that is mounted into routes instead of all Rails app routes
296
302
  * Default: `Rails.application`
@@ -323,41 +329,44 @@ Options to configure routes formatting. These options are available both in Ruby
323
329
  Configuration above will create a nice javascript file with `Routes` object that has all the rails routes available:
324
330
 
325
331
  ``` js
326
- import * as Routes from 'routes';
332
+ import {
333
+ user_path, user_project_path, company_path
334
+ } as Routes from 'routes';
327
335
 
328
- Routes.users_path()
336
+ users_path()
329
337
  // => "/users"
330
338
 
331
- Routes.user_path(1)
339
+ user_path(1)
332
340
  // => "/users/1"
333
341
 
334
- Routes.user_path(1, {format: 'json'})
342
+ user_path(1, {format: 'json'})
335
343
  // => "/users/1.json"
336
344
 
337
- Routes.user_path(1, {anchor: 'profile'})
345
+ user_path(1, {anchor: 'profile'})
338
346
  // => "/users/1#profile"
339
347
 
340
- Routes.new_user_project_path(1, {format: 'json'})
348
+ new_user_project_path(1, {format: 'json'})
341
349
  // => "/users/1/projects/new.json"
342
350
 
343
- Routes.user_project_path(1,2, {q: 'hello', custom: true})
351
+ user_project_path(1,2, {q: 'hello', custom: true})
344
352
  // => "/users/1/projects/2?q=hello&custom=true"
345
353
 
346
- Routes.user_project_path(1,2, {hello: ['world', 'mars']})
354
+ user_project_path(1,2, {hello: ['world', 'mars']})
347
355
  // => "/users/1/projects/2?hello%5B%5D=world&hello%5B%5D=mars"
348
356
 
349
357
  var google = {id: 1, name: "Google"};
350
- Routes.company_path(google)
358
+ company_path(google)
351
359
  // => "/companies/1"
352
360
 
353
361
  var google = {id: 1, name: "Google", to_param: "google"};
354
- Routes.company_path(google)
362
+ company_path(google)
355
363
  // => "/companies/google"
356
364
  ```
357
365
 
358
366
  In order to make routes helpers available globally:
359
367
 
360
368
  ``` js
369
+ import * as Routes from '../routes';
361
370
  jQuery.extend(window, Routes)
362
371
  ```
363
372
 
@@ -366,22 +375,18 @@ jQuery.extend(window, Routes)
366
375
  Possible to get `spec` of route by function `toString`:
367
376
 
368
377
  ```js
369
- Routes.users_path.toString() // => "/users(.:format)"
370
- Routes.user_path.toString() // => "/users/:id(.:format)"
371
- ```
372
-
373
- This function allow to get the same `spec` for route, if you will get string representation of the route function:
378
+ import {user_path, users_path} from '../routes'
374
379
 
375
- ```js
376
- '' + Routes.users_path // => "/users(.:format)", a string representation of the object
377
- '' + Routes.user_path // => "/users/:id(.:format)"
380
+ users_path.toString() // => "/users(.:format)"
381
+ user_path.toString() // => "/users/:id(.:format)"
378
382
  ```
379
383
 
384
+
380
385
  Route function also contain method `requiredParams` inside which returns required param names array:
381
386
 
382
387
  ```js
383
- Routes.users_path.requiredParams() // => []
384
- Routes.user_path.requiredParams() // => ['id']
388
+ users_path.requiredParams() // => []
389
+ user_path.requiredParams() // => ['id']
385
390
  ```
386
391
 
387
392
 
@@ -395,8 +400,10 @@ Sometimes the destinction between JS Hash and Object can not be found by JsRoute
395
400
  In this case you would need to pass a special key to help:
396
401
 
397
402
  ``` js
398
- Routes.company_project_path({company_id: 1, id: 2}) // => Not enough parameters
399
- Routes.company_project_path({company_id: 1, id: 2, _options: true}) // => "/companies/1/projects/2"
403
+ import {company_project_path} from '../routes'
404
+
405
+ company_project_path({company_id: 1, id: 2}) // => Not enough parameters
406
+ company_project_path({company_id: 1, id: 2, _options: true}) // => "/companies/1/projects/2"
400
407
  ```
401
408
 
402
409
 
@@ -417,7 +424,7 @@ import {
417
424
  inbox_message_path,
418
425
  inbox_attachment_path,
419
426
  user_path,
420
- } from 'routes.js.erb'
427
+ } from '../routes'
421
428
  ```
422
429
 
423
430
  Such import structure allows for moddern JS bundlers like [Webpack](https://webpack.js.org/) to only include explicitly imported routes into JS bundle file.
@@ -5,4 +5,4 @@ source "http://rubygems.org"
5
5
  gem "railties", "~> 5.0.5"
6
6
  gem "sprockets", "~> 3.0"
7
7
 
8
- gemspec :path => "../"
8
+ gemspec path: "../"
@@ -5,4 +5,4 @@ source "http://rubygems.org"
5
5
  gem "railties", "~> 5.1.3"
6
6
  gem "sprockets", "~> 3.0"
7
7
 
8
- gemspec :path => "../"
8
+ gemspec path: "../"
@@ -5,4 +5,4 @@ source "http://rubygems.org"
5
5
  gem "railties", "~> 5.2.3"
6
6
  gem "sprockets", "~> 3.0"
7
7
 
8
- gemspec :path => "../"
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "railties", "~> 7.0.3.1"
6
+ gem "sprockets", "~> 4.0"
7
+
8
+ gemspec path: "../"
@@ -29,7 +29,8 @@ end
29
29
 
30
30
 
31
31
  class Engine < ::Rails::Engine
32
- if defined?(::Sprockets::Railtie)
32
+ def self.install_sprockets!
33
+ return if defined?(@installed_sprockets)
33
34
  require 'sprockets/version'
34
35
  v2 = Gem::Dependency.new('', ' ~> 2')
35
36
  vgte3 = Gem::Dependency.new('', ' >= 3')
@@ -58,6 +59,10 @@ class Engine < ::Rails::Engine
58
59
  raise StandardError, "Sprockets version #{sprockets_version} is not supported"
59
60
  end
60
61
  end
62
+ @installed_sprockets = true
63
+ end
64
+ if defined?(::Sprockets::Railtie)
65
+ install_sprockets!
61
66
  end
62
67
  end
63
68
  end
@@ -52,7 +52,7 @@ end
52
52
  {},
53
53
  {module_type: 'DTS'}
54
54
  ].map do |config|
55
- File.join('/', JsRoutes.new(config).configuration.output_file) + "\n"
55
+ File.join('/', JsRoutes::Configuration.new(config).output_file) + "\n"
56
56
  end.join
57
57
  end
58
58
  end
@@ -2,7 +2,7 @@ require "js_routes/configuration"
2
2
  require "js_routes/route"
3
3
 
4
4
  module JsRoutes
5
- class Instance
5
+ class Instance # :nodoc:
6
6
 
7
7
  attr_reader :configuration
8
8
  #
@@ -55,7 +55,7 @@ module JsRoutes
55
55
  'ROUTES_OBJECT' => routes_object,
56
56
  'RAILS_VERSION' => ActionPack.version,
57
57
  'DEPRECATED_GLOBBING_BEHAVIOR' => ActionPack::VERSION::MAJOR == 4 && ActionPack::VERSION::MINOR == 0,
58
-
58
+ 'DEPRECATED_FALSE_PARAMETER_BEHAVIOR' => ActionPack::VERSION::MAJOR < 7,
59
59
  'APP_CLASS' => application.class.to_s,
60
60
  'DEFAULT_URL_OPTIONS' => json(@configuration.default_url_options),
61
61
  'PREFIX' => json(@configuration.prefix),
@@ -21,12 +21,16 @@ module JsRoutes
21
21
  def update_js_routes
22
22
  new_mtime = routes_mtime
23
23
  unless new_mtime == @mtime
24
- JsRoutes.generate!
25
- JsRoutes.definitions!
24
+ regenerate
26
25
  @mtime = new_mtime
27
26
  end
28
27
  end
29
28
 
29
+ def regenerate
30
+ JsRoutes.generate!
31
+ JsRoutes.definitions!
32
+ end
33
+
30
34
  def routes_mtime
31
35
  File.mtime(@routes_file)
32
36
  rescue Errno::ENOENT
@@ -1,3 +1,3 @@
1
1
  module JsRoutes
2
- VERSION = "2.2.3"
2
+ VERSION = "2.2.5"
3
3
  end
data/lib/routes.d.ts CHANGED
@@ -53,6 +53,7 @@ declare type KeywordUrlOptions = Optional<{
53
53
  port: string | number;
54
54
  anchor: string;
55
55
  trailing_slash: boolean;
56
+ params: RouteParameters;
56
57
  }>;
57
58
  declare type RouteOptions = KeywordUrlOptions & RouteParameters;
58
59
  declare type PartsTable = Record<string, {
@@ -63,6 +64,7 @@ declare type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "DTS" | "NIL";
63
64
  declare const RubyVariables: {
64
65
  PREFIX: string;
65
66
  DEPRECATED_GLOBBING_BEHAVIOR: boolean;
67
+ DEPRECATED_FALSE_PARAMETER_BEHAVIOR: boolean;
66
68
  SPECIAL_OPTIONS_KEY: string;
67
69
  DEFAULT_URL_OPTIONS: RouteParameters;
68
70
  SERIALIZER: Serializer;
data/lib/routes.js CHANGED
@@ -2,7 +2,10 @@
2
2
  * File generated by js-routes RubyVariables.GEM_VERSION
3
3
  * Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4
4
  */
5
- RubyVariables.WRAPPER(() => {
5
+ // eslint-disable-next-line
6
+ RubyVariables.WRAPPER(
7
+ // eslint-disable-next-line
8
+ () => {
6
9
  const hasProp = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
7
10
  let NodeTypes;
8
11
  (function (NodeTypes) {
@@ -176,7 +179,11 @@ RubyVariables.WRAPPER(() => {
176
179
  }
177
180
  path_identifier(object) {
178
181
  const result = this.unwrap_path_identifier(object);
179
- return this.is_nullable(result) || result === false ? "" : "" + result;
182
+ return this.is_nullable(result) ||
183
+ (RubyVariables.DEPRECATED_FALSE_PARAMETER_BEHAVIOR &&
184
+ result === false)
185
+ ? ""
186
+ : "" + result;
180
187
  }
181
188
  unwrap_path_identifier(object) {
182
189
  let result = object;
@@ -222,12 +229,23 @@ RubyVariables.WRAPPER(() => {
222
229
  ...options,
223
230
  };
224
231
  const keyword_parameters = {};
225
- const query_parameters = {};
232
+ let query_parameters = {};
226
233
  for (const key in options) {
227
234
  if (!hasProp(options, key))
228
235
  continue;
229
236
  const value = options[key];
230
- if (this.is_reserved_option(key)) {
237
+ if (key === "params") {
238
+ if (this.is_object(value)) {
239
+ query_parameters = {
240
+ ...query_parameters,
241
+ ...value,
242
+ };
243
+ }
244
+ else {
245
+ throw new Error("params value should always be an object");
246
+ }
247
+ }
248
+ else if (this.is_reserved_option(key)) {
231
249
  keyword_parameters[key] = value;
232
250
  }
233
251
  else {
@@ -251,7 +269,7 @@ RubyVariables.WRAPPER(() => {
251
269
  return { keyword_parameters, query_parameters };
252
270
  }
253
271
  build_route(parts, required_params, default_options, route, absolute, args) {
254
- const { keyword_parameters, query_parameters, } = this.partition_parameters(parts, required_params, default_options, args);
272
+ const { keyword_parameters, query_parameters } = this.partition_parameters(parts, required_params, default_options, args);
255
273
  const missing_params = required_params.filter((param) => !hasProp(query_parameters, param) ||
256
274
  this.is_nullable(query_parameters[param]));
257
275
  if (missing_params.length) {
data/lib/routes.ts CHANGED
@@ -70,6 +70,7 @@ type KeywordUrlOptions = Optional<{
70
70
  port: string | number;
71
71
  anchor: string;
72
72
  trailing_slash: boolean;
73
+ params: RouteParameters;
73
74
  }>;
74
75
 
75
76
  type RouteOptions = KeywordUrlOptions & RouteParameters;
@@ -81,6 +82,7 @@ type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "DTS" | "NIL";
81
82
  declare const RubyVariables: {
82
83
  PREFIX: string;
83
84
  DEPRECATED_GLOBBING_BEHAVIOR: boolean;
85
+ DEPRECATED_FALSE_PARAMETER_BEHAVIOR: boolean;
84
86
  SPECIAL_OPTIONS_KEY: string;
85
87
  DEFAULT_URL_OPTIONS: RouteParameters;
86
88
  SERIALIZER: Serializer;
@@ -95,7 +97,9 @@ declare const define:
95
97
 
96
98
  declare const module: { exports: any } | undefined;
97
99
 
100
+ // eslint-disable-next-line
98
101
  RubyVariables.WRAPPER(
102
+ // eslint-disable-next-line
99
103
  (): RouterExposedMethods => {
100
104
  const hasProp = (value: unknown, key: string) =>
101
105
  Object.prototype.hasOwnProperty.call(value, key);
@@ -222,7 +226,7 @@ RubyVariables.WRAPPER(
222
226
  "port",
223
227
  "protocol",
224
228
  ] as const;
225
- type ReservedOption = typeof ReservedOptions[any];
229
+ type ReservedOption = (typeof ReservedOptions)[any];
226
230
 
227
231
  class UtilsClass {
228
232
  configuration: Configuration = {
@@ -233,7 +237,7 @@ RubyVariables.WRAPPER(
233
237
  RubyVariables.SERIALIZER || this.default_serializer.bind(this),
234
238
  };
235
239
 
236
- default_serializer(value: any, prefix?: string | null): string {
240
+ default_serializer(value: unknown, prefix?: string | null): string {
237
241
  if (this.is_nullable(value)) {
238
242
  return "";
239
243
  }
@@ -292,14 +296,16 @@ RubyVariables.WRAPPER(
292
296
  }
293
297
  return {
294
298
  args: args.slice(0, args.length - 1),
295
- options: (last_el as any) as RouteOptions,
299
+ options: last_el as unknown as RouteOptions,
296
300
  };
297
301
  } else {
298
302
  return { args, options: {} };
299
303
  }
300
304
  }
301
305
 
302
- looks_like_serialized_model(object: any): object is ModelRouteParameter {
306
+ looks_like_serialized_model(
307
+ object: unknown
308
+ ): object is ModelRouteParameter {
303
309
  return (
304
310
  this.is_object(object) &&
305
311
  !(this.configuration.special_options_key in object) &&
@@ -309,11 +315,15 @@ RubyVariables.WRAPPER(
309
315
 
310
316
  path_identifier(object: QueryRouteParameter): string {
311
317
  const result = this.unwrap_path_identifier(object);
312
- return this.is_nullable(result) || result === false ? "" : "" + result;
318
+ return this.is_nullable(result) ||
319
+ (RubyVariables.DEPRECATED_FALSE_PARAMETER_BEHAVIOR &&
320
+ result === false)
321
+ ? ""
322
+ : "" + result;
313
323
  }
314
324
 
315
325
  unwrap_path_identifier(object: QueryRouteParameter): unknown {
316
- let result: any = object;
326
+ let result: unknown = object;
317
327
  if (!this.is_object(object)) {
318
328
  return object;
319
329
  }
@@ -365,11 +375,20 @@ RubyVariables.WRAPPER(
365
375
  };
366
376
 
367
377
  const keyword_parameters: KeywordUrlOptions = {};
368
- const query_parameters: RouteParameters = {};
378
+ let query_parameters: RouteParameters = {};
369
379
  for (const key in options) {
370
380
  if (!hasProp(options, key)) continue;
371
381
  const value = options[key];
372
- if (this.is_reserved_option(key)) {
382
+ if (key === "params") {
383
+ if (this.is_object(value)) {
384
+ query_parameters = {
385
+ ...query_parameters,
386
+ ...(value as RouteParameters),
387
+ };
388
+ } else {
389
+ throw new Error("params value should always be an object");
390
+ }
391
+ } else if (this.is_reserved_option(key)) {
373
392
  keyword_parameters[key] = value as any;
374
393
  } else {
375
394
  if (
@@ -401,15 +420,13 @@ RubyVariables.WRAPPER(
401
420
  absolute: boolean,
402
421
  args: OptionalRouteParameter[]
403
422
  ): string {
404
- const {
405
- keyword_parameters,
406
- query_parameters,
407
- } = this.partition_parameters(
408
- parts,
409
- required_params,
410
- default_options,
411
- args
412
- );
423
+ const { keyword_parameters, query_parameters } =
424
+ this.partition_parameters(
425
+ parts,
426
+ required_params,
427
+ default_options,
428
+ args
429
+ );
413
430
  const missing_params = required_params.filter(
414
431
  (param) =>
415
432
  !hasProp(query_parameters, param) ||
data/package.json CHANGED
@@ -12,13 +12,13 @@
12
12
  "dependencies": {
13
13
  "@typescript-eslint/eslint-plugin": "^4.9.0",
14
14
  "@typescript-eslint/parser": "^4.9.0",
15
- "eslint": "^7.14.0",
16
- "eslint-config-prettier": "^6.15.0",
17
- "eslint-plugin-import": "^2.22.1",
15
+ "eslint": "^8.35.0",
16
+ "eslint-config-prettier": "^8.7.0",
17
+ "eslint-plugin-import": "^2.27.5",
18
18
  "husky": "^4.3.0",
19
19
  "lint-staged": "^10.5.2",
20
20
  "pinst": "^2.1.1",
21
- "prettier": "^2.2.1"
21
+ "prettier": "^2.8.4"
22
22
  },
23
23
  "scripts": {
24
24
  "build": "tsc && yarn lint:fix",
@@ -31,7 +31,8 @@
31
31
  }
32
32
  },
33
33
  "lint-staged": {
34
- "./lib/routes.ts": ["yarn lint:fix" ]
34
+ "./lib/routes.ts": [
35
+ "yarn lint:fix"
36
+ ]
35
37
  }
36
-
37
38
  }
@@ -3,29 +3,29 @@ require "spec_helper"
3
3
  describe JsRoutes, "#serialize" do
4
4
 
5
5
  before(:each) do
6
- evaljs(JsRoutes.generate({module_type: nil, namespace: 'Routes'}))
6
+ evaljs(JsRoutes.generate(module_type: nil, namespace: 'Routes'))
7
7
  end
8
8
 
9
9
  it "should provide this method" do
10
- expect(evaljs("Routes.serialize({a: 1, b: [2,3], c: {d: 4, e: 5}, f: ''})")).to eq(
10
+ expectjs("Routes.serialize({a: 1, b: [2,3], c: {d: 4, e: 5}, f: ''})").to eq(
11
11
  "a=1&b%5B%5D=2&b%5B%5D=3&c%5Bd%5D=4&c%5Be%5D=5&f="
12
12
  )
13
13
  end
14
14
 
15
15
  it "should provide this method" do
16
- expect(evaljs("Routes.serialize({a: 1, b: [2,3], c: {d: 4, e: 5}, f: ''})")).to eq(
16
+ expectjs("Routes.serialize({a: 1, b: [2,3], c: {d: 4, e: 5}, f: ''})").to eq(
17
17
  "a=1&b%5B%5D=2&b%5B%5D=3&c%5Bd%5D=4&c%5Be%5D=5&f="
18
18
  )
19
19
  end
20
20
 
21
21
  it "works with JS suckiness" do
22
- expect(evaljs(
22
+ expectjs(
23
23
  [
24
24
  "const query = Object.create(null);",
25
25
  "query.a = 1;",
26
26
  "query.b = 2;",
27
27
  "Routes.serialize(query);",
28
28
  ].join("\n")
29
- )).to eq("a=1&b=2")
29
+ ).to eq("a=1&b=2")
30
30
  end
31
31
  end