js_rails_routes 0.8.1 → 0.9.0

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: abaf98e11062f86106203b6b2d68bbf65c83c00e0781122019cc1d0ca20a5fe3
4
- data.tar.gz: 38cb07f953ea05bdb1a754a76bfc8e8fd2637c778b69eb35a6d39eb6484b5296
3
+ metadata.gz: ff931b1b9f29b4a86444a8613d653a32961bc378e86d44fdc25cf319bcd9cc86
4
+ data.tar.gz: ce054504cf1e89cde6d76bca05f5fb03834740e8ff0a2e9acdea76326ef3e8ce
5
5
  SHA512:
6
- metadata.gz: 8c2c5bff02e8b53f16ec78fa3e818b693786f454c5cf226a13e3c9d67371e69d1e3d5eae538e5c9c52116abf5394902311afe8f5604ffa56e34fe77ba925d930
7
- data.tar.gz: 10ebd6b90a0bf90b566ed76fa1aeef2ee87612c59c44728917fcc63107075d38c640e21cf450bd4b60d0479973aae407a14cd0112099cf33a134fd2677d2c814
6
+ metadata.gz: 932d0666655cf343988cdd19730ed038b662d6efa86c36a1e97c63a9f98a7c1cbe51a4abdc5a14903814f6a578e597a123e782ebb85a5c3dfa9e0807f7fbca25
7
+ data.tar.gz: b6c2ad6c62c1c95b6db17386468801a1fc543c60fed064b253a36458c3b14cb3614b9928c495a1f26d53e02946735a452106388ad5f46ebd8af96b494b5578a6
data/CHANGELOG.md CHANGED
@@ -6,6 +6,10 @@ This change log adheres to [keepachangelog.com](http://keepachangelog.com).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.9.0] - 2018-08-24
10
+ ### Added
11
+ - Improved TypeScript params typing support
12
+
9
13
  ## [0.8.1] - 2018-08-21
10
14
  ### Fixed
11
15
  - Camelize `params` keys as `camelize` option
@@ -66,7 +70,8 @@ This change log adheres to [keepachangelog.com](http://keepachangelog.com).
66
70
  ### Added
67
71
  - Implement "js:rails:routes" task
68
72
 
69
- [Unreleased]: https://github.com/yuku-t/js_rails_routes/compare/v0.8.1...HEAD
73
+ [Unreleased]: https://github.com/yuku-t/js_rails_routes/compare/v0.9.0...HEAD
74
+ [0.9.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.8.1...v0.9.0
70
75
  [0.8.1]: https://github.com/yuku-t/js_rails_routes/compare/v0.8.0...v0.8.1
71
76
  [0.8.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.7.1...v0.8.0
72
77
  [0.7.1]: https://github.com/yuku-t/js_rails_routes/compare/v0.7.0...v0.7.1
@@ -7,8 +7,10 @@ module JSRailsRoutes
7
7
  module Language
8
8
  class TypeScript < JavaScript
9
9
  PROCESS_FUNC = <<~TYPESCRIPT
10
- type Params = Record<string, string | number>
11
- function process(route: string, params: Params, keys: string[]): string {
10
+ type Value = string | number
11
+ type Params<Keys extends string> = { [key in Keys]: Value } & Record<string, Value>
12
+ function process(route: string, params: Record<string, Value> | undefined, keys: string[]): string {
13
+ if (!params) return route
12
14
  var query = [];
13
15
  for (var param in params) if (params.hasOwnProperty(param)) {
14
16
  if (keys.indexOf(param) === -1) {
@@ -24,7 +26,8 @@ module JSRailsRoutes
24
26
  def handle_route(route)
25
27
  path, keys = parse(route.path)
26
28
  name = function_name(route.name)
27
- "export function #{name}(params: Params) { return process('#{path}', params, [#{keys.join(',')}]); }"
29
+ params = keys.empty? ? 'params?: Record<string, Value>' : "params: Params<#{keys.join(' | ')}>"
30
+ "export function #{name}(#{params}) { return process('#{path}', params, [#{keys.join(',')}]); }"
28
31
  end
29
32
 
30
33
  # @note Implementation for {JSRailsRoutes::Language::Base#ext}
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSRailsRoutes
4
- VERSION = '0.8.1'
4
+ VERSION = '0.9.0'
5
5
  end
@@ -10,8 +10,10 @@ RSpec.describe JSRailsRoutes::Language::TypeScript do
10
10
 
11
11
  it 'returns a typescript function' do
12
12
  is_expected.to eq <<~TYPESCRIPT
13
- type Params = Record<string, string | number>
14
- function process(route: string, params: Params, keys: string[]): string {
13
+ type Value = string | number
14
+ type Params<Keys extends string> = { [key in Keys]: Value } & Record<string, Value>
15
+ function process(route: string, params: Record<string, Value> | undefined, keys: string[]): string {
16
+ if (!params) return route
15
17
  var query = [];
16
18
  for (var param in params) if (params.hasOwnProperty(param)) {
17
19
  if (keys.indexOf(param) === -1) {
@@ -40,10 +42,10 @@ RSpec.describe JSRailsRoutes::Language::TypeScript do
40
42
  it 'returns a typescript with snake_case functions' do
41
43
  is_expected.to eq <<~TYPESCRIPT
42
44
  #{described_class::PROCESS_FUNC}
43
- export function articles_path(params: Params) { return process('/articles', params, []); }
44
- export function new_article_path(params: Params) { return process('/articles/new', params, []); }
45
- export function edit_article_path(params: Params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
46
- export function article_path(params: Params) { return process('/articles/' + params.id + '', params, ['id']); }
45
+ export function articles_path(params?: Record<string, Value>) { return process('/articles', params, []); }
46
+ export function new_article_path(params?: Record<string, Value>) { return process('/articles/new', params, []); }
47
+ export function edit_article_path(params: Params<'id'>) { return process('/articles/' + params.id + '/edit', params, ['id']); }
48
+ export function article_path(params: Params<'id'>) { return process('/articles/' + params.id + '', params, ['id']); }
47
49
  TYPESCRIPT
48
50
  end
49
51
  end
@@ -58,10 +60,10 @@ RSpec.describe JSRailsRoutes::Language::TypeScript do
58
60
  it 'returns a javascript with lowerCamelCase functions' do
59
61
  is_expected.to eq <<~TYPESCRIPT
60
62
  #{described_class::PROCESS_FUNC}
61
- export function articlesPath(params: Params) { return process('/articles', params, []); }
62
- export function newArticlePath(params: Params) { return process('/articles/new', params, []); }
63
- export function editArticlePath(params: Params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
64
- export function articlePath(params: Params) { return process('/articles/' + params.id + '', params, ['id']); }
63
+ export function articlesPath(params?: Record<string, Value>) { return process('/articles', params, []); }
64
+ export function newArticlePath(params?: Record<string, Value>) { return process('/articles/new', params, []); }
65
+ export function editArticlePath(params: Params<'id'>) { return process('/articles/' + params.id + '/edit', params, ['id']); }
66
+ export function articlePath(params: Params<'id'>) { return process('/articles/' + params.id + '', params, ['id']); }
65
67
  TYPESCRIPT
66
68
  end
67
69
  end
@@ -76,10 +78,10 @@ RSpec.describe JSRailsRoutes::Language::TypeScript do
76
78
  it 'returns a javascript with UpperCamelCase functions' do
77
79
  is_expected.to eq <<~TYPESCRIPT
78
80
  #{described_class::PROCESS_FUNC}
79
- export function ArticlesPath(params: Params) { return process('/articles', params, []); }
80
- export function NewArticlePath(params: Params) { return process('/articles/new', params, []); }
81
- export function EditArticlePath(params: Params) { return process('/articles/' + params.Id + '/edit', params, ['Id']); }
82
- export function ArticlePath(params: Params) { return process('/articles/' + params.Id + '', params, ['Id']); }
81
+ export function ArticlesPath(params?: Record<string, Value>) { return process('/articles', params, []); }
82
+ export function NewArticlePath(params?: Record<string, Value>) { return process('/articles/new', params, []); }
83
+ export function EditArticlePath(params: Params<'Id'>) { return process('/articles/' + params.Id + '/edit', params, ['Id']); }
84
+ export function ArticlePath(params: Params<'Id'>) { return process('/articles/' + params.Id + '', params, ['Id']); }
83
85
  TYPESCRIPT
84
86
  end
85
87
  end
@@ -94,7 +96,7 @@ RSpec.describe JSRailsRoutes::Language::TypeScript do
94
96
  it 'returns a javascript matching to the regexp' do
95
97
  is_expected.to eq <<~TYPESCRIPT
96
98
  #{described_class::PROCESS_FUNC}
97
- export function new_article_path(params: Params) { return process('/articles/new', params, []); }
99
+ export function new_article_path(params?: Record<string, Value>) { return process('/articles/new', params, []); }
98
100
  TYPESCRIPT
99
101
  end
100
102
  end
@@ -109,9 +111,9 @@ RSpec.describe JSRailsRoutes::Language::TypeScript do
109
111
  it 'returns a javascript not matching to the regexp' do
110
112
  is_expected.to eq <<~TYPESCRIPT
111
113
  #{described_class::PROCESS_FUNC}
112
- export function articles_path(params: Params) { return process('/articles', params, []); }
113
- export function edit_article_path(params: Params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
114
- export function article_path(params: Params) { return process('/articles/' + params.id + '', params, ['id']); }
114
+ export function articles_path(params?: Record<string, Value>) { return process('/articles', params, []); }
115
+ export function edit_article_path(params: Params<'id'>) { return process('/articles/' + params.id + '/edit', params, ['id']); }
116
+ export function article_path(params: Params<'id'>) { return process('/articles/' + params.id + '', params, ['id']); }
115
117
  TYPESCRIPT
116
118
  end
117
119
  end
@@ -126,7 +128,7 @@ RSpec.describe JSRailsRoutes::Language::TypeScript do
126
128
  it 'returns a javascript matching to the regexp' do
127
129
  is_expected.to eq <<~TYPESCRIPT
128
130
  #{described_class::PROCESS_FUNC}
129
- export function new_article_path(params: Params) { return process('/articles/new', params, []); }
131
+ export function new_article_path(params?: Record<string, Value>) { return process('/articles/new', params, []); }
130
132
  TYPESCRIPT
131
133
  end
132
134
  end
@@ -141,9 +143,9 @@ RSpec.describe JSRailsRoutes::Language::TypeScript do
141
143
  it 'returns a javascript not matching to the regexp' do
142
144
  is_expected.to eq <<~TYPESCRIPT
143
145
  #{described_class::PROCESS_FUNC}
144
- export function articles_path(params: Params) { return process('/articles', params, []); }
145
- export function edit_article_path(params: Params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
146
- export function article_path(params: Params) { return process('/articles/' + params.id + '', params, ['id']); }
146
+ export function articles_path(params?: Record<string, Value>) { return process('/articles', params, []); }
147
+ export function edit_article_path(params: Params<'id'>) { return process('/articles/' + params.id + '/edit', params, ['id']); }
148
+ export function article_path(params: Params<'id'>) { return process('/articles/' + params.id + '', params, ['id']); }
147
149
  TYPESCRIPT
148
150
  end
149
151
  end
@@ -93,31 +93,31 @@ RSpec.describe JSRailsRoutes do
93
93
  it 'generates typescript files' do
94
94
  subject
95
95
 
96
- expect(File.read(app_root.join('app/assets/javascripts/rails-routes.ts'))).to eq <<~JAVASCRIPT
96
+ expect(File.read(app_root.join('app/assets/javascripts/rails-routes.ts'))).to eq <<~TYPESCRIPT
97
97
  // Don't edit manually. `rake #{task}` generates this file.
98
98
  #{JSRailsRoutes::Language::TypeScript::PROCESS_FUNC}
99
- export function blogs_path(params: Params) { return process('/blogs', params, []); }
100
- export function new_blog_path(params: Params) { return process('/blogs/new', params, []); }
101
- export function edit_blog_path(params: Params) { return process('/blogs/' + params.id + '/edit', params, ['id']); }
102
- export function blog_path(params: Params) { return process('/blogs/' + params.id + '', params, ['id']); }
103
- export function users_path(params: Params) { return process('/users', params, []); }
104
- export function new_user_path(params: Params) { return process('/users/new', params, []); }
105
- export function edit_user_path(params: Params) { return process('/users/' + params.id + '/edit', params, ['id']); }
106
- export function user_path(params: Params) { return process('/users/' + params.id + '', params, ['id']); }
107
- JAVASCRIPT
108
-
109
- expect(File.read(app_root.join('app/assets/javascripts/admin-routes.ts'))).to eq <<~JAVASCRIPT
99
+ export function blogs_path(params?: Record<string, Value>) { return process('/blogs', params, []); }
100
+ export function new_blog_path(params?: Record<string, Value>) { return process('/blogs/new', params, []); }
101
+ export function edit_blog_path(params: Params<'id'>) { return process('/blogs/' + params.id + '/edit', params, ['id']); }
102
+ export function blog_path(params: Params<'id'>) { return process('/blogs/' + params.id + '', params, ['id']); }
103
+ export function users_path(params?: Record<string, Value>) { return process('/users', params, []); }
104
+ export function new_user_path(params?: Record<string, Value>) { return process('/users/new', params, []); }
105
+ export function edit_user_path(params: Params<'id'>) { return process('/users/' + params.id + '/edit', params, ['id']); }
106
+ export function user_path(params: Params<'id'>) { return process('/users/' + params.id + '', params, ['id']); }
107
+ TYPESCRIPT
108
+
109
+ expect(File.read(app_root.join('app/assets/javascripts/admin-routes.ts'))).to eq <<~TYPESCRIPT
110
110
  // Don't edit manually. `rake #{task}` generates this file.
111
111
  #{JSRailsRoutes::Language::TypeScript::PROCESS_FUNC}
112
- export function notes_path(params: Params) { return process('/notes', params, []); }
113
- export function new_note_path(params: Params) { return process('/notes/new', params, []); }
114
- export function edit_note_path(params: Params) { return process('/notes/' + params.id + '/edit', params, ['id']); }
115
- export function note_path(params: Params) { return process('/notes/' + params.id + '', params, ['id']); }
116
- export function photos_path(params: Params) { return process('/photos', params, []); }
117
- export function new_photo_path(params: Params) { return process('/photos/new', params, []); }
118
- export function edit_photo_path(params: Params) { return process('/photos/' + params.id + '/edit', params, ['id']); }
119
- export function photo_path(params: Params) { return process('/photos/' + params.id + '', params, ['id']); }
120
- JAVASCRIPT
112
+ export function notes_path(params?: Record<string, Value>) { return process('/notes', params, []); }
113
+ export function new_note_path(params?: Record<string, Value>) { return process('/notes/new', params, []); }
114
+ export function edit_note_path(params: Params<'id'>) { return process('/notes/' + params.id + '/edit', params, ['id']); }
115
+ export function note_path(params: Params<'id'>) { return process('/notes/' + params.id + '', params, ['id']); }
116
+ export function photos_path(params?: Record<string, Value>) { return process('/photos', params, []); }
117
+ export function new_photo_path(params?: Record<string, Value>) { return process('/photos/new', params, []); }
118
+ export function edit_photo_path(params: Params<'id'>) { return process('/photos/' + params.id + '/edit', params, ['id']); }
119
+ export function photo_path(params: Params<'id'>) { return process('/photos/' + params.id + '', params, ['id']); }
120
+ TYPESCRIPT
121
121
  end
122
122
  end
123
123
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js_rails_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuku Takahashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-21 00:00:00.000000000 Z
11
+ date: 2018-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails