@webiny/app 6.3.0 → 6.4.0-beta.1
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.
- package/App.js +70 -77
- package/App.js.map +1 -1
- package/AppContainer.js +3 -6
- package/AppContainer.js.map +1 -1
- package/apollo-client/InMemoryCache.js +11 -13
- package/apollo-client/InMemoryCache.js.map +1 -1
- package/apollo-client/IntrospectionFragmentMatcher.js +26 -38
- package/apollo-client/IntrospectionFragmentMatcher.js.map +1 -1
- package/components/Image.js +11 -17
- package/components/Image.js.map +1 -1
- package/components/index.js +0 -2
- package/config/RouterConfig/Route.js +23 -28
- package/config/RouterConfig/Route.js.map +1 -1
- package/config/RouterConfig.js +11 -10
- package/config/RouterConfig.js.map +1 -1
- package/config.js +19 -20
- package/config.js.map +1 -1
- package/contexts/Ui/index.js +26 -28
- package/contexts/Ui/index.js.map +1 -1
- package/core/Plugin.js +7 -9
- package/core/Plugin.js.map +1 -1
- package/core/Plugins.js +15 -34
- package/core/Plugins.js.map +1 -1
- package/core/Provider.js +5 -13
- package/core/Provider.js.map +1 -1
- package/core/createProvider.js +3 -6
- package/core/createProvider.js.map +1 -1
- package/core/createProviderPlugin.js +8 -13
- package/core/createProviderPlugin.js.map +1 -1
- package/errors/AuthenticationErrorEvent.js +8 -5
- package/errors/AuthenticationErrorEvent.js.map +1 -1
- package/errors/NetworkErrorEvent.js +8 -5
- package/errors/NetworkErrorEvent.js.map +1 -1
- package/errors/abstractions.js +3 -2
- package/errors/abstractions.js.map +1 -1
- package/errors/index.js +1 -4
- package/exports/admin/env-config.js +0 -2
- package/exports/admin/graphql-client.js +0 -2
- package/exports/admin/local-storage.js +0 -2
- package/exports/admin/router.js +0 -2
- package/exports/admin/security.js +0 -2
- package/exports/admin.js +0 -2
- package/features/envConfig/EnvConfig.js +12 -13
- package/features/envConfig/EnvConfig.js.map +1 -1
- package/features/envConfig/abstractions.js +2 -1
- package/features/envConfig/abstractions.js.map +1 -1
- package/features/envConfig/feature.js +9 -8
- package/features/envConfig/feature.js.map +1 -1
- package/features/envConfig/index.js +0 -2
- package/features/eventPublisher/EventPublisher.js +13 -21
- package/features/eventPublisher/EventPublisher.js.map +1 -1
- package/features/eventPublisher/abstractions.js +7 -9
- package/features/eventPublisher/abstractions.js.map +1 -1
- package/features/eventPublisher/feature.js +13 -12
- package/features/eventPublisher/feature.js.map +1 -1
- package/features/eventPublisher/index.js +1 -3
- package/features/graphqlClient/AuthenticationErrorPublishing.js +26 -26
- package/features/graphqlClient/AuthenticationErrorPublishing.js.map +1 -1
- package/features/graphqlClient/BatchingGraphQLClient.js +104 -149
- package/features/graphqlClient/BatchingGraphQLClient.js.map +1 -1
- package/features/graphqlClient/FetchGraphQLClient.js +41 -48
- package/features/graphqlClient/FetchGraphQLClient.js.map +1 -1
- package/features/graphqlClient/NetworkErrorPublishing.js +35 -42
- package/features/graphqlClient/NetworkErrorPublishing.js.map +1 -1
- package/features/graphqlClient/RequestValue.js +42 -41
- package/features/graphqlClient/RequestValue.js.map +1 -1
- package/features/graphqlClient/RetryGraphQLClient.js +36 -45
- package/features/graphqlClient/RetryGraphQLClient.js.map +1 -1
- package/features/graphqlClient/__tests__/GraphQLClient.test.js +383 -356
- package/features/graphqlClient/__tests__/GraphQLClient.test.js.map +1 -1
- package/features/graphqlClient/abstractions.js +2 -1
- package/features/graphqlClient/abstractions.js.map +1 -1
- package/features/graphqlClient/feature.js +14 -20
- package/features/graphqlClient/feature.js.map +1 -1
- package/features/graphqlClient/index.js +0 -2
- package/features/graphqlClient/types.js +0 -3
- package/features/localStorage/BrowserLocalStorageGateway.js +56 -63
- package/features/localStorage/BrowserLocalStorageGateway.js.map +1 -1
- package/features/localStorage/LocalStorage.js +27 -24
- package/features/localStorage/LocalStorage.js.map +1 -1
- package/features/localStorage/LocalStorageRepository.js +75 -88
- package/features/localStorage/LocalStorageRepository.js.map +1 -1
- package/features/localStorage/abstractions.js +5 -16
- package/features/localStorage/abstractions.js.map +1 -1
- package/features/localStorage/feature.js +17 -28
- package/features/localStorage/feature.js.map +1 -1
- package/features/localStorage/index.js +1 -3
- package/features/mainGraphQLClient/MainGraphQLClient.js +18 -14
- package/features/mainGraphQLClient/MainGraphQLClient.js.map +1 -1
- package/features/mainGraphQLClient/abstractions.js +2 -1
- package/features/mainGraphQLClient/abstractions.js.map +1 -1
- package/features/mainGraphQLClient/feature.js +11 -10
- package/features/mainGraphQLClient/feature.js.map +1 -1
- package/features/mainGraphQLClient/index.js +0 -2
- package/features/router/HistoryRouterGateway.js +75 -105
- package/features/router/HistoryRouterGateway.js.map +1 -1
- package/features/router/HistoryRouterGateway.test.js +184 -193
- package/features/router/HistoryRouterGateway.test.js.map +1 -1
- package/features/router/Route.js +38 -54
- package/features/router/Route.js.map +1 -1
- package/features/router/RouteUrl.js +84 -217
- package/features/router/RouteUrl.js.map +1 -1
- package/features/router/Router.js +67 -124
- package/features/router/Router.js.map +1 -1
- package/features/router/RouterPresenter.js +44 -49
- package/features/router/RouterPresenter.js.map +1 -1
- package/features/router/RouterRepository.js +103 -111
- package/features/router/RouterRepository.js.map +1 -1
- package/features/router/RouterRepository.test.js +119 -150
- package/features/router/RouterRepository.test.js.map +1 -1
- package/features/router/abstractions.js +4 -14
- package/features/router/abstractions.js.map +1 -1
- package/features/router/feature.js +13 -12
- package/features/router/feature.js.map +1 -1
- package/features/router/index.js +1 -3
- package/helpers/InterfaceGenerator/date.js +0 -3
- package/helpers/InterfaceGenerator/id.js +0 -3
- package/helpers/InterfaceGenerator/identity.js +0 -3
- package/helpers/InterfaceGenerator/index.js +0 -3
- package/helpers/InterfaceGenerator/numeric.js +0 -3
- package/helpers/InterfaceGenerator/truthful.js +0 -3
- package/hooks/useAutocomplete/index.js +0 -2
- package/hooks/useAutocomplete/useAutocomplete.js +12 -15
- package/hooks/useAutocomplete/useAutocomplete.js.map +1 -1
- package/hooks/useDataList/functions/getData.js +2 -1
- package/hooks/useDataList/functions/getData.js.map +1 -1
- package/hooks/useDataList/functions/getError.js +2 -1
- package/hooks/useDataList/functions/getError.js.map +1 -1
- package/hooks/useDataList/functions/getMeta.js +2 -1
- package/hooks/useDataList/functions/getMeta.js.map +1 -1
- package/hooks/useDataList/functions/index.js +0 -2
- package/hooks/useDataList/functions/searchDataByKey.js +9 -14
- package/hooks/useDataList/functions/searchDataByKey.js.map +1 -1
- package/hooks/useDataList/index.js +0 -2
- package/hooks/useDataList/useDataList.js +131 -152
- package/hooks/useDataList/useDataList.js.map +1 -1
- package/hooks/useDataList/utils/index.js +0 -2
- package/hooks/useDataList/utils/prepareLoadListParams.js +25 -34
- package/hooks/useDataList/utils/prepareLoadListParams.js.map +1 -1
- package/hooks/useHandler.js +12 -16
- package/hooks/useHandler.js.map +1 -1
- package/hooks/useHandlers.js +18 -20
- package/hooks/useHandlers.js.map +1 -1
- package/hooks/useRegisterLegacyPlugin.js +7 -6
- package/hooks/useRegisterLegacyPlugin.js.map +1 -1
- package/hooks/useUi.js +2 -3
- package/hooks/useUi.js.map +1 -1
- package/i18n/i18n.js +8 -4
- package/i18n/i18n.js.map +1 -1
- package/i18n/index.js +0 -2
- package/index.js +6 -11
- package/package.json +14 -16
- package/plugins/AddQuerySelectionPlugin.js +36 -61
- package/plugins/AddQuerySelectionPlugin.js.map +1 -1
- package/plugins/ApolloCacheObjectIdPlugin.js +12 -11
- package/plugins/ApolloCacheObjectIdPlugin.js.map +1 -1
- package/plugins/ApolloDynamicLink.js +20 -22
- package/plugins/ApolloDynamicLink.js.map +1 -1
- package/plugins/ApolloLinkPlugin.js +16 -15
- package/plugins/ApolloLinkPlugin.js.map +1 -1
- package/plugins/ConsoleLinkPlugin.js +19 -21
- package/plugins/ConsoleLinkPlugin.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin/ErrorOverlay.js +39 -45
- package/plugins/NetworkErrorLinkPlugin/ErrorOverlay.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin/GqlErrorOverlay.js +18 -22
- package/plugins/NetworkErrorLinkPlugin/GqlErrorOverlay.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin/LocalAwsLambdaTimeoutMessage.js +21 -29
- package/plugins/NetworkErrorLinkPlugin/LocalAwsLambdaTimeoutMessage.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin/StyledComponents.js +6 -15
- package/plugins/NetworkErrorLinkPlugin/StyledComponents.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin/Typography.js +9 -14
- package/plugins/NetworkErrorLinkPlugin/Typography.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin/assets/close_24px.js +19 -0
- package/plugins/NetworkErrorLinkPlugin/assets/close_24px.js.map +1 -0
- package/plugins/NetworkErrorLinkPlugin/createErrorOverlay.js +11 -18
- package/plugins/NetworkErrorLinkPlugin/createErrorOverlay.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin.js +41 -58
- package/plugins/NetworkErrorLinkPlugin.js.map +1 -1
- package/plugins/OmitTypenameLinkPlugin.js +9 -14
- package/plugins/OmitTypenameLinkPlugin.js.map +1 -1
- package/plugins/TenantHeaderLinkPlugin.js +23 -32
- package/plugins/TenantHeaderLinkPlugin.js.map +1 -1
- package/plugins/components/Image.js +8 -15
- package/plugins/components/Image.js.map +1 -1
- package/plugins/image.js +94 -133
- package/plugins/image.js.map +1 -1
- package/plugins/index.js +35 -59
- package/plugins/index.js.map +1 -1
- package/presentation/envConfig/useEnvConfig.js +4 -8
- package/presentation/envConfig/useEnvConfig.js.map +1 -1
- package/presentation/localStorage/index.js +0 -2
- package/presentation/localStorage/useLocalStorage.js +10 -16
- package/presentation/localStorage/useLocalStorage.js.map +1 -1
- package/presentation/localStorage/useLocalStorageValue.js +15 -13
- package/presentation/localStorage/useLocalStorageValue.js.map +1 -1
- package/presentation/localStorage/useLocalStorageValues.js +23 -30
- package/presentation/localStorage/useLocalStorageValues.js.map +1 -1
- package/presentation/router/RouteElementRegistry.js +22 -25
- package/presentation/router/RouteElementRegistry.js.map +1 -1
- package/presentation/router/abstractions.js +2 -1
- package/presentation/router/abstractions.js.map +1 -1
- package/presentation/router/components/Redirect.js +8 -10
- package/presentation/router/components/Redirect.js.map +1 -1
- package/presentation/router/components/RouteContent.js +14 -17
- package/presentation/router/components/RouteContent.js.map +1 -1
- package/presentation/router/components/RouteLink.js +11 -16
- package/presentation/router/components/RouteLink.js.map +1 -1
- package/presentation/router/components/SimpleLink.js +18 -24
- package/presentation/router/components/SimpleLink.js.map +1 -1
- package/presentation/router/hooks/useRoute.js +15 -23
- package/presentation/router/hooks/useRoute.js.map +1 -1
- package/presentation/router/hooks/useRouter.js +22 -23
- package/presentation/router/hooks/useRouter.js.map +1 -1
- package/presentation/router/index.js +0 -2
- package/presentation/router/types.js +0 -3
- package/renderApp.js +7 -6
- package/renderApp.js.map +1 -1
- package/router.js +0 -3
- package/rslib-runtime.js +14 -0
- package/rslib-runtime.js.map +1 -0
- package/shared/di/DiContainerProvider.js +8 -12
- package/shared/di/DiContainerProvider.js.map +1 -1
- package/shared/di/createFeature.js +7 -8
- package/shared/di/createFeature.js.map +1 -1
- package/shared/di/useFeature.js +7 -3
- package/shared/di/useFeature.js.map +1 -1
- package/static/svg/close_24px.33adaadc.svg +1 -0
- package/types.js +0 -3
- package/utils/createGenericContext.js +17 -21
- package/utils/createGenericContext.js.map +1 -1
- package/utils/createHashing.js +13 -16
- package/utils/createHashing.js.map +1 -1
- package/utils/index.js +0 -2
- package/utils/legacyPluginToReactComponent.js +11 -10
- package/utils/legacyPluginToReactComponent.js.map +1 -1
- package/components/index.js.map +0 -1
- package/errors/index.js.map +0 -1
- package/exports/admin/env-config.js.map +0 -1
- package/exports/admin/graphql-client.js.map +0 -1
- package/exports/admin/local-storage.js.map +0 -1
- package/exports/admin/router.js.map +0 -1
- package/exports/admin/security.js.map +0 -1
- package/exports/admin.js.map +0 -1
- package/features/envConfig/index.js.map +0 -1
- package/features/eventPublisher/index.js.map +0 -1
- package/features/graphqlClient/index.js.map +0 -1
- package/features/graphqlClient/types.js.map +0 -1
- package/features/localStorage/index.js.map +0 -1
- package/features/mainGraphQLClient/index.js.map +0 -1
- package/features/router/index.js.map +0 -1
- package/helpers/InterfaceGenerator/date.js.map +0 -1
- package/helpers/InterfaceGenerator/id.js.map +0 -1
- package/helpers/InterfaceGenerator/identity.js.map +0 -1
- package/helpers/InterfaceGenerator/index.js.map +0 -1
- package/helpers/InterfaceGenerator/numeric.js.map +0 -1
- package/helpers/InterfaceGenerator/truthful.js.map +0 -1
- package/hooks/useAutocomplete/index.js.map +0 -1
- package/hooks/useDataList/functions/index.js.map +0 -1
- package/hooks/useDataList/index.js.map +0 -1
- package/hooks/useDataList/utils/index.js.map +0 -1
- package/i18n/index.js.map +0 -1
- package/index.js.map +0 -1
- package/presentation/localStorage/index.js.map +0 -1
- package/presentation/router/index.js.map +0 -1
- package/presentation/router/types.js.map +0 -1
- package/react-butterfiles/Files.d.ts +0 -69
- package/react-butterfiles/Files.js +0 -227
- package/react-butterfiles/Files.js.map +0 -1
- package/react-butterfiles/file.todo +0 -1
- package/react-butterfiles/index.d.ts +0 -2
- package/react-butterfiles/index.js +0 -4
- package/react-butterfiles/index.js.map +0 -1
- package/react-butterfiles/utils/generateId.d.ts +0 -1
- package/react-butterfiles/utils/generateId.js +0 -5
- package/react-butterfiles/utils/generateId.js.map +0 -1
- package/react-butterfiles/utils/readFileContent.d.ts +0 -1
- package/react-butterfiles/utils/readFileContent.js +0 -15
- package/react-butterfiles/utils/readFileContent.js.map +0 -1
- package/router.js.map +0 -1
- package/types.js.map +0 -1
- package/utils/index.js.map +0 -1
|
@@ -1,232 +1,99 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
* Public API uses static methods, which internally construct RouteUrl instances
|
|
6
|
-
* to handle the logic through proper instance methods.
|
|
7
|
-
*/
|
|
8
|
-
export class RouteUrl {
|
|
9
|
-
static patternCache = new Map();
|
|
10
|
-
constructor(pattern, params, baseUrl) {
|
|
11
|
-
this.pattern = pattern;
|
|
12
|
-
this.params = params;
|
|
13
|
-
this.baseUrl = this.normalizeBaseUrl(baseUrl);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Generate a URL from a route pattern and parameters.
|
|
18
|
-
* Path parameters are injected into the pattern, remaining parameters become query strings.
|
|
19
|
-
*
|
|
20
|
-
* @param pattern - Route pattern (e.g., '/security/api-keys/:id')
|
|
21
|
-
* @param params - Parameters object (e.g., { id: 123, new: true })
|
|
22
|
-
* @param baseUrl - Optional base URL to prepend (e.g., '/tenant123')
|
|
23
|
-
* @returns Generated URL string
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* RouteUrl.fromPattern('/security/api-keys/:id', { id: 123, new: true })
|
|
27
|
-
* // Returns: '/security/api-keys/123?new=true'
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* RouteUrl.fromPattern('/file-manager', {}, '/tenant123')
|
|
31
|
-
* // Returns: '/tenant123/file-manager'
|
|
32
|
-
*/
|
|
33
|
-
static fromPattern(pattern, params, baseUrl) {
|
|
34
|
-
const routeUrl = new RouteUrl(pattern, params, baseUrl);
|
|
35
|
-
return routeUrl.generate();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Match a pathname against a route pattern and extract parameters.
|
|
40
|
-
* If baseUrl is provided, it will be stripped from the pathname before matching.
|
|
41
|
-
*
|
|
42
|
-
* @param pathname - The pathname to match (e.g., '/tenant123/security/api-keys/123')
|
|
43
|
-
* @param pattern - Route pattern (e.g., '/security/api-keys/:id')
|
|
44
|
-
* @param baseUrl - Optional base URL to strip before matching (e.g., '/tenant123')
|
|
45
|
-
* @returns Object with extracted params if matched, null otherwise
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* RouteUrl.match('/security/api-keys/123', '/security/api-keys/:id')
|
|
49
|
-
* // Returns: { params: { id: '123' } }
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* RouteUrl.match('/tenant123/file-manager', '/file-manager', '/tenant123')
|
|
53
|
-
* // Returns: { params: {} }
|
|
54
|
-
*/
|
|
55
|
-
static match(pathname, pattern, baseUrl) {
|
|
56
|
-
const routeUrl = new RouteUrl(pattern, undefined, baseUrl);
|
|
57
|
-
return routeUrl.matchPathname(pathname);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Generate a URL from the instance's pattern, params, and baseUrl.
|
|
62
|
-
*/
|
|
63
|
-
generate() {
|
|
64
|
-
if (!this.params) {
|
|
65
|
-
return this.baseUrl + this.pattern;
|
|
1
|
+
class RouteUrl {
|
|
2
|
+
static{
|
|
3
|
+
this.patternCache = new Map();
|
|
66
4
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
// Replace :param with actual values
|
|
72
|
-
let url = this.replacePathParams();
|
|
73
|
-
|
|
74
|
-
// Handle empty url
|
|
75
|
-
if (!url || url === "") {
|
|
76
|
-
url = "/";
|
|
5
|
+
constructor(pattern, params, baseUrl){
|
|
6
|
+
this.pattern = pattern;
|
|
7
|
+
this.params = params;
|
|
8
|
+
this.baseUrl = this.normalizeBaseUrl(baseUrl);
|
|
77
9
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
// Add query parameters for keys not used in the path
|
|
83
|
-
const queryString = this.buildQueryString(pathKeys);
|
|
84
|
-
if (queryString) {
|
|
85
|
-
url += `?${queryString}`;
|
|
86
|
-
}
|
|
87
|
-
return url;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Match a pathname against the instance's pattern.
|
|
92
|
-
*/
|
|
93
|
-
matchPathname(pathname) {
|
|
94
|
-
// Strip baseUrl from pathname if provided
|
|
95
|
-
if (this.baseUrl && pathname.startsWith(this.baseUrl)) {
|
|
96
|
-
pathname = pathname.slice(this.baseUrl.length) || "/";
|
|
97
|
-
} else if (this.baseUrl) {
|
|
98
|
-
// pathname doesn't start with baseUrl, no match
|
|
99
|
-
return null;
|
|
10
|
+
static fromPattern(pattern, params, baseUrl) {
|
|
11
|
+
const routeUrl = new RouteUrl(pattern, params, baseUrl);
|
|
12
|
+
return routeUrl.generate();
|
|
100
13
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return {
|
|
105
|
-
params: {}
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Get or create cached pattern
|
|
110
|
-
let compiled = RouteUrl.patternCache.get(this.pattern);
|
|
111
|
-
if (!compiled) {
|
|
112
|
-
compiled = this.compilePattern();
|
|
113
|
-
RouteUrl.patternCache.set(this.pattern, compiled);
|
|
14
|
+
static match(pathname, pattern, baseUrl) {
|
|
15
|
+
const routeUrl = new RouteUrl(pattern, void 0, baseUrl);
|
|
16
|
+
return routeUrl.matchPathname(pathname);
|
|
114
17
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
18
|
+
generate() {
|
|
19
|
+
if (!this.params) return this.baseUrl + this.pattern;
|
|
20
|
+
const pathKeys = this.extractPathKeys();
|
|
21
|
+
let url = this.replacePathParams();
|
|
22
|
+
if (!url || "" === url) url = "/";
|
|
23
|
+
url = this.baseUrl + url;
|
|
24
|
+
const queryString = this.buildQueryString(pathKeys);
|
|
25
|
+
if (queryString) url += `?${queryString}`;
|
|
26
|
+
return url;
|
|
118
27
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
while ((match = paramRegex.exec(this.pattern)) !== null) {
|
|
141
|
-
pathKeys.add(match[1]);
|
|
28
|
+
matchPathname(pathname) {
|
|
29
|
+
if (this.baseUrl && pathname.startsWith(this.baseUrl)) pathname = pathname.slice(this.baseUrl.length) || "/";
|
|
30
|
+
else if (this.baseUrl) return null;
|
|
31
|
+
if ("*" === this.pattern || "(.*)" === this.pattern) return {
|
|
32
|
+
params: {}
|
|
33
|
+
};
|
|
34
|
+
let compiled = RouteUrl.patternCache.get(this.pattern);
|
|
35
|
+
if (!compiled) {
|
|
36
|
+
compiled = this.compilePattern();
|
|
37
|
+
RouteUrl.patternCache.set(this.pattern, compiled);
|
|
38
|
+
}
|
|
39
|
+
const match = pathname.match(compiled.regex);
|
|
40
|
+
if (!match) return null;
|
|
41
|
+
const params = {};
|
|
42
|
+
compiled.keys.forEach((key, index)=>{
|
|
43
|
+
const value = match[index + 1];
|
|
44
|
+
if (void 0 !== value) params[key] = decodeURIComponent(value);
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
params
|
|
48
|
+
};
|
|
142
49
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
replacePathParams() {
|
|
150
|
-
return this.pattern.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, (_, key) => {
|
|
151
|
-
const value = this.params[key];
|
|
152
|
-
if (value === undefined || value === null) {
|
|
153
|
-
return "";
|
|
154
|
-
}
|
|
155
|
-
return encodeURIComponent(String(value));
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Build query string from params that are not path parameters.
|
|
161
|
-
*/
|
|
162
|
-
buildQueryString(pathKeys) {
|
|
163
|
-
const queryParams = {};
|
|
164
|
-
for (const key in this.params) {
|
|
165
|
-
if (!pathKeys.has(key)) {
|
|
166
|
-
queryParams[key] = this.params[key];
|
|
167
|
-
}
|
|
50
|
+
extractPathKeys() {
|
|
51
|
+
const pathKeys = new Set();
|
|
52
|
+
const paramRegex = /:([a-zA-Z_][a-zA-Z0-9_]*)/g;
|
|
53
|
+
let match;
|
|
54
|
+
while(null !== (match = paramRegex.exec(this.pattern)))pathKeys.add(match[1]);
|
|
55
|
+
return pathKeys;
|
|
168
56
|
}
|
|
169
|
-
|
|
170
|
-
|
|
57
|
+
replacePathParams() {
|
|
58
|
+
return this.pattern.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, (_, key)=>{
|
|
59
|
+
const value = this.params[key];
|
|
60
|
+
if (null == value) return "";
|
|
61
|
+
return encodeURIComponent(String(value));
|
|
62
|
+
});
|
|
171
63
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
64
|
+
buildQueryString(pathKeys) {
|
|
65
|
+
const queryParams = {};
|
|
66
|
+
for(const key in this.params)if (!pathKeys.has(key)) queryParams[key] = this.params[key];
|
|
67
|
+
if (0 === Object.keys(queryParams).length) return "";
|
|
68
|
+
const searchParams = new URLSearchParams();
|
|
69
|
+
for(const key in queryParams){
|
|
70
|
+
const value = queryParams[key];
|
|
71
|
+
if (null != value) if (Array.isArray(value)) value.forEach((v)=>searchParams.append(key, String(v)));
|
|
72
|
+
else searchParams.append(key, String(value));
|
|
180
73
|
}
|
|
181
|
-
|
|
74
|
+
return searchParams.toString();
|
|
182
75
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
normalizeBaseUrl(baseUrl) {
|
|
190
|
-
if (!baseUrl || baseUrl === "/") {
|
|
191
|
-
return "";
|
|
76
|
+
normalizeBaseUrl(baseUrl) {
|
|
77
|
+
if (!baseUrl || "/" === baseUrl) return "";
|
|
78
|
+
let normalized = baseUrl;
|
|
79
|
+
if (!normalized.startsWith("/")) normalized = "/" + normalized;
|
|
80
|
+
if (normalized.endsWith("/")) normalized = normalized.slice(0, -1);
|
|
81
|
+
return normalized;
|
|
192
82
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
83
|
+
compilePattern() {
|
|
84
|
+
const keys = [];
|
|
85
|
+
let regexPattern = this.pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\\\*/g, ".*");
|
|
86
|
+
regexPattern = regexPattern.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, (_, key)=>{
|
|
87
|
+
keys.push(key);
|
|
88
|
+
return "([^/]+)";
|
|
89
|
+
});
|
|
90
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
|
91
|
+
return {
|
|
92
|
+
regex,
|
|
93
|
+
keys
|
|
94
|
+
};
|
|
198
95
|
}
|
|
199
|
-
|
|
200
|
-
// Remove trailing /
|
|
201
|
-
if (normalized.endsWith("/")) {
|
|
202
|
-
normalized = normalized.slice(0, -1);
|
|
203
|
-
}
|
|
204
|
-
return normalized;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Compile the instance's pattern into a regex and extract parameter names.
|
|
209
|
-
*/
|
|
210
|
-
compilePattern() {
|
|
211
|
-
const keys = [];
|
|
212
|
-
|
|
213
|
-
// Escape special regex characters except for :param syntax
|
|
214
|
-
let regexPattern = this.pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&") // Escape special chars
|
|
215
|
-
.replace(/\\\*/g, ".*"); // Handle * wildcard (already escaped by above)
|
|
216
|
-
|
|
217
|
-
// Replace :param with capture groups
|
|
218
|
-
regexPattern = regexPattern.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, (_, key) => {
|
|
219
|
-
keys.push(key);
|
|
220
|
-
return "([^/]+)"; // Match any non-slash characters
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
// Create regex that matches the entire pathname
|
|
224
|
-
const regex = new RegExp(`^${regexPattern}$`);
|
|
225
|
-
return {
|
|
226
|
-
regex,
|
|
227
|
-
keys
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
96
|
}
|
|
97
|
+
export { RouteUrl };
|
|
231
98
|
|
|
232
99
|
//# sourceMappingURL=RouteUrl.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["RouteUrl","patternCache","Map","constructor","pattern","params","baseUrl","normalizeBaseUrl","fromPattern","routeUrl","generate","match","pathname","undefined","matchPathname","pathKeys","extractPathKeys","url","replacePathParams","queryString","buildQueryString","startsWith","slice","length","compiled","get","compilePattern","set","regex","keys","forEach","key","index","value","decodeURIComponent","Set","paramRegex","exec","add","replace","_","encodeURIComponent","String","queryParams","has","Object","searchParams","URLSearchParams","Array","isArray","v","append","toString","normalized","endsWith","regexPattern","push","RegExp"],"sources":["RouteUrl.ts"],"sourcesContent":["interface Params {\n [key: string]: any;\n}\n\ninterface MatchResult {\n params: Record<string, string>;\n}\n\n/**\n * Utility class for route URL pattern matching and generation.\n * Handles simple route patterns with :param syntax (e.g., /cms/entries/:modelId).\n *\n * Public API uses static methods, which internally construct RouteUrl instances\n * to handle the logic through proper instance methods.\n */\nexport class RouteUrl {\n private static patternCache = new Map<string, { regex: RegExp; keys: string[] }>();\n\n private readonly pattern: string;\n private readonly params: Params | undefined;\n private readonly baseUrl: string;\n\n private constructor(pattern: string, params?: Params, baseUrl?: string) {\n this.pattern = pattern;\n this.params = params;\n this.baseUrl = this.normalizeBaseUrl(baseUrl);\n }\n\n /**\n * Generate a URL from a route pattern and parameters.\n * Path parameters are injected into the pattern, remaining parameters become query strings.\n *\n * @param pattern - Route pattern (e.g., '/security/api-keys/:id')\n * @param params - Parameters object (e.g., { id: 123, new: true })\n * @param baseUrl - Optional base URL to prepend (e.g., '/tenant123')\n * @returns Generated URL string\n *\n * @example\n * RouteUrl.fromPattern('/security/api-keys/:id', { id: 123, new: true })\n * // Returns: '/security/api-keys/123?new=true'\n *\n * @example\n * RouteUrl.fromPattern('/file-manager', {}, '/tenant123')\n * // Returns: '/tenant123/file-manager'\n */\n static fromPattern(pattern: string, params?: Params, baseUrl?: string): string {\n const routeUrl = new RouteUrl(pattern, params, baseUrl);\n return routeUrl.generate();\n }\n\n /**\n * Match a pathname against a route pattern and extract parameters.\n * If baseUrl is provided, it will be stripped from the pathname before matching.\n *\n * @param pathname - The pathname to match (e.g., '/tenant123/security/api-keys/123')\n * @param pattern - Route pattern (e.g., '/security/api-keys/:id')\n * @param baseUrl - Optional base URL to strip before matching (e.g., '/tenant123')\n * @returns Object with extracted params if matched, null otherwise\n *\n * @example\n * RouteUrl.match('/security/api-keys/123', '/security/api-keys/:id')\n * // Returns: { params: { id: '123' } }\n *\n * @example\n * RouteUrl.match('/tenant123/file-manager', '/file-manager', '/tenant123')\n * // Returns: { params: {} }\n */\n static match(pathname: string, pattern: string, baseUrl?: string): MatchResult | null {\n const routeUrl = new RouteUrl(pattern, undefined, baseUrl);\n return routeUrl.matchPathname(pathname);\n }\n\n /**\n * Generate a URL from the instance's pattern, params, and baseUrl.\n */\n private generate(): string {\n if (!this.params) {\n return this.baseUrl + this.pattern;\n }\n\n // Extract path parameter names from pattern\n const pathKeys = this.extractPathKeys();\n\n // Replace :param with actual values\n let url = this.replacePathParams();\n\n // Handle empty url\n if (!url || url === \"\") {\n url = \"/\";\n }\n\n // Prepend baseUrl\n url = this.baseUrl + url;\n\n // Add query parameters for keys not used in the path\n const queryString = this.buildQueryString(pathKeys);\n if (queryString) {\n url += `?${queryString}`;\n }\n\n return url;\n }\n\n /**\n * Match a pathname against the instance's pattern.\n */\n private matchPathname(pathname: string): MatchResult | null {\n // Strip baseUrl from pathname if provided\n if (this.baseUrl && pathname.startsWith(this.baseUrl)) {\n pathname = pathname.slice(this.baseUrl.length) || \"/\";\n } else if (this.baseUrl) {\n // pathname doesn't start with baseUrl, no match\n return null;\n }\n\n // Handle wildcard pattern\n if (this.pattern === \"*\" || this.pattern === \"(.*)\") {\n return { params: {} };\n }\n\n // Get or create cached pattern\n let compiled = RouteUrl.patternCache.get(this.pattern);\n if (!compiled) {\n compiled = this.compilePattern();\n RouteUrl.patternCache.set(this.pattern, compiled);\n }\n\n const match = pathname.match(compiled.regex);\n if (!match) {\n return null;\n }\n\n // Extract parameters from capture groups\n const params: Record<string, string> = {};\n compiled.keys.forEach((key, index) => {\n const value = match[index + 1];\n if (value !== undefined) {\n params[key] = decodeURIComponent(value);\n }\n });\n\n return { params };\n }\n\n /**\n * Extract path parameter names from the pattern.\n */\n private extractPathKeys(): Set<string> {\n const pathKeys = new Set<string>();\n const paramRegex = /:([a-zA-Z_][a-zA-Z0-9_]*)/g;\n let match;\n while ((match = paramRegex.exec(this.pattern)) !== null) {\n pathKeys.add(match[1]);\n }\n return pathKeys;\n }\n\n /**\n * Replace :param placeholders with actual values from params.\n */\n private replacePathParams(): string {\n return this.pattern.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, (_, key) => {\n const value = this.params![key];\n if (value === undefined || value === null) {\n return \"\";\n }\n return encodeURIComponent(String(value));\n });\n }\n\n /**\n * Build query string from params that are not path parameters.\n */\n private buildQueryString(pathKeys: Set<string>): string {\n const queryParams: Record<string, any> = {};\n\n for (const key in this.params) {\n if (!pathKeys.has(key)) {\n queryParams[key] = this.params[key];\n }\n }\n\n if (Object.keys(queryParams).length === 0) {\n return \"\";\n }\n\n const searchParams = new URLSearchParams();\n\n for (const key in queryParams) {\n const value = queryParams[key];\n if (value !== undefined && value !== null) {\n if (Array.isArray(value)) {\n value.forEach(v => searchParams.append(key, String(v)));\n } else {\n searchParams.append(key, String(value));\n }\n }\n }\n\n return searchParams.toString();\n }\n\n /**\n * Normalize baseUrl by ensuring it starts with / and doesn't end with /\n */\n private normalizeBaseUrl(baseUrl?: string): string {\n if (!baseUrl || baseUrl === \"/\") {\n return \"\";\n }\n\n let normalized = baseUrl;\n\n // Ensure it starts with /\n if (!normalized.startsWith(\"/\")) {\n normalized = \"/\" + normalized;\n }\n\n // Remove trailing /\n if (normalized.endsWith(\"/\")) {\n normalized = normalized.slice(0, -1);\n }\n\n return normalized;\n }\n\n /**\n * Compile the instance's pattern into a regex and extract parameter names.\n */\n private compilePattern(): { regex: RegExp; keys: string[] } {\n const keys: string[] = [];\n\n // Escape special regex characters except for :param syntax\n let regexPattern = this.pattern\n .replace(/[.+?^${}()|[\\]\\\\]/g, \"\\\\$&\") // Escape special chars\n .replace(/\\\\\\*/g, \".*\"); // Handle * wildcard (already escaped by above)\n\n // Replace :param with capture groups\n regexPattern = regexPattern.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, (_, key) => {\n keys.push(key);\n return \"([^/]+)\"; // Match any non-slash characters\n });\n\n // Create regex that matches the entire pathname\n const regex = new RegExp(`^${regexPattern}$`);\n\n return { regex, keys };\n }\n}\n"],"mappings":"AAQA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMA,QAAQ,CAAC;EAClB,OAAeC,YAAY,GAAG,IAAIC,GAAG,CAA4C,CAAC;EAM1EC,WAAWA,CAACC,OAAe,EAAEC,MAAe,EAAEC,OAAgB,EAAE;IACpE,IAAI,CAACF,OAAO,GAAGA,OAAO;IACtB,IAAI,CAACC,MAAM,GAAGA,MAAM;IACpB,IAAI,CAACC,OAAO,GAAG,IAAI,CAACC,gBAAgB,CAACD,OAAO,CAAC;EACjD;;EAEA;AACJ;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACI,OAAOE,WAAWA,CAACJ,OAAe,EAAEC,MAAe,EAAEC,OAAgB,EAAU;IAC3E,MAAMG,QAAQ,GAAG,IAAIT,QAAQ,CAACI,OAAO,EAAEC,MAAM,EAAEC,OAAO,CAAC;IACvD,OAAOG,QAAQ,CAACC,QAAQ,CAAC,CAAC;EAC9B;;EAEA;AACJ;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACI,OAAOC,KAAKA,CAACC,QAAgB,EAAER,OAAe,EAAEE,OAAgB,EAAsB;IAClF,MAAMG,QAAQ,GAAG,IAAIT,QAAQ,CAACI,OAAO,E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,OAAO,KAAK,GAAG,EAAE;MAC7B,OAAO,EAAE;IACb;IAEA,IAAI+C,UAAU,GAAG/C,OAAO;;IAExB;IACA,IAAI,CAAC+C,UAAU,CAAChC,UAAU,CAAC,GAAG,CAAC,EAAE;MAC7BgC,UAAU,GAAG,GAAG,GAAGA,UAAU;IACjC;;IAEA;IACA,IAAIA,UAAU,CAACC,QAAQ,CAAC,GAAG,CAAC,EAAE;MAC1BD,UAAU,GAAGA,UAAU,CAAC/B,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACxC;IAEA,OAAO+B,UAAU;EACrB;;EAEA;AACJ;AACA;EACY3B,cAAcA,CAAA,EAAsC;IACxD,MAAMG,IAAc,GAAG,EAAE;;IAEzB;IACA,IAAI0B,YAAY,GAAG,IAAI,CAACnD,OAAO,CAC1BmC,OAAO,CAAC,oBAAoB,EAAE,MAAM,CAAC,CAAC;IAAA,CACtCA,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;;IAE7B;IACAgB,YAAY,GAAGA,YAAY,CAAChB,OAAO,CAAC,4BAA4B,EAAE,CAACC,CAAC,EAAET,GAAG,KAAK;MAC1EF,IAAI,CAAC2B,IAAI,CAACzB,GAAG,CAAC;MACd,OAAO,SAAS,CAAC,CAAC;IACtB,CAAC,CAAC;;IAEF;IACA,MAAMH,KAAK,GAAG,IAAI6B,MAAM,CAAC,IAAIF,YAAY,GAAG,CAAC;IAE7C,OAAO;MAAE3B,KAAK;MAAEC;IAAK,CAAC;EAC1B;AACJ","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"features/router/RouteUrl.js","sources":["../../../src/features/router/RouteUrl.ts"],"sourcesContent":["interface Params {\n [key: string]: any;\n}\n\ninterface MatchResult {\n params: Record<string, string>;\n}\n\n/**\n * Utility class for route URL pattern matching and generation.\n * Handles simple route patterns with :param syntax (e.g., /cms/entries/:modelId).\n *\n * Public API uses static methods, which internally construct RouteUrl instances\n * to handle the logic through proper instance methods.\n */\nexport class RouteUrl {\n private static patternCache = new Map<string, { regex: RegExp; keys: string[] }>();\n\n private readonly pattern: string;\n private readonly params: Params | undefined;\n private readonly baseUrl: string;\n\n private constructor(pattern: string, params?: Params, baseUrl?: string) {\n this.pattern = pattern;\n this.params = params;\n this.baseUrl = this.normalizeBaseUrl(baseUrl);\n }\n\n /**\n * Generate a URL from a route pattern and parameters.\n * Path parameters are injected into the pattern, remaining parameters become query strings.\n *\n * @param pattern - Route pattern (e.g., '/security/api-keys/:id')\n * @param params - Parameters object (e.g., { id: 123, new: true })\n * @param baseUrl - Optional base URL to prepend (e.g., '/tenant123')\n * @returns Generated URL string\n *\n * @example\n * RouteUrl.fromPattern('/security/api-keys/:id', { id: 123, new: true })\n * // Returns: '/security/api-keys/123?new=true'\n *\n * @example\n * RouteUrl.fromPattern('/file-manager', {}, '/tenant123')\n * // Returns: '/tenant123/file-manager'\n */\n static fromPattern(pattern: string, params?: Params, baseUrl?: string): string {\n const routeUrl = new RouteUrl(pattern, params, baseUrl);\n return routeUrl.generate();\n }\n\n /**\n * Match a pathname against a route pattern and extract parameters.\n * If baseUrl is provided, it will be stripped from the pathname before matching.\n *\n * @param pathname - The pathname to match (e.g., '/tenant123/security/api-keys/123')\n * @param pattern - Route pattern (e.g., '/security/api-keys/:id')\n * @param baseUrl - Optional base URL to strip before matching (e.g., '/tenant123')\n * @returns Object with extracted params if matched, null otherwise\n *\n * @example\n * RouteUrl.match('/security/api-keys/123', '/security/api-keys/:id')\n * // Returns: { params: { id: '123' } }\n *\n * @example\n * RouteUrl.match('/tenant123/file-manager', '/file-manager', '/tenant123')\n * // Returns: { params: {} }\n */\n static match(pathname: string, pattern: string, baseUrl?: string): MatchResult | null {\n const routeUrl = new RouteUrl(pattern, undefined, baseUrl);\n return routeUrl.matchPathname(pathname);\n }\n\n /**\n * Generate a URL from the instance's pattern, params, and baseUrl.\n */\n private generate(): string {\n if (!this.params) {\n return this.baseUrl + this.pattern;\n }\n\n // Extract path parameter names from pattern\n const pathKeys = this.extractPathKeys();\n\n // Replace :param with actual values\n let url = this.replacePathParams();\n\n // Handle empty url\n if (!url || url === \"\") {\n url = \"/\";\n }\n\n // Prepend baseUrl\n url = this.baseUrl + url;\n\n // Add query parameters for keys not used in the path\n const queryString = this.buildQueryString(pathKeys);\n if (queryString) {\n url += `?${queryString}`;\n }\n\n return url;\n }\n\n /**\n * Match a pathname against the instance's pattern.\n */\n private matchPathname(pathname: string): MatchResult | null {\n // Strip baseUrl from pathname if provided\n if (this.baseUrl && pathname.startsWith(this.baseUrl)) {\n pathname = pathname.slice(this.baseUrl.length) || \"/\";\n } else if (this.baseUrl) {\n // pathname doesn't start with baseUrl, no match\n return null;\n }\n\n // Handle wildcard pattern\n if (this.pattern === \"*\" || this.pattern === \"(.*)\") {\n return { params: {} };\n }\n\n // Get or create cached pattern\n let compiled = RouteUrl.patternCache.get(this.pattern);\n if (!compiled) {\n compiled = this.compilePattern();\n RouteUrl.patternCache.set(this.pattern, compiled);\n }\n\n const match = pathname.match(compiled.regex);\n if (!match) {\n return null;\n }\n\n // Extract parameters from capture groups\n const params: Record<string, string> = {};\n compiled.keys.forEach((key, index) => {\n const value = match[index + 1];\n if (value !== undefined) {\n params[key] = decodeURIComponent(value);\n }\n });\n\n return { params };\n }\n\n /**\n * Extract path parameter names from the pattern.\n */\n private extractPathKeys(): Set<string> {\n const pathKeys = new Set<string>();\n const paramRegex = /:([a-zA-Z_][a-zA-Z0-9_]*)/g;\n let match;\n while ((match = paramRegex.exec(this.pattern)) !== null) {\n pathKeys.add(match[1]);\n }\n return pathKeys;\n }\n\n /**\n * Replace :param placeholders with actual values from params.\n */\n private replacePathParams(): string {\n return this.pattern.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, (_, key) => {\n const value = this.params![key];\n if (value === undefined || value === null) {\n return \"\";\n }\n return encodeURIComponent(String(value));\n });\n }\n\n /**\n * Build query string from params that are not path parameters.\n */\n private buildQueryString(pathKeys: Set<string>): string {\n const queryParams: Record<string, any> = {};\n\n for (const key in this.params) {\n if (!pathKeys.has(key)) {\n queryParams[key] = this.params[key];\n }\n }\n\n if (Object.keys(queryParams).length === 0) {\n return \"\";\n }\n\n const searchParams = new URLSearchParams();\n\n for (const key in queryParams) {\n const value = queryParams[key];\n if (value !== undefined && value !== null) {\n if (Array.isArray(value)) {\n value.forEach(v => searchParams.append(key, String(v)));\n } else {\n searchParams.append(key, String(value));\n }\n }\n }\n\n return searchParams.toString();\n }\n\n /**\n * Normalize baseUrl by ensuring it starts with / and doesn't end with /\n */\n private normalizeBaseUrl(baseUrl?: string): string {\n if (!baseUrl || baseUrl === \"/\") {\n return \"\";\n }\n\n let normalized = baseUrl;\n\n // Ensure it starts with /\n if (!normalized.startsWith(\"/\")) {\n normalized = \"/\" + normalized;\n }\n\n // Remove trailing /\n if (normalized.endsWith(\"/\")) {\n normalized = normalized.slice(0, -1);\n }\n\n return normalized;\n }\n\n /**\n * Compile the instance's pattern into a regex and extract parameter names.\n */\n private compilePattern(): { regex: RegExp; keys: string[] } {\n const keys: string[] = [];\n\n // Escape special regex characters except for :param syntax\n let regexPattern = this.pattern\n .replace(/[.+?^${}()|[\\]\\\\]/g, \"\\\\$&\") // Escape special chars\n .replace(/\\\\\\*/g, \".*\"); // Handle * wildcard (already escaped by above)\n\n // Replace :param with capture groups\n regexPattern = regexPattern.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, (_, key) => {\n keys.push(key);\n return \"([^/]+)\"; // Match any non-slash characters\n });\n\n // Create regex that matches the entire pathname\n const regex = new RegExp(`^${regexPattern}$`);\n\n return { regex, keys };\n }\n}\n"],"names":["RouteUrl","Map","pattern","params","baseUrl","routeUrl","pathname","undefined","pathKeys","url","queryString","compiled","match","key","index","value","decodeURIComponent","Set","paramRegex","_","encodeURIComponent","String","queryParams","Object","searchParams","URLSearchParams","Array","v","normalized","keys","regexPattern","regex","RegExp"],"mappings":"AAeO,MAAMA;;aACM,YAAY,GAAG,IAAIC;;IAMlC,YAAoBC,O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
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
2
|
+
class Router {
|
|
3
|
+
constructor(baseUrl = ""){
|
|
4
|
+
this.routes = [];
|
|
5
|
+
this.baseUrl = baseUrl;
|
|
6
|
+
}
|
|
7
|
+
setRoutes(routes) {
|
|
8
|
+
routes.forEach((route)=>{
|
|
9
|
+
const index = this.routes.findIndex((r)=>r.name === route.name);
|
|
10
|
+
if (index > -1) this.routes[index] = route;
|
|
11
|
+
else this.routes.push(route);
|
|
12
|
+
});
|
|
13
|
+
this.sortRoutes();
|
|
14
|
+
}
|
|
15
|
+
findRoute(name) {
|
|
16
|
+
return this.routes.find((r)=>r.name === name);
|
|
17
|
+
}
|
|
18
|
+
resolve(pathname, queryParams) {
|
|
19
|
+
for (const route of this.routes){
|
|
20
|
+
const pattern = "*" === route.path ? "(.*)" : route.path;
|
|
21
|
+
const matchResult = RouteUrl.match(pathname, pattern, this.baseUrl);
|
|
22
|
+
if (matchResult) {
|
|
23
|
+
const matchedRoute = {
|
|
24
|
+
name: route.name,
|
|
25
|
+
path: route.path,
|
|
26
|
+
pathname,
|
|
27
|
+
params: {
|
|
28
|
+
...matchResult.params,
|
|
29
|
+
...queryParams || {}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const onMatch = (matched)=>{
|
|
33
|
+
route.onMatch(matched);
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
matchedRoute,
|
|
37
|
+
onMatch
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
getBaseUrl() {
|
|
44
|
+
return this.baseUrl;
|
|
45
|
+
}
|
|
46
|
+
sortRoutes() {
|
|
47
|
+
const INDEX_PATH = "/";
|
|
48
|
+
const WILDCARD_PATTERNS = [
|
|
49
|
+
"*",
|
|
50
|
+
"(.*)"
|
|
51
|
+
];
|
|
52
|
+
this.routes.sort((a, b)=>{
|
|
53
|
+
const pathA = a.path || "*";
|
|
54
|
+
const pathB = b.path || "*";
|
|
55
|
+
const isWildcardA = WILDCARD_PATTERNS.includes(pathA);
|
|
56
|
+
const isWildcardB = WILDCARD_PATTERNS.includes(pathB);
|
|
57
|
+
const isIndexA = pathA === INDEX_PATH;
|
|
58
|
+
const isIndexB = pathB === INDEX_PATH;
|
|
59
|
+
if (isWildcardA && isWildcardB || isIndexA && isIndexB) return 0;
|
|
60
|
+
if (isIndexA && isWildcardB) return -1;
|
|
61
|
+
if (isWildcardA && isIndexB) return 1;
|
|
62
|
+
if (isWildcardA) return 1;
|
|
63
|
+
if (isWildcardB) return -1;
|
|
64
|
+
if (isIndexA) return 1;
|
|
65
|
+
if (isIndexB) return -1;
|
|
66
|
+
return 0;
|
|
67
|
+
});
|
|
68
68
|
}
|
|
69
|
-
return null;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Get the current baseUrl.
|
|
74
|
-
*/
|
|
75
|
-
getBaseUrl() {
|
|
76
|
-
return this.baseUrl;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Sort routes to ensure proper matching order.
|
|
81
|
-
* Wildcard routes (*) and index routes (/) are moved to the bottom.
|
|
82
|
-
*/
|
|
83
|
-
sortRoutes() {
|
|
84
|
-
const INDEX_PATH = "/";
|
|
85
|
-
const WILDCARD_PATTERNS = ["*", "(.*)"];
|
|
86
|
-
this.routes.sort((a, b) => {
|
|
87
|
-
const pathA = a.path || "*";
|
|
88
|
-
const pathB = b.path || "*";
|
|
89
|
-
const isWildcardA = WILDCARD_PATTERNS.includes(pathA);
|
|
90
|
-
const isWildcardB = WILDCARD_PATTERNS.includes(pathB);
|
|
91
|
-
const isIndexA = pathA === INDEX_PATH;
|
|
92
|
-
const isIndexB = pathB === INDEX_PATH;
|
|
93
|
-
|
|
94
|
-
// Both are wildcards or both are index - maintain order
|
|
95
|
-
if (isWildcardA && isWildcardB || isIndexA && isIndexB) {
|
|
96
|
-
return 0;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Index path should come before wildcard
|
|
100
|
-
if (isIndexA && isWildcardB) {
|
|
101
|
-
return -1;
|
|
102
|
-
}
|
|
103
|
-
if (isWildcardA && isIndexB) {
|
|
104
|
-
return 1;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Push wildcards to the bottom
|
|
108
|
-
if (isWildcardA) {
|
|
109
|
-
return 1;
|
|
110
|
-
}
|
|
111
|
-
if (isWildcardB) {
|
|
112
|
-
return -1;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// Push index to the bottom (but above wildcards)
|
|
116
|
-
if (isIndexA) {
|
|
117
|
-
return 1;
|
|
118
|
-
}
|
|
119
|
-
if (isIndexB) {
|
|
120
|
-
return -1;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// All other routes maintain their order
|
|
124
|
-
return 0;
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
69
|
}
|
|
70
|
+
export { Router };
|
|
128
71
|
|
|
129
72
|
//# sourceMappingURL=Router.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"features/router/Router.js","sources":["../../../src/features/router/Router.ts"],"sourcesContent":["import type { RouteDefinition, MatchedRoute } from \"./abstractions.js\";\nimport { RouteUrl } from \"./RouteUrl.js\";\n\ninterface ResolveResult {\n matchedRoute: MatchedRoute;\n onMatch: (route: MatchedRoute) => void;\n}\n\n/**\n * Router manages a collection of route definitions and resolves pathnames to matched routes.\n */\nexport class Router {\n private routes: RouteDefinition[] = [];\n private baseUrl: string;\n\n constructor(baseUrl: string = \"\") {\n this.baseUrl = baseUrl;\n }\n\n /**\n * Set or update route definitions. Routes with the same name will be replaced.\n *\n * @param routes - Array of route definitions to add\n */\n setRoutes(routes: RouteDefinition[]): void {\n routes.forEach(route => {\n const index = this.routes.findIndex(r => r.name === route.name);\n\n if (index > -1) {\n this.routes[index] = route;\n } else {\n this.routes.push(route);\n }\n });\n\n this.sortRoutes();\n }\n\n /**\n * Find a route by name.\n *\n * @param name - Route name to find\n * @returns Route definition if found, undefined otherwise\n */\n findRoute(name: string): RouteDefinition | undefined {\n return this.routes.find(r => r.name === name);\n }\n\n /**\n * Resolve a pathname to a matched route.\n * The pathname will be matched against patterns after stripping the baseUrl.\n *\n * @param pathname - The pathname to resolve (e.g., '/tenant123/cms/entries/123')\n * @param queryParams - Optional query parameters to merge into route params\n * @returns Matched route with onMatch callback, or null if no route matches\n */\n resolve(pathname: string, queryParams?: Record<string, unknown>): ResolveResult | null {\n for (const route of this.routes) {\n const pattern = route.path === \"*\" ? \"(.*)\" : route.path;\n const matchResult = RouteUrl.match(pathname, pattern, this.baseUrl);\n\n if (matchResult) {\n const matchedRoute: MatchedRoute = {\n name: route.name,\n path: route.path,\n pathname,\n params: { ...matchResult.params, ...(queryParams || {}) }\n };\n\n const onMatch = (matched: MatchedRoute) => {\n route.onMatch(matched);\n };\n\n return { matchedRoute, onMatch };\n }\n }\n\n return null;\n }\n\n /**\n * Get the current baseUrl.\n */\n getBaseUrl(): string {\n return this.baseUrl;\n }\n\n /**\n * Sort routes to ensure proper matching order.\n * Wildcard routes (*) and index routes (/) are moved to the bottom.\n */\n private sortRoutes(): void {\n const INDEX_PATH = \"/\";\n const WILDCARD_PATTERNS = [\"*\", \"(.*)\"];\n\n this.routes.sort((a, b) => {\n const pathA = a.path || \"*\";\n const pathB = b.path || \"*\";\n\n const isWildcardA = WILDCARD_PATTERNS.includes(pathA);\n const isWildcardB = WILDCARD_PATTERNS.includes(pathB);\n const isIndexA = pathA === INDEX_PATH;\n const isIndexB = pathB === INDEX_PATH;\n\n // Both are wildcards or both are index - maintain order\n if ((isWildcardA && isWildcardB) || (isIndexA && isIndexB)) {\n return 0;\n }\n\n // Index path should come before wildcard\n if (isIndexA && isWildcardB) {\n return -1;\n }\n if (isWildcardA && isIndexB) {\n return 1;\n }\n\n // Push wildcards to the bottom\n if (isWildcardA) {\n return 1;\n }\n if (isWildcardB) {\n return -1;\n }\n\n // Push index to the bottom (but above wildcards)\n if (isIndexA) {\n return 1;\n }\n if (isIndexB) {\n return -1;\n }\n\n // All other routes maintain their order\n return 0;\n });\n }\n}\n"],"names":["Router","baseUrl","routes","route","index","r","name","pathname","queryParams","pattern","matchResult","RouteUrl","matchedRoute","onMatch","matched","INDEX_PATH","WILDCARD_PATTERNS","a","b","pathA","pathB","isWildcardA","isWildcardB","isIndexA","isIndexB"],"mappings":";AAWO,MAAMA;IAIT,YAAYC,UAAkB,EAAE,CAAE;aAH1B,MAAM,GAAsB,EAAE;QAIlC,IAAI,CAAC,OAAO,GAAGA;IACnB;IAOA,UAAUC,MAAyB,EAAQ;QACvCA,OAAO,OAAO,CAACC,CAAAA;YACX,MAAMC,QAAQ,IAAI,CAAC,MAAM,CAAC,SAAS,CAACC,CAAAA,IAAKA,EAAE,IAAI,KAAKF,MAAM,IAAI;YAE9D,IAAIC,QAAQ,IACR,IAAI,CAAC,MAAM,CAACA,MAAM,GAAGD;iBAErB,IAAI,CAAC,MAAM,CAAC,IAAI,CAACA;QAEzB;QAEA,IAAI,CAAC,UAAU;IACnB;IAQA,UAAUG,IAAY,EAA+B;QACjD,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAACD,CAAAA,IAAKA,EAA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