js-routes 1.4.13 → 2.0.3

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: 597119aa17518bd0b078b80c99c5f2e2a4cb4f5fa58526872b8d960acb9b6102
4
- data.tar.gz: 285c6f0aca0c140822e89b65afead328bbddc6eb3a08ef85944b68e8c3068515
3
+ metadata.gz: 59b90f4f1eabd64f07f185e7fc3e26cc90ff44c0e705191543bfff3cb149e3ef
4
+ data.tar.gz: 3f10d123ffa344bf190f014e92796d51c4b3a63ea285ef4ee66376ce44adb7e7
5
5
  SHA512:
6
- metadata.gz: 501157d25557db0b0511acaa3226ca0fefbb3520bbec48ca9f1005e7300eb2dc0c115e9eae42ef085c17a78c8f2c0822c8b226cb8df639f20a81d9b62de6a421
7
- data.tar.gz: fecba94667f0a316142d03b8c5b4ca3a95790278a65de6c2b247805d82c16adece41d1a5f395af38913dbd811cdd2c6ad4a8b80c3cae2b30ecc5eec96a27a1c3
6
+ metadata.gz: f2211d6912507490776862a1f9a807b73ae2c77abaa247cfa71afcb8fbb87ec0c6f2fbb24d9219aff51a04e9eb8d47ab43c6e38ec0e598975bb3ccf5b2157b46
7
+ data.tar.gz: 8d2227c3bea7dec3fd3e92fd006752ce4af370f181f0e7115dc9870fb3b5f9d0ba4540398c6eae4de86096d1cf5a2b57de3396b08b63c2326191d0e1f0d5490a
data/.eslintrc.js ADDED
@@ -0,0 +1,15 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: [
4
+ 'eslint:recommended',
5
+ 'plugin:@typescript-eslint/eslint-recommended',
6
+ 'plugin:@typescript-eslint/recommended',
7
+ 'prettier/@typescript-eslint',
8
+ ],
9
+ parser: '@typescript-eslint/parser',
10
+ plugins: ['@typescript-eslint'],
11
+ rules: {
12
+ '@typescript-eslint/ban-types': 'off',
13
+ '@typescript-eslint/no-explicit-any': 'off'
14
+ },
15
+ };
data/.gitignore CHANGED
@@ -14,6 +14,7 @@ log
14
14
 
15
15
  # jeweler generated
16
16
  pkg
17
+ tmp
17
18
 
18
19
  node_modules
19
20
 
