js-routes 2.3.5 → 2.3.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5340711dfd4078cf7c63d618dbd4957f58fd79d0e3f82ca6b1f6a0b6cd0381a5
4
- data.tar.gz: 00a7ae1424f6e61211f7bd0cc26f86542c3054c8d17ae062f026da2a44da7b32
3
+ metadata.gz: 8e1323cccad426e331d7f99923e967b09d06e028ec3f2665798dad91ff5ce582
4
+ data.tar.gz: 4ddb07ef468133b25d7ace1313b948f689a77a1c576e3102e88ff2b87f82a5c5
5
5
  SHA512:
6
- metadata.gz: '00963c11a5f95f269924111950e8a3f9b9c9fc9347d3a52e745b191de8d6550d9d2d518d32088b3e98eec2e3fe8022f585523b99c71460be2d234f02c0f02cef'
7
- data.tar.gz: 452e998d9c85ce6a7061cf002cd8cac7e77538fd71353fb3ea359f500060628a521dbc3f2e36b0dfdf43d3a4252d59566872914901e5e931ce55c19dca45231a
6
+ metadata.gz: 9840b6fa125bec3de173ce942e7daae8cac4de026d424d70a4bb82d67d9cf075d050c182c02fadef9d5c64a989599dd3a0cbc59197652d836b44f3452588bad2
7
+ data.tar.gz: 449669e3e6719f2c1ee22bd1b6b7bfaa34ca2821f7d6050563d76f43781c8e4efc8c1d6e986443007451550002abad649e6dc6bdf10629ba683be3ccf3f3f1ee
data/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # Changelog
2
2
 
