Skip to content

Commit 767af55

Browse files
feat: add url prefixes
1 parent 1db3b24 commit 767af55

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
interface IRouterServiceParams<TRoutesConfig extends TRouterConfig> {
22
routes: TRoutesConfig;
33
domain?: string;
4+
prefix?: string;
45
}
56

67
type TRouterConfig = {

src/manager.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,25 @@ class Manager<TRoutesConfig extends TRouterConfig> {
3939
*/
4040
protected readonly domain?: string;
4141

42+
/**
43+
* Url prefix (e.g. language code)
44+
*/
45+
protected prefix?: string;
46+
4247
/**
4348
* @constructor
4449
*/
45-
constructor({ routes, domain }: IRouterServiceParams<TRoutesConfig>) {
50+
constructor({ routes, domain, prefix }: IRouterServiceParams<TRoutesConfig>) {
4651
this.domain = domain;
4752
this.routes = routes;
53+
this.prefix = prefix;
54+
}
55+
56+
/**
57+
* Set url prefix
58+
*/
59+
public setPrefix(prefix?: string): void {
60+
this.prefix = prefix;
4861
}
4962

5063
/**
@@ -104,8 +117,9 @@ class Manager<TRoutesConfig extends TRouterConfig> {
104117
): string => {
105118
const path = this.getRouteUrl(route as string, { isFullPath: true });
106119
const url = generatePath(path, params);
120+
const withPrefix = this.prefix ? `/${this.prefix}${url}` : url;
107121

108-
return hasDomain && this.domain ? `${this.domain}${url}` : url;
122+
return hasDomain && this.domain ? `${this.domain}${withPrefix}` : withPrefix;
109123
};
110124

111125
/**

0 commit comments

Comments
 (0)