@i18n-micro/path-strategy 1.0.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.
Files changed (37) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +196 -0
  3. package/dist/base-strategy-B5mBf3XX.cjs +2 -0
  4. package/dist/base-strategy-B5mBf3XX.cjs.map +1 -0
  5. package/dist/base-strategy-DVqe8ehd.js +790 -0
  6. package/dist/base-strategy-DVqe8ehd.js.map +1 -0
  7. package/dist/index.cjs +2 -0
  8. package/dist/index.cjs.map +1 -0
  9. package/dist/index.d.ts +394 -0
  10. package/dist/index.mjs +45 -0
  11. package/dist/index.mjs.map +1 -0
  12. package/dist/no-prefix-strategy.cjs +2 -0
  13. package/dist/no-prefix-strategy.cjs.map +1 -0
  14. package/dist/no-prefix-strategy.d.ts +283 -0
  15. package/dist/no-prefix-strategy.mjs +40 -0
  16. package/dist/no-prefix-strategy.mjs.map +1 -0
  17. package/dist/prefix-and-default-strategy.cjs +2 -0
  18. package/dist/prefix-and-default-strategy.cjs.map +1 -0
  19. package/dist/prefix-and-default-strategy.d.ts +284 -0
  20. package/dist/prefix-and-default-strategy.mjs +129 -0
  21. package/dist/prefix-and-default-strategy.mjs.map +1 -0
  22. package/dist/prefix-except-default-strategy.cjs +2 -0
  23. package/dist/prefix-except-default-strategy.cjs.map +1 -0
  24. package/dist/prefix-except-default-strategy.d.ts +298 -0
  25. package/dist/prefix-except-default-strategy.mjs +245 -0
  26. package/dist/prefix-except-default-strategy.mjs.map +1 -0
  27. package/dist/prefix-strategy.cjs +2 -0
  28. package/dist/prefix-strategy.cjs.map +1 -0
  29. package/dist/prefix-strategy.d.ts +277 -0
  30. package/dist/prefix-strategy.mjs +40 -0
  31. package/dist/prefix-strategy.mjs.map +1 -0
  32. package/dist/types.cjs +2 -0
  33. package/dist/types.cjs.map +1 -0
  34. package/dist/types.d.ts +108 -0
  35. package/dist/types.mjs +2 -0
  36. package/dist/types.mjs.map +1 -0
  37. package/package.json +84 -0
