RoutingHelper
The RoutingHelper object offers util functions for URL paths.
import { RoutingHelper } from '@archibald/core';
addLeadingSlash
This method adds a leading slash to the path if none is there.
RoutingHelper.addLeadingSlash(path);
| Name | Type | Optional | Description |
|---|---|---|---|
| path | string | ✔️ | URL path (e.g. en/users). |
hasLeadingSlash
This method returns whether the path has a leading slash.
RoutingHelper.hasLeadingSlash(path);
| Name | Type | Optional | Description |
|---|---|---|---|
| path | string | ✔️ | URL path (e.g. /en/users). |
fixTrailingSlash
This method fixes the trailing slash of a path. It is expected that the path does not contain a query.
RoutingHelper.fixTrailingSlash(path);
| Name | Type | Description |
|---|---|---|
| path | string | Path which should be fixed. |
startsWithPrefix
This method returns whether the path starts with the given route prefix.
RoutingHelper.startsWithPrefix(path, routePrefix);
| Name | Type | Optional | Description |
|---|---|---|---|
| path | string | ✔️ | URL path to check. |
| routePrefix | string | ✔️ | Route prefix if available. |
endsWithSlash
This method returns whether the path ends with a slash.
RoutingHelper.endsWithSlash(path);
| Name | Type | Optional | Description |
|---|---|---|---|
| path | string | ✔️ | URL path to check. |
addPrefixToPath
This method prepends the route prefix to the path (unless the path already starts with it) and cleans up the result.
RoutingHelper.addPrefixToPath(routePrefix, path, removeTrailingSlash);
| Name | Type | Optional | Description |
|---|---|---|---|
| routePrefix | string | ✔️ | Route prefix if available. |
| path | string | ✔️ | URL path. |
| removeTrailingSlash | boolean | ✔️ | Toggle if the trailing slash should be removed. Default: true. |
removePrefixFromPath
This method cleans the pathname from a configured route prefix.
RoutingHelper.removePrefixFromPath(path, routePrefix, removeTrailingSlash);
| Name | Type | Optional | Description |
|---|---|---|---|
| path | string | URL path (e.g. /en/users). | |
| routePrefix | string | ✔️ | The prefix string of the route. |
| removeTrailingSlash | boolean | ✔️ | Toggle if the trailing slash should be removed. Default: true. |
cleanUpPath
This method removes multiple neighbouring slashes from the path (e.g. /de//p////123/ becomes /de/p/123) and removes the trailing slash by default.
RoutingHelper.cleanUpPath(path, removeTrailingSlash);
| Name | Type | Optional | Description |
|---|---|---|---|
| path | string | ✔️ | URL path which should be cleaned. |
| removeTrailingSlash | boolean | ✔️ | Toggle if the trailing slash should be removed. Default: true. |
extractLanguage
This method extracts the language of a given url.
RoutingHelper.extractLanguage(path, routePrefix);
| Name | Type | Optional | Description |
|---|---|---|---|
| path | string | ✔️ | Path of which language should be extracted. |
| routePrefix | string | ✔️ | Route prefix if available. |
fixLanguagePath
This method checks if the path contains a supported language, otherwise redirects to a supported one. It returns { url, language }.
RoutingHelper.fixLanguagePath(languages, path, preferredLanguage, routePrefix);
| Name | Type | Optional | Description |
|---|---|---|---|
| languages | string[] | Accepted languages. | |
| path | string | Path which should be fixed. | |
| preferredLanguage | string | ✔️ | Preferred language which should be redirected to. |
| routePrefix | string | ✔️ | Route prefix if available. |