@lwrjs/router 0.12.0-alpha.1 → 0.12.0-alpha.10

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.
Files changed (53) hide show
  1. package/README.md +777 -0
  2. package/build/bundle/prod/lwr/navigation/navigation.js +1 -1
  3. package/build/bundle/prod/lwr/router/router.js +1 -1
  4. package/build/bundle/prod/lwr/routerContainer/routerContainer.js +1 -1
  5. package/build/cjs/modules/lwr/domRouter/domRouter.cjs +20 -10
  6. package/build/cjs/modules/lwr/historyRouter/historyRouter.cjs +3 -3
  7. package/build/cjs/modules/lwr/navigation/navigationApi.cjs +4 -4
  8. package/build/cjs/modules/lwr/navigation/navigationMixin.cjs +4 -4
  9. package/build/cjs/modules/lwr/router/router.cjs +14 -13
  10. package/build/cjs/modules/lwr/routerUtils/routeUtils.cjs +21 -8
  11. package/build/cjs/modules/lwr/routerUtils/routerUtils.cjs +1 -0
  12. package/build/cjs/modules/lwr/serverRouter/serverRouter.cjs +10 -10
  13. package/build/cjs/services/index.cjs +0 -1
  14. package/build/cjs/services/module-provider/index.cjs +13 -36
  15. package/build/cjs/services/module-provider/utils.cjs +18 -8
  16. package/build/es/modules/lwr/contextUtils/navigationApiStore.d.ts +3 -2
  17. package/build/es/modules/lwr/domRouter/domRouter.d.ts +14 -8
  18. package/build/es/modules/lwr/domRouter/domRouter.js +25 -10
  19. package/build/es/modules/lwr/historyRouter/historyRouter.d.ts +3 -2
  20. package/build/es/modules/lwr/historyRouter/historyRouter.js +4 -4
  21. package/build/es/modules/lwr/navigation/navigationApi.d.ts +9 -8
  22. package/build/es/modules/lwr/navigation/navigationApi.js +10 -10
  23. package/build/es/modules/lwr/navigation/navigationMixin.js +4 -4
  24. package/build/es/modules/lwr/router/router.d.ts +1 -1
  25. package/build/es/modules/lwr/router/router.js +15 -14
  26. package/build/es/modules/lwr/routerUtils/routeUtils.d.ts +7 -5
  27. package/build/es/modules/lwr/routerUtils/routeUtils.js +22 -8
  28. package/build/es/modules/lwr/routerUtils/routerUtils.d.ts +1 -1
  29. package/build/es/modules/lwr/routerUtils/routerUtils.js +1 -1
  30. package/build/es/modules/lwr/routerUtils/types.d.ts +17 -13
  31. package/build/es/modules/lwr/serverRouter/serverRouter.d.ts +4 -3
  32. package/build/es/modules/lwr/serverRouter/serverRouter.js +11 -11
  33. package/build/es/services/index.d.ts +3 -1
  34. package/build/es/services/index.js +0 -1
  35. package/build/es/services/module-provider/index.d.ts +6 -7
  36. package/build/es/services/module-provider/index.js +15 -47
  37. package/build/es/services/module-provider/utils.d.ts +2 -2
  38. package/build/es/services/module-provider/utils.js +24 -8
  39. package/build/modules/lwr/contextUtils/contextUtils.js +1 -0
  40. package/build/modules/lwr/domRouter/domRouter.js +27 -10
  41. package/build/modules/lwr/domRouterUtils/historyUtils.js +1 -0
  42. package/build/modules/lwr/historyRouter/historyRouter.js +4 -4
  43. package/build/modules/lwr/navigation/navigationApi.js +10 -10
  44. package/build/modules/lwr/navigation/navigationMixin.js +4 -4
  45. package/build/modules/lwr/router/router.js +15 -14
  46. package/build/modules/lwr/routerBridge/routerBridge.js +0 -1
  47. package/build/modules/lwr/routerContainer/utils.js +3 -1
  48. package/build/modules/lwr/routerUtils/routeDefUtils.js +0 -1
  49. package/build/modules/lwr/routerUtils/routeUtils.js +23 -8
  50. package/build/modules/lwr/routerUtils/routerUtils.js +1 -1
  51. package/build/modules/lwr/routerUtils/typeUtils.js +1 -0
  52. package/build/modules/lwr/serverRouter/serverRouter.js +11 -11
  53. package/package.json +9 -9
