@choosemycompany/lib 0.10.0 → 0.10.1

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.js CHANGED
@@ -16,45 +16,45 @@ function O(e) {
16
16
  "@choosemycompany/lib: useHttp() was called before configureHttp(). Declare the base URLs from an app plugin."
17
17
  );
18
18
  if (e === void 0) return k.baseUrl;
19
- const a = k.targets ?? {}, i = Object.hasOwn(a, e) ? a[e] : void 0;
20
- if (typeof i != "string" || !i) {
21
- const t = Object.keys(a), n = t.length > 0 ? t.join(", ") : "none";
19
+ const a = k.targets ?? {}, t = Object.hasOwn(a, e) ? a[e] : void 0;
20
+ if (typeof t != "string" || !t) {
21
+ const i = Object.keys(a), n = i.length > 0 ? i.join(", ") : "none";
22
22
  throw new Error(
23
23
  `@choosemycompany/lib: no HTTP target named "${e}". Declared targets: ${n}.`
24
24
  );
25
25
  }
26
- return i;
26
+ return t;
27
27
  }
28
28
  const W = "/auth/api/v2/session/tokens", Q = "/auth/api/v2/", Y = "cmc-auth-session-refresh", X = V("cmc-auth-session", () => {
29
- const e = A(!1), a = A(!1), i = A(void 0);
30
- let t, n = 0;
29
+ const e = A(!1), a = A(!1), t = A(void 0);
30
+ let i, n = 0;
31
31
  const o = T(() => e.value && !a.value);
32
32
  function u() {
33
- e.value = !0, a.value = !1, i.value = void 0;
33
+ e.value = !0, a.value = !1, t.value = void 0;
34
34
  }
35
35
  function f() {
36
- n += 1, e.value = !1, a.value = !1, i.value = void 0;
36
+ n += 1, e.value = !1, a.value = !1, t.value = void 0;
37
37
  }
38
38
  function g() {
39
- n += 1, e.value = !1, a.value = !0, i.value = "logout";
39
+ n += 1, e.value = !1, a.value = !0, t.value = "logout";
40
40
  }
41
- function l(v) {
42
- n += 1, e.value = !1, a.value = !0, i.value = v;
41
+ function l(B) {
42
+ n += 1, e.value = !1, a.value = !0, t.value = B;
43
43
  }
44
44
  async function b() {
45
45
  if (a.value)
46
46
  throw new Error("Session is expired: no refresh will be attempted");
47
- return t ??= m(h).finally(() => {
48
- t = void 0;
49
- }), t;
47
+ return i ??= m(h).finally(() => {
48
+ i = void 0;
49
+ }), i;
50
50
  }
51
- async function m(v) {
51
+ async function m(B) {
52
52
  const p = globalThis.navigator?.locks;
53
- if (!p) return v();
54
- await p.request(Y, v);
53
+ if (!p) return B();
54
+ await p.request(Y, B);
55
55
  }
56
56
  async function h() {
57
- const v = n, p = () => v !== n, P = new URL(W, O()).href;
57
+ const B = n, p = () => B !== n, P = new URL(W, O()).href;
58
58
  let M;
59
59
  try {
60
60
  M = await fetch(P, {
@@ -71,12 +71,12 @@ const W = "/auth/api/v2/session/tokens", Q = "/auth/api/v2/", Y = "cmc-auth-sess
71
71
  u();
72
72
  }
73
73
  function C() {
74
- i.value = "refresh-unreachable";
74
+ t.value = "refresh-unreachable";
75
75
  }
76
76
  return {
77
77
  isOpen: e,
78
78
  isExpired: a,
79
- expiryReason: i,
79
+ expiryReason: t,
80
80
  isAuthenticated: o,
81
81
  open: u,
82
82
  expire: l,
@@ -94,19 +94,19 @@ function ae(e) {
94
94
  function ne() {
95
95
  return async (e, a) => {
96
96
  if (ee()) return fetch(e, a);
97
- const i = await fetch(e, a);
98
- if (i.status !== 401 || ae(e) || new Headers(a?.headers).has("Authorization")) return i;
99
- let t;
97
+ const t = await fetch(e, a);
98
+ if (t.status !== 401 || ae(e) || new Headers(a?.headers).has("Authorization")) return t;
99
+ let i;
100
100
  try {
101
- t = X();
101
+ i = X();
102
102
  } catch {
103
- return i;
103
+ return t;
104
104
  }
105
- if (t.isExpired) return i;
105
+ if (i.isExpired) return t;
106
106
  try {
107
- await t.refresh();
107
+ await i.refresh();
108
108
  } catch (n) {
109
- return t.isExpired || console.warn("[cmc-lib] token refresh failed, returning the original 401", n), i;
109
+ return i.isExpired || console.warn("[cmc-lib] token refresh failed, returning the original 401", n), t;
110
110
  }
111
111
  return fetch(e, a);
112
112
  };
@@ -115,32 +115,32 @@ function be(e) {
115
115
  const a = q(O(e), {
116
116
  headers: {}
117
117
  }, ne());
118
- function i(n) {
118
+ function t(n) {
119
119
  a.options.headers = {
120
120
  ...a.options.headers ?? {},
121
121
  Authorization: `Bearer ${n}`
122
122
  };
123
123
  }
124
- function t() {
124
+ function i() {
125
125
  a.options.headers && delete a.options.headers.Authorization;
126
126
  }
127
- return { http: a, setToken: i, clearToken: t };
127
+ return { http: a, setToken: t, clearToken: i };
128
128
  }
129
- function ve(e, a) {
130
- return e.replace(ie, (i, t) => {
131
- const n = Object.hasOwn(a, t) ? a[t] : void 0;
129
+ function Be(e, a) {
130
+ return e.replace(ie, (t, i) => {
131
+ const n = Object.hasOwn(a, i) ? a[i] : void 0;
132
132
  if (typeof n != "string" && typeof n != "number")
133
- throw new Error(`Missing URL parameter "${t}" for template "${e}"`);
133
+ throw new Error(`Missing URL parameter "${i}" for template "${e}"`);
134
134
  return encodeURIComponent(String(n));
135
135
  });
136
136
  }
137
137
  const ie = /\{(\w+)\}/g;
138
138
  function N(e) {
139
- return Object.entries(e).sort(([a], [i]) => a.localeCompare(i)).map(([a, i]) => `${a}=${i}`).join("|");
139
+ return Object.entries(e).sort(([a], [t]) => a.localeCompare(t)).map(([a, t]) => `${a}=${t}`).join("|");
140
140
  }
141
141
  const te = "create";
142
- function Be(e) {
143
- const a = A({}), i = A({}), t = A({}), n = A({}), o = A(0);
142
+ function ve(e) {
143
+ const a = A({}), t = A({}), i = A({}), n = A({}), o = A(0);
144
144
  let u = 0;
145
145
  const f = e.getItemId ?? ((r) => {
146
146
  const d = r.id;
@@ -157,9 +157,9 @@ function Be(e) {
157
157
  n.value[r] = d;
158
158
  try {
159
159
  const s = await d;
160
- return delete t.value[r], s;
160
+ return delete i.value[r], s;
161
161
  } catch (s) {
162
- throw t.value[r] = s, s;
162
+ throw i.value[r] = s, s;
163
163
  } finally {
164
164
  n.value[r] === d && delete n.value[r];
165
165
  }
@@ -173,16 +173,16 @@ function Be(e) {
173
173
  const c = n.value[r];
174
174
  if (c)
175
175
  return c;
176
- const B = e.fetchOne, S = (async () => {
176
+ const v = e.fetchOne, S = (async () => {
177
177
  const I = u;
178
178
  try {
179
- const E = await B(r);
179
+ const E = await v(r);
180
180
  if (I !== u)
181
181
  return;
182
182
  const R = e.enrichItem ? e.enrichItem(E) : E;
183
- return a.value[r] = R, delete t.value[r], a.value[r];
183
+ return a.value[r] = R, delete i.value[r], a.value[r];
184
184
  } catch (E) {
185
- I === u && (t.value[r] = E);
185
+ I === u && (i.value[r] = E);
186
186
  return;
187
187
  } finally {
188
188
  I === u && delete n.value[r];
@@ -191,7 +191,7 @@ function Be(e) {
191
191
  return n.value[r] = S, S;
192
192
  }
193
193
  function m(r) {
194
- const d = N(r), s = i.value[d];
194
+ const d = N(r), s = t.value[d];
195
195
  if (s)
196
196
  return {
197
197
  items: s.itemIds.map((c) => a.value[c]).filter((c) => c !== void 0),
@@ -205,11 +205,11 @@ function Be(e) {
205
205
  if (!e.fetchPage)
206
206
  throw new Error("useStore.list called but fetchPage is not defined in store options");
207
207
  const s = N(r), { force: c = !1 } = d;
208
- if (i.value[s]?.revision === o.value && !c)
208
+ if (t.value[s]?.revision === o.value && !c)
209
209
  return m(r);
210
- const B = n.value[s];
211
- if (B)
212
- return await B, m(r);
210
+ const v = n.value[s];
211
+ if (v)
212
+ return await v, m(r);
213
213
  const S = e.fetchPage, I = (async () => {
214
214
  const E = o.value, R = u;
215
215
  try {
@@ -221,16 +221,16 @@ function Be(e) {
221
221
  const z = e.enrichItem ? e.enrichItem(G) : G, D = f(z);
222
222
  a.value[D] = z, L.push(D);
223
223
  }
224
- i.value[s] = {
224
+ t.value[s] = {
225
225
  itemIds: L,
226
226
  total: w.total,
227
227
  hasMore: w.hasMore,
228
228
  page: w.page,
229
229
  limit: w.limit,
230
230
  revision: E
231
- }, delete t.value[s];
231
+ }, delete i.value[s];
232
232
  } catch (w) {
233
- R === u && (t.value[s] = w);
233
+ R === u && (i.value[s] = w);
234
234
  } finally {
235
235
  R === u && delete n.value[s];
236
236
  }
@@ -240,14 +240,14 @@ function Be(e) {
240
240
  async function C(r, d = te) {
241
241
  if (!e.createMutation)
242
242
  throw new Error("useStore.create called but createMutation is not defined in store options");
243
- const s = e.createMutation, c = await l(d, (async () => s(r))()), B = e.enrichItem ? e.enrichItem(c) : c, S = f(B);
244
- return a.value[S] = B, i.value = {}, p(), B;
243
+ const s = e.createMutation, c = await l(d, (async () => s(r))()), v = e.enrichItem ? e.enrichItem(c) : c, S = f(v);
244
+ return a.value[S] = v, p(), v;
245
245
  }
246
- async function v(r, d) {
246
+ async function B(r, d) {
247
247
  if (!e.updateMutation)
248
248
  throw new Error("useStore.update called but updateMutation is not defined in store options");
249
- const s = e.updateMutation, c = await l(r, (async () => s(r, d))()), B = e.enrichItem ? e.enrichItem(c) : c;
250
- return a.value[r] = B, p(), B;
249
+ const s = e.updateMutation, c = await l(r, (async () => s(r, d))()), v = e.enrichItem ? e.enrichItem(c) : c;
250
+ return a.value[r] = v, p(), v;
251
251
  }
252
252
  function p() {
253
253
  o.value += 1;
@@ -257,43 +257,43 @@ function Be(e) {
257
257
  throw new Error("useStore.remove called but removeMutation is not defined in store options");
258
258
  const d = e.removeMutation;
259
259
  await l(r, (async () => d(r))()), delete a.value[r];
260
- for (const s of Object.values(i.value)) {
260
+ for (const s of Object.values(t.value)) {
261
261
  const c = s.itemIds.indexOf(r);
262
262
  c !== -1 && (s.itemIds.splice(c, 1), s.total = Math.max(0, s.total - 1));
263
263
  }
264
264
  }
265
265
  function M() {
266
- t.value = {};
266
+ i.value = {};
267
267
  }
268
268
  function U() {
269
- u += 1, a.value = {}, i.value = {}, t.value = {}, n.value = {};
269
+ u += 1, a.value = {}, t.value = {}, i.value = {}, n.value = {};
270
270
  }
271
- return { items: a, views: i, errors: t, isPending: g, list: h, reset: U, retrieve: b, getView: m, withPending: l, create: C, update: v, markViewsStale: p, remove: P, clearErrors: M };
271
+ return { items: a, views: t, errors: i, isPending: g, list: h, reset: U, retrieve: b, getView: m, withPending: l, create: C, update: B, markViewsStale: p, remove: P, clearErrors: M };
272
272
  }
273
273
  function Ae(e, a) {
274
- const i = (t) => {
275
- const n = e.value, o = t.target;
274
+ const t = (i) => {
275
+ const n = e.value, o = i.target;
276
276
  !n || !o || n.contains(o) || a();
277
277
  };
278
278
  F(() => {
279
- document.addEventListener("click", i);
279
+ document.addEventListener("click", t);
280
280
  }), x(() => {
281
- document.removeEventListener("click", i);
281
+ document.removeEventListener("click", t);
282
282
  });
283
283
  }
284
284
  function Ce() {
285
285
  const { locale: e } = y();
286
- function a(i, t = null) {
287
- const n = re[i.trim().toUpperCase()];
286
+ function a(t, i = null) {
287
+ const n = re[t.trim().toUpperCase()];
288
288
  if (!n)
289
- throw new Error(`Unknown country code: ${i}`);
290
- if (t && n[t])
291
- return n[t];
289
+ throw new Error(`Unknown country code: ${t}`);
290
+ if (i && n[i])
291
+ return n[i];
292
292
  if (n[e.value])
293
293
  return n[e.value];
294
294
  if (e.value !== "en" && n.en)
295
295
  return n.en;
296
- throw new Error(`Unknown country code: ${i}`);
296
+ throw new Error(`Unknown country code: ${t}`);
297
297
  }
298
298
  return a;
299
299
  }
@@ -435,12 +435,12 @@ const re = {
435
435
  ZA: { en: "South Africa", fr: "Afrique Du Sud", es: "Sudáfrica", de: "Südafrika", it: "Sudafrica", pt: "África Do Sul", da: "Sydafrika" },
436
436
  ZM: { en: "Zambia", fr: "Zambie", es: "Zambia", de: "Sambia", it: "Zambia", pt: "Zâmbia", da: "Zambia" },
437
437
  ZW: { en: "Zimbabwe", fr: "Zimbabwe", es: "Zimbabue", de: "Simbabwe", it: "Zimbabwe", pt: "Zimbábue", da: "Zimbabwe" }
438
- }, we = ({ code: e, message: a, data: i }) => {
439
- const t = a || "An unexpected error occurred", n = J({
438
+ }, we = ({ code: e, message: a, data: t }) => {
439
+ const i = a || "An unexpected error occurred", n = J({
440
440
  statusCode: e,
441
- statusMessage: t,
442
- message: t,
443
- data: i
441
+ statusMessage: i,
442
+ message: i,
443
+ data: t
444
444
  });
445
445
  throw j(n), n;
446
446
  }, oe = "ilcJCL_lmpwcEJfSkKND";
@@ -449,9 +449,9 @@ function Me() {
449
449
  return { reportConversion: () => {
450
450
  if (typeof window > "u" || typeof window.gtag > "u" || !e)
451
451
  return;
452
- const i = `${e}/${oe}`;
452
+ const t = `${e}/${oe}`;
453
453
  window.gtag("event", "conversion", {
454
- send_to: i
454
+ send_to: t
455
455
  });
456
456
  } };
457
457
  }
@@ -461,9 +461,9 @@ function Se() {
461
461
  return { contactUrl: T(() => e.value === "fr" ? `${H}/contact` : `${H}/${e.value}/contact`) };
462
462
  }
463
463
  function Ie() {
464
- async function e(t) {
464
+ async function e(i) {
465
465
  await Promise.all(
466
- t.map(
466
+ i.map(
467
467
  (n) => new Promise((o) => {
468
468
  if (!n) return o(null);
469
469
  const u = new Image();
@@ -472,13 +472,13 @@ function Ie() {
472
472
  )
473
473
  );
474
474
  }
475
- function a(t, n) {
476
- return n.split(".").reduce((o, u) => o && typeof o == "object" && u in o ? o[u] : "", t) ?? "";
475
+ function a(i, n) {
476
+ return n.split(".").reduce((o, u) => o && typeof o == "object" && u in o ? o[u] : "", i) ?? "";
477
477
  }
478
- function i(t, n, o) {
478
+ function t(i, n, o) {
479
479
  if (import.meta.server)
480
480
  return async (f) => {
481
- t.value = !0;
481
+ i.value = !0;
482
482
  };
483
483
  const u = $(
484
484
  `${n}LoadedImageCache`,
@@ -486,68 +486,68 @@ function Ie() {
486
486
  );
487
487
  return async (f) => {
488
488
  if (!f) {
489
- t.value = !1;
489
+ i.value = !1;
490
490
  return;
491
491
  }
492
492
  const g = o.map((m) => a(f, m)), l = `${n}|${g.join("|")}`;
493
493
  if (u.value[l]) {
494
- t.value = !0;
494
+ i.value = !0;
495
495
  return;
496
496
  }
497
- t.value = !1;
497
+ i.value = !1;
498
498
  const b = g.filter(Boolean).map(String);
499
- await e(b), u.value[l] = !0, t.value = !0;
499
+ await e(b), u.value[l] = !0, i.value = !0;
500
500
  };
501
501
  }
502
- return i;
502
+ return t;
503
503
  }
504
504
  const se = {
505
505
  en: ["US", "UK"]
506
506
  }, ue = "FR";
507
507
  function de(e) {
508
- const a = e.trim().replace("_", "-"), [i, t] = a.split("-");
508
+ const a = e.trim().replace("_", "-"), [t, i] = a.split("-");
509
509
  return {
510
- language: i?.toLowerCase() ?? "",
511
- region: t?.toUpperCase() ?? ""
510
+ language: t?.toLowerCase() ?? "",
511
+ region: i?.toUpperCase() ?? ""
512
512
  };
513
513
  }
514
514
  function K(e) {
515
515
  return e.toString().trim().toUpperCase();
516
516
  }
517
517
  function Ee() {
518
- function e(a, i) {
519
- if (!i.length) return;
520
- const t = i.map(K), n = new Set(t), { language: o, region: u } = de(a), f = [];
518
+ function e(a, t) {
519
+ if (!t.length) return;
520
+ const i = t.map(K), n = new Set(i), { language: o, region: u } = de(a), f = [];
521
521
  u && f.push(u);
522
522
  const g = se[o];
523
523
  g?.length && f.push(...g.map(K)), o && f.push(o.toUpperCase()), f.push(ue);
524
524
  for (const l of f)
525
525
  if (n.has(l))
526
526
  return l;
527
- return t[0];
527
+ return i[0];
528
528
  }
529
529
  return { getRegionFromLocale: e };
530
530
  }
531
531
  function Re() {
532
532
  const { t: e } = y();
533
- function a(i, t) {
534
- const n = e(i), o = [], f = t.map((h) => h.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), g = new RegExp(`\\[(${f})\\]([\\s\\S]*?)\\[\\/\\1\\]`, "g"), l = t.reduce((h, C) => (h[C] = !1, h), {});
533
+ function a(t, i) {
534
+ const n = e(t), o = [], f = i.map((h) => h.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), g = new RegExp(`\\[(${f})\\]([\\s\\S]*?)\\[\\/\\1\\]`, "g"), l = i.reduce((h, C) => (h[C] = !1, h), {});
535
535
  let b = 0, m = g.exec(n);
536
536
  for (; m; ) {
537
- const { index: h, 0: C, 1: v, 2: p } = m;
538
- h > b && o.push({ text: n.slice(b, h), ...l }), p && v && o.push({ text: p, ...l, [v]: !0 }), b = h + C.length, m = g.exec(n);
537
+ const { index: h, 0: C, 1: B, 2: p } = m;
538
+ h > b && o.push({ text: n.slice(b, h), ...l }), p && B && o.push({ text: p, ...l, [B]: !0 }), b = h + C.length, m = g.exec(n);
539
539
  }
540
540
  return b < n.length && o.push({ text: n.slice(b), ...l }), o;
541
541
  }
542
542
  return { parse: a };
543
543
  }
544
- function ke(e, a, i = 768) {
545
- const t = A(i - 1), n = T(
546
- () => t.value >= i ? a : e
544
+ function ke(e, a, t = 768) {
545
+ const i = A(t - 1), n = T(
546
+ () => i.value >= t ? a : e
547
547
  );
548
548
  if (import.meta.client) {
549
549
  const o = () => {
550
- t.value = window.innerWidth;
550
+ i.value = window.innerWidth;
551
551
  };
552
552
  F(() => {
553
553
  o(), window.addEventListener("resize", o);
@@ -555,20 +555,20 @@ function ke(e, a, i = 768) {
555
555
  window.removeEventListener("resize", o);
556
556
  });
557
557
  }
558
- return { size: n, windowWidth: t };
558
+ return { size: n, windowWidth: i };
559
559
  }
560
560
  function ye() {
561
561
  const { locale: e } = y();
562
- function a(i, t = "fr") {
563
- if (!i) return "";
562
+ function a(t, i = "fr") {
563
+ if (!t) return "";
564
564
  const n = e.value;
565
- return i[n] ?? i[t] ?? "";
565
+ return t[n] ?? t[i] ?? "";
566
566
  }
567
567
  return a;
568
568
  }
569
569
  export {
570
570
  te as CREATE_PENDING_KEY,
571
- ve as buildUrl,
571
+ Be as buildUrl,
572
572
  he as configureHttp,
573
573
  N as getViewKey,
574
574
  we as showAndThrowError,
@@ -582,6 +582,6 @@ export {
582
582
  Ee as useLocaleHelpers,
583
583
  Re as useParseTranslation,
584
584
  ke as useResponsiveSize,
585
- Be as useStore,
585
+ ve as useStore,
586
586
  ye as useTranslate
587
587
  };
@@ -7,9 +7,9 @@ type ViewKey = string;
7
7
  * the single source of truth for item data, avoiding the duplication issue
8
8
  * where a view's `items[]` could drift from `items.value[id]` after a re-fetch.
9
9
  *
10
- * `revision` is the store's revision when the page was requested: an `update()`
11
- * or a `markViewsStale()` landing after that request makes the view stale, and
12
- * the next `list()` on it refetches the page.
10
+ * `revision` is the store's revision when the page was requested: a `create()`,
11
+ * an `update()` or a `markViewsStale()` landing after that request makes the
12
+ * view stale, and the next `list()` on it refetches the page.
13
13
  */
14
14
  type StoredView<Id extends IdKey> = ViewKeyParams & {
15
15
  itemIds: Id[];
@@ -60,7 +60,7 @@ export type Store<Id extends IdKey, Item, Params extends ViewKeyParams, CreatePa
60
60
  getView: (params: Params) => View<Item> | undefined;
61
61
  /** Wraps an arbitrary promise in the store's pending + error lifecycle under `key`. */
62
62
  withPending: <T>(key: ViewKey | Id, promise: Promise<T>) => Promise<T>;
63
- /** Calls `createMutation`, inserts the item into `items`, and invalidates all views. */
63
+ /** Calls `createMutation` (wrapped in `withPending(key)`), inserts the item into `items`, and marks every view stale. */
64
64
  create: (payload: CreatePayload, key?: ViewKey | Id) => Promise<Item>;
65
65
  /** Calls `updateMutation` (wrapped in `withPending(id)`), replaces `items[id]`, and marks every view stale. */
66
66
  update: (id: Id, patch: UpdatePatch) => Promise<Item>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@choosemycompany/lib",
3
3
  "description": "Shared composables, stores and types for ChooseMyCompany front apps",
4
- "version": "0.10.0",
4
+ "version": "0.10.1",
5
5
  "private": false,
6
6
  "type": "module",
7
7
  "packageManager": "pnpm@10.17.0",