@choosemycompany/lib 0.1.0 → 0.2.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/README.md +2 -3
- package/dist/analytics/useGtagConversion.d.ts +36 -0
- package/dist/analytics/useHubspotContactUrl.d.ts +25 -0
- package/dist/errors/useErrorHandling.d.ts +36 -0
- package/dist/http/types.d.ts +113 -0
- package/dist/http/useHttp.d.ts +23 -0
- package/dist/i18n/types.d.ts +37 -0
- package/dist/i18n/useCountriesTranslation.d.ts +46 -0
- package/dist/{useLocaleHelpers.d.ts → i18n/useLocaleHelpers.d.ts} +5 -0
- package/dist/i18n/useParseTranslation.d.ts +45 -0
- package/dist/i18n/usePrimaryTypeTranslation.d.ts +34 -0
- package/dist/i18n/useTranslate.d.ts +32 -0
- package/dist/index.d.ts +35 -15
- package/dist/index.js +225 -191
- package/dist/store/types.d.ts +94 -0
- package/dist/store/useStore.d.ts +94 -0
- package/dist/ui/useClickOutside.d.ts +39 -0
- package/dist/ui/useImageCacheWatcherCallback.d.ts +34 -0
- package/dist/ui/useResponsiveSize.d.ts +33 -0
- package/package.json +13 -11
- package/dist/types/errors.d.ts +0 -15
- package/dist/types/index.d.ts +0 -8
- package/dist/types/language.d.ts +0 -2
- package/dist/types/pagination.d.ts +0 -12
- package/dist/types/request.d.ts +0 -7
- package/dist/types/response.d.ts +0 -8
- package/dist/types/routes.d.ts +0 -6
- package/dist/types/store.d.ts +0 -15
- package/dist/types/view.d.ts +0 -12
- package/dist/useClickOutside.d.ts +0 -2
- package/dist/useCountriesTranslation.d.ts +0 -6
- package/dist/useErrorHandling.d.ts +0 -5
- package/dist/useGtagConversion.d.ts +0 -8
- package/dist/useHttp.d.ts +0 -6
- package/dist/useHubspotContactUrl.d.ts +0 -3
- package/dist/useImageCacheWatcherCallback.d.ts +0 -2
- package/dist/useParseTranslation.d.ts +0 -6
- package/dist/usePrimaryTypeTranslation.d.ts +0 -3
- package/dist/useResponsiveSize.d.ts +0 -4
- package/dist/useStore.d.ts +0 -20
- package/dist/useTranslate.d.ts +0 -4
- package/dist/useUrlHelpers.d.ts +0 -8
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
import { IdKey, ListOptions, RetrieveOptions, StoreOptions, View, ViewKeyParams } from './types';
|
|
3
|
+
/** Deterministic string built from a `ViewKeyParams` object — see {@link getViewKey}. */
|
|
4
|
+
type ViewKey = string;
|
|
5
|
+
/**
|
|
6
|
+
* Internal shape of a cached view. Keeps only `itemIds[]` so that `items` remains
|
|
7
|
+
* the single source of truth for item data, avoiding the duplication issue
|
|
8
|
+
* where a view's `items[]` could drift from `items.value[id]` after a re-fetch.
|
|
9
|
+
*/
|
|
10
|
+
type StoredView<Id extends IdKey> = ViewKeyParams & {
|
|
11
|
+
itemIds: Id[];
|
|
12
|
+
total: number;
|
|
13
|
+
hasMore: boolean;
|
|
14
|
+
};
|
|
15
|
+
type StoreItems<Id extends IdKey, Item> = Record<Id, Item>;
|
|
16
|
+
type StoreViews<Id extends IdKey> = Record<ViewKey, StoredView<Id>>;
|
|
17
|
+
type StoreErrors = Record<IdKey, unknown>;
|
|
18
|
+
/**
|
|
19
|
+
* Builds a deterministic string key from a `ViewKeyParams` (or any superset) so
|
|
20
|
+
* that two semantically equal queries always hash to the same view, regardless
|
|
21
|
+
* of property order.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* getViewKey({ page: 1, limit: 10 }) // "limit=10|page=1"
|
|
26
|
+
* getViewKey({ limit: 10, page: 1 }) // "limit=10|page=1" (same)
|
|
27
|
+
* getViewKey({ page: 1, limit: 10, q: 'x' }) // "limit=10|page=1|q=x"
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function getViewKey(params: ViewKeyParams): ViewKey;
|
|
31
|
+
/**
|
|
32
|
+
* Public shape returned by {@link useStore}. See each method's JSDoc on the
|
|
33
|
+
* implementation below for behaviour details.
|
|
34
|
+
*
|
|
35
|
+
* @typeParam Id - Item identifier (string or number).
|
|
36
|
+
* @typeParam Item - Item shape stored in the cache.
|
|
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
|
+
* @typeParam CreatePayload - Payload shape passed to `create()` (defaults to `Item`).
|
|
40
|
+
* @typeParam UpdatePatch - Patch shape passed to `update()` (defaults to `Partial<Item>`).
|
|
41
|
+
*/
|
|
42
|
+
export type Store<Id extends IdKey, Item, Params extends ViewKeyParams, FilterItem = unknown, CreatePayload = Item, UpdatePatch = Partial<Item>> = {
|
|
43
|
+
/** Canonical item cache keyed by id. The single source of truth for item data. */
|
|
44
|
+
items: Ref<StoreItems<Id, Item>>;
|
|
45
|
+
/** Cached pages, keyed by `getViewKey(params)`. Each stores `itemIds[]` + pagination meta. */
|
|
46
|
+
views: Ref<StoreViews<Id>>;
|
|
47
|
+
/** Latest error per key (id for retrieve, view key for list, arbitrary for `track`). */
|
|
48
|
+
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
|
+
/** Returns `true` if there is an in-flight promise registered under `key`. */
|
|
54
|
+
isPending: (key: ViewKey | Id) => boolean;
|
|
55
|
+
/** Fetches one item by id (cache-first unless `force: true`). Returns `undefined` on failure. */
|
|
56
|
+
retrieve: (id: Id, retrieveOptions?: RetrieveOptions) => Promise<Item | undefined>;
|
|
57
|
+
/** Fetches one page (cache-first unless `force: true`). Returns the resolved `View<Item>`. */
|
|
58
|
+
list: (params: Params, listOptions?: ListOptions) => Promise<View<Item> | undefined>;
|
|
59
|
+
/** Synchronous lookup: rebuilds a `View<Item>` from the cache without fetching. */
|
|
60
|
+
getView: (params: Params) => View<Item> | undefined;
|
|
61
|
+
/** Wraps an arbitrary promise in the store's pending + error lifecycle under `key`. */
|
|
62
|
+
track: <T>(key: ViewKey | Id, promise: Promise<T>) => Promise<T>;
|
|
63
|
+
/** Calls `createMutation`, inserts the item into `items`, and invalidates all views. */
|
|
64
|
+
create: (payload: CreatePayload) => Promise<Item>;
|
|
65
|
+
/** Calls `updateMutation` (tracked by `id`), replaces `items[id]`. */
|
|
66
|
+
update: (id: Id, patch: UpdatePatch) => Promise<Item>;
|
|
67
|
+
/** Calls `removeMutation` (tracked by `id`), deletes `items[id]`, removes id from every view. */
|
|
68
|
+
remove: (id: Id) => Promise<void>;
|
|
69
|
+
/** Clears every entry from `errors`. */
|
|
70
|
+
clearErrors: () => void;
|
|
71
|
+
/** Resets the store back to its initial empty state. */
|
|
72
|
+
reset: () => void;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Creates a typed store backed by the provided fetchers and mutations.
|
|
76
|
+
*
|
|
77
|
+
* The store has no global side effects — each call returns a fresh instance.
|
|
78
|
+
* To share state across components, wrap it in a Pinia store (or any other
|
|
79
|
+
* singleton pattern) and call `useStore(...)` once inside the setup.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* const store = useStore<string, Organization, OrgParams>({
|
|
84
|
+
* fetchOne: id => api.getOrganization(id),
|
|
85
|
+
* fetchPage: params => api.listOrganizations(params).then(toView),
|
|
86
|
+
* updateMutation: (id, patch) => api.patchOrganization(id, patch),
|
|
87
|
+
* })
|
|
88
|
+
*
|
|
89
|
+
* await store.list({ page: 1, limit: 20 })
|
|
90
|
+
* await store.update('org-42', { name: 'Acme Inc.' })
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
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>;
|
|
94
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
/**
|
|
3
|
+
* Invokes `callback` whenever the user clicks anywhere outside the element
|
|
4
|
+
* referenced by `target`. The lifecycle is bound to the calling component:
|
|
5
|
+
* the listener is attached on mount and removed on unmount.
|
|
6
|
+
*
|
|
7
|
+
* Typical use case: closing a dropdown / popover when the user clicks away.
|
|
8
|
+
*
|
|
9
|
+
* The listener uses the document's bubbling `click` event, which means:
|
|
10
|
+
* - clicks on the target itself or any of its descendants are ignored
|
|
11
|
+
* (no risk of toggling the popover when the user clicks inside it)
|
|
12
|
+
* - the callback runs synchronously after Vue has propagated DOM updates
|
|
13
|
+
*
|
|
14
|
+
* **Caveat**: if you toggle the visibility of the popover with a button
|
|
15
|
+
* **outside** the `target` (e.g. a trigger button placed next to the popover),
|
|
16
|
+
* the click on that button will fire `callback` — wrap the trigger + popover
|
|
17
|
+
* in a common parent and pass that parent as `target`.
|
|
18
|
+
*
|
|
19
|
+
* @param target - Vue ref pointing to the wrapper element. Pass the same ref
|
|
20
|
+
* you use in your template's `ref="…"` binding.
|
|
21
|
+
* @param callback - Function invoked on every outside click.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```vue
|
|
25
|
+
* <script setup>
|
|
26
|
+
* const popover = ref<HTMLElement | null>(null)
|
|
27
|
+
* const open = ref(false)
|
|
28
|
+
* useClickOutside(popover, () => { open.value = false })
|
|
29
|
+
* </script>
|
|
30
|
+
*
|
|
31
|
+
* <template>
|
|
32
|
+
* <div ref="popover">
|
|
33
|
+
* <button @click="open = !open">Toggle</button>
|
|
34
|
+
* <div v-if="open">...content...</div>
|
|
35
|
+
* </div>
|
|
36
|
+
* </template>
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function useClickOutside(target: Ref<HTMLElement | null>, callback: () => void): void;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
/**
|
|
3
|
+
* Factory returning a `imageCacheWatcherCallback` builder, used to defer
|
|
4
|
+
* "content ready" flags until every related image has actually been decoded
|
|
5
|
+
* by the browser — preventing the typical FOUC where a card pops in with
|
|
6
|
+
* text first, then its image a few hundred milliseconds later.
|
|
7
|
+
*
|
|
8
|
+
* The callback returned by the builder is meant to be passed to a Vue `watch`
|
|
9
|
+
* on the object whose images you want to gate on (`watchedObject`). It
|
|
10
|
+
* memoizes loaded URL sets via Nuxt `useState` so navigating back to the same
|
|
11
|
+
* content does not re-trigger the wait.
|
|
12
|
+
*
|
|
13
|
+
* Server-side rendering: the callback is replaced by a no-op that
|
|
14
|
+
* synchronously flips `areImagesLoaded` to `true`, so SSR markup is not
|
|
15
|
+
* blocked waiting for images that can't load on the server anyway.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const imageCacheWatcherCallback = useImageCacheWatcherCallback()
|
|
20
|
+
* const profile = ref<Profile | undefined>()
|
|
21
|
+
* const imagesLoaded = ref(false)
|
|
22
|
+
*
|
|
23
|
+
* const onProfileChanged = imageCacheWatcherCallback<Profile>(
|
|
24
|
+
* imagesLoaded,
|
|
25
|
+
* 'profile', // cache namespace
|
|
26
|
+
* ['avatar', 'cover'], // fields that hold image URLs (dot-paths supported)
|
|
27
|
+
* )
|
|
28
|
+
*
|
|
29
|
+
* watch(profile, onProfileChanged, { immediate: true })
|
|
30
|
+
*
|
|
31
|
+
* // Template: <ProfileCard v-show="imagesLoaded" :profile="profile" />
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function useImageCacheWatcherCallback(): <T extends Record<string, unknown>>(areImagesLoaded: Ref<boolean>, cacheKeyPrefix: string, fields: string[]) => (_watchedObject: T | undefined) => Promise<void>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactive helper that picks one of two numeric sizes based on the current
|
|
3
|
+
* window width — useful for inline numeric props (font sizes, icon sizes,
|
|
4
|
+
* column counts) that can't be driven by pure CSS media queries.
|
|
5
|
+
*
|
|
6
|
+
* Behavior:
|
|
7
|
+
* - On the **server** (SSR), the listener is never attached. `windowWidth`
|
|
8
|
+
* starts at `threshold - 1`, so `size` resolves to `smallSize`. This makes
|
|
9
|
+
* SSR output deterministic and mobile-first.
|
|
10
|
+
* - On the **client**, the window width is read on mount and re-read on every
|
|
11
|
+
* `resize` event. The listener is cleaned up on unmount.
|
|
12
|
+
*
|
|
13
|
+
* @param smallSize - Value to use when `window.innerWidth < threshold`.
|
|
14
|
+
* @param largeSize - Value to use when `window.innerWidth >= threshold`.
|
|
15
|
+
* @param threshold - Breakpoint in pixels (default `768`, the standard
|
|
16
|
+
* Tailwind / Bootstrap "md" boundary).
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* // Icon size: 16px on mobile, 24px on tablet+
|
|
21
|
+
* const { size } = useResponsiveSize(16, 24)
|
|
22
|
+
*
|
|
23
|
+
* // Custom breakpoint at 1024px
|
|
24
|
+
* const { size: cols } = useResponsiveSize(1, 3, 1024)
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @returns An object exposing the picked `size` and the raw `windowWidth`
|
|
28
|
+
* (handy when the caller needs to react to width in multiple places).
|
|
29
|
+
*/
|
|
30
|
+
export declare function useResponsiveSize(smallSize: number, largeSize: number, threshold?: number): {
|
|
31
|
+
size: import('vue').ComputedRef<number>;
|
|
32
|
+
windowWidth: import('vue').Ref<number, number>;
|
|
33
|
+
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@choosemycompany/lib",
|
|
3
3
|
"description": "Shared composables, stores and types for ChooseMyCompany front apps",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
7
|
+
"packageManager": "pnpm@10.17.0",
|
|
7
8
|
"sideEffects": false,
|
|
8
9
|
"files": [
|
|
9
10
|
"dist"
|
|
@@ -17,6 +18,16 @@
|
|
|
17
18
|
"types": "./dist/index.d.ts"
|
|
18
19
|
}
|
|
19
20
|
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "vite build",
|
|
23
|
+
"watch": "vite build --watch",
|
|
24
|
+
"clean": "rm -rf dist",
|
|
25
|
+
"prepublishOnly": "pnpm run build",
|
|
26
|
+
"lint": "eslint .",
|
|
27
|
+
"lint:fix": "eslint . --fix",
|
|
28
|
+
"typecheck": "tsc --noEmit",
|
|
29
|
+
"test": "vitest run"
|
|
30
|
+
},
|
|
20
31
|
"peerDependencies": {
|
|
21
32
|
"@nuxtjs/i18n": "^10.0.0",
|
|
22
33
|
"h3": "^1.0.0",
|
|
@@ -49,14 +60,5 @@
|
|
|
49
60
|
"vue": "^3.5.22",
|
|
50
61
|
"vue-i18n": "^10.0.0",
|
|
51
62
|
"vue-router": "^4.5.1"
|
|
52
|
-
},
|
|
53
|
-
"scripts": {
|
|
54
|
-
"build": "vite build",
|
|
55
|
-
"watch": "vite build --watch",
|
|
56
|
-
"clean": "rm -rf dist",
|
|
57
|
-
"lint": "eslint .",
|
|
58
|
-
"lint:fix": "eslint . --fix",
|
|
59
|
-
"typecheck": "tsc --noEmit",
|
|
60
|
-
"test": "vitest run"
|
|
61
63
|
}
|
|
62
|
-
}
|
|
64
|
+
}
|
package/dist/types/errors.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export type ErrorItem = {
|
|
2
|
-
field: string;
|
|
3
|
-
message: string;
|
|
4
|
-
detail?: string;
|
|
5
|
-
};
|
|
6
|
-
export type MandeErrorBody = {
|
|
7
|
-
errors?: Array<{
|
|
8
|
-
message?: string;
|
|
9
|
-
error?: string;
|
|
10
|
-
field?: string;
|
|
11
|
-
}>;
|
|
12
|
-
};
|
|
13
|
-
export type MandeError = Error & {
|
|
14
|
-
body?: MandeErrorBody;
|
|
15
|
-
};
|
package/dist/types/index.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export type { StoreOptions, RetrieveOptions, ListOptions } from './store';
|
|
2
|
-
export type { ViewKeyParams, View, ViewOptions } from './view';
|
|
3
|
-
export type { PaginatedQuery, Pagination } from './pagination';
|
|
4
|
-
export type { ListResponse, RetrieveResponse } from './response';
|
|
5
|
-
export type { RequestOptions } from './request';
|
|
6
|
-
export type { ErrorItem, MandeError, MandeErrorBody } from './errors';
|
|
7
|
-
export type { Language, Translation } from './language';
|
|
8
|
-
export type { InternationalizedRoute } from './routes';
|
package/dist/types/language.d.ts
DELETED
package/dist/types/request.d.ts
DELETED
package/dist/types/response.d.ts
DELETED
package/dist/types/routes.d.ts
DELETED
package/dist/types/store.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { View, ViewKeyParams } from './view';
|
|
2
|
-
export type StoreOptions<Id, Item, Params extends ViewKeyParams> = {
|
|
3
|
-
params?: Params;
|
|
4
|
-
getItemId: (item: Item) => Id;
|
|
5
|
-
fetchOne?: (id: Id) => Promise<Item>;
|
|
6
|
-
fetchPage?: (params: Params) => Promise<View<Item>>;
|
|
7
|
-
enrichItem?: (item: Item) => Item;
|
|
8
|
-
ttlMs?: number;
|
|
9
|
-
};
|
|
10
|
-
export type RetrieveOptions = {
|
|
11
|
-
force?: boolean;
|
|
12
|
-
};
|
|
13
|
-
export type ListOptions = {
|
|
14
|
-
force?: boolean;
|
|
15
|
-
};
|
package/dist/types/view.d.ts
DELETED
package/dist/useHttp.d.ts
DELETED
package/dist/useStore.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Ref } from 'vue';
|
|
2
|
-
import { ListOptions, RetrieveOptions, StoreOptions, View, ViewKeyParams } from './types';
|
|
3
|
-
type ViewKey = string;
|
|
4
|
-
type StoreItems<Id, Item> = Record<Id & (string | number), Item>;
|
|
5
|
-
type StoreViews<Item> = Record<ViewKey, View<Item>>;
|
|
6
|
-
type StoreErrors = unknown[];
|
|
7
|
-
export declare function getViewKey(params: ViewKeyParams): ViewKey;
|
|
8
|
-
export type Store<Id, Item, Params extends ViewKeyParams, FilterItem = unknown> = {
|
|
9
|
-
items: Ref<StoreItems<Id, Item>>;
|
|
10
|
-
views: Ref<StoreViews<Item>>;
|
|
11
|
-
errors: Ref<StoreErrors>;
|
|
12
|
-
search: Ref<string>;
|
|
13
|
-
filters: Ref<FilterItem[]>;
|
|
14
|
-
isPending: (key: ViewKey | Id & (string | number)) => boolean;
|
|
15
|
-
retrieve: (id: Id, retrieveOptions?: RetrieveOptions) => Promise<Item | undefined>;
|
|
16
|
-
list: (params: Params, listOptions?: ListOptions) => Promise<void>;
|
|
17
|
-
reset: () => void;
|
|
18
|
-
};
|
|
19
|
-
export declare function useStore<Id, Item, Params extends ViewKeyParams, FilterItem = unknown>(options: StoreOptions<Id, Item, Params>): Store<Id, Item, Params, FilterItem>;
|
|
20
|
-
export {};
|
package/dist/useTranslate.d.ts
DELETED
package/dist/useUrlHelpers.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export declare function useUrlHelpers(): {
|
|
2
|
-
getCoverUrl: (url: string) => string;
|
|
3
|
-
getLogoUrl: (url: string) => string;
|
|
4
|
-
getCorporateUrl: (url: string) => string;
|
|
5
|
-
getAssetsUrl: (url: string) => string;
|
|
6
|
-
getSiteUrl: (url: string) => string;
|
|
7
|
-
getHomeUrl: (homeName?: string) => string | undefined;
|
|
8
|
-
};
|