@choosemycompany/lib 0.5.0 → 0.6.0

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.d.ts CHANGED
@@ -18,7 +18,7 @@
18
18
  * @packageDocumentation
19
19
  */
20
20
  export { useHttp } from './http/useHttp';
21
- export { useStore, getViewKey, type Store } from './store/useStore';
21
+ export { useStore, getViewKey, CREATE_PENDING_KEY, type Store } from './store/useStore';
22
22
  export { useClickOutside } from './ui/useClickOutside';
23
23
  export { useCountriesTranslation, type CountryCodes } from './i18n/useCountriesTranslation';
24
24
  export { showAndThrowError } from './errors/useErrorHandling';
package/dist/index.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import { mande as D } from "mande";
2
2
  import { useRuntimeConfig as L, showError as K, useState as H } from "nuxt/app";
3
- import { ref as v, onMounted as z, onBeforeUnmount as F, computed as N, onUnmounted as O } from "vue";
3
+ import { ref as M, onMounted as z, onBeforeUnmount as F, computed as N, onUnmounted as O } from "vue";
4
4
  import { useI18n as k } from "vue-i18n";
5
5
  import { createError as q } from "h3";
6
6
  let w;
7
7
  function Z() {
8
8
  return w || (w = L().public.cmcApiUrl, w);
9
9
  }