@@ -60,3 +61,5 @@ gemfiles/*.lock
60
61
  /spec/dummy/app/assets/javascripts/routes.js
61
62
  /spec/dummy/logs
62
63
  /spec/dummy/tmp
64
+ node_modules
65
+
data/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 14
data/.travis.yml CHANGED
@@ -5,7 +5,6 @@ before_install:
5
5
  - gem install bundler # need for jruby and ruby-head
6
6
 
7
7
  rvm:
8
- - 2.3.1
9
8
  - 2.4.1
10
9
  - 2.5.3
11
10
  - 2.6.0
@@ -22,6 +21,8 @@ gemfile:
22
21
  - gemfiles/rails50_sprockets_3.gemfile
23
22
  - gemfiles/rails51_sprockets_3.gemfile
24
23
  - gemfiles/rails52_sprockets_3.gemfile
24
+ env:
25
+ TRAVIS_CI: true
25
26
 
26
27
  sudo: false
27
28
  dist: xenial
data/CHANGELOG.md CHANGED
@@ -1,5 +1,32 @@
1
1
  ## master
2
2
 
3
+ ## v2.0.3
4
+
5
+ * Fixed backward compatibility issue [#275](https://github.com/railsware/js-routes/issues/275)
6
+
7
+ ## v2.0.2
8
+
9
+ * Fixed backward compatibility issue [#274](https://github.com/railsware/js-routes/issues/274)
10
+
11
+ ## v2.0.1
12
+
13
+ * Fixed backward compatibility issue [#272](https://github.com/railsware/js-routes/issues/272)
14
+
15
+ ## v2.0.0
16
+
17
+ Version 2.0 has some breaking changes.
18
+ See [UPGRADE TO 2.0](./VERSION_2_UPGRADE.md) for guidance.
19
+
20
+ * `module_type` option support
21
+ * `documentation` option spport
22
+ * Migrated implementation to typescript
23
+ * ESM tree shaking support
24
+ * Support camel case `toParam` version of `to_param` property
25
+
26
+ ## v1.4.14
27
+
28
+ * Fix compatibility with UMD modules #237 [Comment](https://github.com/railsware/js-routes/issues/237#issuecomment-752754679)
29
+
3
30
  ## v1.4.13
4
31
 
5
32
  * Improve compatibility with node environment #269.
data/Rakefile CHANGED
@@ -12,7 +12,10 @@ require 'bundler/gem_tasks'
12
12
  require 'rspec/core'
13
13
  require 'rspec/core/rake_task'
14
14
  require 'appraisal'
15
- load "rails/tasks/routes.rake"
15
+ require 'rails/version'
16
+ if Rails.version < "6.1"
17
+ load "rails/tasks/routes.rake"
18
+ end
16
19
 
17
20
  RSpec::Core::RakeTask.new(:spec)
18
21
 
@@ -29,6 +32,6 @@ namespace :spec do
29
32
  draw_routes
30
33
  all_routes = Rails.application.routes.routes
31
34
  inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
32
- puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, ENV['CONTROLLER'])
35
+ puts inspector.format(ActionDispatch::Routing::ConsoleFormatter::Sheet.new)
33
36
  end
34
37
  end
data/Readme.md CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  Generates javascript file that defines all Rails named routes as javascript helpers
6
6
 
7
+ [UPGRADE TO 2.0](./VERSION_2_UPGRADE.md)
8
+
7
9
  ## Intallation
8
10
 
9
11
  Your Rails Gemfile:
@@ -14,6 +16,8 @@ gem "js-routes"
14
16
 
15
17
  ## Setup
16
18
 
19
+ ### Quick Start
20
+
17
21
  Run:
18
22
 
19
23
  ```
@@ -23,19 +27,24 @@ rake js:routes
23
27
  Make routes available globally in `app/javascript/packs/application.js`:
24
28
 
25
29
  ``` javascript
26
- window.Routes = require('routes');
30
+ import * as Routes from 'routes';
31
+ window.Routes = Routes;
27
32
  ```
28
33
 
29
34
  Individual routes can be imported using:
30
35
 
31
36
  ``` javascript
32
- import {edit_post_path, new_post_path} from 'routes';
37
+ import {edit_post_path} from 'routes';
38
+ console.log(edit_post_path(1))
33
39
  ```
34
40
 
35
41
  **Note**: that this setup requires `rake js:routes` to be run each time routes file is updated.
36
42
 
43
+ <div id='webpacker'></div>
44
+
37
45
  #### Webpacker + automatic updates
38
46
 
47
+
39
48
  This setup can automatically update your routes without `rake js:routes` being called manually.
40
49
  It requires [rails-erb-loader](https://github.com/usabilityhub/rails-erb-loader) npm package to work.
41
50
 
@@ -62,9 +71,9 @@ module.exports = {
62
71
  }
63
72
  ```
64
73
 
65
- Enable `erb` extension in `config/webpacker/environment.js`:
74
+ Enable `erb` extension in `config/webpack/environment.js`:
66
75
 
67
- ```
76
+ ``` javascript
68
77
  const erb = require('./loaders/erb')
69
78
  environment.loaders.append('erb', erb)
70
79
  ```
@@ -78,13 +87,23 @@ Create routes file `app/javascript/routes.js.erb`:
78
87
  Use routes wherever you need them `app/javascript/packs/application.js`:
79
88
 
80
89
  ``` javascript
81
- window.Routes = require('routes.js.erb');
90
+ import * as Routes from 'routes.js.erb';
91
+ window.Routes = Routes;
82
92
  ```
83
93
 
84
94
  #### Sprockets (Deprecated)
85
95
 
86
96
  If you are using [Sprockets](https://github.com/rails/sprockets-rails) you may configure js-routes in the following way.
87
97
 
98
+ Setup the initializer (e.g. `config/initializers/js_routes.rb`):
99
+
100
+ ``` ruby
101
+ JsRoutes.setup do |config|
102
+ config.module_type = nil
103
+ config.namespace = 'Routes'
104
+ end
105
+ ```
106
+
88
107
  Require JsRoutes in `app/assets/javascripts/application.js` or other bundle
89
108
 
90
109
  ``` js
@@ -114,6 +133,7 @@ end
114
133
  Or dynamically in JavaScript, although only [Formatter Options](#formatter-options) are supported (see below)
115
134
 
116
135
  ``` js
136
+ import * as Routes from 'routes'
117
137
  Routes.configure({
118
138
  option: value
119
139
  });
@@ -126,6 +146,12 @@ Routes.config(); // current config
126
146
 
127
147
  Options to configure JavaScript file generator. These options are only available in Ruby context but not JavaScript.
128
148
 
149
+ * `module_type` - JavaScript module type for generated code. [Article](https://dev.to/iggredible/what-the-heck-are-cjs-amd-umd-and-esm-ikm)
150
+ * Options: `ESM`, `UMD`, `CJS`, `AMD`, `nil`.
151
+ * Default: `ESM`
152
+ * `nil` option can be used in case you don't want generated code to export anything.
153
+ * `documentation` - specifies if each route should be annotated with [JSDoc](https://jsdoc.app/) comment
154
+ * Default: `true`
129
155
  * `exclude` - Array of regexps to exclude from routes.
130
156
  * Default: `[]`
131
157
  * The regexp applies only to the name before the `_path` suffix, eg: you want to match exactly `settings_path`, the regexp should be `/^settings$/`
@@ -134,11 +160,10 @@ Options to configure JavaScript file generator. These options are only available
134
160
  * The regexp applies only to the name before the `_path` suffix, eg: you want to match exactly `settings_path`, the regexp should be `/^settings$/`
135
161
  * `namespace` - global object used to access routes.
136
162
  * Supports nested namespace like `MyProject.routes`
137
- * Default: `Routes`
138
- * `camel_case` - Generate camel case route names.
163
+ * Default: `nil`
164
+ * `camel_case` - specifies if route helpers should be generated in camel case instead of underscore case.
139
165
  * Default: `false`
140
- * `url_links` - Generate `*_url` helpers (in addition to the default `*_path` helpers).
141
- * Example: `true`
166
+ * `url_links` - specifies if `*_url` helpers should be generated (in addition to the default `*_path` helpers).
142
167
  * Default: `false`
143
168
  * 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`.
144
169
  * `compact` - Remove `_path` suffix in path routes(`*_url` routes stay untouched if they were enabled)
@@ -173,29 +198,30 @@ Options to configure routes formatting. These options are available both in Ruby
173
198
  In case you need multiple route files for different parts of your application, you have to create the files manually.
174
199
  If your application has an `admin` and an `application` namespace for example:
175
200
 
176
- ```
177
- # app/javascript/admin/routes.js.erb
178
- <%= JsRoutes.generate(namespace: "AdminRoutes", include: /admin/) %>
201
+ ``` erb
202
+ // app/javascript/admin/routes.js.erb
203
+ <%= JsRoutes.generate(include: /admin/) %>
179
204
  ```
180
205
 
181
- ```
182
- # app/javascript/customer/routes.js.erb
183
- <%= JsRoutes.generate(namespace: "CustomerRoutes", exclude: /admin/) %>
206
+ ``` erb
207
+ // app/javascript/customer/routes.js.erb
208
+ <%= JsRoutes.generate(exclude: /admin/) %>
184
209
  ```
185
210
 
186
- In order to generate the routes JS code to a string:
211
+ You can manipulate the generated helper manually by injecting ruby into javascript:
187
212
 
188
- ``` ruby
189
- routes_js = JsRoutes.generate(options)
213
+ ``` erb
214
+ export const routes = <%= JsRoutes.generate(module_type: nil, namespace: nil) %>
190
215
  ```
191
216
 
192
217
  If you want to generate the routes files outside of the asset pipeline, you can use `JsRoutes.generate!`:
193
218
 
194
219
  ``` ruby
195
- path = "app/javascript"
196
- JsRoutes.generate!("#{path}/app_routes.js", namespace: "AppRoutes", exclude: [/^admin_/, /^api_/])
197
- JsRoutes.generate!("#{path}/adm_routes.js", namespace: "AdmRoutes", include: /^admin_/)
198
- JsRoutes.generate!("#{path}/api_routes.js", namespace: "ApiRoutes", include: /^api_/, default_url_options: {format: "json"})
220
+ path = Rails.root.join("app/javascript")
221
+
222
+ JsRoutes.generate!("#{path}/app_routes.js", exclude: [/^admin_/, /^api_/])
223
+ JsRoutes.generate!("#{path}/adm_routes.js", include: /^admin_/)
224
+ JsRoutes.generate!("#{path}/api_routes.js", include: /^api_/, default_url_options: {format: "json"})
199
225
  ```
200
226
 
201
227
  ## Usage
@@ -203,6 +229,8 @@ JsRoutes.generate!("#{path}/api_routes.js", namespace: "ApiRoutes", include: /^a
203
229
  Configuration above will create a nice javascript file with `Routes` object that has all the rails routes available:
204
230
 
205
231
  ``` js
232
+ import * as Routes from 'routes';
233
+
206
234
  Routes.users_path() // => "/users"
207
235
  Routes.user_path(1) // => "/users/1"
208
236
  Routes.user_path(1, {format: 'json'}) // => "/users/1.json"
@@ -243,11 +271,11 @@ This function allow to get the same `spec` for route, if you will get string rep
243
271
  '' + Routes.user_path // => "/users/:id(.:format)"
244
272
  ```
245
273
 
246
- Route function also contain inside attribute `required_params` required param names as array:
274
+ Route function also contain method `requiredParams` inside which returns required param names array:
247
275
 
248
276
  ```js
249
- Routes.users_path.required_params // => []
250
- Routes.user_path.required_params // => ['id']
277
+ Routes.users_path.requiredParams() // => []
278
+ Routes.user_path.requiredParams() // => ['id']
251
279
  ```
252
280
 
253
281
 
@@ -268,9 +296,34 @@ Routes.company_project_path({company_id: 1, id: 2, _options: true}) // => "/comp
268
296
 
269
297
  ## What about security?
270
298
 
271
- JsRoutes itself does not have security holes. It makes URLs
272
- without access protection more reachable by potential attacker.
273
- In order to prevent this use `:exclude` option for sensitive urls like `/admin_/`
299
+ JsRoutes itself does not have security holes.
300
+ It makes URLs without access protection more reachable by potential attacker.
301
+ If that is an issue for you, you may use one of the following solutions:
302
+
303
+ ### Explicit Import + ESM Tree shaking
304
+
305
+ Make sure `module_type` is set to `ESM` (the default) and JS files import only required routes into the file like:
306
+
307
+ ``` javascript
308
+ import {
309
+ inbox_path,
310
+ inboxes_path,
311
+ inbox_message_path,
312
+ inbox_attachment_path,
313
+ user_path,
314
+ } from 'routes.js.erb'
315
+ ```
316
+
317
+ ### Exclude option
318
+
319
+ Split your routes into multiple files related to each section of your website like:
320
+
321
+ ``` javascript
322
+ // admin-routes.js.erb
323
+ <%= JsRoutes.generate(include: /^admin_/)
324
+ // app-routes.js.erb
325
+ <%= JsRoutes.generate(exclude: /^admin_/)
326
+ ```
274
327
 
275
328
  ## JsRoutes and Heroku
276
329
 
@@ -290,11 +343,20 @@ There are some alternatives available. Most of them has only basic feature and d
290
343
  Advantages of this one are:
291
344
 
292
345
  * Rails 4,5,6 support
346
+ * [ESM Tree shaking](https://webpack.js.org/guides/tree-shaking/) support
293
347
  * Rich options set
294
348
  * Full rails compatibility
295
349
  * Support Rails `#to_param` convention for seo optimized paths
296
350
  * Well tested
297
351
 
352
+ ## Version 2 TODO
353
+
354
+ * Add routes generation .d.ts file
355
+ * Add config option on the output format: js, ts, d.ts
356
+ * Add prettier
357
+ * Add eslint
358
+ * Add development guide
359
+
298
360
  #### Thanks to [contributors](https://github.com/railsware/js-routes/contributors)
299
361
 
300
362
  #### Have fun
@@ -0,0 +1,61 @@
1
+ ## Version 2.0 upgrade notes
2
+
3
+ ### Using ESM module by default
4
+
5
+ The default setting are now changed:
6
+
7
+ Setting | Old | New
8
+ --- | --- | ---
9
+ module\_type | nil | ESM
10
+ namespace | Routes | nil
11
+
12
+ This is more optimized setup for WebPacker. You can restore the old configuration like this:
13
+
14
+ ``` ruby
15
+ JsRoutes.setup do |config|
16
+ config.module_type = nil
17
+ config.namespace = 'Routes'
18
+ end
19
+ ```
20
+
21
+ However, [ESM+Webpacker](/Readme.md#webpacker) upgrade is recommended.
22
+
23
+ ### `required_params` renamed
24
+
25
+ In case you are using `required_params` property, it is now renamed and converted to a method:
26
+
27
+ ``` javascript
28
+ // Old style
29
+ Routes.post_path.required_params // => ['id']
30
+ // New style
31
+ Routes.post_path.requiredParams() // => ['id']
32
+ ```
33
+
34
+ ### ParameterMissing error rework
35
+
36
+ `ParameterMissing` is renamed to `ParametersMissing` error and now list all missing parameters instead of just first encountered in its message. Missing parameters are now available via `ParametersMissing#keys` property.
37
+
38
+ ``` javascript
39
+ try {
40
+ return Routes.inbox_path();
41
+ } catch(error) {
42
+ if (error.name === 'ParametersMissing') {
43
+ console.warn(`Missing route keys ${error.keys.join(', ')}. Ignoring.`);
44
+ return "#";
45
+ } else {
46
+ throw error;
47
+ }
48
+ }
49
+ ```
50
+
51
+ ### JSDoc comment format
52
+
53
+ New version of js-routes generates function comment in the [JSDoc](https://jsdoc.app) format.
54
+ If you have any problems with that disable the annotation:
55
+
56
+ ``` ruby
57
+ JsRoutes.setup do |config|
58
+ config.documentation = false
59
+ end
60
+ ```
61
+
data/js-routes.gemspec CHANGED
@@ -7,13 +7,16 @@ Gem::Specification.new do |s|
7
7
  s.name = %q{js-routes}
8
8
  s.version = JsRoutes::VERSION
9
9
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ if s.respond_to? :required_rubygems_version=
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0")
12
+ end
11
13
  s.authors = ["Bogdan Gusiev"]
12
14
  s.description = %q{Generates javascript file that defines all Rails named routes as javascript helpers}
13
15
  s.email = %q{agresso@gmail.com}
14
16
  s.extra_rdoc_files = [
15
17
  "LICENSE.txt"
16
18
  ]
19
+ s.required_ruby_version = '>= 2.4.0'
17
20
  s.files = `git ls-files`.split("\n")
18
21
  s.homepage = %q{http://github.com/railsware/js-routes}
19
22
  s.licenses = ["MIT"]
@@ -24,13 +27,13 @@ Gem::Specification.new do |s|
24
27
  s.add_development_dependency(%q<sprockets-rails>)
25
28
  s.add_development_dependency(%q<rspec>, [">= 3.0.0"])
26
29
  s.add_development_dependency(%q<bundler>, [">= 1.1.0"])
27
- s.add_development_dependency(%q<coffee-script>, [">= 0"])
28
30
  s.add_development_dependency(%q<appraisal>, [">= 0.5.2"])
31
+ s.add_development_dependency(%q<bump>, [">= 0.10.0"])
29
32
  if defined?(JRUBY_VERSION)
30
33
  s.add_development_dependency(%q<therubyrhino>, [">= 2.0.4"])
31
34
  else
32
35
  s.add_development_dependency(%q<byebug>)
33
36
  s.add_development_dependency(%q<pry-byebug>)
34
- s.add_development_dependency(%q<mini_racer>, [">= 0.2.4"])
37
+ s.add_development_dependency(%q<mini_racer>, [">= 0.4.0"])
35
38
  end
36
39
  end