@@ -0,0 +1,277 @@
1
+ import { I18nRouteParams } from '@i18n-micro/types';
2
+ import { Locale } from '@i18n-micro/types';
3
+ import { Strategies } from '@i18n-micro/types';
4
+
5
+ declare abstract class BasePathStrategy implements PathStrategy {
6
+ protected ctx: PathStrategyContext;
7
+ protected resolver: RouteResolver;
8
+ constructor(ctx: PathStrategyContext);
9
+ setRouter(router: RouterAdapter): void;
10
+ getDefaultLocale(): string;
11
+ getLocales(): Locale[];
12
+ getStrategy(): PathStrategyContext['strategy'];
13
+ getLocalizedRouteNamePrefix(): string;
14
+ getGlobalLocaleRoutes(): PathStrategyContext['globalLocaleRoutes'];
15
+ getRouteLocales(): PathStrategyContext['routeLocales'];
16
+ getRoutesLocaleLinks(): PathStrategyContext['routesLocaleLinks'];
17
+ getNoPrefixRedirect(): boolean | undefined;
18
+ /** Strips localization prefix/suffix and returns the "base" route name for one locale. */
19
+ protected getBaseRouteName(route: RouteLike, locale: string): string | null;
20
+ /** Returns the base route name (without localized prefix/suffix) by trying all locales. */
21
+ getRouteBaseName(route: RouteLike): string | null;
22
+ /** Resolves target path for a locale, checking globalLocaleRoutes first. */
23
+ protected resolvePathForLocale(path: string, targetLocale: string): string;
24
+ protected buildLocalizedName(baseName: string, locale: string): string;
25
+ /** Builds path for target locale (strategy decides: with or without prefix). */
26
+ protected abstract buildLocalizedPath(path: string, locale: string, isCustom: boolean): string;
27
+ /** Builds localized route name for target locale. */
28
+ protected abstract buildLocalizedRouteName(baseName: string, locale: string): string;
29
+ protected getLocaleObject(code: string): Locale | undefined;
30
+ protected applyBaseUrl(localeCode: string, route: RouteLike | string): RouteLike | string;
31
+ /**
32
+ * Merges target route (strategy result) with query and hash from source route.
33
+ * Returns normalized RouteLike object.
34
+ */
35
+ protected preserveQueryAndHash(target: RouteLike | string, source?: RouteLike | null): RouteLike | string;
36
+ protected resolvePathWithParams(path: string, params?: Record<string, unknown>): string;
37
+ protected getPathWithoutLocaleAndBaseName(route: ResolvedRouteLike): {
38
+ pathWithoutLocale: string;
39
+ baseRouteName: string | null;
40
+ };
41
+ /** Look up custom path segment for targetLocale in globalLocaleRoutes. */
42
+ protected getCustomPathSegment(route: ResolvedRouteLike, targetLocale: string): string | null;
43
+ protected getAllowedLocalesForRoute(route: ResolvedRouteLike): string[];
44
+ getCanonicalPath(_route: ResolvedRouteLike, _targetLocale: string): string | null;
45
+ protected getPathForUnlocalizedRouteByName(routeName: string): string | null;
46
+ /**
47
+ * Try to resolve route by localized name. Returns RouteLike with query/hash from sourceRoute to preserve them.
48
+ */
49
+ protected tryResolveByLocalizedName(routeName: string, targetLocale: string, sourceRoute?: RouteLike): RouteLike | null;
50
+ /**
51
+ * Try to resolve route by localized name with params. Returns RouteLike to preserve query/hash.
52
+ */
53
+ protected tryResolveByLocalizedNameWithParams(routeName: string, targetLocale: string, params: Record<string, unknown>, sourceRoute?: RouteLike): RouteLike | null;
54
+ protected getPathForUnlocalizedRoute(route: ResolvedRouteLike): string | null;
55
+ /**
56
+ * Builds localized path from baseName + params when router does not have the route.
57
+ * Tries two conventions:
58
+ * 1) Hyphen form (Nuxt test-[id].vue → /test-:id): when single param key equals last baseName segment (e.g. test-id + id → test-:id).
59
+ * 2) Slash form (kebab→slash): path segments from baseName, last N replaced by :paramKey (e.g. test-id → /test/:id).
60
+ */
61
+ protected buildPathFromBaseNameAndParams(baseName: string, params: Record<string, unknown>, targetLocale: string): string | null;
62
+ protected getPathWithoutLocale(path: string): {
63
+ pathWithoutLocale: string;
64
+ localeFromPath: string | null;
65
+ };
66
+ getLocaleFromPath(path: string): string | null;
67
+ abstract resolveLocaleFromPath(path: string): string | null;
68
+ /**
69
+ * Returns path to redirect to, or null if no redirect needed.
70
+ * Use in middleware: strategy.getRedirect(to.fullPath, detectedLocale)
71
+ */
72
+ abstract getRedirect(currentPath: string, targetLocale: string): string | null;
73
+ /**
74
+ * Builds SEO attributes (canonical + hreflangs) from current route.
75
+ * Respects routeLocales: only allowed locales for this route get an hreflang entry.
76
+ * routesLocaleLinks is used when resolving the route key for routeLocales lookup.
77
+ */
78
+ getSeoAttributes(currentRoute: ResolvedRouteLike): SeoAttributes;
79
+ /**
80
+ * Builds full URL (path + optional baseUrl for locale).
81
+ */
82
+ protected buildFullUrl(localeCode: string, path: string): string;
83
+ /** When router knows neither targetName nor baseName — what to return (strategy may override). */
84
+ protected getSwitchLocaleFallbackWhenNoRoute(route: ResolvedRouteLike, targetName: string): RouteLike | string;
85
+ /**
86
+ * Default: baseName → buildLocalizedRouteName → hasRoute → applyBaseUrl; fallback to baseName.
87
+ */
88
+ switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
89
+ /**
90
+ * Template Method: BaseStrategy knows "how" (normalize → delegate to strategy).
91
+ * Always returns RouteLike with path and fullPath (never a string).
92
+ */
93
+ localeRoute(targetLocale: string, routeOrPath: RouteLike | string, currentRoute?: ResolvedRouteLike): RouteLike;
94
+ /** Normalizes resolveLocaleRoute result into RouteLike (path and fullPath always set). */
95
+ protected ensureRouteLike(value: RouteLike | string, source?: RouteLike | null): RouteLike;
96
+ /**
97
+ * Normalizes localeRoute input into a single structure (path string or route with resolved).
98
+ */
99
+ protected normalizeRouteInput(routeOrPath: RouteLike | string, _currentRoute?: ResolvedRouteLike): NormalizedRouteInput;
100
+ /** Logging when ctx.debug (for localeRoute debugging). Disabled by default. */
101
+ private debugLog;
102
+ /**
103
+ * Default resolution: uses buildLocalizedPath and buildLocalizedRouteName.
104
+ * Strategies with different logic (e.g. prefix-except-default) override this method.
105
+ */
106
+ protected resolveLocaleRoute(targetLocale: string, normalized: NormalizedRouteInput, _currentRoute?: ResolvedRouteLike): RouteLike | string;
107
+ }
108
+
109
+ /** Custom path rules: route key -> locale -> path segment. false = unlocalized route. */
110
+ declare type GlobalLocaleRoutes = Record<string, Record<string, string> | false>;
111
+
112
+ declare interface HreflangTag {
113
+ rel: 'alternate';
114
+ href: string;
115
+ hreflang: string;
116
+ }
117
+
118
+ /** Normalized input for resolveLocaleRoute (template method). */
119
+ declare type NormalizedRouteInput = {
120
+ kind: 'path';
121
+ path: string;
122
+ } | {
123
+ kind: 'route';
124
+ inputName: string | null;
125
+ sourceRoute: RouteLike;
126
+ resolved: ResolvedRouteLike;
127
+ };
128
+
129
+ declare interface PathStrategy {
130
+ /**
131
+ * Returns a route object to navigate to another locale from the current page.
132
+ */
133
+ switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
134
+ /**
135
+ * Localizes the given route (object or string) for the target locale.
136
+ * Always returns RouteLike (path and fullPath set). Analogue of $localeRoute / resolveLocalizedRoute.
137
+ */
138
+ localeRoute(targetLocale: string, routeOrPath: RouteLike | string, currentRoute?: ResolvedRouteLike): RouteLike;
139
+ /**
140
+ * Checks if there is a custom path for the given route in globalLocaleRoutes.
141
+ * Used by redirect logic. Returns a path or null.
142
+ */
143
+ getCanonicalPath(route: ResolvedRouteLike, targetLocale: string): string | null;
144
+ /**
145
+ * Determines locale from URL path (strategy-specific).
146
+ * Returns locale code or null if path does not contain a locale (e.g. no_prefix).
147
+ */
148
+ resolveLocaleFromPath(path: string): string | null;
149
+ /**
150
+ * Tries to determine locale from URL path (first segment).
151
+ * Used for initial state and redirect logic. Base implementation in BasePathStrategy.
152
+ */
153
+ getLocaleFromPath(path: string): string | null;
154
+ /**
155
+ * Returns path to redirect to for the given current path and target locale, or null if no redirect needed.
156
+ * Use in middleware: const redirectPath = strategy.getRedirect(to.fullPath, detectedLocale)
157
+ */
158
+ getRedirect(currentPath: string, targetLocale: string): string | null;
159
+ /**
160
+ * Returns SEO attributes (canonical, hreflangs) for useHead.
161
+ */
162
+ getSeoAttributes(currentRoute: ResolvedRouteLike): SeoAttributes;
163
+ /**
164
+ * Sets the router adapter (e.g. when creating singleton and passing router later).
165
+ */
166
+ setRouter(router: RouterAdapter): void;
167
+ getDefaultLocale(): string;
168
+ getLocales(): Locale[];
169
+ getStrategy(): Strategies;
170
+ getLocalizedRouteNamePrefix(): string;
171
+ getGlobalLocaleRoutes(): GlobalLocaleRoutes | undefined;
172
+ getRouteLocales(): Record<string, string[]> | undefined;
173
+ getRoutesLocaleLinks(): Record<string, string> | undefined;
174
+ getNoPrefixRedirect(): boolean | undefined;
175
+ }
176
+
177
+ declare interface PathStrategyContext {
178
+ strategy: Strategies;
179
+ defaultLocale: string;
180
+ locales: Locale[];
181
+ localizedRouteNamePrefix: string;
182
+ globalLocaleRoutes?: GlobalLocaleRoutes;
183
+ routeLocales?: Record<string, string[]>;
184
+ routesLocaleLinks?: Record<string, string>;
185
+ includeDefaultLocaleRoute?: boolean;
186
+ noPrefixRedirect?: boolean;
187
+ debug?: boolean;
188
+ router: RouterAdapter;
189
+ }
190
+
191
+ declare class PrefixPathStrategy extends BasePathStrategy {
192
+ protected buildLocalizedPath(path: string, locale: string, _isCustom: boolean): string;
193
+ protected buildLocalizedRouteName(baseName: string, locale: string): string;
194
+ getCanonicalPath(route: ResolvedRouteLike, targetLocale: string): string | null;
195
+ resolveLocaleFromPath(path: string): string | null;
196
+ getRedirect(currentPath: string, detectedLocale: string): string | null;
197
+ private buildPathWithPrefix;
198
+ }
199
+ export { PrefixPathStrategy }
200
+ export { PrefixPathStrategy as Strategy }
201
+
202
+ /** Resolved route (path and fullPath required). */
203
+ declare interface ResolvedRouteLike extends RouteLike {
204
+ name: string | null;
205
+ path: string;
206
+ fullPath: string;
207
+ }
208
+
209
+ /** Route-like shape (compatible with vue-router). */
210
+ declare interface RouteLike {
211
+ name?: string | null;
212
+ path?: string;
213
+ params?: Record<string, unknown>;
214
+ query?: Record<string, unknown>;
215
+ hash?: string;
216
+ fullPath?: string;
217
+ }
218
+
219
+ declare interface RouterAdapter {
220
+ hasRoute: (name: string) => boolean;
221
+ resolve: (to: RouteLike | string) => ResolvedRouteLike;
222
+ getRoutes?: () => unknown[];
223
+ }
224
+
225
+ declare class RouteResolver {
226
+ private ctx;
227
+ constructor(ctx: PathStrategyContext);
228
+ /**
229
+ * Substitutes params into path template (:key, :key(), [...key]).
230
+ * Uses one combined regex per key to avoid creating multiple RegExp in a loop.
231
+ */
232
+ resolvePathWithParams(path: string, params?: Record<string, unknown>): string;
233
+ /**
234
+ * Analyzes route: path without locale and base name (normalizer + route-name).
235
+ */
236
+ private analyzeRoute;
237
+ /** Public access to route analysis (for strategies). */
238
+ getPathWithoutLocaleAndBaseName(route: ResolvedRouteLike): {
239
+ pathWithoutLocale: string;
240
+ baseRouteName: string | null;
241
+ };
242
+ /**
243
+ * Lookup keys for config (same order for resolveCustomPath, getPathForUnlocalizedRoute, getAllowedLocalesForRoute).
244
+ */
245
+ private getLookupKeys;
246
+ /**
247
+ * Resolves custom path for targetLocale from globalLocaleRoutes.
248
+ */
249
+ resolveCustomPath(route: ResolvedRouteLike, targetLocale: string): string | null;
250
+ /**
251
+ * Unlocalized route (globalLocaleRoutes[key] === false) — returns path without locale.
252
+ */
253
+ getPathForUnlocalizedRoute(route: ResolvedRouteLike): string | null;
254
+ /**
255
+ * Unlocalized by name (when no route object available).
256
+ */
257
+ getPathForUnlocalizedRouteByName(routeName: string): string | null;
258
+ /**
259
+ * Allowed locales for route (routeLocales).
260
+ */
261
+ getAllowedLocalesForRoute(route: ResolvedRouteLike): string[];
262
+ /**
263
+ * Parent path for nested route (parent key -> targetLocale path).
264
+ */
265
+ getParentPathForNested(nameSegments: string[], targetLocale: string): string;
266
+ }
267
+
268
+ declare interface SeoAttributes {
269
+ canonical?: string;
270
+ hreflangs?: HreflangTag[];
271
+ }
272
+
273
+ declare interface SwitchLocaleOptions {
274
+ i18nRouteParams?: I18nRouteParams;
275
+ }
276
+
277
+ export { }
@@ -0,0 +1,40 @@
1
+ import { B as u, j as c, n as P, c as l, w as m, a as g, g as d, i as f } from "./base-strategy-DVqe8ehd.js";
2
+ class S extends u {
3
+ buildLocalizedPath(a, e, t) {
4
+ return c(e, P(a));
5
+ }
6
+ buildLocalizedRouteName(a, e) {
7
+ return this.buildLocalizedName(a, e);
8
+ }
9
+ getCanonicalPath(a, e) {
10
+ const t = this.getCustomPathSegment(a, e);
11
+ if (!t) return null;
12
+ const s = t.startsWith("/") ? t : `/${t}`;
13
+ return l(`/${e}${s}`);
14
+ }
15
+ resolveLocaleFromPath(a) {
16
+ const { localeFromPath: e } = this.getPathWithoutLocale(a);
17
+ return e;
18
+ }
19
+ getRedirect(a, e) {
20
+ const { pathWithoutLocale: t, localeFromPath: s } = this.getPathWithoutLocale(a), r = this.ctx.globalLocaleRoutes, n = t === "/" ? "/" : m(t), o = r && (r[t] === !1 || r[n] === !1);
21
+ if (o && s !== null)
22
+ return g(t);
23
+ if (o && s === null)
24
+ return null;
25
+ const i = this.buildPathWithPrefix(t, e), h = d(a);
26
+ return s === e && f(h, i) ? null : i;
27
+ }
28
+ buildPathWithPrefix(a, e) {
29
+ const t = this.resolvePathForLocale(a, e);
30
+ if (t === "/" || t === "")
31
+ return `/${e}`;
32
+ const s = `/${e}`, r = t.startsWith("/") ? t : `/${t}`;
33
+ return l(s + r);
34
+ }
35
+ }
36
+ export {
37
+ S as PrefixPathStrategy,
38
+ S as Strategy
39
+ };
40
+ //# sourceMappingURL=prefix-strategy.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prefix-strategy.mjs","sources":["../src/strategies/prefix.ts"],"sourcesContent":["import type { ResolvedRouteLike } from '../core/types'\nimport { BasePathStrategy } from './base-strategy'\nimport { cleanDoubleSlashes, isSamePath, withoutLeadingSlash } from 'ufo'\nimport { getCleanPath, joinUrl, normalizePath, normalizePathForCompare } from '../utils/path'\n\nexport class PrefixPathStrategy extends BasePathStrategy {\n protected buildLocalizedPath(path: string, locale: string, _isCustom: boolean): string {\n return joinUrl(locale, normalizePath(path))\n }\n\n protected buildLocalizedRouteName(baseName: string, locale: string): string {\n return this.buildLocalizedName(baseName, locale)\n }\n\n override getCanonicalPath(route: ResolvedRouteLike, targetLocale: string): string | null {\n const segment = this.getCustomPathSegment(route, targetLocale)\n if (!segment) return null\n const normalized = segment.startsWith('/') ? segment : `/${segment}`\n return cleanDoubleSlashes(`/${targetLocale}${normalized}`)\n }\n\n resolveLocaleFromPath(path: string): string | null {\n const { localeFromPath } = this.getPathWithoutLocale(path)\n return localeFromPath\n }\n\n getRedirect(currentPath: string, detectedLocale: string): string | null {\n const { pathWithoutLocale, localeFromPath } = this.getPathWithoutLocale(currentPath)\n const gr = this.ctx.globalLocaleRoutes\n const pathKey = pathWithoutLocale === '/' ? '/' : withoutLeadingSlash(pathWithoutLocale)\n const isUnlocalized = gr && (gr[pathWithoutLocale] === false || gr[pathKey] === false)\n if (isUnlocalized && localeFromPath !== null) {\n return normalizePathForCompare(pathWithoutLocale)\n }\n if (isUnlocalized && localeFromPath === null) {\n return null\n }\n const expectedPath = this.buildPathWithPrefix(pathWithoutLocale, detectedLocale)\n const currentPathOnly = getCleanPath(currentPath)\n if (localeFromPath === detectedLocale && isSamePath(currentPathOnly, expectedPath)) {\n return null\n }\n return expectedPath\n }\n\n private buildPathWithPrefix(pathWithoutLocale: string, locale: string): string {\n const resolved = this.resolvePathForLocale(pathWithoutLocale, locale)\n if (resolved === '/' || resolved === '') {\n return `/${locale}`\n }\n const prefix = `/${locale}`\n const withSlash = resolved.startsWith('/') ? resolved : `/${resolved}`\n return cleanDoubleSlashes(prefix + withSlash)\n }\n}\n\n/** Alias for Nuxt alias consumption: only this strategy is bundled when importing from #i18n-strategy. */\nexport { PrefixPathStrategy as Strategy }\n"],"names":["PrefixPathStrategy","BasePathStrategy","path","locale","_isCustom","joinUrl","normalizePath","baseName","route","targetLocale","segment","normalized","cleanDoubleSlashes","localeFromPath","currentPath","detectedLocale","pathWithoutLocale","gr","pathKey","withoutLeadingSlash","isUnlocalized","normalizePathForCompare","expectedPath","currentPathOnly","getCleanPath","isSamePath","resolved","prefix","withSlash"],"mappings":";AAKO,MAAMA,UAA2BC,EAAiB;AAAA,EAC7C,mBAAmBC,GAAcC,GAAgBC,GAA4B;AACrF,WAAOC,EAAQF,GAAQG,EAAcJ,CAAI,CAAC;AAAA,EAC5C;AAAA,EAEU,wBAAwBK,GAAkBJ,GAAwB;AAC1E,WAAO,KAAK,mBAAmBI,GAAUJ,CAAM;AAAA,EACjD;AAAA,EAES,iBAAiBK,GAA0BC,GAAqC;AACvF,UAAMC,IAAU,KAAK,qBAAqBF,GAAOC,CAAY;AAC7D,QAAI,CAACC,EAAS,QAAO;AACrB,UAAMC,IAAaD,EAAQ,WAAW,GAAG,IAAIA,IAAU,IAAIA,CAAO;AAClE,WAAOE,EAAmB,IAAIH,CAAY,GAAGE,CAAU,EAAE;AAAA,EAC3D;AAAA,EAEA,sBAAsBT,GAA6B;AACjD,UAAM,EAAE,gBAAAW,EAAA,IAAmB,KAAK,qBAAqBX,CAAI;AACzD,WAAOW;AAAA,EACT;AAAA,EAEA,YAAYC,GAAqBC,GAAuC;AACtE,UAAM,EAAE,mBAAAC,GAAmB,gBAAAH,EAAA,IAAmB,KAAK,qBAAqBC,CAAW,GAC7EG,IAAK,KAAK,IAAI,oBACdC,IAAUF,MAAsB,MAAM,MAAMG,EAAoBH,CAAiB,GACjFI,IAAgBH,MAAOA,EAAGD,CAAiB,MAAM,MAASC,EAAGC,CAAO,MAAM;AAChF,QAAIE,KAAiBP,MAAmB;AACtC,aAAOQ,EAAwBL,CAAiB;AAElD,QAAII,KAAiBP,MAAmB;AACtC,aAAO;AAET,UAAMS,IAAe,KAAK,oBAAoBN,GAAmBD,CAAc,GACzEQ,IAAkBC,EAAaV,CAAW;AAChD,WAAID,MAAmBE,KAAkBU,EAAWF,GAAiBD,CAAY,IACxE,OAEFA;AAAA,EACT;AAAA,EAEQ,oBAAoBN,GAA2Bb,GAAwB;AAC7E,UAAMuB,IAAW,KAAK,qBAAqBV,GAAmBb,CAAM;AACpE,QAAIuB,MAAa,OAAOA,MAAa;AACnC,aAAO,IAAIvB,CAAM;AAEnB,UAAMwB,IAAS,IAAIxB,CAAM,IACnByB,IAAYF,EAAS,WAAW,GAAG,IAAIA,IAAW,IAAIA,CAAQ;AACpE,WAAOd,EAAmBe,IAASC,CAAS;AAAA,EAC9C;AACF;"}
package/dist/types.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=types.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,108 @@
1
+ import { I18nRouteParams } from '@i18n-micro/types';
2
+ import { Locale } from '@i18n-micro/types';
3
+ import { Strategies } from '@i18n-micro/types';
4
+
5
+ /** Custom path rules: route key -> locale -> path segment. false = unlocalized route. */
6
+ declare type GlobalLocaleRoutes = Record<string, Record<string, string> | false>;
7
+
8
+ export declare interface HreflangTag {
9
+ rel: 'alternate';
10
+ href: string;
11
+ hreflang: string;
12
+ }
13
+
14
+ export declare interface PathStrategy {
15
+ /**
16
+ * Returns a route object to navigate to another locale from the current page.
17
+ */
18
+ switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
19
+ /**
20
+ * Localizes the given route (object or string) for the target locale.
21
+ * Always returns RouteLike (path and fullPath set). Analogue of $localeRoute / resolveLocalizedRoute.
22
+ */
23
+ localeRoute(targetLocale: string, routeOrPath: RouteLike | string, currentRoute?: ResolvedRouteLike): RouteLike;
24
+ /**
25
+ * Checks if there is a custom path for the given route in globalLocaleRoutes.
26
+ * Used by redirect logic. Returns a path or null.
27
+ */
28
+ getCanonicalPath(route: ResolvedRouteLike, targetLocale: string): string | null;
29
+ /**
30
+ * Determines locale from URL path (strategy-specific).
31
+ * Returns locale code or null if path does not contain a locale (e.g. no_prefix).
32
+ */
33
+ resolveLocaleFromPath(path: string): string | null;
34
+ /**
35
+ * Tries to determine locale from URL path (first segment).
36
+ * Used for initial state and redirect logic. Base implementation in BasePathStrategy.
37
+ */
38
+ getLocaleFromPath(path: string): string | null;
39
+ /**
40
+ * Returns path to redirect to for the given current path and target locale, or null if no redirect needed.
41
+ * Use in middleware: const redirectPath = strategy.getRedirect(to.fullPath, detectedLocale)
42
+ */
43
+ getRedirect(currentPath: string, targetLocale: string): string | null;
44
+ /**
45
+ * Returns SEO attributes (canonical, hreflangs) for useHead.
46
+ */
47
+ getSeoAttributes(currentRoute: ResolvedRouteLike): SeoAttributes;
48
+ /**
49
+ * Sets the router adapter (e.g. when creating singleton and passing router later).
50
+ */
51
+ setRouter(router: RouterAdapter): void;
52
+ getDefaultLocale(): string;
53
+ getLocales(): Locale[];
54
+ getStrategy(): Strategies;
55
+ getLocalizedRouteNamePrefix(): string;
56
+ getGlobalLocaleRoutes(): GlobalLocaleRoutes | undefined;
57
+ getRouteLocales(): Record<string, string[]> | undefined;
58
+ getRoutesLocaleLinks(): Record<string, string> | undefined;
59
+ getNoPrefixRedirect(): boolean | undefined;
60
+ }
61
+
62
+ export declare interface PathStrategyContext {
63
+ strategy: Strategies;
64
+ defaultLocale: string;
65
+ locales: Locale[];
66
+ localizedRouteNamePrefix: string;
67
+ globalLocaleRoutes?: GlobalLocaleRoutes;
68
+ routeLocales?: Record<string, string[]>;
69
+ routesLocaleLinks?: Record<string, string>;
70
+ includeDefaultLocaleRoute?: boolean;
71
+ noPrefixRedirect?: boolean;
72
+ debug?: boolean;
73
+ router: RouterAdapter;
74
+ }
75
+
76
+ /** Resolved route (path and fullPath required). */
77
+ export declare interface ResolvedRouteLike extends RouteLike {
78
+ name: string | null;
79
+ path: string;
80
+ fullPath: string;
81
+ }
82
+
83
+ /** Route-like shape (compatible with vue-router). */
84
+ export declare interface RouteLike {
85
+ name?: string | null;
86
+ path?: string;
87
+ params?: Record<string, unknown>;
88
+ query?: Record<string, unknown>;
89
+ hash?: string;
90
+ fullPath?: string;
91
+ }
92
+
93
+ export declare interface RouterAdapter {
94
+ hasRoute: (name: string) => boolean;
95
+ resolve: (to: RouteLike | string) => ResolvedRouteLike;
96
+ getRoutes?: () => unknown[];
97
+ }
98
+
99
+ export declare interface SeoAttributes {
100
+ canonical?: string;
101
+ hreflangs?: HreflangTag[];
102
+ }
103
+
104
+ export declare interface SwitchLocaleOptions {
105
+ i18nRouteParams?: I18nRouteParams;
106
+ }
107
+
108
+ export { }
package/dist/types.mjs ADDED
@@ -0,0 +1,2 @@
1
+
2
+ //# sourceMappingURL=types.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,84 @@
1
+ {
2
+ "name": "@i18n-micro/path-strategy",
3
+ "version": "1.0.0",
4
+ "description": "Shared strategy-based path and route name builder for Nuxt I18n Micro frontend routing.",
5
+ "repository": "s00d/nuxt-i18n-micro",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "author": {
9
+ "name": "s00d",
10
+ "email": "Virus191288@gmail.com",
11
+ "url": "https://s00d.github.io/"
12
+ },
13
+ "homepage": "https://github.com/s00d/nuxt-i18n-micro",
14
+ "bugs": {
15
+ "url": "https://github.com/s00d/nuxt-i18n-micro/issues"
16
+ },
17
+ "main": "dist/index.cjs",
18
+ "module": "dist/index.mjs",
19
+ "types": "dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.mjs",
24
+ "require": "./dist/index.cjs",
25
+ "default": "./dist/index.mjs"
26
+ },
27
+ "./prefix": {
28
+ "types": "./dist/prefix-strategy.d.ts",
29
+ "import": "./dist/prefix-strategy.mjs",
30
+ "require": "./dist/prefix-strategy.cjs",
31
+ "default": "./dist/prefix-strategy.mjs"
32
+ },
33
+ "./no-prefix": {
34
+ "types": "./dist/no-prefix-strategy.d.ts",
35
+ "import": "./dist/no-prefix-strategy.mjs",
36
+ "require": "./dist/no-prefix-strategy.cjs",
37
+ "default": "./dist/no-prefix-strategy.mjs"
38
+ },
39
+ "./prefix-except-default": {
40
+ "types": "./dist/prefix-except-default-strategy.d.ts",
41
+ "import": "./dist/prefix-except-default-strategy.mjs",
42
+ "require": "./dist/prefix-except-default-strategy.cjs",
43
+ "default": "./dist/prefix-except-default-strategy.mjs"
44
+ },
45
+ "./prefix-and-default": {
46
+ "types": "./dist/prefix-and-default-strategy.d.ts",
47
+ "import": "./dist/prefix-and-default-strategy.mjs",
48
+ "require": "./dist/prefix-and-default-strategy.cjs",
49
+ "default": "./dist/prefix-and-default-strategy.mjs"
50
+ },
51
+ "./types": {
52
+ "types": "./dist/types.d.ts",
53
+ "import": "./dist/types.mjs",
54
+ "require": "./dist/types.cjs",
55
+ "default": "./dist/types.mjs"
56
+ }
57
+ },
58
+ "publishConfig": {
59
+ "access": "public"
60
+ },
61
+ "keywords": [
62
+ "nuxt",
63
+ "i18n",
64
+ "routing",
65
+ "paths",
66
+ "strategy"
67
+ ],
68
+ "files": [
69
+ "dist",
70
+ "README.md"
71
+ ],
72
+ "dependencies": {
73
+ "ufo": "^1.5.4",
74
+ "@i18n-micro/types": "1.1.0"
75
+ },
76
+ "devDependencies": {
77
+ "vite": "^7.3.1",
78
+ "vite-plugin-dts": "^4.5.4"
79
+ },
80
+ "scripts": {
81
+ "build": "vite build",
82
+ "test": "jest"
83
+ }
84
+ }