@@ -1,27 +1,27 @@
1
1
  import { getNavigationHelm } from 'lwr/contextUtils';
2
2
  /**
3
- * Navigate programmatically.
3
+ * Navigate programmatically to a page reference.
4
4
  * The Promise used within is deliberately not returned.
5
5
  *
6
6
  * @param {HTMLElement} context - The navigation context
7
- * @param {object | string} loc - A route or URL for navigating
8
- * @param {*} options - Usually a boolean; when true the previous browser history
9
- * entry should be replaced by this one
7
+ * @param {PageReference} pageReference - A page reference location
8
+ * @param {boolean} replace - When true the previous browser history entry should be replaced by this one
9
+ * @param {NavigateOptions} options - Additional navigation options (i.e. switch locale)
10
10
  */
11
- export function navigate(context, pageReference, replace) {
11
+ export function navigate(context, pageReference, replace, options) {
12
12
  const api = getNavigationHelm(context);
13
- api.navigate(pageReference, replace);
13
+ api.navigate(pageReference, replace, options);
14
14
  }
15
15
 
16
16
  /**
17
- * Generate a URL for the given route.
17
+ * Generate a URL for the given page reference.
18
18
  *
19
19
  * @param {HTMLElement} context - The navigation context
20
- * @param {object} route - A route
20
+ * @param {PageReference} pageReference - A page reference location
21
21
  *
22
22
  * @returns {Promise<string>}
23
23
  */
24
- export function generateUrl(context, pageReference) {
24
+ export function generateUrl(context, pageReference, options) {
25
25
  const api = getNavigationHelm(context);
26
- return api.generateUrl(pageReference);
26
+ return api.generateUrl(pageReference, options);
27
27
  }
@@ -62,16 +62,16 @@ function NavigationMixin(Base) {
62
62
  }
63
63
  }
64
64
  }
