js-routes 2.0.5 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/Readme.md +34 -22
- data/VERSION_2_UPGRADE.md +25 -20
- data/js-routes.gemspec +1 -1
- data/lib/js_routes/version.rb +1 -1
- data/lib/js_routes.rb +177 -99
- data/lib/routes.d.ts +64 -49
- data/lib/routes.js +60 -51
- data/lib/routes.ts +123 -84
- data/lib/tasks/js_routes.rake +8 -2
- data/package.json +2 -1
- data/spec/js_routes/default_serializer_spec.rb +1 -1
- data/spec/js_routes/module_types/dts/routes.spec.d.ts +114 -0
- data/spec/js_routes/module_types/dts/test.spec.ts +56 -0
- data/spec/js_routes/module_types/dts_spec.rb +111 -0
- data/spec/js_routes/options_spec.rb +5 -1
- data/spec/js_routes/rails_routes_compatibility_spec.rb +1 -1
- data/spec/js_routes/zzz_last_post_rails_init_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/tsconfig.json +4 -0
- data/tsconfig.json +4 -8
- metadata +8 -5
- data/lib/routes.js.map +0 -1
data/lib/tasks/js_routes.rake
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
namespace :js do
|
2
|
-
desc "Make a js file
|
2
|
+
desc "Make a js file with all rails route URL helpers"
|
3
3
|
task routes: :environment do
|
4
4
|
require "js-routes"
|
5
|
-
|
6
5
|
JsRoutes.generate!
|
7
6
|
end
|
7
|
+
|
8
|
+
namespace :routes do
|
9
|
+
desc "Make a js file with all rails route URL helpers and typescript definitions for them"
|
10
|
+
task typescript: "js:routes" do
|
11
|
+
JsRoutes.definitions!
|
12
|
+
end
|
13
|
+
end
|
8
14
|
end
|
data/package.json
CHANGED
@@ -21,6 +21,7 @@
|
|
21
21
|
"prettier": "^2.2.1"
|
22
22
|
},
|
23
23
|
"scripts": {
|
24
|
+
"build": "tsc && yarn lint:fix",
|
24
25
|
"lint:fix": "yarn eslint --fix && yarn prettier --write lib/routes.ts",
|
25
26
|
"postinstall": "yarn husky-upgrade"
|
26
27
|
},
|
@@ -30,7 +31,7 @@
|
|
30
31
|
}
|
31
32
|
},
|
32
33
|
"lint-staged": {
|
33
|
-
"
|
34
|
+
"./lib/routes.ts": ["yarn lint:fix" ]
|
34
35
|
}
|
35
36
|
|
36
37
|
}
|
@@ -0,0 +1,114 @@
|
|
1
|
+
/**
|
2
|
+
* File generated by js-routes RubyVariables.GEM_VERSION
|
3
|
+
* Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
|
4
|
+
*/
|
5
|
+
declare type Optional<T> = {
|
6
|
+
[P in keyof T]?: T[P] | null;
|
7
|
+
};
|
8
|
+
declare type BaseRouteParameter = string | boolean | Date | number;
|
9
|
+
declare type MethodRouteParameter = BaseRouteParameter | (() => BaseRouteParameter);
|
10
|
+
declare type ModelRouteParameter = {
|
11
|
+
id: MethodRouteParameter;
|
12
|
+
} | {
|
13
|
+
to_param: MethodRouteParameter;
|
14
|
+
} | {
|
15
|
+
toParam: MethodRouteParameter;
|
16
|
+
};
|
17
|
+
declare type RequiredRouteParameter = BaseRouteParameter | ModelRouteParameter;
|
18
|
+
declare type OptionalRouteParameter = undefined | null | RequiredRouteParameter;
|
19
|
+
declare type QueryRouteParameter = OptionalRouteParameter | QueryRouteParameter[] | {
|
20
|
+
[k: string]: QueryRouteParameter;
|
21
|
+
};
|
22
|
+
declare type RouteParameters = Record<string, QueryRouteParameter>;
|
23
|
+
declare type Serializable = Record<string, unknown>;
|
24
|
+
declare type Serializer = (value: Serializable) => string;
|
25
|
+
declare type RouteHelperExtras = {
|
26
|
+
requiredParams(): string[];
|
27
|
+
toString(): string;
|
28
|
+
};
|
29
|
+
declare type RequiredParameters<T extends number> = T extends 1 ? [RequiredRouteParameter] : T extends 2 ? [RequiredRouteParameter, RequiredRouteParameter] : T extends 3 ? [RequiredRouteParameter, RequiredRouteParameter, RequiredRouteParameter] : T extends 4 ? [
|
30
|
+
RequiredRouteParameter,
|
31
|
+
RequiredRouteParameter,
|
32
|
+
RequiredRouteParameter,
|
33
|
+
RequiredRouteParameter
|
34
|
+
] : RequiredRouteParameter[];
|
35
|
+
declare type RouteHelperOptions<T extends string> = RouteOptions & Optional<Record<T, OptionalRouteParameter>>;
|
36
|
+
declare type RouteHelper<T extends number = number, U extends string = string> = ((...args: [...RequiredParameters<T>, RouteHelperOptions<U>]) => string) & RouteHelperExtras;
|
37
|
+
declare type RouteHelpers = Record<string, RouteHelper>;
|
38
|
+
declare type Configuration = {
|
39
|
+
prefix: string;
|
40
|
+
default_url_options: RouteParameters;
|
41
|
+
special_options_key: string;
|
42
|
+
serializer: Serializer;
|
43
|
+
};
|
44
|
+
interface RouterExposedMethods {
|
45
|
+
config(): Configuration;
|
46
|
+
configure(arg: Partial<Configuration>): Configuration;
|
47
|
+
serialize: Serializer;
|
48
|
+
}
|
49
|
+
declare type KeywordUrlOptions = Optional<{
|
50
|
+
host: string;
|
51
|
+
protocol: string;
|
52
|
+
subdomain: string;
|
53
|
+
port: string | number;
|
54
|
+
anchor: string;
|
55
|
+
trailing_slash: boolean;
|
56
|
+
}>;
|
57
|
+
declare type RouteOptions = KeywordUrlOptions & RouteParameters;
|
58
|
+
declare type PartsTable = Record<string, {
|
59
|
+
r?: boolean;
|
60
|
+
d?: OptionalRouteParameter;
|
61
|
+
}>;
|
62
|
+
declare type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "DTS" | "NIL";
|
63
|
+
declare const RubyVariables: {
|
64
|
+
PREFIX: string;
|
65
|
+
DEPRECATED_GLOBBING_BEHAVIOR: boolean;
|
66
|
+
SPECIAL_OPTIONS_KEY: string;
|
67
|
+
DEFAULT_URL_OPTIONS: RouteParameters;
|
68
|
+
SERIALIZER: Serializer;
|
69
|
+
NAMESPACE: string;
|
70
|
+
ROUTES_OBJECT: RouteHelpers;
|
71
|
+
MODULE_TYPE: ModuleType;
|
72
|
+
WRAPPER: <T>(callback: T) => T;
|
73
|
+
};
|
74
|
+
declare const define: undefined | (((arg: unknown[], callback: () => unknown) => void) & {
|
75
|
+
amd?: unknown;
|
76
|
+
});
|
77
|
+
declare const module: {
|
78
|
+
exports: any;
|
79
|
+
} | undefined;
|
80
|
+
export const configure: RouterExposedMethods['configure'];
|
81
|
+
|
82
|
+
export const config: RouterExposedMethods['config'];
|
83
|
+
|
84
|
+
export const serialize: RouterExposedMethods['serialize'];
|
85
|
+
|
86
|
+
/**
|
87
|
+
* Generates rails route to
|
88
|
+
* /inboxes/:inbox_id/messages/:message_id/attachments/:id(.:format)
|
89
|
+
* @param {any} inbox_id
|
90
|
+
* @param {any} message_id
|
91
|
+
* @param {any} id
|
92
|
+
* @param {object | undefined} options
|
93
|
+
* @returns {string} route path
|
94
|
+
*/
|
95
|
+
export const inbox_message_attachment_path: ((
|
96
|
+
inbox_id: RequiredRouteParameter,
|
97
|
+
message_id: RequiredRouteParameter,
|
98
|
+
id: RequiredRouteParameter,
|
99
|
+
options?: {format?: OptionalRouteParameter} & RouteOptions
|
100
|
+
) => string) & RouteHelperExtras;
|
101
|
+
|
102
|
+
/**
|
103
|
+
* Generates rails route to
|
104
|
+
* /inboxes(.:format)
|
105
|
+
* @param {object | undefined} options
|
106
|
+
* @returns {string} route path
|
107
|
+
*/
|
108
|
+
export const inboxes_path: ((
|
109
|
+
options?: {format?: OptionalRouteParameter} & RouteOptions
|
110
|
+
) => string) & RouteHelperExtras;
|
111
|
+
|
112
|
+
// By some reason this line prevents all types in a file
|
113
|
+
// from being automatically exported
|
114
|
+
export {};
|
@@ -0,0 +1,56 @@
|
|
1
|
+
import {
|
2
|
+
inbox_message_attachment_path,
|
3
|
+
inboxes_path,
|
4
|
+
serialize,
|
5
|
+
configure,
|
6
|
+
config,
|
7
|
+
} from "./routes.spec";
|
8
|
+
|
9
|
+
// Route Helpers
|
10
|
+
inboxes_path();
|
11
|
+
inboxes_path({
|
12
|
+
locale: "en",
|
13
|
+
search: {
|
14
|
+
q: "ukraine",
|
15
|
+
page: 3,
|
16
|
+
keywords: ["large", "small", { advanced: true }],
|
17
|
+
},
|
18
|
+
});
|
19
|
+
|
20
|
+
inbox_message_attachment_path(1, "2", true);
|
21
|
+
inbox_message_attachment_path(
|
22
|
+
{ id: 1 },
|
23
|
+
{ to_param: () => "2" },
|
24
|
+
{ toParam: () => true }
|
25
|
+
);
|
26
|
+
inbox_message_attachment_path(1, "2", true, { format: "json" });
|
27
|
+
inboxes_path.toString();
|
28
|
+
inboxes_path.requiredParams();
|
29
|
+
|
30
|
+
// serialize test
|
31
|
+
const SerializerArgument = {
|
32
|
+
locale: "en",
|
33
|
+
search: {
|
34
|
+
q: "ukraine",
|
35
|
+
page: 3,
|
36
|
+
keywords: ["large", "small", { advanced: true }],
|
37
|
+
},
|
38
|
+
};
|
39
|
+
serialize(SerializerArgument);
|
40
|
+
config().serializer(SerializerArgument);
|
41
|
+
|
42
|
+
// configure test
|
43
|
+
configure({
|
44
|
+
default_url_options: { port: 1, host: null },
|
45
|
+
prefix: "",
|
46
|
+
special_options_key: "_options",
|
47
|
+
serializer: (value) => JSON.stringify(value),
|
48
|
+
});
|
49
|
+
|
50
|
+
// config tests
|
51
|
+
const Config = config();
|
52
|
+
console.log(
|
53
|
+
Config.prefix,
|
54
|
+
Config.default_url_options,
|
55
|
+
Config.special_options_key
|
56
|
+
);
|
@@ -0,0 +1,111 @@
|
|
1
|
+
|
2
|
+
require "active_support/core_ext/string/strip"
|
3
|
+
require "fileutils"
|
4
|
+
require "open3"
|
5
|
+
require "spec_helper"
|
6
|
+
|
7
|
+
describe JsRoutes, "compatibility with DTS" do
|
8
|
+
|
9
|
+
OPTIONS = {module_type: 'DTS', include: [/^inboxes$/, /^inbox_message_attachment$/]}
|
10
|
+
let(:extra_options) do
|
11
|
+
{}
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:generated_js) do
|
15
|
+
JsRoutes.generate({**OPTIONS, **extra_options})
|
16
|
+
end
|
17
|
+
|
18
|
+
context "when file is generated" do
|
19
|
+
let(:dir_name) do
|
20
|
+
File.expand_path(__dir__ + "/dts")
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:file_name) do
|
24
|
+
dir_name + "/routes.spec.d.ts"
|
25
|
+
end
|
26
|
+
|
27
|
+
before do
|
28
|
+
FileUtils.mkdir_p(dir_name)
|
29
|
+
File.write(file_name, generated_js)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "has no compile errors", :slow do
|
33
|
+
command = "yarn tsc --strict --noEmit -p spec/tsconfig.json"
|
34
|
+
stdout, stderr, status = Open3.capture3(command)
|
35
|
+
expect(stderr).to eq("")
|
36
|
+
expect(stdout).to include("Done in ")
|
37
|
+
expect(status).to eq(0)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when camel case is enabled" do
|
42
|
+
let(:extra_options) { {camel_case: true} }
|
43
|
+
|
44
|
+
it "camelizes route name and arguments" do
|
45
|
+
|
46
|
+
expect(generated_js).to include(<<-DOC.rstrip)
|
47
|
+
/**
|
48
|
+
* Generates rails route to
|
49
|
+
* /inboxes/:inbox_id/messages/:message_id/attachments/:id(.:format)
|
50
|
+
* @param {any} inboxId
|
51
|
+
* @param {any} messageId
|
52
|
+
* @param {any} id
|
53
|
+
* @param {object | undefined} options
|
54
|
+
* @returns {string} route path
|
55
|
+
*/
|
56
|
+
export const inboxMessageAttachmentPath: ((
|
57
|
+
inboxId: RequiredRouteParameter,
|
58
|
+
messageId: RequiredRouteParameter,
|
59
|
+
id: RequiredRouteParameter,
|
60
|
+
options?: {format?: OptionalRouteParameter} & RouteOptions
|
61
|
+
) => string) & RouteHelperExtras;
|
62
|
+
DOC
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it "exports route helpers" do
|
67
|
+
expect(generated_js).to include(<<-DOC.rstrip)
|
68
|
+
/**
|
69
|
+
* Generates rails route to
|
70
|
+
* /inboxes(.:format)
|
71
|
+
* @param {object | undefined} options
|
72
|
+
* @returns {string} route path
|
73
|
+
*/
|
74
|
+
export const inboxes_path: ((
|
75
|
+
options?: {format?: OptionalRouteParameter} & RouteOptions
|
76
|
+
) => string) & RouteHelperExtras;
|
77
|
+
DOC
|
78
|
+
expect(generated_js).to include(<<-DOC.rstrip)
|
79
|
+
/**
|
80
|
+
* Generates rails route to
|
81
|
+
* /inboxes/:inbox_id/messages/:message_id/attachments/:id(.:format)
|
82
|
+
* @param {any} inbox_id
|
83
|
+
* @param {any} message_id
|
84
|
+
* @param {any} id
|
85
|
+
* @param {object | undefined} options
|
86
|
+
* @returns {string} route path
|
87
|
+
*/
|
88
|
+
export const inbox_message_attachment_path: ((
|
89
|
+
inbox_id: RequiredRouteParameter,
|
90
|
+
message_id: RequiredRouteParameter,
|
91
|
+
id: RequiredRouteParameter,
|
92
|
+
options?: {format?: OptionalRouteParameter} & RouteOptions
|
93
|
+
) => string) & RouteHelperExtras
|
94
|
+
DOC
|
95
|
+
end
|
96
|
+
|
97
|
+
it "exports utility methods" do
|
98
|
+
expect(generated_js).to include("export const serialize: RouterExposedMethods['serialize'];")
|
99
|
+
end
|
100
|
+
|
101
|
+
it "prevents all types from automatic export" do
|
102
|
+
expect(generated_js).to include("export {};")
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "compiled javascript asset" do
|
106
|
+
subject { ERB.new(File.read("app/assets/javascripts/js-routes.js.erb")).result(binding) }
|
107
|
+
it "should have js routes code" do
|
108
|
+
is_expected.to include("export const inbox_message_path = __jsr.r(")
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -5,7 +5,7 @@
|
|
5
5
|
require 'spec_helper'
|
6
6
|
require "fileutils"
|
7
7
|
|
8
|
-
describe "after Rails initialization" do
|
8
|
+
describe "after Rails initialization", :slow do
|
9
9
|
NAME = Rails.root.join('app', 'assets', 'javascripts', 'routes.js').to_s
|
10
10
|
|
11
11
|
def sprockets_v3?
|
@@ -122,7 +122,7 @@ describe "after Rails initialization" do
|
|
122
122
|
end
|
123
123
|
end
|
124
124
|
|
125
|
-
describe "JSRoutes thread safety" do
|
125
|
+
describe "JSRoutes thread safety", :slow do
|
126
126
|
before do
|
127
127
|
begin
|
128
128
|
Rails.application.initialize!
|
data/spec/spec_helper.rb
CHANGED
data/spec/tsconfig.json
ADDED
data/tsconfig.json
CHANGED
@@ -3,14 +3,10 @@
|
|
3
3
|
"target": "es2019",
|
4
4
|
"lib": ["es2020", "dom"],
|
5
5
|
"module": "commonjs",
|
6
|
-
"
|
7
|
-
"sourceMap": true,
|
6
|
+
"sourceMap": false,
|
8
7
|
"declaration": true,
|
9
8
|
"allowSyntheticDefaultImports": true,
|
10
|
-
"resolveJsonModule":
|
11
|
-
"typeRoots": ["node_modules/@types", "./@types"],
|
12
|
-
|
13
|
-
"strict": true,
|
9
|
+
"resolveJsonModule": false,
|
14
10
|
"strictNullChecks": true,
|
15
11
|
"noImplicitAny": true,
|
16
12
|
"noImplicitThis": true,
|
@@ -22,11 +18,11 @@
|
|
22
18
|
"forceConsistentCasingInFileNames": true,
|
23
19
|
"emitDecoratorMetadata": true,
|
24
20
|
"experimentalDecorators": true
|
21
|
+
|
25
22
|
},
|
26
23
|
"ts-node": {
|
27
24
|
"files": true,
|
28
25
|
"transpileOnly": false
|
29
26
|
},
|
30
|
-
"include": ["
|
31
|
-
"exclude": ["node_modules", "**/thorn", "**/support", "**/tmp"]
|
27
|
+
"include": ["lib/*.ts"]
|
32
28
|
}
|
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.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bogdan Gusiev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 3.
|
47
|
+
version: 3.10.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 3.
|
54
|
+
version: 3.10.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: bundler
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -174,7 +174,6 @@ files:
|
|
174
174
|
- lib/js_routes/version.rb
|
175
175
|
- lib/routes.d.ts
|
176
176
|
- lib/routes.js
|
177
|
-
- lib/routes.js.map
|
178
177
|
- lib/routes.ts
|
179
178
|
- lib/tasks/js_routes.rake
|
180
179
|
- package.json
|
@@ -184,6 +183,9 @@ files:
|
|
184
183
|
- spec/js_routes/default_serializer_spec.rb
|
185
184
|
- spec/js_routes/module_types/amd_spec.rb
|
186
185
|
- spec/js_routes/module_types/cjs_spec.rb
|
186
|
+
- spec/js_routes/module_types/dts/routes.spec.d.ts
|
187
|
+
- spec/js_routes/module_types/dts/test.spec.ts
|
188
|
+
- spec/js_routes/module_types/dts_spec.rb
|
187
189
|
- spec/js_routes/module_types/esm_spec.rb
|
188
190
|
- spec/js_routes/module_types/umd_spec.rb
|
189
191
|
- spec/js_routes/options_spec.rb
|
@@ -191,6 +193,7 @@ files:
|
|
191
193
|
- spec/js_routes/zzz_last_post_rails_init_spec.rb
|
192
194
|
- spec/spec_helper.rb
|
193
195
|
- spec/support/routes.rb
|
196
|
+
- spec/tsconfig.json
|
194
197
|
- tsconfig.json
|
195
198
|
- yarn.lock
|
196
199
|
homepage: http://github.com/railsware/js-routes
|