ts_routes 0.9.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +8 -10
- data/CHANGELOG.md +5 -0
- data/Gemfile +2 -0
- data/README.md +43 -9
- data/Rakefile +2 -0
- data/bin/console +1 -0
- data/lib/ts_routes/generator.rb +2 -3
- data/lib/ts_routes/runtime.ts +11 -6
- data/lib/ts_routes/version.rb +3 -1
- data/lib/ts_routes.rb +2 -0
- data/package-lock.json +6 -0
- data/package.json +5 -3
- data/ts_routes.gemspec +2 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27b91ef355275cd7f79b055e8493485146c49362
|
4
|
+
data.tar.gz: 2e8d7e12cb04fe15d048b831b60599cb3cbef694
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a81dce07a3cefa6151604d0b0a5841777632518e9fa5e7aa45dab0e3c2dd4fe6f4c0a512fed4577e1faccffaad64c51764df16bdf2ba275d4a1ea9ef6680c7d
|
7
|
+
data.tar.gz: 399ca6066a7183134f0d5ecf054dd7cfd8d2bfc82f23f7a8a34615e1039da9044de4c8b97aa5d77488e3360257d574be35d26809e9a14baf386b769b86391d87
|
data/.rubocop.yml
CHANGED
@@ -4,18 +4,16 @@ AllCops:
|
|
4
4
|
Exclude:
|
5
5
|
- "vendor/**/*"
|
6
6
|
- "node_modules/**/*"
|
7
|
+
DisabledByDefault: true
|
7
8
|
|
8
|
-
|
9
|
-
Enabled:
|
9
|
+
Performance:
|
10
|
+
Enabled: true
|
10
11
|
|
11
|
-
|
12
|
+
Performance/RedundantBlockCall:
|
12
13
|
Enabled: false
|
13
14
|
|
14
|
-
|
15
|
-
Enabled:
|
15
|
+
Style/FrozenStringLiteralComment:
|
16
|
+
Enabled: true
|
16
17
|
|
17
|
-
|
18
|
-
Enabled:
|
19
|
-
|
20
|
-
Performance/RedundantBlockCall:
|
21
|
-
Enabled: false
|
18
|
+
Style/EmptyLineAfterMagicComment:
|
19
|
+
Enabled: true
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# TsRoutes for Rails [![Gem Version](https://badge.fury.io/rb/ts_routes.svg)](https://badge.fury.io/rb/ts_routes) [![Build Status](https://travis-ci.org/bitjourney/ts_routes-rails.svg?branch=master)](https://travis-ci.org/bitjourney/ts_routes-rails)
|
2
2
|
|
3
|
-
This gem generates Rails URL helpers in TypeScript,
|
3
|
+
This gem generates Rails URL helpers in TypeScript, which is synchronized to `routes.rb`.
|
4
4
|
|
5
|
+
This is inspired by [js-routes](https://github.com/railsware/js-routes), which invents the great idea to export URL helpers to JavaScript.
|
5
6
|
|
6
7
|
## Usage
|
7
8
|
|
8
9
|
In your `lib/tasks/ts_routes.rake`:
|
9
10
|
|
10
|
-
```ruby
|
11
|
-
|
11
|
+
```ruby
|
12
12
|
namespace :ts do
|
13
13
|
TS_ROUTES_FILENAME = "javascripts/generated/routes.ts"
|
14
14
|
|
@@ -27,9 +27,8 @@ Then, execute `rake ts:routes` to generate `routes.ts` in your favorite path.
|
|
27
27
|
|
28
28
|
And you can import it in TypeScript code:
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
import * as Routes from './generated/RailsRoutes.ts';
|
30
|
+
```typescript
|
31
|
+
import * as Routes from './generated/routes';
|
33
32
|
|
34
33
|
console.log(Routes.entriesPath({ page: 1, per: 20 })); // => /entries?page=1&per=20
|
35
34
|
console.log(Routes.entryPath(1)); // => /entries/1
|
@@ -39,10 +38,41 @@ Generated URL helpers are almost compatible with Rails, but they are more strict
|
|
39
38
|
|
40
39
|
* You must pass required parameters to the helpers as non-named (i.e. normal) arguments
|
41
40
|
* i.e. `Routes.entryPath(1)` for `/entries/:id`
|
42
|
-
* `Routes.entryPath({ id })` is
|
41
|
+
* `Routes.entryPath({ id })` is not allowed
|
42
|
+
* Required parameters must not be `null` nor `undefined`
|
43
|
+
* i.e. `Routes.entyPath(null)` does not compile
|
43
44
|
* You must pass optional parameters as the last argument
|
44
45
|
* i.e. `Routes.entriesPath({ page: 1, per: 2 })`
|
45
46
|
|
47
|
+
### Options
|
48
|
+
|
49
|
+
Here are options for `TsRoutes.generate`:
|
50
|
+
|
51
|
+
<table>
|
52
|
+
<tr><th>name</th><th>description</th><th>default</th></tr>
|
53
|
+
<tr><td>routes</td><td>Rails routes to export</td><td>Rails.application.routes</td></tr>
|
54
|
+
<tr><td>camel_case</td><td>naming style; doesn't change if false</td><td>true</td></tr>
|
55
|
+
<tr><td>route_suffix</td><td>suffix for each route</td><td>"path"</td></tr>
|
56
|
+
<tr><td>include</td><td>Array of Regexp patterns to include</td><td>nil</td></tr>
|
57
|
+
<tr><td>exclude</td><td>Array of Regexp patterns to exclude</td><td>nil</td></tr>
|
58
|
+
<tr><td>header</td><td>additional parts of generated files</td><td>"/* tslint:disable */"</td></tr>
|
59
|
+
</table>
|
60
|
+
|
61
|
+
Note that `TsRoutes.generate(options)` is a shortcut of `TsRoutes::Generator.new(options).generate`.
|
62
|
+
|
63
|
+
### How to Keep routes.ts Up-To-Date
|
64
|
+
|
65
|
+
Use [Guard](https://github.com/guard/guard):
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
# In Guardfile
|
69
|
+
|
70
|
+
# Run `rake ts:routes` when routes.rb is updated.
|
71
|
+
guard :rake, task: 'ts:routes' do
|
72
|
+
watch(%r{config/routes\.rb$})
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
46
76
|
## Installation
|
47
77
|
|
48
78
|
Add this line to your application's Gemfile:
|
@@ -53,11 +83,15 @@ gem 'ts_routes'
|
|
53
83
|
|
54
84
|
And then execute:
|
55
85
|
|
56
|
-
|
86
|
+
```console
|
87
|
+
$ bundle
|
88
|
+
```
|
57
89
|
|
58
90
|
Or install it yourself as:
|
59
91
|
|
60
|
-
|
92
|
+
```console
|
93
|
+
$ gem install ts_routes
|
94
|
+
```
|
61
95
|
|
62
96
|
## Development
|
63
97
|
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
data/lib/ts_routes/generator.rb
CHANGED
@@ -8,7 +8,7 @@ module TsRoutes
|
|
8
8
|
FILTERED_DEFAULT_PARTS = [:controller, :action, :subdomain]
|
9
9
|
URL_OPTIONS = [:protocol, :domain, :host, :port, :subdomain]
|
10
10
|
|
11
|
-
DEFAULT_FILE_HEADER = "/* tslint:disable
|
11
|
+
DEFAULT_FILE_HEADER = "/* tslint:disable */"
|
12
12
|
|
13
13
|
# @return [ActionDispatch::Routing::RouteSet]
|
14
14
|
attr_reader :routes
|
@@ -88,8 +88,7 @@ module TsRoutes
|
|
88
88
|
# @param [ActionDispatch::Journey::Route] route
|
89
89
|
# @param [ActionDispatch::Journey::Route] parent_route
|
90
90
|
def build_route_function(route, parent_route = nil)
|
91
|
-
|
92
|
-
route_name = build_route_name(*name_parts, route_suffix)
|
91
|
+
route_name = build_route_name(parent_route&.name, route.name, route_suffix)
|
93
92
|
|
94
93
|
required_param_declarations = route.required_parts.map do |name|
|
95
94
|
symbol = find_spec(route.path.spec, name)
|
data/lib/ts_routes/runtime.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/* This is generated by ts_routes-rails */
|
2
2
|
|
3
|
-
type ScalarType = string | number | boolean;
|
3
|
+
export type ScalarType = string | number | boolean;
|
4
4
|
|
5
5
|
function $buildOptions(options: any, names: string[]): string {
|
6
6
|
if (options) {
|
@@ -12,7 +12,7 @@ function $buildOptions(options: any, names: string[]): string {
|
|
12
12
|
continue;
|
13
13
|
}
|
14
14
|
|
15
|
-
const value = options[
|
15
|
+
const value = options[key];
|
16
16
|
|
17
17
|
if (key === "anchor") {
|
18
18
|
anchor = `#${$encode(value)}`;
|
@@ -33,9 +33,10 @@ function $buildQuery(q: string[], key: string, value: any) {
|
|
33
33
|
for (const v of value) {
|
34
34
|
$buildQuery(q, `${key}[]`, v);
|
35
35
|
}
|
36
|
-
} else if ($isNotNull(value)) {
|
36
|
+
} else if ($isNotNull(value)) {
|
37
|
+
// i.e. non-null, non-scalar, non-array type
|
37
38
|
for (const k of Object.keys(value)) {
|
38
|
-
$buildQuery(q, `${key}[${k}]`, value[
|
39
|
+
$buildQuery(q, `${key}[${k}]`, value[k]);
|
39
40
|
}
|
40
41
|
}
|
41
42
|
}
|
@@ -49,7 +50,11 @@ function $isNotNull(value: any): boolean {
|
|
49
50
|
}
|
50
51
|
|
51
52
|
function $isScalarType(value: any): value is ScalarType {
|
52
|
-
return
|
53
|
+
return (
|
54
|
+
typeof value === "string" ||
|
55
|
+
typeof value === "number" ||
|
56
|
+
typeof value === "boolean"
|
57
|
+
);
|
53
58
|
}
|
54
59
|
|
55
60
|
function $isPresent(value: any): boolean {
|
@@ -57,5 +62,5 @@ function $isPresent(value: any): boolean {
|
|
57
62
|
}
|
58
63
|
|
59
64
|
function $hasPresentOwnProperty(options: any, key: string): boolean {
|
60
|
-
return options && options.hasOwnProperty(key) && $isPresent(options[
|
65
|
+
return options && options.hasOwnProperty(key) && $isPresent(options[key]);
|
61
66
|
}
|
data/lib/ts_routes/version.rb
CHANGED
data/lib/ts_routes.rb
CHANGED
data/package-lock.json
CHANGED
@@ -141,6 +141,12 @@
|
|
141
141
|
"integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME=",
|
142
142
|
"dev": true
|
143
143
|
},
|
144
|
+
"prettier": {
|
145
|
+
"version": "1.4.4",
|
146
|
+
"resolved": "https://registry.npmjs.org/prettier/-/prettier-1.4.4.tgz",
|
147
|
+
"integrity": "sha512-GuuPazIvjW1DG26yLQgO+nagmRF/h9M4RaCtZWqu/eFW7csdZkQEwPJUeXX10d+LzmCnR9DuIZndqIOn3p2YoA==",
|
148
|
+
"dev": true
|
149
|
+
},
|
144
150
|
"resolve": {
|
145
151
|
"version": "1.3.3",
|
146
152
|
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.3.3.tgz",
|
data/package.json
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
{
|
2
2
|
"name": "ts_routes",
|
3
3
|
"version": "1.0.0",
|
4
|
-
"description": "ts_routes-rails JavaScript runtime",
|
5
|
-
"
|
4
|
+
"description": "ts_routes-rails JavaScript runtime (not a standalone package)",
|
5
|
+
"private": true,
|
6
6
|
"directories": {
|
7
7
|
"lib": "lib",
|
8
8
|
"test": "test"
|
@@ -11,10 +11,12 @@
|
|
11
11
|
"typescript": "^2.4.0"
|
12
12
|
},
|
13
13
|
"devDependencies": {
|
14
|
+
"prettier": "*",
|
14
15
|
"tslint": "*"
|
15
16
|
},
|
16
17
|
"scripts": {
|
17
|
-
"
|
18
|
+
"format": "prettier --write 'lib/**/*.ts' 'test/**/*.ts'",
|
19
|
+
"test": "rake test"
|
18
20
|
},
|
19
21
|
"repository": "https://github.com/bitjourney/ts_routes-rails",
|
20
22
|
"author": "FUJI Goro",
|
data/ts_routes.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ts_routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- FUJI Goro (gfx)
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- ".gitignore"
|
77
77
|
- ".rubocop.yml"
|
78
78
|
- ".travis.yml"
|
79
|
+
- CHANGELOG.md
|
79
80
|
- Gemfile
|
80
81
|
- LICENSE.txt
|
81
82
|
- README.md
|