js-routes 2.0.7 → 2.1.2
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 +17 -0
- data/Readme.md +113 -63
- data/VERSION_2_UPGRADE.md +24 -12
- data/js-routes.gemspec +1 -1
- data/lib/js_routes/generators/webpacker.rb +32 -0
- data/lib/js_routes/version.rb +1 -1
- data/lib/js_routes.rb +188 -100
- data/lib/routes.d.ts +34 -12
- data/lib/routes.js +61 -50
- data/lib/routes.ts +126 -84
- data/lib/tasks/js_routes.rake +8 -2
- data/lib/templates/erb.js +11 -0
- data/lib/templates/initializer.rb +5 -0
- data/lib/templates/routes.js.erb +1 -0
- 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/zzz_last_post_rails_init_spec.rb +2 -2
- data/spec/tsconfig.json +4 -0
- data/tsconfig.json +3 -7
- metadata +12 -4
data/lib/js_routes.rb
CHANGED
@@ -7,48 +7,26 @@ require 'active_support/core_ext/string/indent'
|
|
7
7
|
|
8
8
|
class JsRoutes
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
class Configuration
|
11
|
+
DEFAULTS = {
|
12
|
+
namespace: nil,
|
13
|
+
exclude: [],
|
14
|
+
include: //,
|
15
|
+
file: nil,
|
16
|
+
prefix: -> { Rails.application.config.relative_url_root || "" },
|
17
|
+
url_links: false,
|
18
|
+
camel_case: false,
|
19
|
+
default_url_options: {},
|
20
|
+
compact: false,
|
21
|
+
serializer: nil,
|
22
|
+
special_options_key: "_options",
|
23
|
+
application: -> { Rails.application },
|
24
|
+
module_type: 'ESM',
|
25
|
+
documentation: true,
|
26
|
+
} #:nodoc:
|
27
|
+
|
28
|
+
attr_accessor(*DEFAULTS.keys)
|
13
29
|
|
14
|
-
DEFAULTS = {
|
15
|
-
namespace: nil,
|
16
|
-
exclude: [],
|
17
|
-
include: //,
|
18
|
-
file: -> do
|
19
|
-
webpacker_dir = Rails.root.join('app', 'javascript')
|
20
|
-
sprockets_dir = Rails.root.join('app','assets','javascripts')
|
21
|
-
sprockets_file = sprockets_dir.join('routes.js')
|
22
|
-
webpacker_file = webpacker_dir.join('routes.js')
|
23
|
-
!Dir.exist?(webpacker_dir) && defined?(::Sprockets) ? sprockets_file : webpacker_file
|
24
|
-
end,
|
25
|
-
prefix: -> { Rails.application.config.relative_url_root || "" },
|
26
|
-
url_links: false,
|
27
|
-
camel_case: false,
|
28
|
-
default_url_options: {},
|
29
|
-
compact: false,
|
30
|
-
serializer: nil,
|
31
|
-
special_options_key: "_options",
|
32
|
-
application: -> { Rails.application },
|
33
|
-
module_type: 'ESM',
|
34
|
-
documentation: true,
|
35
|
-
} #:nodoc:
|
36
|
-
|
37
|
-
NODE_TYPES = {
|
38
|
-
GROUP: 1,
|
39
|
-
CAT: 2,
|
40
|
-
SYMBOL: 3,
|
41
|
-
OR: 4,
|
42
|
-
STAR: 5,
|
43
|
-
LITERAL: 6,
|
44
|
-
SLASH: 7,
|
45
|
-
DOT: 8
|
46
|
-
} #:nodoc:
|
47
|
-
|
48
|
-
FILTERED_DEFAULT_PARTS = [:controller, :action] #:nodoc:
|
49
|
-
URL_OPTIONS = [:protocol, :domain, :host, :port, :subdomain] #:nodoc:
|
50
|
-
|
51
|
-
class Configuration < Struct.new(*DEFAULTS.keys)
|
52
30
|
def initialize(attributes = nil)
|
53
31
|
assign(DEFAULTS)
|
54
32
|
return unless attributes
|
@@ -60,6 +38,7 @@ class JsRoutes
|
|
60
38
|
value = value.call if value.is_a?(Proc)
|
61
39
|
send(:"#{attribute}=", value)
|
62
40
|
end
|
41
|
+
normalize_and_verify
|
63
42
|
self
|
64
43
|
end
|
65
44
|
|
@@ -76,7 +55,53 @@ class JsRoutes
|
|
76
55
|
end
|
77
56
|
|
78
57
|
def esm?
|
79
|
-
|
58
|
+
module_type === 'ESM'
|
59
|
+
end
|
60
|
+
|
61
|
+
def dts?
|
62
|
+
self.module_type === 'DTS'
|
63
|
+
end
|
64
|
+
|
65
|
+
def modern?
|
66
|
+
esm? || dts?
|
67
|
+
end
|
68
|
+
|
69
|
+
def require_esm
|
70
|
+
raise "ESM module type is required" unless modern?
|
71
|
+
end
|
72
|
+
|
73
|
+
def source_file
|
74
|
+
File.dirname(__FILE__) + "/" + default_file_name
|
75
|
+
end
|
76
|
+
|
77
|
+
def output_file
|
78
|
+
webpacker_dir = Rails.root.join('app', 'javascript')
|
79
|
+
sprockets_dir = Rails.root.join('app','assets','javascripts')
|
80
|
+
file_name = file || default_file_name
|
81
|
+
sprockets_file = sprockets_dir.join(file_name)
|
82
|
+
webpacker_file = webpacker_dir.join(file_name)
|
83
|
+
!Dir.exist?(webpacker_dir) && defined?(::Sprockets) ? sprockets_file : webpacker_file
|
84
|
+
end
|
85
|
+
|
86
|
+
def normalize_and_verify
|
87
|
+
normalize
|
88
|
+
verify
|
89
|
+
end
|
90
|
+
|
91
|
+
protected
|
92
|
+
|
93
|
+
def default_file_name
|
94
|
+
dts? ? "routes.d.ts" : "routes.js"
|
95
|
+
end
|
96
|
+
|
97
|
+
def normalize
|
98
|
+
self.module_type = module_type&.upcase || 'NIL'
|
99
|
+
end
|
100
|
+
|
101
|
+
def verify
|
102
|
+
if module_type != 'NIL' && namespace
|
103
|
+
raise "JsRoutes namespace option can only be used if module_type is nil"
|
104
|
+
end
|
80
105
|
end
|
81
106
|
end
|
82
107
|
|
@@ -87,22 +112,28 @@ class JsRoutes
|
|
87
112
|
class << self
|
88
113
|
def setup(&block)
|
89
114
|
configuration.tap(&block) if block
|
115
|
+
configuration.normalize_and_verify
|
90
116
|
end
|
91
117
|
|
92
118
|
def configuration
|
93
119
|
@configuration ||= Configuration.new
|
94
120
|
end
|
95
121
|
|
96
|
-
def generate(opts
|
122
|
+
def generate(**opts)
|
97
123
|
new(opts).generate
|
98
124
|
end
|
99
125
|
|
100
|
-
def generate!(file_name=nil, opts
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
126
|
+
def generate!(file_name=nil, **opts)
|
127
|
+
new(file: file_name, **opts).generate!
|
128
|
+
end
|
129
|
+
|
130
|
+
def definitions(**opts)
|
131
|
+
generate(module_type: 'DTS', **opts)
|
132
|
+
end
|
133
|
+
|
134
|
+
def definitions!(file_name = nil, **opts)
|
135
|
+
file_name ||= configuration.file&.sub!(%r{\.(j|t)s\Z}, ".d.ts")
|
136
|
+
generate!(file_name, module_type: 'DTS', **opts)
|
106
137
|
end
|
107
138
|
|
108
139
|
def json(string)
|
@@ -123,48 +154,55 @@ class JsRoutes
|
|
123
154
|
if named_routes.to_a.empty? && application.respond_to?(:reload_routes!)
|
124
155
|
application.reload_routes!
|
125
156
|
end
|
157
|
+
content = File.read(@configuration.source_file)
|
126
158
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
'RAILS_VERSION' => ActionPack.version,
|
131
|
-
'DEPRECATED_GLOBBING_BEHAVIOR' => ActionPack::VERSION::MAJOR == 4 && ActionPack::VERSION::MINOR == 0,
|
132
|
-
|
133
|
-
'APP_CLASS' => application.class.to_s,
|
134
|
-
'NAMESPACE' => json(@configuration.namespace),
|
135
|
-
'DEFAULT_URL_OPTIONS' => json(@configuration.default_url_options),
|
136
|
-
'PREFIX' => json(@configuration.prefix),
|
137
|
-
'SPECIAL_OPTIONS_KEY' => json(@configuration.special_options_key),
|
138
|
-
'SERIALIZER' => @configuration.serializer || json(nil),
|
139
|
-
'MODULE_TYPE' => json(@configuration.module_type),
|
140
|
-
'WRAPPER' => @configuration.esm? ? 'const __jsr = ' : '',
|
141
|
-
}.inject(File.read(File.dirname(__FILE__) + "/routes.js")) do |js, (key, value)|
|
142
|
-
js.gsub!("RubyVariables.#{key}", value.to_s) ||
|
159
|
+
if !@configuration.dts?
|
160
|
+
content = js_variables.inject(content) do |js, (key, value)|
|
161
|
+
js.gsub!("RubyVariables.#{key}", value.to_s) ||
|
143
162
|
raise("Missing key #{key} in JS template")
|
144
|
-
|
163
|
+
end
|
164
|
+
end
|
165
|
+
content + routes_export + prevent_types_export
|
145
166
|
end
|
146
167
|
|
147
|
-
def generate!
|
148
|
-
# Some libraries like Devise
|
149
|
-
# until initialization process
|
168
|
+
def generate!
|
169
|
+
# Some libraries like Devise did not load their routes yet
|
170
|
+
# so we will wait until initialization process finishes
|
150
171
|
# https://github.com/railsware/js-routes/issues/7
|
151
172
|
Rails.configuration.after_initialize do
|
152
|
-
|
153
|
-
|
154
|
-
js_content = generate
|
173
|
+
file_path = Rails.root.join(@configuration.output_file)
|
174
|
+
source_code = generate
|
155
175
|
|
156
176
|
# We don't need to rewrite file if it already exist and have same content.
|
157
177
|
# It helps asset pipeline or webpack understand that file wasn't changed.
|
158
|
-
next if File.exist?(file_path) && File.read(file_path) ==
|
178
|
+
next if File.exist?(file_path) && File.read(file_path) == source_code
|
159
179
|
|
160
180
|
File.open(file_path, 'w') do |f|
|
161
|
-
f.write
|
181
|
+
f.write source_code
|
162
182
|
end
|
163
183
|
end
|
164
184
|
end
|
165
185
|
|
166
186
|
protected
|
167
187
|
|
188
|
+
def js_variables
|
189
|
+
{
|
190
|
+
'GEM_VERSION' => JsRoutes::VERSION,
|
191
|
+
'ROUTES_OBJECT' => routes_object,
|
192
|
+
'RAILS_VERSION' => ActionPack.version,
|
193
|
+
'DEPRECATED_GLOBBING_BEHAVIOR' => ActionPack::VERSION::MAJOR == 4 && ActionPack::VERSION::MINOR == 0,
|
194
|
+
|
195
|
+
'APP_CLASS' => application.class.to_s,
|
196
|
+
'NAMESPACE' => json(@configuration.namespace),
|
197
|
+
'DEFAULT_URL_OPTIONS' => json(@configuration.default_url_options),
|
198
|
+
'PREFIX' => json(@configuration.prefix),
|
199
|
+
'SPECIAL_OPTIONS_KEY' => json(@configuration.special_options_key),
|
200
|
+
'SERIALIZER' => @configuration.serializer || json(nil),
|
201
|
+
'MODULE_TYPE' => json(@configuration.module_type),
|
202
|
+
'WRAPPER' => @configuration.esm? ? 'const __jsr = ' : '',
|
203
|
+
}
|
204
|
+
end
|
205
|
+
|
168
206
|
def application
|
169
207
|
@configuration.application
|
170
208
|
end
|
@@ -178,32 +216,52 @@ class JsRoutes
|
|
178
216
|
end
|
179
217
|
|
180
218
|
def routes_object
|
181
|
-
return json({}) if @configuration.
|
219
|
+
return json({}) if @configuration.modern?
|
182
220
|
properties = routes_list.map do |comment, name, body|
|
183
221
|
"#{comment}#{name}: #{body}".indent(2)
|
184
222
|
end
|
185
223
|
"{\n" + properties.join(",\n\n") + "}\n"
|
186
224
|
end
|
187
225
|
|
188
|
-
|
189
|
-
[
|
226
|
+
def static_exports
|
227
|
+
[:configure, :config, :serialize].map do |name|
|
228
|
+
[
|
229
|
+
"", name,
|
230
|
+
@configuration.dts? ?
|
231
|
+
"RouterExposedMethods['#{name}']" :
|
232
|
+
"__jsr.#{name}"
|
233
|
+
]
|
234
|
+
end
|
190
235
|
end
|
191
236
|
|
192
237
|
def routes_export
|
193
|
-
return "" unless @configuration.
|
194
|
-
[*
|
195
|
-
"#{comment}export const #{name}
|
196
|
-
end.join
|
238
|
+
return "" unless @configuration.modern?
|
239
|
+
[*static_exports, *routes_list].map do |comment, name, body|
|
240
|
+
"#{comment}export const #{name}#{export_separator}#{body};\n\n"
|
241
|
+
end.join
|
242
|
+
end
|
243
|
+
|
244
|
+
def prevent_types_export
|
245
|
+
return "" unless @configuration.dts?
|
246
|
+
<<-JS
|
247
|
+
// By some reason this line prevents all types in a file
|
248
|
+
// from being automatically exported
|
249
|
+
export {};
|
250
|
+
JS
|
251
|
+
end
|
252
|
+
|
253
|
+
def export_separator
|
254
|
+
@configuration.dts? ? ': ' : ' = '
|
197
255
|
end
|
198
256
|
|
199
257
|
def routes_list
|
200
258
|
named_routes.sort_by(&:first).flat_map do |_, route|
|
201
259
|
route_helpers_if_match(route) + mounted_app_routes(route)
|
202
|
-
end
|
260
|
+
end
|
203
261
|
end
|
204
262
|
|
205
263
|
def mounted_app_routes(route)
|
206
|
-
rails_engine_app =
|
264
|
+
rails_engine_app = app_from_route(route)
|
207
265
|
if rails_engine_app.respond_to?(:superclass) &&
|
208
266
|
rails_engine_app.superclass == Rails::Engine && !route.path.anchored
|
209
267
|
rails_engine_app.routes.named_routes.flat_map do |_, engine_route|
|
@@ -214,7 +272,7 @@ class JsRoutes
|
|
214
272
|
end
|
215
273
|
end
|
216
274
|
|
217
|
-
def
|
275
|
+
def app_from_route(route)
|
218
276
|
# rails engine in Rails 4.2 use additional
|
219
277
|
# ActionDispatch::Routing::Mapper::Constraints, which contain app
|
220
278
|
if route.app.respond_to?(:app) && route.app.respond_to?(:constraints)
|
@@ -229,6 +287,19 @@ class JsRoutes
|
|
229
287
|
end
|
230
288
|
|
231
289
|
class JsRoute #:nodoc:
|
290
|
+
FILTERED_DEFAULT_PARTS = [:controller, :action]
|
291
|
+
URL_OPTIONS = [:protocol, :domain, :host, :port, :subdomain]
|
292
|
+
NODE_TYPES = {
|
293
|
+
GROUP: 1,
|
294
|
+
CAT: 2,
|
295
|
+
SYMBOL: 3,
|
296
|
+
OR: 4,
|
297
|
+
STAR: 5,
|
298
|
+
LITERAL: 6,
|
299
|
+
SLASH: 7,
|
300
|
+
DOT: 8
|
301
|
+
}
|
302
|
+
|
232
303
|
attr_reader :configuration, :route, :parent_route
|
233
304
|
|
234
305
|
def initialize(configuration, route, parent_route = nil)
|
@@ -238,22 +309,36 @@ class JsRoutes
|
|
238
309
|
end
|
239
310
|
|
240
311
|
def helpers
|
241
|
-
|
242
|
-
[]
|
243
|
-
else
|
244
|
-
[false, true].map do |absolute|
|
245
|
-
absolute && !@configuration[:url_links] ?
|
246
|
-
nil : [ documentation, helper_name(absolute), body(absolute) ]
|
247
|
-
end
|
312
|
+
helper_types.map do |absolute|
|
313
|
+
[ documentation, helper_name(absolute), body(absolute) ]
|
248
314
|
end
|
249
315
|
end
|
250
316
|
|
317
|
+
def helper_types
|
318
|
+
return [] unless match_configuration?
|
319
|
+
@configuration[:url_links] ? [true, false] : [false]
|
320
|
+
end
|
321
|
+
|
251
322
|
def body(absolute)
|
252
|
-
|
323
|
+
@configuration.dts? ?
|
324
|
+
definition_body : "__jsr.r(#{arguments(absolute).map{|a| json(a)}.join(', ')})"
|
325
|
+
end
|
326
|
+
|
327
|
+
def definition_body
|
328
|
+
args = required_parts.map{|p| "#{apply_case(p)}: RequiredRouteParameter"}
|
329
|
+
args << "options?: #{optional_parts_type} & RouteOptions"
|
330
|
+
"((\n#{args.join(",\n").indent(2)}\n) => string) & RouteHelperExtras"
|
253
331
|
end
|
254
332
|
|
333
|
+
def optional_parts_type
|
334
|
+
@optional_parts_type ||=
|
335
|
+
"{" + optional_parts.map {|p| "#{p}?: OptionalRouteParameter"}.join(', ') + "}"
|
336
|
+
end
|
337
|
+
|
338
|
+
protected
|
339
|
+
|
255
340
|
def arguments(absolute)
|
256
|
-
absolute ? base_arguments
|
341
|
+
absolute ? [*base_arguments, true] : base_arguments
|
257
342
|
end
|
258
343
|
|
259
344
|
def match_configuration?
|
@@ -298,10 +383,15 @@ JS
|
|
298
383
|
route.required_parts
|
299
384
|
end
|
300
385
|
|
301
|
-
|
386
|
+
def optional_parts
|
387
|
+
route.path.optional_names
|
388
|
+
end
|
302
389
|
|
303
390
|
def base_arguments
|
304
|
-
|
391
|
+
@base_arguments ||= [parts_table, serialize(spec, parent_spec)]
|
392
|
+
end
|
393
|
+
|
394
|
+
def parts_table
|
305
395
|
parts_table = {}
|
306
396
|
route.parts.each do |part, hash|
|
307
397
|
parts_table[part] ||= {}
|
@@ -318,11 +408,7 @@ JS
|
|
318
408
|
parts_table[part][:d] = value
|
319
409
|
end
|
320
410
|
end
|
321
|
-
|
322
|
-
parts_table, serialize(spec, parent_spec)
|
323
|
-
].map do |argument|
|
324
|
-
json(argument)
|
325
|
-
end
|
411
|
+
parts_table
|
326
412
|
end
|
327
413
|
|
328
414
|
def documentation_params
|
@@ -371,3 +457,5 @@ JS
|
|
371
457
|
end
|
372
458
|
end
|
373
459
|
end
|
460
|
+
|
461
|
+
require "js_routes/generators/webpacker"
|
data/lib/routes.d.ts
CHANGED
@@ -2,14 +2,38 @@
|
|
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
|
-
declare type
|
6
|
-
|
7
|
-
|
8
|
-
declare type
|
9
|
-
|
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 = {
|
10
26
|
requiredParams(): string[];
|
11
27
|
toString(): string;
|
12
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;
|
13
37
|
declare type RouteHelpers = Record<string, RouteHelper>;
|
14
38
|
declare type Configuration = {
|
15
39
|
prefix: string;
|
@@ -17,9 +41,6 @@ declare type Configuration = {
|
|
17
41
|
special_options_key: string;
|
18
42
|
serializer: Serializer;
|
19
43
|
};
|
20
|
-
declare type Optional<T> = {
|
21
|
-
[P in keyof T]?: T[P] | null;
|
22
|
-
};
|
23
44
|
interface RouterExposedMethods {
|
24
45
|
config(): Configuration;
|
25
46
|
configure(arg: Partial<Configuration>): Configuration;
|
@@ -29,15 +50,16 @@ declare type KeywordUrlOptions = Optional<{
|
|
29
50
|
host: string;
|
30
51
|
protocol: string;
|
31
52
|
subdomain: string;
|
32
|
-
port: string;
|
53
|
+
port: string | number;
|
33
54
|
anchor: string;
|
34
55
|
trailing_slash: boolean;
|
35
56
|
}>;
|
57
|
+
declare type RouteOptions = KeywordUrlOptions & RouteParameters;
|
36
58
|
declare type PartsTable = Record<string, {
|
37
59
|
r?: boolean;
|
38
|
-
d?:
|
60
|
+
d?: OptionalRouteParameter;
|
39
61
|
}>;
|
40
|
-
declare type ModuleType = "CJS" | "AMD" | "UMD" | "ESM";
|
62
|
+
declare type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "DTS" | "NIL";
|
41
63
|
declare const RubyVariables: {
|
42
64
|
PREFIX: string;
|
43
65
|
DEPRECATED_GLOBBING_BEHAVIOR: boolean;
|
@@ -46,7 +68,7 @@ declare const RubyVariables: {
|
|
46
68
|
SERIALIZER: Serializer;
|
47
69
|
NAMESPACE: string;
|
48
70
|
ROUTES_OBJECT: RouteHelpers;
|
49
|
-
MODULE_TYPE: ModuleType
|
71
|
+
MODULE_TYPE: ModuleType;
|
50
72
|
WRAPPER: <T>(callback: T) => T;
|
51
73
|
};
|
52
74
|
declare const define: undefined | (((arg: unknown[], callback: () => unknown) => void) & {
|
data/lib/routes.js
CHANGED
@@ -16,27 +16,35 @@ RubyVariables.WRAPPER((that) => {
|
|
16
16
|
NodeTypes[NodeTypes["DOT"] = 8] = "DOT";
|
17
17
|
})(NodeTypes || (NodeTypes = {}));
|
18
18
|
const Root = that;
|
19
|
+
const isBroswer = typeof window !== "undefined";
|
19
20
|
const ModuleReferences = {
|
20
21
|
CJS: {
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
define(routes) {
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
24
|
+
module.exports = routes;
|
25
|
+
},
|
26
|
+
isSupported() {
|
27
|
+
return typeof module === "object";
|
28
|
+
},
|
24
29
|
},
|
25
30
|
AMD: {
|
26
|
-
define
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
31
|
+
define(routes) {
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
33
|
+
define([], function () {
|
34
|
+
return routes;
|
35
|
+
});
|
36
|
+
},
|
37
|
+
isSupported() {
|
38
|
+
return typeof define === "function" && !!define.amd;
|
39
|
+
},
|
32
40
|
},
|
33
41
|
UMD: {
|
34
|
-
define
|
35
|
-
if (ModuleReferences.AMD.
|
42
|
+
define(routes) {
|
43
|
+
if (ModuleReferences.AMD.isSupported()) {
|
36
44
|
ModuleReferences.AMD.define(routes);
|
37
45
|
}
|
38
46
|
else {
|
39
|
-
if (ModuleReferences.CJS.
|
47
|
+
if (ModuleReferences.CJS.isSupported()) {
|
40
48
|
try {
|
41
49
|
ModuleReferences.CJS.define(routes);
|
42
50
|
}
|
@@ -47,11 +55,36 @@ RubyVariables.WRAPPER((that) => {
|
|
47
55
|
}
|
48
56
|
}
|
49
57
|
},
|
50
|
-
|
58
|
+
isSupported() {
|
59
|
+
return (ModuleReferences.AMD.isSupported() ||
|
60
|
+
ModuleReferences.CJS.isSupported());
|
61
|
+
},
|
51
62
|
},
|
52
63
|
ESM: {
|
53
|
-
define
|
54
|
-
|
64
|
+
define() {
|
65
|
+
// Module can only be defined using ruby code generation
|
66
|
+
},
|
67
|
+
isSupported() {
|
68
|
+
// Its impossible to check if "export" keyword is supported
|
69
|
+
return true;
|
70
|
+
},
|
71
|
+
},
|
72
|
+
NIL: {
|
73
|
+
define(routes) {
|
74
|
+
Utils.namespace(Root, RubyVariables.NAMESPACE, routes);
|
75
|
+
},
|
76
|
+
isSupported() {
|
77
|
+
return !!Root;
|
78
|
+
},
|
79
|
+
},
|
80
|
+
DTS: {
|
81
|
+
// Acts the same as ESM
|
82
|
+
define(routes) {
|
83
|
+
ModuleReferences.ESM.define(routes);
|
84
|
+
},
|
85
|
+
isSupported() {
|
86
|
+
return ModuleReferences.ESM.isSupported();
|
87
|
+
},
|
55
88
|
},
|
56
89
|
};
|
57
90
|
class ParametersMissing extends Error {
|
@@ -62,7 +95,6 @@ RubyVariables.WRAPPER((that) => {
|
|
62
95
|
this.name = ParametersMissing.name;
|
63
96
|
}
|
64
97
|
}
|
65
|
-
const DeprecatedGlobbingBehavior = RubyVariables.DEPRECATED_GLOBBING_BEHAVIOR;
|
66
98
|
const UriEncoderSegmentRegex = /[^a-zA-Z0-9\-._~!$&'()*+,;=:@]/g;
|
67
99
|
const ReservedOptions = [
|
68
100
|
"anchor",
|
@@ -140,7 +172,7 @@ RubyVariables.WRAPPER((that) => {
|
|
140
172
|
}
|
141
173
|
looks_like_serialized_model(object) {
|
142
174
|
return (this.is_object(object) &&
|
143
|
-
!
|
175
|
+
!(this.configuration.special_options_key in object) &&
|
144
176
|
("id" in object || "to_param" in object || "toParam" in object));
|
145
177
|
}
|
146
178
|
path_identifier(object) {
|
@@ -298,9 +330,7 @@ RubyVariables.WRAPPER((that) => {
|
|
298
330
|
}
|
299
331
|
}
|
300
332
|
encode_segment(segment) {
|
301
|
-
return segment.replace(UriEncoderSegmentRegex,
|
302
|
-
return encodeURIComponent(str);
|
303
|
-
});
|
333
|
+
return segment.replace(UriEncoderSegmentRegex, (str) => encodeURIComponent(str));
|
304
334
|
}
|
305
335
|
is_optional_node(node) {
|
306
336
|
return [NodeTypes.STAR, NodeTypes.SYMBOL, NodeTypes.CAT].includes(node);
|
@@ -342,7 +372,9 @@ RubyVariables.WRAPPER((that) => {
|
|
342
372
|
value = value.join("/");
|
343
373
|
}
|
344
374
|
const result = this.path_identifier(value);
|
345
|
-
return
|
375
|
+
return RubyVariables.DEPRECATED_GLOBBING_BEHAVIOR
|
376
|
+
? result
|
377
|
+
: encodeURI(result);
|
346
378
|
}
|
347
379
|
get_prefix() {
|
348
380
|
const prefix = this.configuration.prefix;
|
@@ -386,32 +418,17 @@ RubyVariables.WRAPPER((that) => {
|
|
386
418
|
port = port ? ":" + port : "";
|
387
419
|
return protocol + "://" + subdomain + hostname + port;
|
388
420
|
}
|
389
|
-
has_location() {
|
390
|
-
return this.is_not_nullable(window) && !!window.location;
|
391
|
-
}
|
392
421
|
current_host() {
|
393
|
-
|
394
|
-
|
395
|
-
}
|
396
|
-
else {
|
397
|
-
return null;
|
398
|
-
}
|
422
|
+
var _a;
|
423
|
+
return (isBroswer && ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.hostname)) || "";
|
399
424
|
}
|
400
425
|
current_protocol() {
|
401
|
-
|
402
|
-
|
403
|
-
}
|
404
|
-
else {
|
405
|
-
return "http";
|
406
|
-
}
|
426
|
+
var _a, _b;
|
427
|
+
return ((isBroswer && ((_b = (_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.protocol) === null || _b === void 0 ? void 0 : _b.replace(/:$/, ""))) || "http");
|
407
428
|
}
|
408
429
|
current_port() {
|
409
|
-
|
410
|
-
|
411
|
-
}
|
412
|
-
else {
|
413
|
-
return "";
|
414
|
-
}
|
430
|
+
var _a;
|
431
|
+
return (isBroswer && ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.port)) || "";
|
415
432
|
}
|
416
433
|
is_object(value) {
|
417
434
|
return (typeof value === "object" &&
|
@@ -449,7 +466,7 @@ RubyVariables.WRAPPER((that) => {
|
|
449
466
|
return { ...this.configuration };
|
450
467
|
}
|
451
468
|
is_module_supported(name) {
|
452
|
-
return ModuleReferences[name].
|
469
|
+
return ModuleReferences[name].isSupported();
|
453
470
|
}
|
454
471
|
ensure_module_supported(name) {
|
455
472
|
if (!this.is_module_supported(name)) {
|
@@ -457,9 +474,6 @@ RubyVariables.WRAPPER((that) => {
|
|
457
474
|
}
|
458
475
|
}
|
459
476
|
define_module(name, module) {
|
460
|
-
if (!name) {
|
461
|
-
return;
|
462
|
-
}
|
463
477
|
this.ensure_module_supported(name);
|
464
478
|
ModuleReferences[name].define(module);
|
465
479
|
}
|
@@ -484,9 +498,6 @@ RubyVariables.WRAPPER((that) => {
|
|
484
498
|
},
|
485
499
|
...RubyVariables.ROUTES_OBJECT,
|
486
500
|
};
|
487
|
-
Utils.
|
488
|
-
if (RubyVariables.MODULE_TYPE) {
|
489
|
-
Utils.define_module(RubyVariables.MODULE_TYPE, result);
|
490
|
-
}
|
501
|
+
Utils.define_module(RubyVariables.MODULE_TYPE, result);
|
491
502
|
return result;
|
492
503
|
})(this);
|