3
+ ## Pending
4
+
5
+ ## [2.3.7]
6
+
7
+ * Obfuscate assignment to module.exports in order to prevent warnings in javascript bundlers, like Vite. Fixes [#337](https://github.com/railsware/js-routes/issues/337).
8
+
9
+ ## [2.3.6]
10
+
11
+ * Fixed serialization of empty `Array` and empty `Hash`. Fixes [#336](https://github.com/railsware/js-routes/issues/336).
12
+
13
+ ``` javascript
14
+ blog_path({filters: {}, columns: []}) // => /blog
15
+ ```
16
+
17
+ * Support new Rails 8.1 nil parameter serialization.
18
+ [Rails #53962](https://github.com/rails/rails/pull/53962)
19
+ JsRoutes consistently follows current rails version behavior:
20
+
21
+ ``` ruby
22
+ root_path(hello: nil)
23
+ # 8.0 => "/?hello="
24
+ # 8.1 => "/?hello"
25
+ ```
26
+
3
27
  ## [2.3.5]
4
28
 
5
29
  * Support `bigint` route parameter
data/Readme.md CHANGED
@@ -117,15 +117,13 @@ Create webpack ERB config `config/webpack/loaders/erb.js`:
117
117
 
118
118
  ``` javascript
119
119
  module.exports = {
120
- module: {
121
- rules: [
122
- {
123
- test: /\.erb$/,
124
- enforce: 'pre',
125
- loader: 'rails-erb-loader'
126
- },
127
- ]
128
- }
120
+ rules: [
121
+ {
122
+ test: /\.erb$/,
123
+ enforce: "pre",
124
+ loader: "rails-erb-loader",
125
+ },
126
+ ],
129
127
  };
130
128
  ```
131
129
 
@@ -43,7 +43,12 @@ module JsRoutes
43
43
  raise("Missing key #{key} in JS template")
44
44
  end
45
45
  end
46
- banner + content + routes_export + prevent_types_export
46
+ unless @configuration.module_type == "NIL"
47
+ banner + content + routes_export + prevent_types_export
48
+ else
49
+ content.sub('"use strict";', "")
50
+ end
51
+
47
52
  end
48
53
 
49
54
  sig { returns(String) }
@@ -94,6 +99,7 @@ module JsRoutes
94
99
  {
95
100
  'ROUTES_OBJECT' => routes_object,
96
101
  'DEPRECATED_FALSE_PARAMETER_BEHAVIOR' => Rails.version < '7.0.0',
102
+ 'DEPRECATED_NIL_QUERY_PARAMETER_BEHAVIOR' => Rails.version < '8.1.0',
97
103
  'DEFAULT_URL_OPTIONS' => json(@configuration.default_url_options),
98
104
  'PREFIX' => json(prefix),
99
105
  'SPECIAL_OPTIONS_KEY' => json(@configuration.special_options_key),
@@ -55,7 +55,7 @@ module JsRoutes
55
55
  if @configuration.dts?
56
56
  definition_body
57
57
  else
58
- # For tree-shaking ESM, add a #__PURE__ comment informing Webpack/minifiers that the call to `__jsr.r`
58
+ # For tree-shaking ESM, add a #__PURE__ comment informing js bundlers that the call to `__jsr.r`
59
59
  # has no side-effects (e.g. modifying global variables) and is safe to remove when unused.
60
60
  # https://webpack.js.org/guides/tree-shaking/#clarifying-tree-shaking-and-sidyeeffects
61
61
  pure_comment = @configuration.esm? ? '/*#__PURE__*/ ' : ''
@@ -1,4 +1,4 @@
1
1
  # typed: strict
2
2
  module JsRoutes
3
- VERSION = "2.3.5"
3
+ VERSION = "2.3.7"
4
4
  end
data/lib/routes.d.ts CHANGED
@@ -1,38 +1,38 @@
1
- declare type Optional<T> = {
1
+ type Optional<T> = {
2
2
  [P in keyof T]?: T[P] | null;
3
3
  };
4
- declare type Collection<T> = Record<string, T>;
5
- declare type BaseRouteParameter = string | boolean | Date | number | bigint;
6
- declare type MethodRouteParameter = BaseRouteParameter | (() => BaseRouteParameter);
7
- declare type ModelRouteParameter = {
4
+ type Collection<T> = Record<string, T>;
5
+ type BaseRouteParameter = string | boolean | Date | number | bigint;
6
+ type MethodRouteParameter = BaseRouteParameter | (() => BaseRouteParameter);
7
+ type ModelRouteParameter = {
8
8
  id: MethodRouteParameter;
9
9
  } | {
10
10
  to_param: MethodRouteParameter;
11
11
  } | {
12
12
  toParam: MethodRouteParameter;
13
13
  };
14
- declare type RequiredRouteParameter = BaseRouteParameter | ModelRouteParameter;
15
- declare type OptionalRouteParameter = undefined | null | RequiredRouteParameter;
16
- declare type QueryRouteParameter = OptionalRouteParameter | QueryRouteParameter[] | {
14
+ type RequiredRouteParameter = BaseRouteParameter | ModelRouteParameter;
15
+ type OptionalRouteParameter = undefined | null | RequiredRouteParameter;
16
+ type QueryRouteParameter = OptionalRouteParameter | QueryRouteParameter[] | {
17
17
  [k: string]: QueryRouteParameter;
18
18
  };
19
- declare type RouteParameters = Collection<QueryRouteParameter>;
20
- declare type Serializable = Collection<unknown>;
21
- declare type Serializer = (value: Serializable) => string;
22
- declare type RouteHelperExtras = {
19
+ type RouteParameters = Collection<QueryRouteParameter>;
20
+ type Serializable = Collection<unknown>;
21
+ type Serializer = (value: Serializable) => string;
22
+ type RouteHelperExtras = {
23
23
  requiredParams(): string[];
24
24
  toString(): string;
25
25
  };
26
- declare type RequiredParameters<T extends number> = T extends 1 ? [RequiredRouteParameter] : T extends 2 ? [RequiredRouteParameter, RequiredRouteParameter] : T extends 3 ? [RequiredRouteParameter, RequiredRouteParameter, RequiredRouteParameter] : T extends 4 ? [
26
+ type RequiredParameters<T extends number> = T extends 1 ? [RequiredRouteParameter] : T extends 2 ? [RequiredRouteParameter, RequiredRouteParameter] : T extends 3 ? [RequiredRouteParameter, RequiredRouteParameter, RequiredRouteParameter] : T extends 4 ? [
27
27
  RequiredRouteParameter,
28
28
  RequiredRouteParameter,
29
29
  RequiredRouteParameter,
30
30
  RequiredRouteParameter
31
31
  ] : RequiredRouteParameter[];
32
- declare type RouteHelperOptions = RouteOptions & Collection<OptionalRouteParameter>;
33
- declare type RouteHelper<T extends number = number> = ((...args: [...RequiredParameters<T>, RouteHelperOptions]) => string) & RouteHelperExtras;
34
- declare type RouteHelpers = Collection<RouteHelper>;
35
- declare type Configuration = {
32
+ type RouteHelperOptions = RouteOptions & Collection<OptionalRouteParameter>;
33
+ type RouteHelper<T extends number = number> = ((...args: [...RequiredParameters<T>, RouteHelperOptions]) => string) & RouteHelperExtras;
34
+ type RouteHelpers = Collection<RouteHelper>;
35
+ type Configuration = {
36
36
  prefix: string;
37
37
  default_url_options: RouteParameters;
38
38
  special_options_key: string;
@@ -43,7 +43,7 @@ interface RouterExposedMethods {
43
43
  configure(arg: Partial<Configuration>): Configuration;
44
44
  serialize: Serializer;
45
45
  }
46
- declare type KeywordUrlOptions = Optional<{
46
+ type KeywordUrlOptions = Optional<{
47
47
  host: string;
48
48
  protocol: string;
49
49
  subdomain: string;
@@ -53,15 +53,16 @@ declare type KeywordUrlOptions = Optional<{
53
53
  script_name: string;
54
54
  params: RouteParameters;
55
55
  }>;
56
- declare type RouteOptions = KeywordUrlOptions & RouteParameters;
57
- declare type PartsTable = Collection<{
56
+ type RouteOptions = KeywordUrlOptions & RouteParameters;
57
+ type PartsTable = Collection<{
58
58
  r?: boolean;
59
59
  d?: OptionalRouteParameter;
60
60
  }>;
61
- declare type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "DTS" | "NIL";
61
+ type ModuleType = "CJS" | "AMD" | "UMD" | "ESM" | "DTS" | "NIL";
62
62
  declare const RubyVariables: {
63
63
  PREFIX: string;
64
64
  DEPRECATED_FALSE_PARAMETER_BEHAVIOR: boolean;
65
+ DEPRECATED_NIL_QUERY_PARAMETER_BEHAVIOR: boolean;
65
66
  SPECIAL_OPTIONS_KEY: string;
66
67
  DEFAULT_URL_OPTIONS: RouteParameters;
67
68
  SERIALIZER: Serializer;
data/lib/routes.js CHANGED
@@ -1,7 +1,5 @@
1
- // eslint-disable-next-line
2
- RubyVariables.WRAPPER(
3
- // eslint-disable-next-line
4
- () => {
1
+ "use strict";
2
+ RubyVariables.WRAPPER(() => {
5
3
  const hasProp = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
6
4
  let NodeTypes;
7
5
  (function (NodeTypes) {
@@ -27,7 +25,12 @@ RubyVariables.WRAPPER(
27
25
  CJS: {
28
26
  define(routes) {
29
27
  if (module) {
30
- module.exports = routes;
28
+ // Some javascript processors (like vite/rolldown)
29
+ // warn on using module dot exports in an ESM module.
30
+ // This just obfuscates that assignment a little so
31
+ // users don't get a warning they can't fix.
32
+ const _mod = module;
33
+ _mod.exports = routes;
31
34
  }
32
35
  },
33
36
  isSupported() {
@@ -79,7 +82,7 @@ RubyVariables.WRAPPER(
79
82
  },
80
83
  NIL: {
81
84
  define() {
82
- // Defined using RubyVariables.WRAPPER
85
+ // Defined using RubyVariables . WRAPPER
83
86
  },
84
87
  isSupported() {
85
88
  return true;
@@ -122,9 +125,6 @@ RubyVariables.WRAPPER(
122
125
  };
123
126
  }
124
127
  default_serializer(value, prefix) {
125
- if (this.is_nullable(value)) {
126
- return "";
127
- }
128
128
  if (!prefix && !this.is_object(value)) {
129
129
  throw new Error("Url parameters should be a javascript hash");
130
130
  }
@@ -140,21 +140,21 @@ RubyVariables.WRAPPER(
140
140
  if (!hasProp(value, key))
141
141
  continue;
142
142
  let prop = value[key];
143
- if (this.is_nullable(prop) && prefix) {
144
- prop = "";
143
+ if (prefix) {
144
+ key = prefix + "[" + key + "]";
145
145
  }
146
- if (this.is_not_nullable(prop)) {
147
- if (prefix) {
148
- key = prefix + "[" + key + "]";
149
- }
150
- result.push(this.default_serializer(prop, key));
146
+ const subvalue = this.default_serializer(prop, key);
147
+ if (subvalue.length) {
148
+ result.push(subvalue);
151
149
  }
152
150
  }
153
151
  }
154
152
  else {
155
- if (this.is_not_nullable(value)) {
156
- result.push(encodeURIComponent(prefix) + "=" + encodeURIComponent("" + value));
157
- }
153
+ const key = encodeURIComponent(prefix);
154
+ result.push(this.is_not_nullable(value) ||
155
+ RubyVariables.DEPRECATED_NIL_QUERY_PARAMETER_BEHAVIOR
156
+ ? key + "=" + encodeURIComponent("" + (value !== null && value !== void 0 ? value : ""))
157
+ : key);
158
158
  }
159
159
  return result.join("&");
160
160
  }
@@ -164,8 +164,7 @@ RubyVariables.WRAPPER(
164
164
  extract_options(number_of_params, args) {
165
165
  const last_el = args[args.length - 1];
166
166
  if ((args.length > number_of_params && last_el === 0) ||
167
- (this.is_object(last_el) &&
168
- !this.looks_like_serialized_model(last_el))) {
167
+ (this.is_object(last_el) && !this.looks_like_serialized_model(last_el))) {
169
168
  if (this.is_object(last_el)) {
170
169
  delete last_el[this.configuration.special_options_key];
171
170
  }
@@ -186,8 +185,7 @@ RubyVariables.WRAPPER(
186
185
  path_identifier(object) {
187
186
  const result = this.unwrap_path_identifier(object);
188
187
  return this.is_nullable(result) ||
189
- (RubyVariables.DEPRECATED_FALSE_PARAMETER_BEHAVIOR &&
190
- result === false)
188
+ (RubyVariables.DEPRECATED_FALSE_PARAMETER_BEHAVIOR && result === false)
191
189
  ? ""
192
190
  : "" + result;
193
191
  }
@@ -211,7 +209,6 @@ RubyVariables.WRAPPER(
211
209
  return this.is_callable(result) ? result.call(object) : result;
212
210
  }
213
211
  partition_parameters(parts, required_params, default_options, call_arguments) {
214
- // eslint-disable-next-line prefer-const
215
212
  let { args, options } = this.extract_options(parts.length, call_arguments);
216
213
  if (args.length > parts.length) {
217
214
  throw new Error("Too many parameters provided for path");
@@ -516,21 +513,13 @@ RubyVariables.WRAPPER(
516
513
  const utils = new UtilsClass();
517
514
  // We want this helper name to be short
518
515
  const __jsr = {
519
- r(parts_table, route_spec, absolute) {
520
- return utils.route(parts_table, route_spec, absolute);
521
- },
516
+ r: utils.route.bind(utils),
522
517
  };
523
518
  return utils.define_module(RubyVariables.MODULE_TYPE, {
524
519
  ...__jsr,
525
- configure: (config) => {
526
- return utils.configure(config);
527
- },
528
- config: () => {
529
- return utils.config();
530
- },
531
- serialize: (object) => {
532
- return utils.serialize(object);
533
- },
520
+ configure: utils.configure.bind(utils),
521
+ config: utils.config.bind(utils),
522
+ serialize: utils.serialize.bind(utils),
534
523
  ...RubyVariables.ROUTES_OBJECT,
535
524
  });
536
525
  })();