@wcstack/fetch 3.5.4 → 4.0.0-rc.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.
package/dist/index.esm.js CHANGED
@@ -34,49 +34,37 @@ function getConfig() {
34
34
  }
35
35
  return frozenConfig;
36
36
  }
37
- // The defaults as shipped, for warnInvalid: a value 3.x lets through unchecked must not change
38
- // what a later call is compared against.
39
- const defaults = { ..._config, tagNames: { ..._config.tagNames } };
40
- const warned = new Set();
37
+ /** A misspelt option would otherwise do nothing: `bootstrapFetch` refuses it. */
38
+ function invalid(key) {
39
+ throw new Error(`[@wcstack/fetch] bootstrapFetch: "${key}" is not one of its options, or not of the option's type.`);
40
+ }
41
41
  /**
42
- * 3.5 forward-compat check. Warns, once per key per page, about an option that 4.0's bootstrapFetch
43
- * throws on: one this package does not have, a value of another type than its default (null, or an
44
- * array for an object, included), or a tag name it does not define or that is not a string. An
45
- * undefined value is left out, as in 4.0. It only warns: setConfig then goes on exactly as in 3.x.
46
- * Bootstrap-time only, never on a hot path.
42
+ * Applies the options given; an undefined value is left out. An option this package does not
43
+ * have, a value of another type than its default (null, or an array for an object, included) or
44
+ * a tag name it does not define throws, and then nothing is applied.
47
45
  */
48
- function warnInvalid(given, known, path = "") {
49
- for (const [key, value] of Object.entries(given)) {
50
- const name = path + key;
51
- const current = known[key];
52
- if (value === undefined) {
53
- continue;
54
- }
55
- if (!Object.prototype.hasOwnProperty.call(known, key) ||
56
- value === null ||
57
- typeof value !== typeof current ||
58
- Array.isArray(value) !== Array.isArray(current)) {
59
- if (!warned.has(name)) {
60
- warned.add(name);
61
- console.warn(`[@wcstack/fetch] bootstrapFetch: "${name}" is not one of its options, or not of the option's type. 3.x ignores it or applies it unchecked; 4.0 throws on it.`);
62
- }
63
- }
64
- else if (name === "tagNames") {
65
- warnInvalid(value, current, "tagNames.");
66
- }
67
- }
68
- }
69
46
  function setConfig(partialConfig) {
70
- warnInvalid(partialConfig, defaults);
71
- if (typeof partialConfig.autoTrigger === "boolean") {
72
- _config.autoTrigger = partialConfig.autoTrigger;
47
+ const options = _config;
48
+ const tags = _config.tagNames;
49
+ const given = Object.entries(partialConfig).filter(([, value]) => value !== undefined);
50
+ const givenTags = Object.entries(partialConfig.tagNames ?? {}).filter(([, tag]) => tag !== undefined);
51
+ for (const [key, value] of given) {
52
+ const current = options[key];
53
+ if (!Object.hasOwn(options, key) || value === null || typeof value !== typeof current || Array.isArray(value) !== Array.isArray(current)) {
54
+ invalid(key);
55
+ }
73
56
  }
74
- if (typeof partialConfig.triggerAttribute === "string") {
75
- _config.triggerAttribute = partialConfig.triggerAttribute;
57
+ for (const [name, tag] of givenTags) {
58
+ if (!Object.hasOwn(tags, name) || typeof tag !== "string")
59
+ invalid(`tagNames.${name}`);
76
60
  }
77
- if (partialConfig.tagNames) {
78
- Object.assign(_config.tagNames, partialConfig.tagNames);
61
+ for (const [key, value] of given) {
62
+ if (key !== "tagNames") {
63
+ options[key] = value;
64
+ }
79
65
  }
66
+ for (const [name, tag] of givenTags)
67
+ tags[name] = tag;
80
68
  frozenConfig = null;
81
69
  }
82
70