10
- function aa() {
10
+ function ea() {
11
11
  const a = Z(), i = D(a, {
12
12
  headers: {}
13
13
  });
@@ -25,8 +25,9 @@ function aa() {
25
25
  function T(a) {
26
26
  return Object.entries(a).sort(([i], [t]) => i.localeCompare(t)).map(([i, t]) => `${i}=${t}`).join("|");
27
27
  }
28
- function ea(a) {
29
- const i = v({}), t = v({}), n = v({}), e = v({}), s = a.getItemId ?? ((r) => {
28
+ const x = "create";
29
+ function na(a) {
30
+ const i = M({}), t = M({}), n = M({}), e = M({}), s = a.getItemId ?? ((r) => {
30
31
  const d = r.id;
31
32
  if (d === void 0)
32
33
  throw new Error(
@@ -34,10 +35,10 @@ function ea(a) {
34
35
  );
35
36
  return d;
36
37
  });
37
- function l(r) {
38
+ function c(r) {
38
39
  return !!e.value[r];
39
40
  }
40
- async function c(r, d) {
41
+ async function l(r, d) {
41
42
  e.value[r] = d;
42
43
  try {
43
44
  const o = await d;
@@ -45,10 +46,10 @@ function ea(a) {
45
46
  } catch (o) {
46
47
  throw n.value[r] = o, o;
47
48
  } finally {
48
- delete e.value[r];
49
+ e.value[r] === d && delete e.value[r];
49
50
  }
50
51
  }
51
- async function f(r, d = {}) {
52
+ async function g(r, d = {}) {
52
53
  if (!a.fetchOne)
53
54
  throw new Error("useStore.retrieve called but fetchOne is not defined in store options");
54
55
  const { force: o = !1 } = d;
@@ -57,10 +58,10 @@ function ea(a) {
57
58
  const u = e.value[r];
58
59
  if (u)
59
60
  return u;
60
- const B = a.fetchOne, S = (async () => {
61
+ const f = a.fetchOne, C = (async () => {
61
62
  try {
62
- const A = await B(r), b = a.enrichItem ? a.enrichItem(A) : A;
63
- return i.value[r] = b, delete n.value[r], i.value[r];
63
+ const A = await f(r), h = a.enrichItem ? a.enrichItem(A) : A;
64
+ return i.value[r] = h, delete n.value[r], i.value[r];
64
65
  } catch (A) {
65
66
  n.value[r] = A;
66
67
  return;
@@ -68,7 +69,7 @@ function ea(a) {
68
69
  delete e.value[r];
69
70
  }
70
71
  })();
71
- return e.value[r] = S, S;
72
+ return e.value[r] = C, C;
72
73
  }
73
74
  function p(r) {
74
75
  const d = T(r), o = t.value[d];
@@ -81,68 +82,68 @@ function ea(a) {
81
82
  limit: o.limit
82
83
  };
83
84
  }
84
- async function m(r, d = {}) {
85
+ async function b(r, d = {}) {
85
86
  if (!a.fetchPage)
86
87
  throw new Error("useStore.list called but fetchPage is not defined in store options");
87
88
  const o = T(r), { force: u = !1 } = d;
88
89
  if (t.value[o] && !u)
89
90
  return p(r);
90
- const B = e.value[o];
91
- if (B)
92
- return await B, p(r);
93
- const S = a.fetchPage, A = (async () => {
91
+ const f = e.value[o];
92
+ if (f)
93
+ return await f, p(r);
94
+ const C = a.fetchPage, A = (async () => {
94
95
  try {
95
- const b = await S(r), R = [];
96
- for (const E of b.items) {
96
+ const h = await C(r), R = [];
97
+ for (const E of h.items) {
97
98
  const y = a.enrichItem ? a.enrichItem(E) : E, P = s(y);
98
99
  i.value[P] = y, R.push(P);
99
100
  }
100
101
  t.value[o] = {
101
102
  itemIds: R,
102
- total: b.total,
103
- hasMore: b.hasMore,
104
- page: b.page,
105
- limit: b.limit
103
+ total: h.total,
104
+ hasMore: h.hasMore,
105
+ page: h.page,
106
+ limit: h.limit
106
107
  }, delete n.value[o];
107
- } catch (b) {
108
- n.value[o] = b;
108
+ } catch (h) {
109
+ n.value[o] = h;
109
110
  } finally {
110
111
  delete e.value[o];
111
112
  }
112
113
  })();
113
114
  return e.value[o] = A, await A, p(r);
114
115
  }
115
- async function h(r) {
116
+ async function B(r, d = x) {
116
117
  if (!a.createMutation)
117
118
  throw new Error("useStore.create called but createMutation is not defined in store options");
118
- const d = await a.createMutation(r), o = a.enrichItem ? a.enrichItem(d) : d, u = s(o);
119
- return i.value[u] = o, t.value = {}, o;
119
+ const o = a.createMutation, u = await l(d, (async () => o(r))()), f = a.enrichItem ? a.enrichItem(u) : u, C = s(f);
120
+ return i.value[C] = f, t.value = {}, f;
120
121
  }
121
- async function g(r, d) {
122
+ async function m(r, d) {
122
123
  if (!a.updateMutation)
123
124
  throw new Error("useStore.update called but updateMutation is not defined in store options");
124
- const o = a.updateMutation, u = await c(r, (async () => o(r, d))()), B = a.enrichItem ? a.enrichItem(u) : u;
125
- return i.value[r] = B, B;
125
+ const o = a.updateMutation, u = await l(r, (async () => o(r, d))()), f = a.enrichItem ? a.enrichItem(u) : u;
126
+ return i.value[r] = f, f;
126
127
  }
127
- async function C(r) {
128
+ async function v(r) {
128
129
  if (!a.removeMutation)
129
130
  throw new Error("useStore.remove called but removeMutation is not defined in store options");
130
131
  const d = a.removeMutation;
131
- await c(r, (async () => d(r))()), delete i.value[r];
132
+ await l(r, (async () => d(r))()), delete i.value[r];
132
133
  for (const o of Object.values(t.value)) {
133
134
  const u = o.itemIds.indexOf(r);
134
135
  u !== -1 && (o.itemIds.splice(u, 1), o.total = Math.max(0, o.total - 1));
135
136
  }
136
137
  }
137
- function M() {
138
+ function I() {
138
139
  n.value = {};
139
140
  }
140
- function I() {
141
+ function S() {
141
142
  i.value = {}, t.value = {}, n.value = {}, e.value = {};
142
143
  }
143
- return { items: i, views: t, errors: n, isPending: l, list: m, reset: I, retrieve: f, getView: p, withPending: c, create: h, update: g, remove: C, clearErrors: M };
144
+ return { items: i, views: t, errors: n, isPending: c, list: b, reset: S, retrieve: g, getView: p, withPending: l, create: B, update: m, remove: v, clearErrors: I };
144
145
  }
145
- function na(a, i) {
146
+ function ia(a, i) {
146
147
  const t = (n) => {
147
148
  const e = a.value, s = n.target;
148
149
  !e || !s || e.contains(s) || i();
@@ -153,10 +154,10 @@ function na(a, i) {
153
154
  document.removeEventListener("click", t);
154
155
  });
155
156
  }
156
- function ia() {
157
+ function ta() {
157
158
  const { locale: a } = k();
158
159
  function i(t, n = null) {
159
- const e = x[t.trim().toUpperCase()];
160
+ const e = V[t.trim().toUpperCase()];
160
161
  if (!e)
161
162
  throw new Error(`Unknown country code: ${t}`);
162
163
  if (n && e[n])
@@ -169,7 +170,7 @@ function ia() {
169
170
  }
170
171
  return i;
171
172
  }
172
- const x = {
173
+ const V = {
173
174
  AD: { en: "Andorra", fr: "Andorre", es: "Andorra", de: "Andorra", it: "Andorra", pt: "Andorra", da: "Andorra" },
174
175
  AE: { en: "United Arab Emirates", fr: "Émirats arabes unis", es: "Emiratos Árabes Unidos", de: "Vereinigte Arabische Emirate", it: "Emirati Arabi Uniti", pt: "Emirados Árabes Unidos", da: "De Forenede Arabiske Emirater" },
175
176
  AF: { en: "Afghanistan", fr: "Afghanistan", es: "Afganistán", de: "Afghanistan", it: "Afghanistan", pt: "Afeganistão", da: "Afghanistan" },
@@ -307,7 +308,7 @@ const x = {
307
308
  ZA: { en: "South Africa", fr: "Afrique Du Sud", es: "Sudáfrica", de: "Südafrika", it: "Sudafrica", pt: "África Do Sul", da: "Sydafrika" },
308
309
  ZM: { en: "Zambia", fr: "Zambie", es: "Zambia", de: "Sambia", it: "Zambia", pt: "Zâmbia", da: "Zambia" },
309
310
  ZW: { en: "Zimbabwe", fr: "Zimbabwe", es: "Zimbabue", de: "Simbabwe", it: "Zimbabwe", pt: "Zimbábue", da: "Zimbabwe" }
310
- }, ta = ({ code: a, message: i, data: t }) => {
311
+ }, ra = ({ code: a, message: i, data: t }) => {
311
312
  const n = i || "An unexpected error occurred", e = q({
312
313
  statusCode: a,
313
314
  statusMessage: n,
@@ -315,68 +316,68 @@ const x = {
315
316
  data: t
316
317
  });
317
318
  throw K(e), e;
318
- }, V = "ilcJCL_lmpwcEJfSkKND";
319
- function ra() {
319
+ }, J = "ilcJCL_lmpwcEJfSkKND";
320
+ function oa() {
320
321
  const { gtagId: a } = L().public;
321
322
  return { reportConversion: () => {
322
323
  if (typeof window > "u" || typeof window.gtag > "u" || !a)
323
324
  return;
324
- const t = `${a}/${V}`;
325
+ const t = `${a}/${J}`;
325
326
  window.gtag("event", "conversion", {
326
327
  send_to: t
327
328
  });
328
329
  } };
329
330
  }
330
331
  const U = "https://5509641.hs-sites-eu1.com";
331
- function oa() {
332
+ function sa() {
332
333
  const { locale: a } = k();
333
334
  return { contactUrl: N(() => a.value === "fr" ? `${U}/contact` : `${U}/${a.value}/contact`) };
334
335
  }
335
- function sa() {
336
+ function da() {
336
337
  async function a(n) {
337
338
  await Promise.all(
338
339
  n.map(
339
340
  (e) => new Promise((s) => {
340
341
  if (!e) return s(null);
341
- const l = new Image();
342
- l.onload = () => s(null), l.onerror = () => s(null), l.src = e;
342
+ const c = new Image();
343
+ c.onload = () => s(null), c.onerror = () => s(null), c.src = e;
343
344
  })
344
345
  )
345
346
  );
346
347
  }
347
348
  function i(n, e) {
348
- return e.split(".").reduce((s, l) => s && typeof s == "object" && l in s ? s[l] : "", n) ?? "";
349
+ return e.split(".").reduce((s, c) => s && typeof s == "object" && c in s ? s[c] : "", n) ?? "";
349
350
  }
350
351
  function t(n, e, s) {
351
352
  if (import.meta.server)
352
- return async (c) => {
353
+ return async (l) => {
353
354
  n.value = !0;
354
355
  };
355
- const l = H(
356
+ const c = H(
356
357
  `${e}LoadedImageCache`,
357
358
  () => ({})
358
359
  );
359
- return async (c) => {
360
- if (!c) {
360
+ return async (l) => {
361
+ if (!l) {
361
362
  n.value = !1;
362
363
  return;
363
364
  }
364
- const f = s.map((h) => i(c, h)), p = `${e}|${f.join("|")}`;
365
- if (l.value[p]) {
365
+ const g = s.map((B) => i(l, B)), p = `${e}|${g.join("|")}`;
366
+ if (c.value[p]) {
366
367
  n.value = !0;
367
368
  return;
368
369
  }
369
370
  n.value = !1;
370
- const m = f.filter(Boolean).map(String);
371
- await a(m), l.value[p] = !0, n.value = !0;
371
+ const b = g.filter(Boolean).map(String);
372
+ await a(b), c.value[p] = !0, n.value = !0;
372
373
  };
373
374
  }
374
375
  return t;
375
376
  }
376
- const J = {
377
+ const j = {
377
378
  en: ["US", "UK"]
378
- }, j = "FR";
379
- function $(a) {
379
+ }, $ = "FR";
380
+ function _(a) {
380
381
  const i = a.trim().replace("_", "-"), [t, n] = i.split("-");
381
382
  return {
382
383
  language: t?.toLowerCase() ?? "",
@@ -386,35 +387,35 @@ function $(a) {
386
387
  function G(a) {
387
388
  return a.toString().trim().toUpperCase();
388
389
  }
389
- function da() {
390
+ function ua() {
390
391
  function a(i, t) {
391
392
  if (!t.length) return;
392
- const n = t.map(G), e = new Set(n), { language: s, region: l } = $(i), c = [];
393
- l && c.push(l);
394
- const f = J[s];
395
- f?.length && c.push(...f.map(G)), s && c.push(s.toUpperCase()), c.push(j);
396
- for (const p of c)
393
+ const n = t.map(G), e = new Set(n), { language: s, region: c } = _(i), l = [];
394
+ c && l.push(c);
395
+ const g = j[s];
396
+ g?.length && l.push(...g.map(G)), s && l.push(s.toUpperCase()), l.push($);
397
+ for (const p of l)
397
398
  if (e.has(p))
398
399
  return p;
399
400
  return n[0];
400
401
  }
401
402
  return { getRegionFromLocale: a };
402
403
  }
403
- function ua() {
404
+ function la() {
404
405
  const { t: a } = k();
405
406
  function i(t, n) {
406
- const e = a(t), s = [], c = n.map((g) => g.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), f = new RegExp(`\\[(${c})\\]([\\s\\S]*?)\\[\\/\\1\\]`, "g"), p = n.reduce((g, C) => (g[C] = !1, g), {});
407
- let m = 0, h = f.exec(e);
408
- for (; h; ) {
409
- const { index: g, 0: C, 1: M, 2: I } = h;
410
- g > m && s.push({ text: e.slice(m, g), ...p }), I && M && s.push({ text: I, ...p, [M]: !0 }), m = g + C.length, h = f.exec(e);
407
+ const e = a(t), s = [], l = n.map((m) => m.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), g = new RegExp(`\\[(${l})\\]([\\s\\S]*?)\\[\\/\\1\\]`, "g"), p = n.reduce((m, v) => (m[v] = !1, m), {});
408
+ let b = 0, B = g.exec(e);
409
+ for (; B; ) {
410
+ const { index: m, 0: v, 1: I, 2: S } = B;
411
+ m > b && s.push({ text: e.slice(b, m), ...p }), S && I && s.push({ text: S, ...p, [I]: !0 }), b = m + v.length, B = g.exec(e);
411
412
  }
412
- return m < e.length && s.push({ text: e.slice(m), ...p }), s;
413
+ return b < e.length && s.push({ text: e.slice(b), ...p }), s;
413
414
  }
414
415
  return { parse: i };
415
416
  }
416
- function la(a, i, t = 768) {
417
- const n = v(t - 1), e = N(
417
+ function ca(a, i, t = 768) {
418
+ const n = M(t - 1), e = N(
418
419
  () => n.value >= t ? i : a
419
420
  );
420
421
  if (import.meta.client) {
@@ -429,7 +430,7 @@ function la(a, i, t = 768) {
429
430
  }
430
431
  return { size: e, windowWidth: n };
431
432
  }
432
- function ca() {
433
+ function pa() {
433
434
  const { locale: a } = k();
434
435
  function i(t, n = "fr") {
435
436
  if (!t) return "";
@@ -439,17 +440,18 @@ function ca() {
439
440
  return i;
440
441
  }
441
442
  export {
443
+ x as CREATE_PENDING_KEY,
442
444
  T as getViewKey,
443
- ta as showAndThrowError,
444
- na as useClickOutside,
445
- ia as useCountriesTranslation,
446
- ra as useGtagConversion,
447
- aa as useHttp,
448
- oa as useHubspotContactUrl,
449
- sa as useImageCacheWatcherCallback,
450
- da as useLocaleHelpers,
451
- ua as useParseTranslation,
452
- la as useResponsiveSize,
453
- ea as useStore,
454
- ca as useTranslate
445
+ ra as showAndThrowError,
446
+ ia as useClickOutside,
447
+ ta as useCountriesTranslation,
448
+ oa as useGtagConversion,
449
+ ea as useHttp,
450
+ sa as useHubspotContactUrl,
451
+ da as useImageCacheWatcherCallback,
452
+ ua as useLocaleHelpers,
453
+ la as useParseTranslation,
454
+ ca as useResponsiveSize,
455
+ na as useStore,
456
+ pa as useTranslate
455
457
  };
@@ -56,7 +56,7 @@ export type Store<Id extends IdKey, Item, Params extends ViewKeyParams, CreatePa
56
56
  /** Wraps an arbitrary promise in the store's pending + error lifecycle under `key`. */
57
57
  withPending: <T>(key: ViewKey | Id, promise: Promise<T>) => Promise<T>;
58
58
  /** Calls `createMutation`, inserts the item into `items`, and invalidates all views. */
59
- create: (payload: CreatePayload) => Promise<Item>;
59
+ create: (payload: CreatePayload, key?: ViewKey | Id) => Promise<Item>;
60
60
  /** Calls `updateMutation` (wrapped in `withPending(id)`), replaces `items[id]`. */
61
61
  update: (id: Id, patch: UpdatePatch) => Promise<Item>;
62
62
  /** Calls `removeMutation` (wrapped in `withPending(id)`), deletes `items[id]`, removes id from every view. */
@@ -85,5 +85,6 @@ export type Store<Id extends IdKey, Item, Params extends ViewKeyParams, CreatePa
85
85
  * await store.update('org-42', { name: 'Acme Inc.' })
86
86
  * ```
87
87
  */
88
+ export declare const CREATE_PENDING_KEY = "create";
88
89
  export declare function useStore<Id extends IdKey, Item, Params extends ViewKeyParams, CreatePayload = Item, UpdatePatch = Partial<Item>>(options: StoreOptions<Id, Item, Params, CreatePayload, UpdatePatch>): Store<Id, Item, Params, CreatePayload, UpdatePatch>;
89
90
  export {};
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.5.0",
4
+ "version": "0.6.0",
5
5
  "private": false,
6
6
  "type": "module",
7
7
  "packageManager": "pnpm@10.17.0",