@nivaro/react 0.3.146 → 0.3.148

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.
@@ -1,6 +1,6 @@
1
1
  import { fieldsFromBody as q, hintFor as F } from "./error-hints.js";
2
- import { isCodeError as L, isRawErrorText as I, plainErrorText as N, STATUS_TITLES as p, errorStatusOf as U, isBareStatusText as D, SOMETHING_WENT_WRONG as x, statusTitle as P } from "./safe-error.js";
3
- const _ = "x-nivaro-request-id", H = /^[A-Z][A-Z0-9_]{2,}$/, W = /^(ERR_|E[A-Z]+$)/, B = /failed to fetch|load failed|networkerror|network request failed|network error|cannot reach/i, S = "Try again. If it keeps happening, ask for help.", u = {
2
+ import { isCodeError as L, isRawErrorText as I, plainErrorText as N, STATUS_TITLES as p, errorStatusOf as P, isBareStatusText as D, SOMETHING_WENT_WRONG as U, statusTitle as x } from "./safe-error.js";
3
+ const _ = "x-nivaro-request-id", H = /^[A-Z][A-Z0-9_]{2,}$/, W = /^(ERR_|E[A-Z]+$)/, B = /failed to fetch|load failed|networkerror|network request failed|network error|cannot reach/i, O = "Try again. If it keeps happening, ask for help.", l = {
4
4
  retry: { kind: "retry", label: "Try again" },
5
5
  signIn: { kind: "sign-in", label: "Sign in again" },
6
6
  reload: { kind: "reload", label: "Reload" },
@@ -13,7 +13,7 @@ function j(t) {
13
13
  const e = t.response;
14
14
  return d(e) ? d(e.data) ? e.data : "data" in e && typeof e.data == "string" ? null : e : d(t.extensions) || !(t instanceof Error) && ("error" in t || "message" in t || "code" in t) ? t : null;
15
15
  }
16
- function k(t) {
16
+ function E(t) {
17
17
  return typeof t == "string" && t.trim() ? t.trim() : void 0;
18
18
  }
19
19
  function K(t, e) {
@@ -21,25 +21,25 @@ function K(t, e) {
21
21
  for (const i of [e == null ? void 0 : e.code, n == null ? void 0 : n.code, d(t) ? t.code : void 0])
22
22
  if (typeof i == "string" && H.test(i) && !W.test(i)) return i;
23
23
  }
24
- function Y(t, e) {
25
- const n = k(e == null ? void 0 : e.request_id) ?? k(e == null ? void 0 : e.requestId);
24
+ function M(t, e) {
25
+ const n = E(e == null ? void 0 : e.request_id) ?? E(e == null ? void 0 : e.requestId);
26
26
  if (n) return n;
27
27
  if (!d(t)) return;
28
- const i = k(t.requestId) ?? k(t.request_id);
28
+ const i = E(t.requestId) ?? E(t.request_id);
29
29
  if (i) return i;
30
30
  const r = d(t.response) ? t.response.headers : void 0;
31
31
  if (d(r)) {
32
32
  const m = r.get;
33
- return k(typeof m == "function" ? m.call(r, _) : r[_]);
33
+ return E(typeof m == "function" ? m.call(r, _) : r[_]);
34
34
  }
35
35
  }
36
- function G(t, e) {
37
- const n = U(t) ?? (e == null ? void 0 : e.statusCode) ?? (e == null ? void 0 : e.status), i = d(e == null ? void 0 : e.extensions) ? e.extensions.status : void 0, r = typeof n == "number" ? n : typeof i == "number" ? i : void 0;
36
+ function Y(t, e) {
37
+ const n = P(t) ?? (e == null ? void 0 : e.statusCode) ?? (e == null ? void 0 : e.status), i = d(e == null ? void 0 : e.extensions) ? e.extensions.status : void 0, r = typeof n == "number" ? n : typeof i == "number" ? i : void 0;
38
38
  return r && r >= 100 && r < 600 ? r : void 0;
39
39
  }
40
- function M(t) {
40
+ function G(t) {
41
41
  for (const e of ["error", "message"]) {
42
- const n = k(t[e]);
42
+ const n = E(t[e]);
43
43
  if (n && !D(n)) return n;
44
44
  }
45
45
  }
@@ -65,134 +65,141 @@ function J(t, e) {
65
65
  function X(t, e = 500) {
66
66
  return t.length > e ? `${t.slice(0, e - 1)}…` : t;
67
67
  }
68
- function O(t, e = "Something went wrong", n = {}) {
69
- const i = j(t), r = d(t) && typeof t.message == "string" ? t.message : void 0, m = d(t) && t.status === 0, o = G(t, i), h = K(t, i), R = Y(t, i), E = { title: e };
70
- o && (E.status = o), h && (E.code = h), R && (E.requestId = R);
68
+ function S(t, e = "Something went wrong", n = {}) {
69
+ const i = j(t), r = d(t) && typeof t.message == "string" ? t.message : void 0, m = d(t) && t.status === 0, o = Y(t, i), g = K(t, i), R = M(t, i), k = { title: e };
70
+ o && (k.status = o), g && (k.code = g), R && (k.requestId = R);
71
71
  const T = (s) => {
72
72
  const w = [
73
- i ? k(i.message) : void 0,
74
- i ? k(i.error) : void 0,
73
+ i ? E(i.message) : void 0,
74
+ i ? E(i.error) : void 0,
75
75
  r,
76
76
  typeof t == "string" ? t : ""
77
- ].filter((g) => !!(g != null && g.trim()) && !D(g)).find((g) => I(g) || !!o && o >= 500 && g !== s.title);
77
+ ].filter((h) => !!(h != null && h.trim()) && !D(h)).find((h) => I(h) || !!o && o >= 500 && h !== s.title);
78
78
  return w && w !== s.title && (s.technical = X(w.trim())), I(s.title) && (s.title = N(s.title, e, o)), s.hint && (I(s.hint) || s.title.includes(s.hint)) && delete s.hint, s;
79
79
  };
80
80
  if (L(t))
81
81
  return T({
82
- ...E,
82
+ ...k,
83
83
  kind: "bug",
84
- title: x,
84
+ title: U,
85
85
  hint: "Try again. If it keeps happening, reload the page.",
86
- action: u.reloadPage
86
+ action: l.reloadPage
87
87
  });
88
- const $ = r ?? (typeof t == "string" ? t : "");
89
- if (!o && !h) {
88
+ const A = r ?? (typeof t == "string" ? t : "");
89
+ if (!o && !g) {
90
90
  if (Z(t))
91
91
  return T({
92
- ...E,
92
+ ...k,
93
93
  kind: "timeout",
94
94
  title: "The server took too long to answer",
95
95
  hint: "Try again in a minute. If it keeps happening, ask for less at once.",
96
- action: u.retry
96
+ action: l.retry
97
97
  });
98
98
  if (Q() && (m || !i))
99
99
  return T({
100
- ...E,
100
+ ...k,
101
101
  kind: "offline",
102
102
  title: "You’re offline",
103
103
  hint: "Check your internet connection, then try again. Nothing you entered is lost.",
104
- action: u.retry
104
+ action: l.retry
105
105
  });
106
- if (m || !i && B.test($))
106
+ if (m || !i && B.test(A))
107
107
  return T({
108
- ...E,
108
+ ...k,
109
109
  kind: "network",
110
110
  title: "Cannot reach the server",
111
111
  hint: "Check your connection, then try again.",
112
- action: u.retry
112
+ action: l.retry
113
113
  });
114
114
  }
115
- const A = (i ? M(i) : void 0) ?? (r && !i ? r : void 0), l = A && !I(A) && !V(o, i, h) ? A.trim() : void 0, a = k(i == null ? void 0 : i.hint) ?? k(i == null ? void 0 : i.explanation), c = o || h ? F(h, o ?? 400) : void 0, v = i ? q(i) : [];
116
- v.length && (E.fields = v);
117
- const f = (s, w, g, C) => T({
118
- ...E,
115
+ const $ = (i ? G(i) : void 0) ?? (r && !i ? r : void 0), u = $ && !I($) && !V(o, i, g) ? $.trim() : void 0, a = E(i == null ? void 0 : i.hint) ?? E(i == null ? void 0 : i.explanation), c = o || g ? F(g, o ?? 400) : void 0, v = i ? q(i) : [];
116
+ v.length && (k.fields = v);
117
+ const f = (s, w, h, C) => T({
118
+ ...k,
119
119
  kind: w,
120
120
  title: s,
121
- ...g ? { hint: g } : {},
121
+ ...h ? { hint: h } : {},
122
122
  ...C ? { action: C } : {}
123
123
  });
124
- if (h === "MAINTENANCE")
124
+ if (g === "MAINTENANCE")
125
125
  return f(
126
- l ?? "The system is down for maintenance",
126
+ u ?? "The system is down for maintenance",
127
127
  "maintenance",
128
128
  a ?? c,
129
- u.reload
129
+ l.reload
130
+ );
131
+ if (g === "APP_MOVED")
132
+ return f(
133
+ u ?? "This app has moved to another server",
134
+ "unavailable",
135
+ a ?? c,
136
+ l.reloadPage
130
137
  );
131
138
  if (o === 401)
132
139
  return f(
133
- l ?? p[401],
140
+ u ?? p[401],
134
141
  "signed-out",
135
142
  a ?? c ?? "Sign in again, then try once more.",
136
- u.signIn
143
+ l.signIn
137
144
  );
138
145
  if (o === 403)
139
146
  return f(
140
- l ?? p[403],
147
+ u ?? p[403],
141
148
  "forbidden",
142
149
  a ?? c,
143
- h && !/ask/i.test(a ?? c ?? "") ? void 0 : u.askAccess
150
+ g && !/ask/i.test(a ?? c ?? "") ? void 0 : l.askAccess
144
151
  );
145
152
  if (o === 404 || o === 410)
146
- return f(l ?? p[404], "not-found", a ?? c, u.back);
153
+ return f(u ?? p[404], "not-found", a ?? c, l.back);
147
154
  if (o === 409 || o === 412) {
148
- const s = a ?? (h ? c : "Reload to see their changes, then make yours again.");
155
+ const s = a ?? (g ? c : "Reload to see their changes, then make yours again.");
149
156
  return f(
150
- l ?? p[409],
157
+ u ?? p[409],
151
158
  "conflict",
152
159
  s,
153
- !s || /\breload\b/i.test(s) ? u.reload : void 0
160
+ !s || /\breload\b/i.test(s) ? l.reload : void 0
154
161
  );
155
162
  }
156
163
  if (o === 423)
157
- return f(l ?? p[423], "locked", a ?? c, u.reload);
158
- if (o === 413) return f(l ?? p[413], "too-large", a ?? c);
164
+ return f(u ?? p[423], "locked", a ?? c, l.reload);
165
+ if (o === 413) return f(u ?? p[413], "too-large", a ?? c);
159
166
  if (o === 429)
160
- return f(l ?? p[429], "rate-limited", a ?? c, u.retry);
167
+ return f(u ?? p[429], "rate-limited", a ?? c, l.retry);
161
168
  if (o === 400 || o === 422) {
162
- const s = v.length ? J(v, n.fieldLabels) : "", w = a ?? (s && (!h || /highlighted/i.test(c ?? "")) ? `Check ${s}, then try again.` : c), g = v.length > 0 || /^(VALIDATION|REQUIRED)/.test(h ?? "");
169
+ const s = v.length ? J(v, n.fieldLabels) : "", w = a ?? (s && (!g || /highlighted/i.test(c ?? "")) ? `Check ${s}, then try again.` : c), h = v.length > 0 || /^(VALIDATION|REQUIRED)/.test(g ?? "");
163
170
  return f(
164
- l ?? (g ? p[422] : p[400]),
165
- g ? "validation" : "refused",
171
+ u ?? (h ? p[422] : p[400]),
172
+ h ? "validation" : "refused",
166
173
  w
167
174
  );
168
175
  }
169
176
  if (o === 502 || o === 503 || o === 504)
170
177
  return f(
171
- l ?? p[o],
178
+ u ?? p[o],
172
179
  o === 504 ? "timeout" : "unavailable",
173
180
  a ?? c,
174
- u.retry
181
+ l.retry
175
182
  );
176
183
  if (o && o >= 500)
177
184
  return f(
178
- l ?? p[500],
185
+ u ?? p[500],
179
186
  "server",
180
- a && !/copy the details/i.test(a) ? a : S,
181
- u.retry
187
+ a && !/copy the details/i.test(a) ? a : O,
188
+ l.retry
182
189
  );
183
190
  if (o && o >= 400)
184
- return f(l ?? P(o) ?? e, "refused", a ?? c);
185
- if (l) return f(l, h ? "refused" : "unknown", a ?? c);
186
- const y = $.trim() ? N($, e) : e;
187
- return f(y, "unknown", a ?? c ?? S, u.retry);
191
+ return f(u ?? x(o) ?? e, "refused", a ?? c);
192
+ if (u) return f(u, g ? "refused" : "unknown", a ?? c);
193
+ const y = A.trim() ? N(A, e) : e;
194
+ return f(y, "unknown", a ?? c ?? O, l.retry);
188
195
  }
189
196
  function rt(t, e = "this", n = {}) {
190
- const i = `Couldn’t load ${e}`, r = O(t, i, n);
191
- return (r.kind === "bug" || r.kind === "server" && r.title === p[500] || r.title === i) && (r.kind === "server" || r.kind === "bug" || r.kind === "unknown") && (r.title = i, r.hint = r.kind === "bug" ? r.hint : S, r.kind !== "bug" && (r.action = u.retry)), r;
197
+ const i = `Couldn’t load ${e}`, r = S(t, i, n);
198
+ return (r.kind === "bug" || r.kind === "server" && r.title === p[500] || r.title === i) && (r.kind === "server" || r.kind === "bug" || r.kind === "unknown") && (r.title = i, r.hint = r.kind === "bug" ? r.hint : O, r.kind !== "bug" && (r.action = l.retry)), r;
192
199
  }
193
200
  function b(t, e = {}) {
194
201
  var m;
195
- const n = et(t) ? t : O(t), i = e.page ?? (typeof window < "u" && window.location ? window.location.pathname : void 0);
202
+ const n = et(t) ? t : S(t), i = e.page ?? (typeof window < "u" && window.location ? window.location.pathname : void 0);
196
203
  return [
197
204
  e.action ? `Action: ${e.action}` : null,
198
205
  `What happened: ${n.title}`,
@@ -241,13 +248,13 @@ function st(t) {
241
248
  return r ? `${r[1].replace(/_/g, " ")} is empty — fill it in the file or map a column to it` : n.replace(/,? table '[^']*'/gi, "").replace(/\s*(INSERT|UPDATE) fails\./gi, "").trim();
242
249
  }
243
250
  function at(t, e) {
244
- const n = O(t, e);
251
+ const n = S(t, e);
245
252
  return n.hint ? `${n.title.replace(/[.!?]\s*$/, "")}. ${n.hint}` : n.title;
246
253
  }
247
254
  export {
248
- S as ASK_FOR_HELP_HINT,
255
+ O as ASK_FOR_HELP_HINT,
249
256
  ot as copyErrorDetails,
250
- O as describeError,
257
+ S as describeError,
251
258
  rt as describeLoadError,
252
259
  b as errorDetailsText,
253
260
  J as errorFieldList,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nivaro/react",
3
- "version": "0.3.146",
3
+ "version": "0.3.148",
4
4
  "description": "React hooks and components for Nivaro CMS forms",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",