js-routes 2.2.0 → 2.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/routes.js CHANGED
@@ -2,7 +2,7 @@
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
- RubyVariables.WRAPPER((that) => {
5
+ RubyVariables.WRAPPER(() => {
6
6
  const hasProp = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
7
7
  let NodeTypes;
8
8
  (function (NodeTypes) {
@@ -15,8 +15,7 @@ RubyVariables.WRAPPER((that) => {
15
15
  NodeTypes[NodeTypes["SLASH"] = 7] = "SLASH";
16
16
  NodeTypes[NodeTypes["DOT"] = 8] = "DOT";
17
17
  })(NodeTypes || (NodeTypes = {}));
18
- const Root = that;
19
- const isBroswer = typeof window !== "undefined";
18
+ const isBrowser = typeof window !== "undefined";
20
19
  const ModuleReferences = {
21
20
  CJS: {
22
21
  define(routes) {
@@ -70,11 +69,11 @@ RubyVariables.WRAPPER((that) => {
70
69
  },
71
70
  },
72
71
  NIL: {
73
- define(routes) {
74
- Utils.namespace(Root, RubyVariables.NAMESPACE, routes);
72
+ define() {
73
+ // Defined using RubyVariables.WRAPPER
75
74
  },
76
75
  isSupported() {
77
- return !RubyVariables.NAMESPACE || !!Root;
76
+ return true;
78
77
  },
79
78
  },
80
79
  DTS: {
@@ -422,15 +421,15 @@ RubyVariables.WRAPPER((that) => {
422
421
  }
423
422
  current_host() {
424
423
  var _a;
425
- return (isBroswer && ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.hostname)) || "";
424
+ return (isBrowser && ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.hostname)) || "";
426
425
  }
427
426
  current_protocol() {
428
427
  var _a, _b;
429
- 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");
428
+ return ((isBrowser && ((_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");
430
429
  }
431
430
  current_port() {
432
431
  var _a;
433
- return (isBroswer && ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.port)) || "";
432
+ return (isBrowser && ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.port)) || "";
434
433
  }
435
434
  is_object(value) {
436
435
  return (typeof value === "object" &&
@@ -445,21 +444,6 @@ RubyVariables.WRAPPER((that) => {
445
444
  is_reserved_option(key) {
446
445
  return ReservedOptions.includes(key);
447
446
  }
448
- namespace(object, namespace, routes) {
449
- const parts = (namespace === null || namespace === void 0 ? void 0 : namespace.split(".")) || [];
450
- if (parts.length === 0) {
451
- return;
452
- }
453
- for (let index = 0; index < parts.length; index++) {
454
- const part = parts[index];
455
- if (index < parts.length - 1) {
456
- object = object[part] || (object[part] = {});
457
- }
458
- else {
459
- object[part] = routes;
460
- }
461
- }
462
- }
463
447
  configure(new_config) {
464
448
  this.configuration = { ...this.configuration, ...new_config };
465
449
  return this.configuration;
@@ -502,4 +486,4 @@ RubyVariables.WRAPPER((that) => {
502
486
  };
503
487
  Utils.define_module(RubyVariables.MODULE_TYPE, result);
504
488
  return result;
505
- })(this);
489
+ })();
data/lib/routes.ts CHANGED
@@ -84,7 +84,6 @@ declare const RubyVariables: {
84
84
  SPECIAL_OPTIONS_KEY: string;
85
85
  DEFAULT_URL_OPTIONS: RouteParameters;
86
86
  SERIALIZER: Serializer;
87
- NAMESPACE: string;
88
87
  ROUTES_OBJECT: RouteHelpers;
89
88
  MODULE_TYPE: ModuleType;
90
89
  WRAPPER: <T>(callback: T) => T;
@@ -97,7 +96,7 @@ declare const define:
97
96
  declare const module: { exports: any } | undefined;
98
97
 
99
98
  RubyVariables.WRAPPER(
100
- (that: unknown): RouterExposedMethods => {
99
+ (): RouterExposedMethods => {
101
100
  const hasProp = (value: unknown, key: string) =>
102
101
  Object.prototype.hasOwnProperty.call(value, key);
103
102
  enum NodeTypes {
@@ -128,8 +127,7 @@ RubyVariables.WRAPPER(
128
127
  [T in keyof RouteNodes]: RouteNode<T>;
129
128
  }[keyof RouteNodes];
130
129
 
131
- const Root = that;
132
- const isBroswer = typeof window !== "undefined";
130
+ const isBrowser = typeof window !== "undefined";
133
131
  type ModuleDefinition = {
134
132
  define: (routes: RouterExposedMethods) => void;
135
133
  isSupported: () => boolean;
@@ -186,11 +184,11 @@ RubyVariables.WRAPPER(
186
184
  },
187
185
  },
188
186
  NIL: {
189
- define(routes) {
190
- Utils.namespace(Root, RubyVariables.NAMESPACE, routes);
187
+ define() {
188
+ // Defined using RubyVariables.WRAPPER
191
189
  },
192
190
  isSupported() {
193
- return !RubyVariables.NAMESPACE || !!Root;
191
+ return true;
194
192
  },
195
193
  },
196
194
  DTS: {
@@ -628,17 +626,17 @@ RubyVariables.WRAPPER(
628
626
  }
629
627
 
630
628
  current_host(): string {
631
- return (isBroswer && window?.location?.hostname) || "";
629
+ return (isBrowser && window?.location?.hostname) || "";
632
630
  }
633
631
 
634
632
  current_protocol(): string {
635
633
  return (
636
- (isBroswer && window?.location?.protocol?.replace(/:$/, "")) || "http"
634
+ (isBrowser && window?.location?.protocol?.replace(/:$/, "")) || "http"
637
635
  );
638
636
  }
639
637
 
640
638
  current_port(): string {
641
- return (isBroswer && window?.location?.port) || "";
639
+ return (isBrowser && window?.location?.port) || "";
642
640
  }
643
641
 
644
642
  is_object(value: unknown): value is Record<string, unknown> {
@@ -660,25 +658,6 @@ RubyVariables.WRAPPER(
660
658
  return ReservedOptions.includes(key as any);
661
659
  }
662
660
 
663
- namespace(
664
- object: any,
665
- namespace: string | null | undefined,
666
- routes: unknown
667
- ): void {
668
- const parts = namespace?.split(".") || [];
669
- if (parts.length === 0) {
670
- return;
671
- }
672
- for (let index = 0; index < parts.length; index++) {
673
- const part = parts[index];
674
- if (index < parts.length - 1) {
675
- object = object[part] || (object[part] = {});
676
- } else {
677
- object[part] = routes;
678
- }
679
- }
680
- }
681
-
682
661
  configure(new_config: Partial<Configuration>): Configuration {
683
662
  this.configuration = { ...this.configuration, ...new_config };
684
663
  return this.configuration;
@@ -734,4 +713,4 @@ RubyVariables.WRAPPER(
734
713
  Utils.define_module(RubyVariables.MODULE_TYPE, result);
735
714
  return result;
736
715
  }
737
- )(this);
716
+ )();
@@ -66,7 +66,6 @@ declare const RubyVariables: {
66
66
  SPECIAL_OPTIONS_KEY: string;
67
67
  DEFAULT_URL_OPTIONS: RouteParameters;
68
68
  SERIALIZER: Serializer;
69
- NAMESPACE: string;
70
69
  ROUTES_OBJECT: RouteHelpers;
71
70
  MODULE_TYPE: ModuleType;
72
71
  WRAPPER: <T>(callback: T) => T;
@@ -45,6 +45,7 @@ describe JsRoutes, "compatibility with NIL (legacy browser)" do
45
45
  let(:_presetup) { "" }
46
46
  before do
47
47
  evaljs("var window = this;")
48
+ evaljs("window.PHM = {}")
48
49
  evaljs(_presetup)
49
50
  evaljs(generated_js)
50
51
  end
@@ -399,13 +399,16 @@ describe JsRoutes, "options" do
399
399
  end
400
400
 
401
401
  let(:_presetup) do
402
- window = {location: {
402
+ location = {
403
403
  protocol: current_protocol,
404
404
  hostname: current_hostname,
405
405
  port: current_port,
406
406
  host: current_host,
407
- }}
408
- "const window = #{ActiveSupport::JSON.encode(window)}"
407
+ }
408
+ [
409
+ "const window = this;",
410
+ "window.location = #{ActiveSupport::JSON.encode(location)};",
411
+ ].join("\n")
409
412
  end
410
413
 
411
414
  context "without specifying a default host" do
@@ -2,8 +2,11 @@ require "spec_helper"
2
2
 
3
3
  describe JsRoutes, "compatibility with Rails" do
4
4
 
5
+ let(:generated_js) do
6
+ JsRoutes.generate({module_type: nil, namespace: 'Routes'})
7
+ end
5
8
  before(:each) do
6
- evaljs(JsRoutes.generate({module_type: nil, namespace: 'Routes'}))
9
+ evaljs(generated_js)
7
10
  end
8
11
 
9
12
  it "should generate collection routing" do
data/spec/spec_helper.rb CHANGED
@@ -42,10 +42,14 @@ end
42
42
  def evaljs(string, force: false, filename: 'context.js')
43
43
  jscontext(force).eval(string, filename: filename)
44
44
  rescue MiniRacer::ParseError => e
45
- message = e.message
46
- _, _, line, _ = message.split(':')
47
- code = line && string.split("\n")[line.to_i-1]
48
- raise "#{message}. Code: #{code.strip}";
45
+ trace = e.message
46
+ _, _, line, _ = trace.split(':')
47
+ if line
48
+ code = string.split("\n")[line.to_i-1]
49
+ raise "#{trace}. Code: #{code.strip}";
50
+ else
51
+ raise e
52
+ end
49
53
  rescue MiniRacer::RuntimeError => e
50
54
  raise e
51
55
  end
@@ -62,6 +66,10 @@ def planner_routes
62
66
  Planner::Engine.routes.url_helpers
63
67
  end
64
68
 
69
+ def log(string)
70
+ evaljs("console.log(#{string})")
71
+ end
72
+
65
73
  ActiveSupport::Inflector.inflections do |inflect|
66
74
  inflect.irregular "budgie", "budgies"
67
75
  end
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.2.0
4
+ version: 2.2.3
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-12-21 00:00:00.000000000 Z
11
+ date: 2022-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -170,10 +170,13 @@ files:
170
170
  - js-routes.gemspec
171
171
  - lib/js-routes.rb
172
172
  - lib/js_routes.rb
173
+ - lib/js_routes/configuration.rb
173
174
  - lib/js_routes/engine.rb
174
175
  - lib/js_routes/generators/middleware.rb
175
176
  - lib/js_routes/generators/webpacker.rb
177
+ - lib/js_routes/instance.rb
176
178
  - lib/js_routes/middleware.rb
179
+ - lib/js_routes/route.rb
177
180
  - lib/js_routes/version.rb
178
181
  - lib/routes.d.ts
179
182
  - lib/routes.js