@choosemycompany/lib 0.1.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.
- 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 +119 -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 +89 -0
- package/dist/store/useStore.d.ts +89 -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
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ pnpm add @choosemycompany/lib
|
|
|
11
11
|
## Utilisation
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
import { useHttp, useStore,
|
|
14
|
+
import { useHttp, useStore, type Store, type View } from '@choosemycompany/lib'
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
Un seul entry point, tout est ré-exporté depuis `@choosemycompany/lib`.
|
|
@@ -23,9 +23,8 @@ Le package s'attend à être consommé **dans un projet Nuxt 4** avec les peer d
|
|
|
23
23
|
- `vue`, `vue-i18n`, `vue-router`, `pinia`
|
|
24
24
|
- `mande` (client HTTP)
|
|
25
25
|
- `h3` (pour `useErrorHandling`)
|
|
26
|
-
- `ufo` (pour `useUrlHelpers`)
|
|
27
26
|
- `@nuxtjs/i18n` (`useLocalePath`)
|
|
28
|
-
- `nuxt` (pour
|
|
27
|
+
- `nuxt` (pour `useRuntimeConfig`, `useState`, `showError` importés depuis `nuxt/app`)
|
|
29
28
|
|
|
30
29
|
Les composables utilisant `useRuntimeConfig()`, `useRoute()`, `useI18n()` doivent être appelés dans un scope Nuxt (setup, plugin, middleware).
|
|
31
30
|
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Augments the global `window` with the (optional) `gtag` function injected
|
|
3
|
+
* by the Google Tag Manager / Global Site Tag snippet. The function is
|
|
4
|
+
* optional — we tolerate its absence at runtime.
|
|
5
|
+
*/
|
|
6
|
+
declare global {
|
|
7
|
+
interface Window {
|
|
8
|
+
gtag?: (...args: unknown[]) => void;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Exposes `reportConversion()` — fires a Google Ads "conversion" event using
|
|
13
|
+
* the gtag ID from `runtimeConfig.public.gtagId` and the package-level
|
|
14
|
+
* conversion label.
|
|
15
|
+
*
|
|
16
|
+
* The function is a **safe no-op** in three situations:
|
|
17
|
+
* - running on the server (no `window`)
|
|
18
|
+
* - the gtag snippet is not loaded (`window.gtag` undefined)
|
|
19
|
+
* - `gtagId` is empty in `runtimeConfig` (e.g. preview / staging environments)
|
|
20
|
+
*
|
|
21
|
+
* That way consumer code can call `reportConversion()` unconditionally without
|
|
22
|
+
* branching for the environment.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const { reportConversion } = useGtagConversion()
|
|
27
|
+
*
|
|
28
|
+
* async function submitForm() {
|
|
29
|
+
* await sendLead(form.value)
|
|
30
|
+
* reportConversion() // tracked in Google Ads
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function useGtagConversion(): {
|
|
35
|
+
reportConversion: () => void;
|
|
36
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the contact-page URL on the CMC HubSpot site, localized to match
|
|
3
|
+
* the current Vue I18n locale.
|
|
4
|
+
*
|
|
5
|
+
* URL pattern:
|
|
6
|
+
* - **French** (default locale on HubSpot) → `${BASE}/contact`
|
|
7
|
+
* - other locales → `${BASE}/{locale}/contact`
|
|
8
|
+
*
|
|
9
|
+
* Reactive: the URL recomputes automatically when the locale changes, so it
|
|
10
|
+
* is safe to bind directly to `:href` in templates.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```vue
|
|
14
|
+
* <script setup>
|
|
15
|
+
* const { contactUrl } = useHubspotContactUrl()
|
|
16
|
+
* </script>
|
|
17
|
+
*
|
|
18
|
+
* <template>
|
|
19
|
+
* <a :href="contactUrl">Contact us</a>
|
|
20
|
+
* </template>
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function useHubspotContactUrl(): {
|
|
24
|
+
contactUrl: import('vue').ComputedRef<string>;
|
|
25
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convenience shorthand to **both** render Nuxt's full-page error overlay
|
|
3
|
+
* **and** throw — in one call.
|
|
4
|
+
*
|
|
5
|
+
* Equivalent to:
|
|
6
|
+
* ```ts
|
|
7
|
+
* const err = createError({ statusCode, statusMessage, message, data })
|
|
8
|
+
* showError(err)
|
|
9
|
+
* throw err
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* The return type is `never` so TypeScript narrows the calling site as
|
|
13
|
+
* unreachable past the call — handy in guard clauses (`if (!user) showAndThrowError(...)`).
|
|
14
|
+
*
|
|
15
|
+
* @param params.code - HTTP-style status code to set on the Nuxt error.
|
|
16
|
+
* @param params.message - Optional human-readable message; defaults to a generic fallback.
|
|
17
|
+
* @param params.data - Optional structured payload attached to the error
|
|
18
|
+
* (renderable by a custom error page or accessible via
|
|
19
|
+
* `useError()`).
|
|
20
|
+
*
|
|
21
|
+
* @throws Always — never returns.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const user = await fetchUser(id)
|
|
26
|
+
* if (!user) {
|
|
27
|
+
* showAndThrowError({ code: 404, message: `User ${id} not found` })
|
|
28
|
+
* }
|
|
29
|
+
* // TypeScript now knows `user` is non-null below this line.
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare const showAndThrowError: ({ code, message, data }: {
|
|
33
|
+
code: number;
|
|
34
|
+
message?: string;
|
|
35
|
+
data?: unknown;
|
|
36
|
+
}) => never;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared HTTP types modeling the CMC API contract.
|
|
3
|
+
*
|
|
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
|
+
*
|
|
8
|
+
* These types are independent of any HTTP client — `useHttp` happens to use
|
|
9
|
+
* `mande`, but consumers can adapt them to any fetcher.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Per-request options accepted by composables that delegate to a CMC fetcher.
|
|
13
|
+
*
|
|
14
|
+
* - `params.signal` — `AbortSignal` to cancel the in-flight request.
|
|
15
|
+
* - `filters` — query-string filter map (key/value pairs flattened by the caller).
|
|
16
|
+
* - `headers` — extra headers merged into the request.
|
|
17
|
+
*/
|
|
18
|
+
export interface RequestOptions {
|
|
19
|
+
params?: {
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
};
|
|
22
|
+
filters?: Record<string, unknown>;
|
|
23
|
+
headers?: Record<string, string>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Standard pagination query parameters accepted by every paginated CMC endpoint.
|
|
27
|
+
*
|
|
28
|
+
* Both fields are optional — backends apply their own defaults (typically
|
|
29
|
+
* `page = 1`, `limit = 20`).
|
|
30
|
+
*/
|
|
31
|
+
export type PaginatedQuery = {
|
|
32
|
+
/** 1-based page index. */
|
|
33
|
+
page?: number;
|
|
34
|
+
/** Number of items per page. */
|
|
35
|
+
limit?: number;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Pagination metadata returned alongside a paginated payload.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* // total pages: 5, current: 2 → previous = 1, next = 3
|
|
43
|
+
* { total: 5, previous: 1, next: 3, totalItems: 100, current: 2, limit: 20 }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export type Pagination = {
|
|
47
|
+
/** Total number of pages. */
|
|
48
|
+
total: number;
|
|
49
|
+
/** Previous page number, or `0` if on the first page. */
|
|
50
|
+
previous: number;
|
|
51
|
+
/** Next page number, or `0` if on the last page. */
|
|
52
|
+
next: number;
|
|
53
|
+
/** Total number of items across all pages. */
|
|
54
|
+
totalItems: number;
|
|
55
|
+
/** Current page number (1-based). */
|
|
56
|
+
current: number;
|
|
57
|
+
/** Items per page used for the current response. */
|
|
58
|
+
limit: number;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Standard CMC list response envelope.
|
|
62
|
+
*
|
|
63
|
+
* @typeParam T - Shape of an individual list item.
|
|
64
|
+
*/
|
|
65
|
+
export type ListResponse<T> = {
|
|
66
|
+
data: T[];
|
|
67
|
+
pagination: Pagination;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
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`.
|
|
74
|
+
*
|
|
75
|
+
* @typeParam T - Shape of the returned resource.
|
|
76
|
+
*/
|
|
77
|
+
export type RetrieveResponse<T> = {
|
|
78
|
+
item: T;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Field-level validation error item, as returned in `MandeErrorBody.errors`.
|
|
82
|
+
*/
|
|
83
|
+
export type ErrorItem = {
|
|
84
|
+
/** Name of the offending field. */
|
|
85
|
+
field: string;
|
|
86
|
+
/** Human-readable error message. */
|
|
87
|
+
message: string;
|
|
88
|
+
/** Optional machine-readable error code or extra context. */
|
|
89
|
+
detail?: string;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Body shape attached to a `MandeError` when the backend returns a structured
|
|
93
|
+
* 4xx/5xx payload. Most fields are optional because legacy endpoints don't
|
|
94
|
+
* always conform.
|
|
95
|
+
*/
|
|
96
|
+
export type MandeErrorBody = {
|
|
97
|
+
errors?: Array<{
|
|
98
|
+
message?: string;
|
|
99
|
+
error?: string;
|
|
100
|
+
field?: string;
|
|
101
|
+
}>;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* `mande`'s `Error` type augmented with the parsed response body. Useful for
|
|
105
|
+
* narrowing `catch (e)` blocks when the caller wants to read field-level errors.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* try {
|
|
110
|
+
* await http.post('/auth/login', credentials)
|
|
111
|
+
* } catch (e) {
|
|
112
|
+
* const err = e as MandeError
|
|
113
|
+
* const fields = err.body?.errors ?? []
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export type MandeError = Error & {
|
|
118
|
+
body?: MandeErrorBody;
|
|
119
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MandeInstance } from 'mande';
|
|
2
|
+
/**
|
|
3
|
+
* Returns a `mande` instance pre-configured against the CMC API base URL,
|
|
4
|
+
* along with two helpers to attach/detach the `Authorization` bearer token.
|
|
5
|
+
*
|
|
6
|
+
* The returned `http` instance exposes the standard `mande` verbs
|
|
7
|
+
* (`get`, `post`, `put`, `patch`, `delete`) and is meant to be the only
|
|
8
|
+
* way the lib (and downstream apps) talk to the CMC backend.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const { http, setToken, clearToken } = useHttp()
|
|
13
|
+
*
|
|
14
|
+
* setToken(accessToken)
|
|
15
|
+
* const { item } = await http.get<RetrieveResponse<User>>('/me')
|
|
16
|
+
* clearToken() // on logout
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function useHttp(): {
|
|
20
|
+
http: MandeInstance;
|
|
21
|
+
setToken: (token: string) => void;
|
|
22
|
+
clearToken: () => void;
|
|
23
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public i18n types shared by every composable in this folder. Reflect the
|
|
3
|
+
* languages actually supported by ChooseMyCompany apps as of today; add a new
|
|
4
|
+
* locale here and downstream code (translations maps, country dictionaries,
|
|
5
|
+
* etc.) will get a compile-time error to keep them in sync.
|
|
6
|
+
*/
|
|
7
|
+
/** Languages currently shipped by ChooseMyCompany apps (ISO 639-1 codes). */
|
|
8
|
+
export type Language = 'en' | 'fr' | 'de' | 'es' | 'it' | 'da' | 'pt';
|
|
9
|
+
/**
|
|
10
|
+
* A translation bundle: a partial map from `Language` to its translated string.
|
|
11
|
+
* Partial because most content is only translated into a subset of languages
|
|
12
|
+
* — see {@link Language} for the full set.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const greeting: Translation = { fr: 'Bonjour', en: 'Hello' }
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export type Translation = Partial<Record<Language, string>>;
|
|
20
|
+
/**
|
|
21
|
+
* Localized route definition consumed by Nuxt i18n. Each language gets its
|
|
22
|
+
* own URL path, but the underlying component file is shared.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const route: InternationalizedRoute = {
|
|
27
|
+
* name: 'about',
|
|
28
|
+
* paths: { fr: '/a-propos', en: '/about', de: '/uber-uns', ... },
|
|
29
|
+
* file: 'AboutPage.vue',
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export type InternationalizedRoute = {
|
|
34
|
+
name: string;
|
|
35
|
+
paths: Record<Language, string>;
|
|
36
|
+
file: `${string}.vue`;
|
|
37
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a `translateCountryName(code, lang?)` function that resolves an
|
|
3
|
+
* ISO 3166-1 alpha-2 country code to its localized country name.
|
|
4
|
+
*
|
|
5
|
+
* Resolution order:
|
|
6
|
+
* 1. caller-provided `lang` (when truthy and present in the country entry)
|
|
7
|
+
* 2. current Vue I18n locale
|
|
8
|
+
* 3. English fallback (`en`), when the current locale isn't already `en`
|
|
9
|
+
*
|
|
10
|
+
* If none of these matches, throws — countries lookup is a low-level helper and
|
|
11
|
+
* we'd rather surface a missing locale loudly than silently render an empty cell.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const translateCountryName = useCountriesTranslation()
|
|
16
|
+
*
|
|
17
|
+
* // current locale: 'fr'
|
|
18
|
+
* translateCountryName('FR') // "France"
|
|
19
|
+
* translateCountryName('us') // case-insensitive: "États-Unis"
|
|
20
|
+
* translateCountryName('FR', 'en') // explicit override: "France"
|
|
21
|
+
* translateCountryName('ZZ') // throws: Unknown country code
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @throws if the country code is unknown, or if no locale (current + 'en') has
|
|
25
|
+
* a name for the country.
|
|
26
|
+
*/
|
|
27
|
+
export declare function useCountriesTranslation(): (code: string, lang?: string | null) => string;
|
|
28
|
+
/**
|
|
29
|
+
* Union of every country code supported by {@link useCountriesTranslation}.
|
|
30
|
+
* Use this as a type constraint to forbid arbitrary strings where a real
|
|
31
|
+
* ISO 3166-1 alpha-2 country code is expected.
|
|
32
|
+
*/
|
|
33
|
+
export type CountryCodes = keyof typeof COUNTRIES;
|
|
34
|
+
/**
|
|
35
|
+
* Static dictionary of ISO 3166-1 alpha-2 country codes mapped to their
|
|
36
|
+
* localized names. Translations available: `en`, `fr`, `es`, `de`, `it`,
|
|
37
|
+
* `pt`, `da`. Not all entries cover every locale — the resolver in
|
|
38
|
+
* {@link useCountriesTranslation} falls back to `en` when needed.
|
|
39
|
+
*
|
|
40
|
+
* Special entry: `INT` ("International") is a CMC convention for content
|
|
41
|
+
* not tied to a specific country, not a real ISO code.
|
|
42
|
+
*/
|
|
43
|
+
declare const COUNTRIES: Record<string, {
|
|
44
|
+
[key: string]: string;
|
|
45
|
+
}>;
|
|
46
|
+
export {};
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { CountryCodes } from './useCountriesTranslation';
|
|
2
|
+
/** Region code accepted by {@link useLocaleHelpers}. */
|
|
2
3
|
type RegionCode = CountryCodes | string;
|
|
4
|
+
/**
|
|
5
|
+
* Locale-aware helpers that don't fit into the per-piece-of-content translators
|
|
6
|
+
* ({@link useTranslate}, {@link useCountriesTranslation}).
|
|
7
|
+
*/
|
|
3
8
|
export declare function useLocaleHelpers(): {
|
|
4
9
|
getRegionFromLocale: (locale: string, availableRegions: RegionCode[]) => CountryCodes | undefined;
|
|
5
10
|
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a `parse(key, tags)` helper that splits a `vue-i18n` translation
|
|
3
|
+
* into typed segments tagged by inline markup.
|
|
4
|
+
*
|
|
5
|
+
* Use this when a single translation string needs to render as a mix of plain
|
|
6
|
+
* text and Vue components (link, bold, icon, …) without splitting the
|
|
7
|
+
* sentence into multiple keys — keeping the full grammar in the translators'
|
|
8
|
+
* hands.
|
|
9
|
+
*
|
|
10
|
+
* Markup format: `[tag]content[/tag]`. The list of recognized `tags` is
|
|
11
|
+
* provided by the caller; anything else is left untouched.
|
|
12
|
+
*
|
|
13
|
+
* Output: an array of `{ text, [tag]: boolean }` segments where exactly one
|
|
14
|
+
* tag flag (or none, for plain runs) is `true`. Empty matches are skipped.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```vue
|
|
18
|
+
* <!-- locale: "Click [link]here[/link] to [bold]continue[/bold]." -->
|
|
19
|
+
* <script setup>
|
|
20
|
+
* const { parse } = useParseTranslation()
|
|
21
|
+
* const parts = parse('cta', ['link', 'bold'])
|
|
22
|
+
* // [
|
|
23
|
+
* // { text: 'Click ', link: false, bold: false },
|
|
24
|
+
* // { text: 'here', link: true, bold: false },
|
|
25
|
+
* // { text: ' to ', link: false, bold: false },
|
|
26
|
+
* // { text: 'continue', link: false, bold: true },
|
|
27
|
+
* // { text: '.', link: false, bold: false },
|
|
28
|
+
* // ]
|
|
29
|
+
* </script>
|
|
30
|
+
*
|
|
31
|
+
* <template>
|
|
32
|
+
* <template v-for="(p, i) in parts" :key="i">
|
|
33
|
+
* <a v-if="p.link" href="…">{{ p.text }}</a>
|
|
34
|
+
* <strong v-else-if="p.bold">{{ p.text }}</strong>
|
|
35
|
+
* <span v-else>{{ p.text }}</span>
|
|
36
|
+
* </template>
|
|
37
|
+
* </template>
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function useParseTranslation(): {
|
|
41
|
+
parse: (key: string, tags: string[]) => Array<{
|
|
42
|
+
text: string;
|
|
43
|
+
[key: string]: unknown;
|
|
44
|
+
}>;
|
|
45
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a `getPrimaryTypeTranslation(baseKey, primaryType)` helper for
|
|
3
|
+
* resolving copy that varies per **primary type** (a CMC concept: the kind of
|
|
4
|
+
* entity a page is about — `school`, `company`, `agency`, …).
|
|
5
|
+
*
|
|
6
|
+
* Translators authoring locale files have three ways to supply a string for a
|
|
7
|
+
* given `baseKey` + `primaryType` pair; the helper tries them in order:
|
|
8
|
+
*
|
|
9
|
+
* 1. **Specific key** — `{baseKey}.{primaryType}` (e.g. `cta.subscribe.school`)
|
|
10
|
+
* 2. **Per-type fallback** — `{baseKey}.{primaryTypes[0]}` (the first declared
|
|
11
|
+
* primary type acts as the default copy when a specific override is missing)
|
|
12
|
+
* 3. **Generic key with interpolation** — `{baseKey}` rendered with a
|
|
13
|
+
* `{primaryType}` placeholder filled by the lowercased translation of
|
|
14
|
+
* `common.primary-type.{primaryType}`
|
|
15
|
+
*
|
|
16
|
+
* Returns `""` if all three strategies miss.
|
|
17
|
+
*
|
|
18
|
+
* @typeParam T - Tuple/array of accepted primary type literals.
|
|
19
|
+
* @param primaryTypes - Authoritative list of supported primary types. The
|
|
20
|
+
* **first** entry is the per-type fallback (strategy #2).
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const PRIMARY_TYPES = ['school', 'company', 'agency'] as const
|
|
25
|
+
* const { getPrimaryTypeTranslation } = usePrimaryTypeTranslation(PRIMARY_TYPES)
|
|
26
|
+
*
|
|
27
|
+
* // locale: { cta: { subscribe: { school: 'Join the school', company: 'Join the company' } } }
|
|
28
|
+
* getPrimaryTypeTranslation('cta.subscribe', 'school') // → 'Join the school'
|
|
29
|
+
* getPrimaryTypeTranslation('cta.subscribe', 'agency') // → falls back to school's copy
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare function usePrimaryTypeTranslation<T extends string>(primaryTypes: readonly T[]): {
|
|
33
|
+
getPrimaryTypeTranslation: (baseKey: string, primaryType: T) => string;
|
|
34
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
type Language = string;
|
|
2
|
+
type Translation = Record<string, string>;
|
|
3
|
+
/**
|
|
4
|
+
* Returns a `translate(translations, fallback?)` function that picks the right
|
|
5
|
+
* string out of a `Translation` map using the **current Vue I18n locale**.
|
|
6
|
+
*
|
|
7
|
+
* Unlike `vue-i18n`'s own `t()`, this is meant for **content data** translations
|
|
8
|
+
* (a strings map carried along with a domain object) rather than UI labels
|
|
9
|
+
* declared up-front in locale files.
|
|
10
|
+
*
|
|
11
|
+
* Resolution order: current locale → caller-provided `fallback` (defaults to
|
|
12
|
+
* `'fr'`) → empty string.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const translate = useTranslate()
|
|
17
|
+
*
|
|
18
|
+
* // Current locale 'en':
|
|
19
|
+
* translate({ fr: 'Bonjour', en: 'Hello' }) // → "Hello"
|
|
20
|
+
*
|
|
21
|
+
* // Current locale 'de' (not in the map), fallback 'en':
|
|
22
|
+
* translate({ fr: 'Bonjour', en: 'Hello' }, 'en') // → "Hello"
|
|
23
|
+
*
|
|
24
|
+
* // Current locale 'de', no fallback match either:
|
|
25
|
+
* translate({ ja: 'こんにちは' }) // → ""
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @returns A reactive `translate` function — using it inside a `computed`
|
|
29
|
+
* re-runs when the locale changes.
|
|
30
|
+
*/
|
|
31
|
+
export declare function useTranslate(): (translations: Translation, fallback?: Language) => string;
|
|
32
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
/**
|
|
2
|
+
* `@choosemycompany/lib` — shared Vue 3 / Nuxt 4 composables for ChooseMyCompany apps.
|
|
3
|
+
*
|
|
4
|
+
* This module is the only public entry point of the package. Anything not
|
|
5
|
+
* re-exported here is considered internal and may change without notice.
|
|
6
|
+
*
|
|
7
|
+
* Grouped by concern:
|
|
8
|
+
* - **HTTP** — {@link useHttp}: thin `mande` wrapper bound to the CMC API base URL.
|
|
9
|
+
* - **Store** — {@link useStore}, {@link getViewKey}: typed cache for `retrieve` /
|
|
10
|
+
* `list` workflows with single-flight, per-key errors, and CRUD mutations.
|
|
11
|
+
* - **i18n** — locale-aware translators for plain strings, country names, primary
|
|
12
|
+
* types, region resolution, and rich tagged messages.
|
|
13
|
+
* - **UI** — small DOM / window-aware helpers: outside-click detection, responsive
|
|
14
|
+
* sizing, image-cache watcher.
|
|
15
|
+
* - **Errors** — {@link showAndThrowError}: Nuxt error trigger shorthand.
|
|
16
|
+
* - **Analytics** — gtag conversion + Hubspot contact URL helpers.
|
|
17
|
+
*
|
|
18
|
+
* @packageDocumentation
|
|
19
|
+
*/
|
|
20
|
+
export { useHttp } from './http/useHttp';
|
|
21
|
+
export { useStore, getViewKey, type Store } from './store/useStore';
|
|
22
|
+
export { useClickOutside } from './ui/useClickOutside';
|
|
23
|
+
export { useCountriesTranslation, type CountryCodes } from './i18n/useCountriesTranslation';
|
|
24
|
+
export { showAndThrowError } from './errors/useErrorHandling';
|
|
25
|
+
export { useGtagConversion } from './analytics/useGtagConversion';
|
|
26
|
+
export { useHubspotContactUrl } from './analytics/useHubspotContactUrl';
|
|
27
|
+
export { useImageCacheWatcherCallback } from './ui/useImageCacheWatcherCallback';
|
|
28
|
+
export { useLocaleHelpers } from './i18n/useLocaleHelpers';
|
|
29
|
+
export { useParseTranslation } from './i18n/useParseTranslation';
|
|
30
|
+
export { usePrimaryTypeTranslation } from './i18n/usePrimaryTypeTranslation';
|
|
31
|
+
export { useResponsiveSize } from './ui/useResponsiveSize';
|
|
32
|
+
export { useTranslate } from './i18n/useTranslate';
|
|
33
|
+
export type { StoreOptions, RetrieveOptions, ListOptions, ViewKeyParams, View, ViewOptions, } from './store/types';
|
|
34
|
+
export type { PaginatedQuery, Pagination, ListResponse, RetrieveResponse, RequestOptions, ErrorItem, MandeError, MandeErrorBody, } from './http/types';
|
|
35
|
+
export type { Language, Translation, InternationalizedRoute, } from './i18n/types';
|