@better-translate/tanstack-router 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.
@@ -0,0 +1,400 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/navigation.tsx
21
+ var navigation_exports = {};
22
+ __export(navigation_exports, {
23
+ createNavigationFunctions: () => createNavigationFunctions
24
+ });
25
+ module.exports = __toCommonJS(navigation_exports);
26
+ var import_react = require("react");
27
+
28
+ // src/shared.ts
29
+ var DEFAULT_ROUTE_TEMPLATE = "/{-$locale}";
30
+ function hasLocale(locales, value) {
31
+ return locales.includes(value);
32
+ }
33
+ function normalizePathname(pathname) {
34
+ if (!pathname) {
35
+ return "/";
36
+ }
37
+ return pathname.startsWith("/") ? pathname : `/${pathname}`;
38
+ }
39
+ function isPathnameInScope(pathname, routing) {
40
+ return analyzePathname(pathname, routing).inScope;
41
+ }
42
+ function stripLocaleFromPathname(pathname, routing) {
43
+ const analyzed = analyzePathname(pathname, routing);
44
+ return analyzed.inScope ? analyzed.deLocalizedPathname : normalizePathname(pathname);
45
+ }
46
+ function localizePathname(pathname, locale, routing) {
47
+ const normalizedPathname = normalizePathname(pathname);
48
+ const deLocalizedPathname = stripLocaleFromPathname(
49
+ normalizedPathname,
50
+ routing
51
+ );
52
+ if (!isPathnameInScope(deLocalizedPathname, routing)) {
53
+ return normalizedPathname;
54
+ }
55
+ const parsedTemplate = parseRouteTemplate(routing.routeTemplate);
56
+ if (!parsedTemplate.isRequired && locale === routing.defaultLocale) {
57
+ return deLocalizedPathname;
58
+ }
59
+ const pathnameSegments = splitPathname(deLocalizedPathname);
60
+ const localizedSegments = [...pathnameSegments];
61
+ localizedSegments.splice(parsedTemplate.localeSegmentIndex, 0, locale);
62
+ return joinPathname(localizedSegments);
63
+ }
64
+ function splitHrefString(href) {
65
+ const hashIndex = href.indexOf("#");
66
+ const searchIndex = href.indexOf("?");
67
+ const pathnameEnd = hashIndex === -1 ? searchIndex === -1 ? href.length : searchIndex : searchIndex === -1 ? hashIndex : Math.min(searchIndex, hashIndex);
68
+ const pathname = href.slice(0, pathnameEnd) || "/";
69
+ const search = searchIndex === -1 ? "" : href.slice(searchIndex, hashIndex === -1 ? href.length : hashIndex);
70
+ const hash = hashIndex === -1 ? "" : href.slice(hashIndex);
71
+ return {
72
+ hash,
73
+ pathname: normalizePathname(pathname),
74
+ search
75
+ };
76
+ }
77
+ function isAbsoluteHref(href) {
78
+ return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(href) || href.startsWith("//");
79
+ }
80
+ function parseRouteTemplate(routeTemplate) {
81
+ const normalizedRouteTemplate = normalizePathname(
82
+ routeTemplate ?? DEFAULT_ROUTE_TEMPLATE
83
+ );
84
+ const localizedSegments = splitPathname(normalizedRouteTemplate);
85
+ let localeSegmentIndex = -1;
86
+ let localeParamName = "";
87
+ let isRequired = false;
88
+ for (let index = 0; index < localizedSegments.length; index += 1) {
89
+ const segment = localizedSegments[index];
90
+ const localeMatch = /^\{(-?)\$([a-zA-Z_$][a-zA-Z0-9_$]*)\}$/.exec(segment);
91
+ if (localeMatch) {
92
+ if (localeSegmentIndex !== -1) {
93
+ throw new Error(
94
+ `Route template "${normalizedRouteTemplate}" must contain exactly one locale segment.`
95
+ );
96
+ }
97
+ localeSegmentIndex = index;
98
+ localeParamName = localeMatch[2];
99
+ isRequired = localeMatch[1] !== "-";
100
+ continue;
101
+ }
102
+ if (looksDynamic(segment)) {
103
+ throw new Error(
104
+ `Route template "${normalizedRouteTemplate}" can only contain one locale segment like "{-$locale}" or "{$locale}" plus static path segments.`
105
+ );
106
+ }
107
+ }
108
+ if (localeSegmentIndex === -1) {
109
+ throw new Error(
110
+ `Route template "${normalizedRouteTemplate}" must contain one locale segment like "{-$locale}" or "{$locale}".`
111
+ );
112
+ }
113
+ const deLocalizedSegments = localizedSegments.filter(
114
+ (_, index) => index !== localeSegmentIndex
115
+ );
116
+ return {
117
+ deLocalizedSegments,
118
+ isRequired,
119
+ localeParamName,
120
+ localeSegment: localizedSegments[localeSegmentIndex],
121
+ localeSegmentIndex,
122
+ localizedSegments,
123
+ routeTemplate: normalizedRouteTemplate
124
+ };
125
+ }
126
+ function analyzePathname(pathname, routing) {
127
+ const parsedTemplate = parseRouteTemplate(routing.routeTemplate);
128
+ const pathnameSegments = splitPathname(pathname);
129
+ let explicitLocale;
130
+ let localePathIndex = -1;
131
+ let pathnameIndex = 0;
132
+ for (let templateIndex = 0; templateIndex < parsedTemplate.localizedSegments.length; templateIndex += 1) {
133
+ if (templateIndex === parsedTemplate.localeSegmentIndex) {
134
+ const maybeLocale = pathnameSegments[pathnameIndex];
135
+ if (maybeLocale && hasLocale(routing.locales, maybeLocale)) {
136
+ explicitLocale = maybeLocale;
137
+ localePathIndex = pathnameIndex;
138
+ pathnameIndex += 1;
139
+ }
140
+ continue;
141
+ }
142
+ const templateSegment = parsedTemplate.localizedSegments[templateIndex];
143
+ const pathnameSegment = pathnameSegments[pathnameIndex];
144
+ if (pathnameSegment !== templateSegment) {
145
+ return {
146
+ deLocalizedPathname: normalizePathname(pathname),
147
+ explicitLocale: void 0,
148
+ inScope: false
149
+ };
150
+ }
151
+ pathnameIndex += 1;
152
+ }
153
+ if (localePathIndex === -1) {
154
+ return {
155
+ deLocalizedPathname: joinPathname(pathnameSegments),
156
+ explicitLocale: void 0,
157
+ inScope: true
158
+ };
159
+ }
160
+ const deLocalizedSegments = [...pathnameSegments];
161
+ deLocalizedSegments.splice(localePathIndex, 1);
162
+ return {
163
+ deLocalizedPathname: joinPathname(deLocalizedSegments),
164
+ explicitLocale,
165
+ inScope: true
166
+ };
167
+ }
168
+ function looksDynamic(segment) {
169
+ return segment.startsWith("[") || segment.startsWith("$") || segment.includes("{") || segment.includes("}");
170
+ }
171
+ function splitPathname(pathname) {
172
+ return normalizePathname(pathname).split("/").filter(Boolean);
173
+ }
174
+ function joinPathname(segments) {
175
+ return segments.length === 0 ? "/" : `/${segments.join("/")}`;
176
+ }
177
+
178
+ // src/navigation.tsx
179
+ function createNavigationFunctions({
180
+ Link: LinkComponent,
181
+ routing,
182
+ useLocation: useInjectedLocation,
183
+ useNavigate: useInjectedNavigate,
184
+ useParams: useInjectedParams,
185
+ useRouter: useInjectedRouter
186
+ }) {
187
+ const parsedTemplate = parseRouteTemplate(routing.routeTemplate);
188
+ function getPathname({
189
+ href,
190
+ locale = routing.defaultLocale
191
+ }) {
192
+ return localizePathname(href, locale, routing);
193
+ }
194
+ const Link = ((rawProps) => {
195
+ const localizedOptions = getLocalizedOptions(
196
+ rawProps,
197
+ useInjectedLocation().pathname,
198
+ resolveActiveLocale(
199
+ useInjectedParams(),
200
+ routing,
201
+ parsedTemplate.localeParamName
202
+ ),
203
+ routing,
204
+ parsedTemplate.localeParamName,
205
+ parsedTemplate.localeSegment,
206
+ parsedTemplate.isRequired
207
+ );
208
+ return (0, import_react.createElement)(
209
+ LinkComponent,
210
+ localizedOptions
211
+ );
212
+ });
213
+ function usePathname() {
214
+ return stripLocaleFromPathname(useInjectedLocation().pathname, routing);
215
+ }
216
+ function useLocale() {
217
+ return resolveActiveLocale(
218
+ useInjectedParams(),
219
+ routing,
220
+ parsedTemplate.localeParamName
221
+ );
222
+ }
223
+ function useNavigate() {
224
+ const navigate = useInjectedNavigate();
225
+ const pathname = useInjectedLocation().pathname;
226
+ const activeLocale = useLocale();
227
+ return ((options) => navigate(
228
+ getLocalizedOptions(
229
+ options,
230
+ pathname,
231
+ activeLocale,
232
+ routing,
233
+ parsedTemplate.localeParamName,
234
+ parsedTemplate.localeSegment,
235
+ parsedTemplate.isRequired
236
+ )
237
+ ));
238
+ }
239
+ function useRouter() {
240
+ const router = useInjectedRouter();
241
+ const pathname = useInjectedLocation().pathname;
242
+ const activeLocale = useLocale();
243
+ return {
244
+ ...router,
245
+ buildLocation(options) {
246
+ return router.buildLocation(
247
+ getLocalizedOptions(
248
+ options,
249
+ pathname,
250
+ activeLocale,
251
+ routing,
252
+ parsedTemplate.localeParamName,
253
+ parsedTemplate.localeSegment,
254
+ parsedTemplate.isRequired
255
+ )
256
+ );
257
+ },
258
+ navigate(options) {
259
+ return router.navigate(
260
+ getLocalizedOptions(
261
+ options,
262
+ pathname,
263
+ activeLocale,
264
+ routing,
265
+ parsedTemplate.localeParamName,
266
+ parsedTemplate.localeSegment,
267
+ parsedTemplate.isRequired
268
+ )
269
+ );
270
+ },
271
+ preloadRoute(options) {
272
+ return router.preloadRoute?.(
273
+ getLocalizedOptions(
274
+ options,
275
+ pathname,
276
+ activeLocale,
277
+ routing,
278
+ parsedTemplate.localeParamName,
279
+ parsedTemplate.localeSegment,
280
+ parsedTemplate.isRequired
281
+ )
282
+ );
283
+ }
284
+ };
285
+ }
286
+ return {
287
+ Link,
288
+ getPathname,
289
+ useLocale,
290
+ useNavigate,
291
+ usePathname,
292
+ useRouter
293
+ };
294
+ }
295
+ function getLocalizedOptions(options, currentPathname, activeLocale, routing, localeParamName, localeSegment, isRequired) {
296
+ const nextLocale = options.locale ?? activeLocale;
297
+ const localized = {
298
+ ...options
299
+ };
300
+ delete localized.locale;
301
+ if (!shouldLocalizeTarget(
302
+ options.to,
303
+ currentPathname,
304
+ routing,
305
+ localeParamName,
306
+ localeSegment
307
+ )) {
308
+ return localized;
309
+ }
310
+ if (typeof options.to === "string" && shouldRewriteTo(options.to) && !pathContainsLocaleParam(options.to, localeParamName)) {
311
+ localized.to = localizePathname(
312
+ options.to,
313
+ nextLocale,
314
+ routing
315
+ );
316
+ }
317
+ localized.params = localizeParams(
318
+ options.params,
319
+ localeParamName,
320
+ isRequired ? nextLocale : nextLocale === routing.defaultLocale ? void 0 : nextLocale
321
+ );
322
+ return localized;
323
+ }
324
+ function localizeParams(params, localeParamName, locale) {
325
+ if (params === true || params === void 0) {
326
+ return (current) => mergeLocaleParam(current, localeParamName, locale);
327
+ }
328
+ if (typeof params === "function") {
329
+ return (current) => mergeLocaleParam(
330
+ params(
331
+ current
332
+ ),
333
+ localeParamName,
334
+ locale
335
+ );
336
+ }
337
+ if (params && typeof params === "object") {
338
+ return mergeLocaleParam(
339
+ params,
340
+ localeParamName,
341
+ locale
342
+ );
343
+ }
344
+ return params;
345
+ }
346
+ function mergeLocaleParam(params, localeParamName, locale) {
347
+ const nextParams = {
348
+ ...params ?? {}
349
+ };
350
+ if (locale === void 0) {
351
+ delete nextParams[localeParamName];
352
+ return nextParams;
353
+ }
354
+ nextParams[localeParamName] = locale;
355
+ return nextParams;
356
+ }
357
+ function shouldLocalizeTarget(to, currentPathname, routing, localeParamName, localeSegment) {
358
+ if (typeof to !== "string" || to === "" || to === "." || to.startsWith(".")) {
359
+ return isPathnameInScope(currentPathname, routing);
360
+ }
361
+ if (to.startsWith("#") || to.startsWith("?")) {
362
+ return isPathnameInScope(currentPathname, routing);
363
+ }
364
+ if (isAbsoluteHref(to)) {
365
+ return false;
366
+ }
367
+ const { pathname } = splitHrefString(to);
368
+ const normalizedPathname = stripLocalePlaceholder(
369
+ pathname,
370
+ localeParamName,
371
+ localeSegment
372
+ );
373
+ return isPathnameInScope(normalizedPathname, routing);
374
+ }
375
+ function pathContainsLocaleParam(to, localeParamName) {
376
+ return to.split("/").some((s) => s === `$${localeParamName}` || s === `{$${localeParamName}}`);
377
+ }
378
+ function shouldRewriteTo(to) {
379
+ return !to.startsWith(".") && !to.startsWith("#") && !to.startsWith("?") && !isAbsoluteHref(to);
380
+ }
381
+ function stripLocalePlaceholder(pathname, localeParamName, localeSegment) {
382
+ const localeTokens = /* @__PURE__ */ new Set([
383
+ localeSegment,
384
+ `$${localeParamName}`,
385
+ `{$${localeParamName}}`
386
+ ]);
387
+ const segments = pathname.split("/").filter(Boolean).filter((segment) => !localeTokens.has(segment));
388
+ return segments.length === 0 ? "/" : `/${segments.join("/")}`;
389
+ }
390
+ function resolveActiveLocale(params, routing, localeParamName) {
391
+ const localeParam = params[localeParamName];
392
+ if (typeof localeParam === "string" && routing.locales.includes(localeParam)) {
393
+ return localeParam;
394
+ }
395
+ return routing.defaultLocale;
396
+ }
397
+ // Annotate the CommonJS export names for ESM import in node:
398
+ 0 && (module.exports = {
399
+ createNavigationFunctions
400
+ });
@@ -0,0 +1,46 @@
1
+ import { ReactElement } from 'react';
2
+ import { BuildLocationFn, AnyRouter, RegisteredRouter, NavigateOptions, LinkComponent, UseNavigateResult, LinkComponentProps } from '@tanstack/react-router';
3
+ import { RoutingConfig } from './index.cjs';
4
+
5
+ type ParamsLike = Record<string, string | undefined>;
6
+ type LocalizedLinkComponent<TLocale extends string, TDefaultFrom extends string> = <TRouter extends AnyRouter = RegisteredRouter, const TFrom extends string = TDefaultFrom, const TTo extends string | undefined = undefined, const TMaskFrom extends string = TFrom, const TMaskTo extends string = "">(props: LinkComponentProps<"a", TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & {
7
+ locale?: TLocale;
8
+ }) => ReactElement;
9
+ interface GetPathnameOptions<TLocale extends string> {
10
+ href: string;
11
+ locale?: TLocale;
12
+ }
13
+ type LocalizedUseNavigateResult<TLocale extends string, TDefaultFrom extends string> = <TRouter extends RegisteredRouter, TTo extends string | undefined, TFrom extends string = TDefaultFrom, TMaskFrom extends string = TFrom, TMaskTo extends string = "">(options: NavigateOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & {
14
+ locale?: TLocale;
15
+ }) => Promise<void>;
16
+ type LocalizedBuildLocationFn<TLocale extends string> = (options: Record<string, unknown> & {
17
+ locale?: TLocale;
18
+ }) => ReturnType<BuildLocationFn>;
19
+ type LocalizedPreloadRouteFn<TLocale extends string> = (options: Record<string, unknown> & {
20
+ locale?: TLocale;
21
+ }) => Promise<unknown>;
22
+ type LocalizedRouter<TLocale extends string, TRouter extends AnyRouter> = Omit<TRouter, "buildLocation" | "navigate" | "preloadRoute"> & {
23
+ buildLocation: LocalizedBuildLocationFn<TLocale>;
24
+ navigate: LocalizedUseNavigateResult<TLocale, string>;
25
+ preloadRoute?: LocalizedPreloadRouteFn<TLocale>;
26
+ };
27
+ interface NavigationFunctionsConfig<TLocale extends string, TDefaultFrom extends string, TRouter extends AnyRouter, TParams extends ParamsLike> {
28
+ Link: LinkComponent<"a", TDefaultFrom>;
29
+ routing: RoutingConfig<TLocale>;
30
+ useLocation: () => {
31
+ pathname: string;
32
+ };
33
+ useNavigate: () => UseNavigateResult<TDefaultFrom>;
34
+ useParams: () => TParams;
35
+ useRouter: () => TRouter;
36
+ }
37
+ declare function createNavigationFunctions<TLocale extends string, TDefaultFrom extends string = string, TRouter extends AnyRouter = AnyRouter, TParams extends ParamsLike = ParamsLike>({ Link: LinkComponent, routing, useLocation: useInjectedLocation, useNavigate: useInjectedNavigate, useParams: useInjectedParams, useRouter: useInjectedRouter, }: NavigationFunctionsConfig<TLocale, TDefaultFrom, TRouter, TParams>): {
38
+ Link: LocalizedLinkComponent<TLocale, TDefaultFrom>;
39
+ getPathname: ({ href, locale, }: GetPathnameOptions<TLocale>) => string;
40
+ useLocale: () => TLocale;
41
+ useNavigate: () => LocalizedUseNavigateResult<TLocale, TDefaultFrom>;
42
+ usePathname: () => string;
43
+ useRouter: () => LocalizedRouter<TLocale, TRouter>;
44
+ };
45
+
46
+ export { type GetPathnameOptions, type LocalizedBuildLocationFn, type LocalizedPreloadRouteFn, type LocalizedRouter, type LocalizedUseNavigateResult, type NavigationFunctionsConfig, createNavigationFunctions };
@@ -0,0 +1,46 @@
1
+ import { ReactElement } from 'react';
2
+ import { BuildLocationFn, AnyRouter, RegisteredRouter, NavigateOptions, LinkComponent, UseNavigateResult, LinkComponentProps } from '@tanstack/react-router';
3
+ import { RoutingConfig } from './index.js';
4
+
5
+ type ParamsLike = Record<string, string | undefined>;
6
+ type LocalizedLinkComponent<TLocale extends string, TDefaultFrom extends string> = <TRouter extends AnyRouter = RegisteredRouter, const TFrom extends string = TDefaultFrom, const TTo extends string | undefined = undefined, const TMaskFrom extends string = TFrom, const TMaskTo extends string = "">(props: LinkComponentProps<"a", TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & {
7
+ locale?: TLocale;
8
+ }) => ReactElement;
9
+ interface GetPathnameOptions<TLocale extends string> {
10
+ href: string;
11
+ locale?: TLocale;
12
+ }
13
+ type LocalizedUseNavigateResult<TLocale extends string, TDefaultFrom extends string> = <TRouter extends RegisteredRouter, TTo extends string | undefined, TFrom extends string = TDefaultFrom, TMaskFrom extends string = TFrom, TMaskTo extends string = "">(options: NavigateOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & {
14
+ locale?: TLocale;
15
+ }) => Promise<void>;
16
+ type LocalizedBuildLocationFn<TLocale extends string> = (options: Record<string, unknown> & {
17
+ locale?: TLocale;
18
+ }) => ReturnType<BuildLocationFn>;
19
+ type LocalizedPreloadRouteFn<TLocale extends string> = (options: Record<string, unknown> & {
20
+ locale?: TLocale;
21
+ }) => Promise<unknown>;
22
+ type LocalizedRouter<TLocale extends string, TRouter extends AnyRouter> = Omit<TRouter, "buildLocation" | "navigate" | "preloadRoute"> & {
23
+ buildLocation: LocalizedBuildLocationFn<TLocale>;
24
+ navigate: LocalizedUseNavigateResult<TLocale, string>;
25
+ preloadRoute?: LocalizedPreloadRouteFn<TLocale>;
26
+ };
27
+ interface NavigationFunctionsConfig<TLocale extends string, TDefaultFrom extends string, TRouter extends AnyRouter, TParams extends ParamsLike> {
28
+ Link: LinkComponent<"a", TDefaultFrom>;
29
+ routing: RoutingConfig<TLocale>;
30
+ useLocation: () => {
31
+ pathname: string;
32
+ };
33
+ useNavigate: () => UseNavigateResult<TDefaultFrom>;
34
+ useParams: () => TParams;
35
+ useRouter: () => TRouter;
36
+ }
37
+ declare function createNavigationFunctions<TLocale extends string, TDefaultFrom extends string = string, TRouter extends AnyRouter = AnyRouter, TParams extends ParamsLike = ParamsLike>({ Link: LinkComponent, routing, useLocation: useInjectedLocation, useNavigate: useInjectedNavigate, useParams: useInjectedParams, useRouter: useInjectedRouter, }: NavigationFunctionsConfig<TLocale, TDefaultFrom, TRouter, TParams>): {
38
+ Link: LocalizedLinkComponent<TLocale, TDefaultFrom>;
39
+ getPathname: ({ href, locale, }: GetPathnameOptions<TLocale>) => string;
40
+ useLocale: () => TLocale;
41
+ useNavigate: () => LocalizedUseNavigateResult<TLocale, TDefaultFrom>;
42
+ usePathname: () => string;
43
+ useRouter: () => LocalizedRouter<TLocale, TRouter>;
44
+ };
45
+
46
+ export { type GetPathnameOptions, type LocalizedBuildLocationFn, type LocalizedPreloadRouteFn, type LocalizedRouter, type LocalizedUseNavigateResult, type NavigationFunctionsConfig, createNavigationFunctions };