65
- [Navigate](pageRef, replace) {
65
+ [Navigate](pageRef, replace, options) {
66
66
  if (!isSSR) {
67
67
  this[GetContext]();
68
- navigate(this[NavContext], pageRef, replace);
68
+ navigate(this[NavContext], pageRef, replace, options);
69
69
  }
70
70
  }
71
- async [GenerateUrl](pageRef) {
71
+ async [GenerateUrl](pageRef, options) {
72
72
  if (!isSSR) {
73
73
  this[GetContext]();
74
- return generateUrl(this[NavContext], pageRef);
74
+ return generateUrl(this[NavContext], pageRef, options);
75
75
  } else {
76
76
  return null;
77
77
  }
@@ -5,7 +5,7 @@
5
5
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6
6
  */
7
7
 
8
- import { freeze, getUrlFromPageReference, getPageReferenceFromUrl, matchRouteByUrl, getUrlFromPageReferenceAndRouteDef, parseRoutes } from 'lwr/routerUtils';
8
+ import { freeze, getUrlFromPageReference, getPageReferenceFromUrl, matchRouteByUrl, getUrlFromPageReferenceAndRouteDef, parseRoutes, DEFAULT_I18N_ROUTER_CONFIG } from 'lwr/routerUtils';
9
9
  import { generateMessage, messages } from 'lwr/routerErrors';
10
10
  import { createObservable } from 'lwr/observable';
11
11
  class RouterImpl {
@@ -25,10 +25,11 @@ class RouterImpl {
25
25
  this.routeObservable = createObservable();
26
26
  this.config = {
27
27
  basePath: config.basePath || '',
28
+ i18n: config.i18n || DEFAULT_I18N_ROUTER_CONFIG,
28
29
  caseSensitive: Boolean(config.caseSensitive),
29
30
  routes: config.routes || [],
30
- generateUrl: address => getUrlFromPageReference(address, this.compiledRoutes, this.config.basePath),
31
- parseUrl: url => getPageReferenceFromUrl(url, this.compiledRoutes, this.config.basePath)
31
+ generateUrl: (address, options) => getUrlFromPageReference(address, this.compiledRoutes, this.config.basePath, this.config.i18n, options),
32
+ parseUrl: url => getPageReferenceFromUrl(url, this.compiledRoutes, this.config.basePath, this.config.i18n)
32
33
  };
33
34
  const {
34
35
  DEPRECATED_getRouteFromUrl,
@@ -48,14 +49,14 @@ class RouterImpl {
48
49
  *
49
50
  * @param address
50
51
  */
51
- generateUrl(address) {
52
+ generateUrl(address, options) {
52
53
  const {
53
54
  DEPRECATED_getUrlFromRoute: getUrlFromRoute
54
55
  } = this.deprecatedConfig;
55
56
  if (getUrlFromRoute) {
56
- return getUrlFromRoute(address, this.config.generateUrl);
57
+ return getUrlFromRoute(address, this.config.generateUrl, options);
57
58
  } else {
58
- return this.config.generateUrl(address);
59
+ return this.config.generateUrl(address, options);
59
60
  }
60
61
  }
61
62
 
@@ -81,13 +82,13 @@ class RouterImpl {
81
82
  *
82
83
  * @param address - The address to match
83
84
  */
84
- matchRoute(address) {
85
- const url = typeof address === 'string' ? address : this.generateUrl(address);
85
+ matchRoute(address, options) {
86
+ const url = typeof address === 'string' ? address : this.generateUrl(address, options);
86
87
  if (url === null) {
87
88
  return null;
88
89
  }
89
- const match = matchRouteByUrl(url, this.compiledRoutes, this.config.basePath);
90
- const pathMatch = match && getUrlFromPageReferenceAndRouteDef(match.route.pageReference, match.routeDefinition, this.config.basePath);
90
+ const match = matchRouteByUrl(url, this.compiledRoutes, this.config.basePath, this.config.i18n, options);
91
+ const pathMatch = match && getUrlFromPageReferenceAndRouteDef(match.route.pageReference, match.routeDefinition, this.config.basePath, this.config.i18n, options);
91
92
  if (!match || !pathMatch) {
92
93
  return null;
93
94
  }
@@ -103,10 +104,10 @@ class RouterImpl {
103
104
  *
104
105
  * @param address - The address to match to a viewset
105
106
  */
106
- async resolveView(address) {
107
+ async resolveView(address, options) {
107
108
  return new Promise((resolve, reject) => {
108
109
  // get the RoutingMatch: { pathMatch, route, routeDefinition }
109
- const routingMatch = this.matchRoute(address);
110
+ const routingMatch = this.matchRoute(address, options);
110
111
  if (!routingMatch) {
111
112
  return reject(generateMessage(messages.NO_ROUTE_MATCH, [JSON.stringify(address)]));
112
113
  }
@@ -138,8 +139,8 @@ class RouterImpl {
138
139
  *
139
140
  * @param {object} address
140
141
  */
141
- navigate(address) {
142
- const routingMatch = this.matchRoute(address);
142
+ navigate(address, options) {
143
+ const routingMatch = this.matchRoute(address, options);
143
144
  if (!routingMatch) {
144
145
  throw new Error(generateMessage(messages.MISSING_ROUTE, [JSON.stringify(address)]));
145
146
  }
@@ -50,7 +50,6 @@ class DomRouterBridge {
50
50
  }) => {
51
51
  this.config.onPostNavigate(pageReference); // ONLY return the page reference
52
52
  });
53
-
54
53
  this.bridgedRouter.addErrorNavigate(this.config.onError);
55
54
 
56
55
  // Prevent the observed router from altering the browser URL or history
@@ -10,7 +10,9 @@ import { invariant, messages } from 'lwr/routerErrors';
10
10
  import { DomRouterImpl } from 'lwr/domRouter';
11
11
  /*
12
12
  * Provides programmatic routing capabilities.
13
- */ // The application may create 1 root router at a time.
13
+ */
14
+
15
+ // The application may create 1 root router at a time.
14
16
  let hasRoot = false;
15
17
  /**
16
18
  * Create a new navigation context, attach to the given node.
@@ -230,6 +230,5 @@ export function getPageReferenceFromUriAndRouteDef(uri, routeDef) {
230
230
  };
231
231
  }
232
232
  }
233
-
234
233
  return null;
235
234
  }
@@ -7,6 +7,11 @@
7
7
 
8
8
  import { getPathFromUrl, getQueryFromUrl, getQueryString, isParam, getParamName, getQueryNames } from './uriUtils';
9
9
  import { getPageReferenceFromUriAndRouteDef, matchRouteDefinitionByPageReference, getPathParams, getQueryParams } from './routeDefUtils';
10
+ export const DEFAULT_I18N_ROUTER_CONFIG = {
11
+ locale: 'en-US',
12
+ defaultLocale: 'en-US'
13
+ };
14
+
10
15
  /**
11
16
  * Returns true if the given path & queryObj match the patterns (if any) defined in the routeDef
12
17
  *
@@ -80,14 +85,22 @@ function getRouteDefinitionForUri(uri, routeDefs) {
80
85
  * @param {string} url - URL string to turn into a route
81
86
  * @param {array[object]} routeDefs - List of Route Definitions to match to the url
82
87
  * @param {string} basePath - Optional: if provided, remove the base path before conversion.
88
+ * @param {object} i18n - Optional: if provided, remove the non-default locale before conversion.
83
89
  *
84
90
  * @returns {object}
85
91
  */
86
- export function matchRouteByUrl(url, routeDefs, basePath = '') {
92
+ export function matchRouteByUrl(url, routeDefs, basePath = '', i18n = DEFAULT_I18N_ROUTER_CONFIG, options) {
87
93
  // Pass in the rest of the URL for matching, without the prefix.
88
- if (basePath && url.indexOf(basePath) === 0) {
94
+ if (basePath && url?.indexOf(basePath) === 0) {
89
95
  url = url.replace(basePath, '');
90
96
  }
97
+ // Remove the locale
98
+ if (options?.locale || i18n?.locale) {
99
+ const localePath = `/${options?.locale || i18n.locale}`;
100
+ if (url?.indexOf(localePath) === 0) {
101
+ url = url.replace(localePath, '');
102
+ }
103
+ }
91
104
 
92
105
  // Parse the URL.
93
106
  const routeDef = getRouteDefinitionForUri(url, routeDefs);
@@ -137,10 +150,10 @@ export function matchRouteByUrl(url, routeDefs, basePath = '') {
137
150
  * @param basePath Base path for the url
138
151
  * @returns the url or null if no match
139
152
  */
140
- export function getUrlFromPageReference(pageReference, routeDefs, basePath = '') {
153
+ export function getUrlFromPageReference(pageReference, routeDefs, basePath = '', i18n = DEFAULT_I18N_ROUTER_CONFIG, options) {
141
154
  const routeDef = matchRouteDefinitionByPageReference(pageReference, routeDefs);
142
155
  if (routeDef) {
143
- return getUrlFromPageReferenceAndRouteDef(pageReference, routeDef, basePath);
156
+ return getUrlFromPageReferenceAndRouteDef(pageReference, routeDef, basePath, i18n, options);
144
157
  }
145
158
  return null;
146
159
  }
@@ -186,7 +199,7 @@ function extractBindingValues(parameters, pageReference, pageBindings) {
186
199
  * @param routeDef routeDef to that defines how serialize the given pageReference
187
200
  * @returns url for the given pageReference
188
201
  */
189
- export function getUrlFromPageReferenceAndRouteDef(pageReference, routeDef, basePath = '') {
202
+ export function getUrlFromPageReferenceAndRouteDef(pageReference, routeDef, basePath = '', i18n = DEFAULT_I18N_ROUTER_CONFIG, options) {
190
203
  const {
191
204
  params,
192
205
  original: {
@@ -229,7 +242,9 @@ export function getUrlFromPageReferenceAndRouteDef(pageReference, routeDef, base
229
242
  });
230
243
  const queryObject = getQueryObjectForParametersAndPageReference(pageReference, queryParameters, routeDef);
231
244
  const queryString = getQueryString(queryObject);
232
- return `${basePath}${toPathUrl}${queryString}`;
245
+ const locale = options?.locale || i18n && i18n.locale;
246
+ const localePart = locale !== i18n.defaultLocale ? `/${locale}` : '';
247
+ return `${basePath}${localePart}${toPathUrl}${queryString}`;
233
248
  }
234
249
 
235
250
  /**
@@ -281,8 +296,8 @@ function getQueryObjectForParametersAndPageReference(pageReference, queryParamet
281
296
  /**
282
297
  * Obtains the pageReference for the given URL
283
298
  */
284
- export function getPageReferenceFromUrl(url, routeDefs, basePath = '') {
285
- const routingMatch = matchRouteByUrl(url, routeDefs, basePath);
299
+ export function getPageReferenceFromUrl(url, routeDefs, basePath = '', i18n = DEFAULT_I18N_ROUTER_CONFIG) {
300
+ const routingMatch = matchRouteByUrl(url, routeDefs, basePath, i18n);
286
301
  if (routingMatch && routingMatch.route && routingMatch.route.pageReference) {
287
302
  return routingMatch.route.pageReference;
288
303
  }
@@ -7,7 +7,7 @@
7
7
 
8
8
  export { createFilterChain } from './filterUtils';
9
9
  export { getPageReferenceFromUriAndRouteDef } from './routeDefUtils';
10
- export { getUrlFromPageReference, getPageReferenceFromUrl, matchRouteByUrl, getUrlFromPageReferenceAndRouteDef } from './routeUtils';
10
+ export { DEFAULT_I18N_ROUTER_CONFIG, getUrlFromPageReference, getPageReferenceFromUrl, matchRouteByUrl, getUrlFromPageReferenceAndRouteDef } from './routeUtils';
11
11
  export { isObject, freeze, guid, isValidRoute } from './typeUtils';
12
12
  export { parseRoutes } from './parseUtils';
13
13
  import { pathToRegexp as ptr, compile as ptrCompile } from './pathToRegexp';
@@ -8,6 +8,7 @@
8
8
  /*
9
9
  * Utilities for checking type, including route types.
10
10
  */
11
+
11
12
  /**
12
13
  * @param {*} o - Item to check if it's an object
13
14
  * @returns {boolean}
@@ -24,7 +24,7 @@ export class ServerRouter {
24
24
  * Perform a hard navigation to the given page reference
25
25
  * Client only!
26
26
  */
27
- async navigate(address) {
27
+ async navigate(address, _replace, options) {
28
28
  if (hasDocument) {
29
29
  // invoke the handleNavigation hook, which intercepts the raw page ref
30
30
  if (this.handleNavHook && !this.handleNavHook(address)) {
@@ -32,7 +32,7 @@ export class ServerRouter {
32
32
  }
33
33
 
34
34
  // continue navigating
35
- const url = await this.getValidatedUrl(address);
35
+ const url = await this.getValidatedUrl(address, options);
36
36
  if (url) {
37
37
  // hard navigation
38
38
  document.location.href = url;
@@ -44,8 +44,8 @@ export class ServerRouter {
44
44
  * lightning/navigation
45
45
  * Generate a URL based on the given page reference
46
46
  */
47
- generateUrl(address) {
48
- return this.router.generateUrl(address);
47
+ generateUrl(address, options) {
48
+ return this.router.generateUrl(address, options);
49
49
  }
50
50
 
51
51
  /**
@@ -66,7 +66,7 @@ export class ServerRouter {
66
66
  url = this.getRelativeUrl(url);
67
67
 
68
68
  // ensure the initial URL matches a valid route definition
69
- const routingMatch = this.router.matchRoute(url);
69
+ const routingMatch = this.router.matchRoute(url, {});
70
70
  if (!routingMatch) {
71
71
  this.processError(generateMessageObject(messages.MISSING_ROUTE, [url]));
72
72
  return;
@@ -75,9 +75,9 @@ export class ServerRouter {
75
75
 
76
76
  // set up the navigation context APIs
77
77
  registerNavigationHelm(this.contextId, {
78
- navigate: address => this.navigate(address),
79
- generateUrl: address => this.generateUrl(address),
80
- // the JS context is lost during hard navigations, so subcribing to route changes will not work
78
+ navigate: (address, replace, options) => this.navigate(address, replace, options),
79
+ generateUrl: (address, options) => this.generateUrl(address, options),
80
+ // the JS context is lost during hard navigation, so subscribing to route changes will not work
81
81
  subscribe: () => {
82
82
  throw new Error('The server router does not support the subscribe API');
83
83
  }
@@ -114,9 +114,9 @@ export class ServerRouter {
114
114
  /**
115
115
  * Validate the page reference passed to the navigate API
116
116
  */
117
- async getValidatedUrl(address) {
117
+ async getValidatedUrl(address, options) {
118
118
  // match the URL to a route definition; fail if there is no match
119
- const routingMatch = this.router.matchRoute(address);
119
+ const routingMatch = this.router.matchRoute(address, {});
120
120
  if (!routingMatch) {
121
121
  this.processError(generateMessageObject(messages.NO_ROUTE_MATCH, [JSON.stringify(address)]));
122
122
  return;
@@ -131,7 +131,7 @@ export class ServerRouter {
131
131
  this.processError(generateMessageObject(messages.PRENAV_FAILED, [JSON.stringify(address)]));
132
132
  return;
133
133
  }
134
- return this.router.generateUrl(address);
134
+ return this.router.generateUrl(address, options);
135
135
  }
136
136
 
137
137
  /**
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
- "version": "0.12.0-alpha.1",
8
+ "version": "0.12.0-alpha.10",
9
9
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
10
10
  "repository": {
11
11
  "type": "git",
@@ -53,15 +53,15 @@
53
53
  "test:server": "jest --config ./jest.server.config.cjs"
54
54
  },
55
55
  "dependencies": {
56
- "@lwrjs/client-modules": "0.12.0-alpha.1",
57
- "@lwrjs/diagnostics": "0.12.0-alpha.1",
58
- "@lwrjs/shared-utils": "0.12.0-alpha.1",
56
+ "@lwrjs/client-modules": "0.12.0-alpha.10",
57
+ "@lwrjs/diagnostics": "0.12.0-alpha.10",
58
+ "@lwrjs/shared-utils": "0.12.0-alpha.10",
59
59
  "ajv": "6.12.6"
60
60
  },
61
61
  "devDependencies": {
62
- "@lwc/jest-preset": "^14.2.1",
63
- "@lwrjs/fs-watch": "0.12.0-alpha.1",
64
- "@rollup/plugin-typescript": "^11.1.5",
62
+ "@lwc/jest-preset": "^14.3.0",
63
+ "@lwrjs/fs-watch": "0.12.0-alpha.10",
64
+ "@rollup/plugin-typescript": "^11.1.6",
65
65
  "jest": "^26.6.3",
66
66
  "rollup": "^2.78.0",
67
67
  "ts-jest": "^26.5.6",
@@ -93,10 +93,10 @@
93
93
  ]
94
94
  },
95
95
  "engines": {
96
- "node": ">=16.0.0"
96
+ "node": ">=18.0.0"
97
97
  },
98
98
  "volta": {
99
99
  "extends": "../../../package.json"
100
100
  },
101
- "gitHead": "138cc3716e923d0367e8279aaf2d6be21e74f440"
101
+ "gitHead": "36759959f624aa40d371dc9ee698dd472813f19c"
102
102
  }