@choosemycompany/lib 0.2.0 → 0.3.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.
@@ -1,7 +1,10 @@
1
1
  /**
2
2
  * Shared HTTP types modeling the CMC API contract.
3
3
  *
4
- * The CMC backend wraps every response in a `{ data, pagination? }` envelope.
4
+ * The CMC backend wraps responses in one of two envelopes:
5
+ * - **list** endpoints return `{ data: T[], pagination }` (see `ListResponse`);
6
+ * - **single-item** (retrieve) endpoints return `{ item: T }` (see `RetrieveResponse`).
7
+ *
5
8
  * These types are independent of any HTTP client — `useHttp` happens to use
6
9
  * `mande`, but consumers can adapt them to any fetcher.
7
10
  */
@@ -64,12 +67,15 @@ export type ListResponse<T> = {
64
67
  pagination: Pagination;
65
68
  };
66
69
  /**
67
- * Standard CMC single-item response envelope.
70
+ * Standard CMC single-item (retrieve) response envelope.
71
+ *
72
+ * The resource is exposed under `item` — **not** `data`. `data` is reserved for
73
+ * the list envelope (`ListResponse`), which also carries `pagination`.
68
74
  *
69
75
  * @typeParam T - Shape of the returned resource.
70
76
  */
71
77
  export type RetrieveResponse<T> = {
72
- data: T;
78
+ item: T;
73
79
  };
74
80
  /**
75
81
  * Field-level validation error item, as returned in `MandeErrorBody.errors`.
@@ -12,7 +12,7 @@ import { MandeInstance } from 'mande';
12
12
  * const { http, setToken, clearToken } = useHttp()
13
13
  *
14
14
  * setToken(accessToken)
15
- * const { data } = await http.get<RetrieveResponse<User>>('/me')
15
+ * const { item } = await http.get<RetrieveResponse<User>>('/me')
16
16
  * clearToken() // on logout
17
17
  * ```
18
18
  */
