From c0794a53f44d03e191c5ec2e93b4427f15e722e4 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Tue, 10 Dec 2024 11:27:43 +0100 Subject: [PATCH] Deprecated prefix option in favor of default_url_options.script_name --- CHANGELOG.md | 1 + Readme.md | 4 +- lib/js_routes/configuration.rb | 7 +- lib/js_routes/utils.rb | 9 +++ lib/routes.js | 3 + lib/routes.ts | 5 ++ .../module_types/dts/routes.spec.d.ts | 1 + spec/js_routes/options_spec.rb | 65 ++++++++++--------- 8 files changed, 60 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe83129..d6079ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [2.3.4] * Migrate to yarn 4 +* Deprecated `prefix` option in favor of `default_url_options.script_name`. * Add support for `script_name` [Rails helper option](https://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for). ``` javascript diff --git a/Readme.md b/Readme.md index 10d0a7e..270c6e6 100644 --- a/Readme.md +++ b/Readme.md @@ -353,10 +353,8 @@ Options to configure routes formatting. These options are available both in Ruby * `default_url_options` - default parameters used when generating URLs * Example: `{format: "json", trailing_slash: true, protocol: "https", subdomain: "api", host: "example.com", port: 3000}` + * See [`url_for` doc](https://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for) for list of supported options * Default: `{}` -* `prefix` - string that will prepend any generated URL. Usually used when app URL root includes a path component. - * Example: `/rails-app` - * Default: `Rails.application.config.relative_url_root` * `serializer` - a JS function that serializes a Javascript Hash object into URL paramters like `{a: 1, b: 2} => "a=1&b=2"`. * Default: `nil`. Uses built-in serializer compatible with Rails * Example: `jQuery.param` - use jQuery's serializer algorithm. You can attach serialize function from your favorite AJAX framework. diff --git a/lib/js_routes/configuration.rb b/lib/js_routes/configuration.rb index ceae54b..e337782 100644 --- a/lib/js_routes/configuration.rb +++ b/lib/js_routes/configuration.rb @@ -19,7 +19,7 @@ class Configuration sig { returns(FileName) } attr_accessor :file sig { returns(Prefix) } - attr_accessor :prefix + attr_reader :prefix sig { returns(T::Boolean) } attr_accessor :url_links sig { returns(T::Boolean) } @@ -91,6 +91,11 @@ def [](attribute) public_send(attribute) end + def prefix=(value) + JsRoutes::Utils.deprecator.warn("JsRoutes configuration prefix is deprecated in favor of default_url_options.script_name.") + @prefix = value + end + sig { params(attributes: Options).returns(JsRoutes::Configuration) } def merge(attributes) clone.assign(attributes) diff --git a/lib/js_routes/utils.rb b/lib/js_routes/utils.rb index 4a8e9f4..9f97b9b 100644 --- a/lib/js_routes/utils.rb +++ b/lib/js_routes/utils.rb @@ -13,6 +13,15 @@ def self.shakapacker nil end end + + sig { returns(T.untyped) } + def self.deprecator + if defined?(Rails) && Rails.version >= "7.1.0" + Rails.deprecator + else + ActiveSupport::Deprecation + end + end end end diff --git a/lib/routes.js b/lib/routes.js index 2710080..dde1b2c 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -490,6 +490,9 @@ RubyVariables.WRAPPER( return ReservedOptions.includes(key); } configure(new_config) { + if (new_config.prefix) { + console.warn("JsRoutes configuration prefix option is deprecated in favor of default_url_options.script_name."); + } this.configuration = { ...this.configuration, ...new_config }; return this.configuration; } diff --git a/lib/routes.ts b/lib/routes.ts index 704d5ab..0485111 100644 --- a/lib/routes.ts +++ b/lib/routes.ts @@ -708,6 +708,11 @@ RubyVariables.WRAPPER( } configure(new_config: Partial): Configuration { + if (new_config.prefix) { + console.warn( + "JsRoutes configuration prefix option is deprecated in favor of default_url_options.script_name." + ); + } this.configuration = { ...this.configuration, ...new_config }; return this.configuration; } diff --git a/spec/js_routes/module_types/dts/routes.spec.d.ts b/spec/js_routes/module_types/dts/routes.spec.d.ts index 501c0db..727a41f 100644 --- a/spec/js_routes/module_types/dts/routes.spec.d.ts +++ b/spec/js_routes/module_types/dts/routes.spec.d.ts @@ -50,6 +50,7 @@ declare type KeywordUrlOptions = Optional<{ port: string | number; anchor: string; trailing_slash: boolean; + script_name: string; params: RouteParameters; }>; declare type RouteOptions = KeywordUrlOptions & RouteParameters; diff --git a/spec/js_routes/options_spec.rb b/spec/js_routes/options_spec.rb index fb9a77d..d144e34 100644 --- a/spec/js_routes/options_spec.rb +++ b/spec/js_routes/options_spec.rb @@ -116,44 +116,54 @@ end end - context "when prefix with trailing slash is specified" do + describe "prefix option" do + around do |e| + JsRoutes::Utils.deprecator.silence do + e.run + end + end - let(:_options) { {:prefix => "/myprefix/" } } + context "with trailing slash is specified" do + let(:_options) { {:prefix => "/myprefix/" } } - it "should render routing with prefix" do + it "should render routing with prefix" do expectjs("Routes.inbox_path(1)").to eq("/myprefix#{test_routes.inbox_path(1)}") - end + end - it "should render routing with prefix set in JavaScript" do - evaljs("Routes.configure({prefix: '/newprefix/'})") - expectjs("Routes.config().prefix").to eq("/newprefix/") - expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}") + it "should render routing with prefix set in JavaScript" do + evaljs("Routes.configure({prefix: '/newprefix/'})") + expectjs("Routes.config().prefix").to eq("/newprefix/") + expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}") + end end - end - - context "when prefix with http:// is specified" do - - let(:_options) { {:prefix => "http://localhost:3000" } } + context "with http:// is specified" do + let(:_options) { {:prefix => "http://localhost:3000" } } - it "should render routing with prefix" do - expectjs("Routes.inbox_path(1)").to eq(_options[:prefix] + test_routes.inbox_path(1)) + it "should render routing with prefix" do + expectjs("Routes.inbox_path(1)").to eq(_options[:prefix] + test_routes.inbox_path(1)) + end end - end - context "when prefix without trailing slash is specified" do + context "without trailing slash is specified" do + let(:_options) { {:prefix => "/myprefix" } } - let(:_options) { {:prefix => "/myprefix" } } + it "should render routing with prefix" do + expectjs("Routes.inbox_path(1)").to eq("/myprefix#{test_routes.inbox_path(1)}") + end - it "should render routing with prefix" do - expectjs("Routes.inbox_path(1)").to eq("/myprefix#{test_routes.inbox_path(1)}") + it "should render routing with prefix set in JavaScript" do + evaljs("Routes.configure({prefix: '/newprefix/'})") + expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}") + end end - it "should render routing with prefix set in JavaScript" do - evaljs("Routes.configure({prefix: '/newprefix/'})") - expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}") + context "combined with url links and default_url_options" do + let(:_options) { { :prefix => "/api", :url_links => true, :default_url_options => {:host => 'example.com'} } } + it "should generate path and url links" do + expectjs("Routes.inbox_url(1)").to eq("http://example.com/api#{test_routes.inbox_path(1)}") + end end - end context "when default format is specified" do @@ -381,13 +391,6 @@ end end - context "with prefix option" do - let(:_options) { { :prefix => "/api", :url_links => true, :default_url_options => {:host => 'example.com'} } } - it "should generate path and url links" do - expectjs("Routes.inbox_url(1)").to eq("http://example.com/api#{test_routes.inbox_path(1)}") - end - end - context "with compact option" do let(:_options) { { :compact => true, :url_links => true, :default_url_options => {:host => 'example.com'} } } it "does not affect url helpers" do