Skip to content

Commit 4d221a4

Browse files
authored
feat: dynamic params interceptor (#82)
1 parent 8b04242 commit 4d221a4

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

packages/vue-i18n-routing/src/compatibles/routing.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ function getLocalizableMetaFromDynamicParams(
292292
}
293293
}
294294

295+
export type MetaDynamicParamsInterceptor = (
296+
route: Route | RouteLocationNormalizedLoaded,
297+
key: Required<I18nRoutingOptions>['dynamicRouteParamsKey']
298+
) => Record<Locale, unknown>
299+
295300
/**
296301
* Returns path of the current route for specified locale.
297302
*
@@ -309,7 +314,10 @@ export function switchLocalePath(this: RoutingProxy, locale: Locale): string {
309314
return ''
310315
}
311316

312-
const { switchLocalePathIntercepter, dynamicRouteParamsKey } = getI18nRoutingOptions(this.router, this)
317+
const { switchLocalePathIntercepter, dynamicRouteParamsKey, dynamicParamsInterceptor } = getI18nRoutingOptions(
318+
this.router,
319+
this
320+
)
313321

314322
// prettier-ignore
315323
const routeValue = isVue3
@@ -318,14 +326,17 @@ export function switchLocalePath(this: RoutingProxy, locale: Locale): string {
318326
? route.value
319327
: route
320328
const routeCopy = routeToObject(routeValue)
329+
const langSwitchParamsIntercepted = dynamicParamsInterceptor?.()?.value?.[locale]
321330
const langSwitchParams = getLocalizableMetaFromDynamicParams(route, dynamicRouteParamsKey)[locale] || {}
322331

332+
const resolvedParams = langSwitchParamsIntercepted ?? langSwitchParams ?? {}
333+
323334
// eslint-disable-next-line @typescript-eslint/no-explicit-any
324335
const _baseRoute: any = {
325336
name,
326337
params: {
327338
...routeCopy.params,
328-
...langSwitchParams
339+
...resolvedParams
329340
}
330341
}
331342
if (isVue2) {

packages/vue-i18n-routing/src/compatibles/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export function getI18nRoutingOptions(
4141
switchLocalePathIntercepter = DefaultSwitchLocalePathIntercepter,
4242
dynamicRouteParamsKey = DEFAULT_DYNAMIC_PARAMS_KEY
4343
}: I18nRoutingGlobalOptions = {}
44-
): Required<I18nRoutingGlobalOptions> {
44+
): Required<Omit<I18nRoutingGlobalOptions, 'dynamicParamsInterceptor'>> &
45+
Pick<I18nRoutingGlobalOptions, 'dynamicParamsInterceptor'> {
4546
const options = getGlobalOptions(router)
4647
return {
4748
defaultLocale: proxy.defaultLocale || options.defaultLocale || defaultLocale,
@@ -55,7 +56,8 @@ export function getI18nRoutingOptions(
5556
prefixable: proxy.prefixable || options.prefixable || prefixable,
5657
switchLocalePathIntercepter:
5758
proxy.switchLocalePathIntercepter || options.switchLocalePathIntercepter || switchLocalePathIntercepter,
58-
dynamicRouteParamsKey: proxy.dynamicRouteParamsKey || options.dynamicRouteParamsKey || dynamicRouteParamsKey
59+
dynamicRouteParamsKey: proxy.dynamicRouteParamsKey || options.dynamicRouteParamsKey || dynamicRouteParamsKey,
60+
dynamicParamsInterceptor: options.dynamicParamsInterceptor || undefined
5961
}
6062
}
6163

packages/vue-i18n-routing/src/extends/router.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import type {
2727
RouteLocationNormalizedLoaded,
2828
RouteLocationNormalized
2929
} from '@intlify/vue-router-bridge'
30+
import type { Ref } from 'vue-demi'
31+
import type { Locale } from 'vue-i18n'
3032

3133
/**
3234
* Global options for i18n routing
@@ -42,8 +44,9 @@ export type I18nRoutingGlobalOptions<Context = unknown> = Pick<
4244
| 'prefixable'
4345
| 'switchLocalePathIntercepter'
4446
| 'dynamicRouteParamsKey'
45-
> & { localeCodes?: string[] }
47+
> & { localeCodes?: string[]; dynamicParamsInterceptor?: DynamicParamsInterceptor }
4648

49+
export type DynamicParamsInterceptor = () => Ref<Record<Locale, unknown>>
4750
const GlobalOptionsRegistory = makeSymbol('vue-i18n-routing-gor')
4851

4952
/**

0 commit comments

Comments
 (0)