package/dist/index.js CHANGED
@@ -1,14 +1,14 @@
1
- import { mande as H } from "mande";
2
- import { useRuntimeConfig as G, showError as F, useState as O } from "nuxt/app";
3
- import { ref as A, onMounted as z, onBeforeUnmount as q, computed as N, onUnmounted as Z } from "vue";
1
+ import { mande as D } from "mande";
2
+ import { useRuntimeConfig as G, 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";
4
4
  import { useI18n as M } from "vue-i18n";
5
- import { createError as x } from "h3";
5
+ import { createError as q } from "h3";
6
6
  let w;
7
- function V() {
7
+ function Z() {
8
8
  return w || (w = G().public.cmcApiUrl, w);
9
9
  }
10
- function na() {
11
- const a = V(), n = H(a, {
10
+ function aa() {
11
+ const a = Z(), n = D(a, {
12
12
  headers: {}
13
13
  });
14
14
  function t(e) {
@@ -25,22 +25,22 @@ function na() {
25
25
  function P(a) {
26
26
  return Object.entries(a).sort(([n], [t]) => n.localeCompare(t)).map(([n, t]) => `${n}=${t}`).join("|");
27
27
  }
28
- function ia(a) {
29
- const n = A({}), t = A({}), i = A({}), e = A({}), o = A(""), d = A([]), l = a.getItemId ?? ((r) => {
30
- const u = r.id;
31
- if (u === void 0)
28
+ function ea(a) {
29
+ const n = v({}), t = v({}), i = v({}), e = v({}), o = a.getItemId ?? ((r) => {
30
+ const l = r.id;
31
+ if (l === void 0)
32
32
  throw new Error(
33
33
  "useStore: no `getItemId` option provided and item has no `id` property. Either pass `getItemId` in the store options, or expose an `id` field on your items."
34
34
  );
35
- return u;
35
+ return l;
36
36
  });
37
- function f(r) {
37
+ function d(r) {
38
38
  return !!e.value[r];
39
39
  }
40
- async function p(r, u) {
41
- e.value[r] = u;
40
+ async function u(r, l) {
41
+ e.value[r] = l;
42
42
  try {
43
- const s = await u;
43
+ const s = await l;
44
44
  return delete i.value[r], s;
45
45
  } catch (s) {
46
46
  throw i.value[r] = s, s;
@@ -48,10 +48,10 @@ function ia(a) {
48
48
  delete e.value[r];
49
49
  }
50
50
  }
51
- async function b(r, u = {}) {
51
+ async function f(r, l = {}) {
52
52
  if (!a.fetchOne)
53
53
  throw new Error("useStore.retrieve called but fetchOne is not defined in store options");
54
- const { force: s = !1 } = u;
54
+ const { force: s = !1 } = l;
55
55
  if (n.value[r] && !s)
56
56
  return n.value[r];
57
57
  const c = e.value[r];
@@ -59,8 +59,8 @@ function ia(a) {
59
59
  return c;
60
60
  const B = a.fetchOne, k = (async () => {
61
61
  try {
62
- const C = await B(r), h = a.enrichItem ? a.enrichItem(C) : C;
63
- return n.value[r] = h, delete i.value[r], n.value[r];
62
+ const C = await B(r), b = a.enrichItem ? a.enrichItem(C) : C;
63
+ return n.value[r] = b, delete i.value[r], n.value[r];
64
64
  } catch (C) {
65
65
  i.value[r] = C;
66
66
  return;
@@ -70,8 +70,8 @@ function ia(a) {
70
70
  })();
71
71
  return e.value[r] = k, k;
72
72
  }
73
- function g(r) {
74
- const u = P(r), s = t.value[u];
73
+ function p(r) {
74
+ const l = P(r), s = t.value[l];
75
75
  if (s)
76
76
  return {
77
77
  items: s.itemIds.map((c) => n.value[c]).filter((c) => c !== void 0),
@@ -81,82 +81,82 @@ function ia(a) {
81
81
  limit: s.limit
82
82
  };
83
83
  }
84
- async function m(r, u = {}) {
84
+ async function m(r, l = {}) {
85
85
  if (!a.fetchPage)
86
86
  throw new Error("useStore.list called but fetchPage is not defined in store options");
87
- const s = P(r), { force: c = !1 } = u;
87
+ const s = P(r), { force: c = !1 } = l;
88
88
  if (t.value[s] && !c)
89
- return g(r);
89
+ return p(r);
90
90
  const B = e.value[s];
91
91
  if (B)
92
- return await B, g(r);
92
+ return await B, p(r);
93
93
  const k = a.fetchPage, C = (async () => {
94
94
  try {
95
- const h = await k(r), R = [];
96
- for (const E of h.items) {
97
- const y = a.enrichItem ? a.enrichItem(E) : E, T = l(y);
95
+ const b = await k(r), R = [];
96
+ for (const E of b.items) {
97
+ const y = a.enrichItem ? a.enrichItem(E) : E, T = o(y);
98
98
  n.value[T] = y, R.push(T);
99
99
  }
100
100
  t.value[s] = {
101
101
  itemIds: R,
102
- total: h.total,
103
- hasMore: h.hasMore,
104
- page: h.page,
105
- limit: h.limit
102
+ total: b.total,
103
+ hasMore: b.hasMore,
104
+ page: b.page,
105
+ limit: b.limit
106
106
  }, delete i.value[s];
107
- } catch (h) {
108
- i.value[s] = h;
107
+ } catch (b) {
108
+ i.value[s] = b;
109
109
  } finally {
110
110
  delete e.value[s];
111
111
  }
112
112
  })();
113
- return e.value[s] = C, await C, g(r);
113
+ return e.value[s] = C, await C, p(r);
114
114
  }
115
- async function v(r) {
115
+ async function h(r) {
116
116
  if (!a.createMutation)
117
117
  throw new Error("useStore.create called but createMutation is not defined in store options");
118
- const u = await a.createMutation(r), s = a.enrichItem ? a.enrichItem(u) : u, c = l(s);
118
+ const l = await a.createMutation(r), s = a.enrichItem ? a.enrichItem(l) : l, c = o(s);
119
119
  return n.value[c] = s, t.value = {}, s;
120
120
  }
121
- async function I(r, u) {
121
+ async function g(r, l) {
122
122
  if (!a.updateMutation)
123
123
  throw new Error("useStore.update called but updateMutation is not defined in store options");
124
- const s = a.updateMutation, c = await p(r, (async () => s(r, u))()), B = a.enrichItem ? a.enrichItem(c) : c;
124
+ const s = a.updateMutation, c = await u(r, (async () => s(r, l))()), B = a.enrichItem ? a.enrichItem(c) : c;
125
125
  return n.value[r] = B, B;
126
126
  }
127
- async function S(r) {
127
+ async function A(r) {
128
128
  if (!a.removeMutation)
129
129
  throw new Error("useStore.remove called but removeMutation is not defined in store options");
130
- const u = a.removeMutation;
131
- await p(r, (async () => u(r))()), delete n.value[r];
130
+ const l = a.removeMutation;
131
+ await u(r, (async () => l(r))()), delete n.value[r];
132
132
  for (const s of Object.values(t.value)) {
133
133
  const c = s.itemIds.indexOf(r);
134
134
  c !== -1 && (s.itemIds.splice(c, 1), s.total = Math.max(0, s.total - 1));
135
135
  }
136
136
  }
137
- function D() {
137
+ function I() {
138
138
  i.value = {};
139
139
  }
140
- function K() {
141
- n.value = {}, t.value = {}, i.value = {}, e.value = {}, o.value = "", d.value = [];
140
+ function S() {
141
+ n.value = {}, t.value = {}, i.value = {}, e.value = {};
142
142
  }
143
- return { items: n, views: t, errors: i, search: o, filters: d, isPending: f, list: m, reset: K, retrieve: b, getView: g, track: p, create: v, update: I, remove: S, clearErrors: D };
143
+ return { items: n, views: t, errors: i, isPending: d, list: m, reset: S, retrieve: f, getView: p, track: u, create: h, update: g, remove: A, clearErrors: I };
144
144
  }
145
- function ta(a, n) {
145
+ function na(a, n) {
146
146
  const t = (i) => {
147
147
  const e = a.value, o = i.target;
148
148
  !e || !o || e.contains(o) || n();
149
149
  };
150
150
  z(() => {
151
151
  document.addEventListener("click", t);
152
- }), q(() => {
152
+ }), F(() => {
153
153
  document.removeEventListener("click", t);
154
154
  });
155
155
  }
156
- function ra() {
156
+ function ia() {
157
157
  const { locale: a } = M();
158
158
  function n(t, i = null) {
159
- const e = J[t.trim().toUpperCase()];
159
+ const e = x[t.trim().toUpperCase()];
160
160
  if (!e)
161
161
  throw new Error(`Unknown country code: ${t}`);
162
162
  if (i && e[i])
@@ -169,7 +169,7 @@ function ra() {
169
169
  }
170
170
  return n;
171
171
  }
172
- const J = {
172
+ const x = {
173
173
  AD: { en: "Andorra", fr: "Andorre", es: "Andorra", de: "Andorra", it: "Andorra", pt: "Andorra", da: "Andorra" },
174
174
  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
175
  AF: { en: "Afghanistan", fr: "Afghanistan", es: "Afganistán", de: "Afghanistan", it: "Afghanistan", pt: "Afeganistão", da: "Afghanistan" },
@@ -307,32 +307,32 @@ const J = {
307
307
  ZA: { en: "South Africa", fr: "Afrique Du Sud", es: "Sudáfrica", de: "Südafrika", it: "Sudafrica", pt: "África Do Sul", da: "Sydafrika" },
308
308
  ZM: { en: "Zambia", fr: "Zambie", es: "Zambia", de: "Sambia", it: "Zambia", pt: "Zâmbia", da: "Zambia" },
309
309
  ZW: { en: "Zimbabwe", fr: "Zimbabwe", es: "Zimbabue", de: "Simbabwe", it: "Zimbabwe", pt: "Zimbábue", da: "Zimbabwe" }
310
- }, oa = ({ code: a, message: n, data: t }) => {
311
- const i = n || "An unexpected error occurred", e = x({
310
+ }, ta = ({ code: a, message: n, data: t }) => {
311
+ const i = n || "An unexpected error occurred", e = q({
312
312
  statusCode: a,
313
313
  statusMessage: i,
314
314
  message: i,
315
315
  data: t
316
316
  });
317
- throw F(e), e;
318
- }, $ = "ilcJCL_lmpwcEJfSkKND";
319
- function sa() {
317
+ throw K(e), e;
318
+ }, V = "ilcJCL_lmpwcEJfSkKND";
319
+ function ra() {
320
320
  const { gtagId: a } = G().public;
321
321
  return { reportConversion: () => {
322
322
  if (typeof window > "u" || typeof window.gtag > "u" || !a)
323
323
  return;
324
- const t = `${a}/${$}`;
324
+ const t = `${a}/${V}`;
325
325
  window.gtag("event", "conversion", {
326
326
  send_to: t
327
327
  });
328
328
  } };
329
329
  }
330
330
  const U = "https://5509641.hs-sites-eu1.com";
331
- function da() {
331
+ function oa() {
332
332
  const { locale: a } = M();
333
333
  return { contactUrl: N(() => a.value === "fr" ? `${U}/contact` : `${U}/${a.value}/contact`) };
334
334
  }
335
- function ua() {
335
+ function sa() {
336
336
  async function a(i) {
337
337
  await Promise.all(
338
338
  i.map(
@@ -349,34 +349,34 @@ function ua() {
349
349
  }
350
350
  function t(i, e, o) {
351
351
  if (import.meta.server)
352
- return async (l) => {
352
+ return async (u) => {
353
353
  i.value = !0;
354
354
  };
355
- const d = O(
355
+ const d = H(
356
356
  `${e}LoadedImageCache`,
357
357
  () => ({})
358
358
  );
359
- return async (l) => {
360
- if (!l) {
359
+ return async (u) => {
360
+ if (!u) {
361
361
  i.value = !1;
362
362
  return;
363
363
  }
364
- const f = o.map((g) => n(l, g)), p = `${e}|${f.join("|")}`;
364
+ const f = o.map((h) => n(u, h)), p = `${e}|${f.join("|")}`;
365
365
  if (d.value[p]) {
366
366
  i.value = !0;
367
367
  return;
368
368
  }
369
369
  i.value = !1;
370
- const b = f.filter(Boolean).map(String);
371
- await a(b), d.value[p] = !0, i.value = !0;
370
+ const m = f.filter(Boolean).map(String);
371
+ await a(m), d.value[p] = !0, i.value = !0;
372
372
  };
373
373
  }
374
374
  return t;
375
375
  }
376
- const j = {
376
+ const J = {
377
377
  en: ["US", "UK"]
378
- }, _ = "FR";
379
- function W(a) {
378
+ }, $ = "FR";
379
+ function j(a) {
380
380
  const n = a.trim().replace("_", "-"), [t, i] = n.split("-");
381
381
  return {
382
382
  language: t?.toLowerCase() ?? "",
@@ -386,45 +386,45 @@ function W(a) {
386
386
  function L(a) {
387
387
  return a.toString().trim().toUpperCase();
388
388
  }
389
- function la() {
389
+ function da() {
390
390
  function a(n, t) {
391
391
  if (!t.length) return;
392
- const i = t.map(L), e = new Set(i), { language: o, region: d } = W(n), l = [];
393
- d && l.push(d);
394
- const f = j[o];
395
- f?.length && l.push(...f.map(L)), o && l.push(o.toUpperCase()), l.push(_);
396
- for (const p of l)
392
+ const i = t.map(L), e = new Set(i), { language: o, region: d } = j(n), u = [];
393
+ d && u.push(d);
394
+ const f = J[o];
395
+ f?.length && u.push(...f.map(L)), o && u.push(o.toUpperCase()), u.push($);
396
+ for (const p of u)
397
397
  if (e.has(p))
398
398
  return p;
399
399
  return i[0];
400
400
  }
401
401
  return { getRegionFromLocale: a };
402
402
  }
403
- function ca() {
403
+ function ua() {
404
404
  const { t: a } = M();
405
405
  function n(t, i) {
406
- const e = a(t), o = [], l = i.map((m) => m.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), f = new RegExp(`\\[(${l})\\]([\\s\\S]*?)\\[\\/\\1\\]`, "g"), p = i.reduce((m, v) => (m[v] = !1, m), {});
407
- let b = 0, g = f.exec(e);
408
- for (; g; ) {
409
- const { index: m, 0: v, 1: I, 2: S } = g;
410
- m > b && o.push({ text: e.slice(b, m), ...p }), S && I && o.push({ text: S, ...p, [I]: !0 }), b = m + v.length, g = f.exec(e);
406
+ const e = a(t), o = [], u = i.map((g) => g.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), f = new RegExp(`\\[(${u})\\]([\\s\\S]*?)\\[\\/\\1\\]`, "g"), p = i.reduce((g, A) => (g[A] = !1, g), {});
407
+ let m = 0, h = f.exec(e);
408
+ for (; h; ) {
409
+ const { index: g, 0: A, 1: I, 2: S } = h;
410
+ g > m && o.push({ text: e.slice(m, g), ...p }), S && I && o.push({ text: S, ...p, [I]: !0 }), m = g + A.length, h = f.exec(e);
411
411
  }
412
- return b < e.length && o.push({ text: e.slice(b), ...p }), o;
412
+ return m < e.length && o.push({ text: e.slice(m), ...p }), o;
413
413
  }
414
414
  return { parse: n };
415
415
  }
416
- function pa(a) {
416
+ function la(a) {
417
417
  const { t: n, te: t, tm: i } = M();
418
418
  function e(o, d) {
419
- const l = `${o}.${d}`, f = `${o}.${a[0]}`;
420
- return t(l) ? n(l) : t(f) ? n(f) : typeof i(o) == "string" ? n(o, {
419
+ const u = `${o}.${d}`, f = `${o}.${a[0]}`;
420
+ return t(u) ? n(u) : t(f) ? n(f) : typeof i(o) == "string" ? n(o, {
421
421
  primaryType: n(`common.primary-type.${d}`).toLowerCase()
422
422
  }) : "";
423
423
  }
424
424
  return { getPrimaryTypeTranslation: e };
425
425
  }
426
- function fa(a, n, t = 768) {
427
- const i = A(t - 1), e = N(
426
+ function ca(a, n, t = 768) {
427
+ const i = v(t - 1), e = N(
428
428
  () => i.value >= t ? n : a
429
429
  );
430
430
  if (import.meta.client) {
@@ -433,13 +433,13 @@ function fa(a, n, t = 768) {
433
433
  };
434
434
  z(() => {
435
435
  o(), window.addEventListener("resize", o);
436
- }), Z(() => {
436
+ }), O(() => {
437
437
  window.removeEventListener("resize", o);
438
438
  });
439
439
  }
440
440
  return { size: e, windowWidth: i };
441
441
  }
442
- function ga() {
442
+ function pa() {
443
443
  const { locale: a } = M();
444
444
  function n(t, i = "fr") {
445
445
  if (!t) return "";
@@ -450,17 +450,17 @@ function ga() {
450
450
  }
451
451
  export {
452
452
  P as getViewKey,
453
- oa as showAndThrowError,
454
- ta as useClickOutside,
455
- ra as useCountriesTranslation,
456
- sa as useGtagConversion,
457
- na as useHttp,
458
- da as useHubspotContactUrl,
459
- ua as useImageCacheWatcherCallback,
460
- la as useLocaleHelpers,
461
- ca as useParseTranslation,
462
- pa as usePrimaryTypeTranslation,
463
- fa as useResponsiveSize,
464
- ia as useStore,
465
- ga as useTranslate
453
+ ta as showAndThrowError,
454
+ na as useClickOutside,
455
+ ia as useCountriesTranslation,
456
+ ra as useGtagConversion,
457
+ aa as useHttp,
458
+ oa as useHubspotContactUrl,
459
+ sa as useImageCacheWatcherCallback,
460
+ da as useLocaleHelpers,
461
+ ua as useParseTranslation,
462
+ la as usePrimaryTypeTranslation,
463
+ ca as useResponsiveSize,
464
+ ea as useStore,
465
+ pa as useTranslate
466
466
  };
@@ -72,11 +72,6 @@ export type StoreOptions<Id extends IdKey, Item, Params extends ViewKeyParams, C
72
72
  updateMutation?: (id: Id, patch: UpdatePatch) => Promise<Item>;
73
73
  /** Deletes an item server-side. Required to call `remove()`. */
74
74
  removeMutation?: (id: Id) => Promise<void>;
75
- /**
76
- * Cache time-to-live in milliseconds. Reserved for future use — currently
77
- * **not implemented**; cached entries live until `reset()` is called.
78
- */
79
- ttlMs?: number;
80
75
  };
81
76
  /**
82
77
  * Options accepted by `retrieve()`.
@@ -35,21 +35,16 @@ export declare function getViewKey(params: ViewKeyParams): ViewKey;
35
35
  * @typeParam Id - Item identifier (string or number).
36
36
  * @typeParam Item - Item shape stored in the cache.
37
37
  * @typeParam Params - Paginated query shape (must extend `ViewKeyParams`).
38
- * @typeParam FilterItem - Element type of the `filters` ref (free-form, owned by the consumer).
39
38
  * @typeParam CreatePayload - Payload shape passed to `create()` (defaults to `Item`).
40
39
  * @typeParam UpdatePatch - Patch shape passed to `update()` (defaults to `Partial<Item>`).
41
40
  */
42
- export type Store<Id extends IdKey, Item, Params extends ViewKeyParams, FilterItem = unknown, CreatePayload = Item, UpdatePatch = Partial<Item>> = {
41
+ export type Store<Id extends IdKey, Item, Params extends ViewKeyParams, CreatePayload = Item, UpdatePatch = Partial<Item>> = {
43
42
  /** Canonical item cache keyed by id. The single source of truth for item data. */
44
43
  items: Ref<StoreItems<Id, Item>>;
45
44
  /** Cached pages, keyed by `getViewKey(params)`. Each stores `itemIds[]` + pagination meta. */
46
45
  views: Ref<StoreViews<Id>>;
47
46
  /** Latest error per key (id for retrieve, view key for list, arbitrary for `track`). */
48
47
  errors: Ref<StoreErrors>;
49
- /** Free-form search state, exposed for consumer convenience; never read internally. */
50
- search: Ref<string>;
51
- /** Free-form filter state, exposed for consumer convenience; never read internally. */
52
- filters: Ref<FilterItem[]>;
53
48
  /** Returns `true` if there is an in-flight promise registered under `key`. */
54
49
  isPending: (key: ViewKey | Id) => boolean;
55
50
  /** Fetches one item by id (cache-first unless `force: true`). Returns `undefined` on failure. */
@@ -90,5 +85,5 @@ export type Store<Id extends IdKey, Item, Params extends ViewKeyParams, FilterIt
90
85
  * await store.update('org-42', { name: 'Acme Inc.' })
91
86
  * ```
92
87
  */
93
- export declare function useStore<Id extends IdKey, Item, Params extends ViewKeyParams, FilterItem = unknown, CreatePayload = Item, UpdatePatch = Partial<Item>>(options: StoreOptions<Id, Item, Params, CreatePayload, UpdatePatch>): Store<Id, Item, Params, FilterItem, CreatePayload, UpdatePatch>;
88
+ 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>;
94
89
  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.2.0",
4
+ "version": "0.3.0",
5
5
  "private": false,
6
6
  "type": "module",
7
7
  "packageManager": "pnpm@10.17.0",