@react-navigation/core 7.0.0-alpha.1 → 7.0.0-alpha.3

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 (47) hide show
  1. package/lib/commonjs/StaticNavigation.js +1 -2
  2. package/lib/commonjs/StaticNavigation.js.map +1 -1
  3. package/lib/commonjs/getPathFromState.js +7 -3
  4. package/lib/commonjs/getPathFromState.js.map +1 -1
  5. package/lib/commonjs/getStateFromPath.js +15 -0
  6. package/lib/commonjs/getStateFromPath.js.map +1 -1
  7. package/lib/commonjs/useRouteCache.js +16 -12
  8. package/lib/commonjs/useRouteCache.js.map +1 -1
  9. package/lib/commonjs/validatePathConfig.js +37 -10
  10. package/lib/commonjs/validatePathConfig.js.map +1 -1
  11. package/lib/module/StaticNavigation.js +1 -2
  12. package/lib/module/StaticNavigation.js.map +1 -1
  13. package/lib/module/getPathFromState.js +7 -3
  14. package/lib/module/getPathFromState.js.map +1 -1
  15. package/lib/module/getStateFromPath.js +15 -0
  16. package/lib/module/getStateFromPath.js.map +1 -1
  17. package/lib/module/useRouteCache.js +16 -12
  18. package/lib/module/useRouteCache.js.map +1 -1
  19. package/lib/module/validatePathConfig.js +37 -10
  20. package/lib/module/validatePathConfig.js.map +1 -1
  21. package/lib/typescript/src/SceneView.d.ts +0 -1
  22. package/lib/typescript/src/SceneView.d.ts.map +1 -1
  23. package/lib/typescript/src/StaticNavigation.d.ts +12 -5
  24. package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
  25. package/lib/typescript/src/getPathFromState.d.ts +1 -0
  26. package/lib/typescript/src/getPathFromState.d.ts.map +1 -1
  27. package/lib/typescript/src/getStateFromPath.d.ts +1 -0
  28. package/lib/typescript/src/getStateFromPath.d.ts.map +1 -1
  29. package/lib/typescript/src/types.d.ts +1 -1
  30. package/lib/typescript/src/types.d.ts.map +1 -1
  31. package/lib/typescript/src/useRouteCache.d.ts.map +1 -1
  32. package/lib/typescript/src/validatePathConfig.d.ts +1 -1
  33. package/lib/typescript/src/validatePathConfig.d.ts.map +1 -1
  34. package/package.json +6 -6
  35. package/src/StaticNavigation.tsx +15 -20
  36. package/src/getPathFromState.tsx +8 -3
  37. package/src/getStateFromPath.tsx +16 -0
  38. package/src/types.tsx +1 -1
  39. package/src/useRouteCache.tsx +17 -16
  40. package/src/validatePathConfig.tsx +58 -14
  41. package/lib/commonjs/fromEntries.js +0 -18
  42. package/lib/commonjs/fromEntries.js.map +0 -1
  43. package/lib/module/fromEntries.js +0 -12
  44. package/lib/module/fromEntries.js.map +0 -1
  45. package/lib/typescript/src/fromEntries.d.ts +0 -2
  46. package/lib/typescript/src/fromEntries.d.ts.map +0 -1
  47. package/src/fromEntries.tsx +0 -11
@@ -1,28 +1,72 @@
1
- const formatToList = (items: string[]) =>
2
- items.map((key) => `- ${key}`).join('\n');
1
+ const formatToList = (items: Record<string, string>) =>
2
+ Object.entries(items)
3
+ .map(([key, value]) => `- ${key} (${value})`)
4
+ .join('\n');
3
5
 
4
- export function validatePathConfig(config: any, root = true) {
5
- const validKeys = ['initialRouteName', 'screens'];
6
+ export function validatePathConfig(config: unknown, root = true) {
7
+ const validation = {
8
+ path: 'string',
9
+ initialRouteName: 'string',
10
+ screens: 'object',
11
+ ...(root
12
+ ? null
13
+ : {
14
+ exact: 'boolean',
15
+ stringify: 'object',
16
+ parse: 'object',
17
+ }),
18
+ };
6
19
 
7
- if (!root) {
8
- validKeys.push('path', 'exact', 'stringify', 'parse');
20
+ if (typeof config !== 'object' || config === null) {
21
+ throw new Error(
22
+ `Expected the configuration to be an object, but got ${JSON.stringify(
23
+ config
24
+ )}.`
25
+ );
9
26
  }
10
27
 
11
- const invalidKeys = Object.keys(config).filter(
12
- (key) => !validKeys.includes(key)
28
+ const validationErrors = Object.fromEntries(
29
+ Object.keys(config)
30
+ .map((key) => {
31
+ if (key in validation) {
32
+ const type = validation[key as keyof typeof validation];
33
+ // @ts-expect-error: we know the key exists
34
+ const value = config[key];
35
+
36
+ if (typeof value !== type) {
37
+ return [key, `expected '${type}', got '${typeof value}'`];
38
+ }
39
+ } else {
40
+ return [key, 'extraneous'];
41
+ }
42
+
43
+ return null;
44
+ })
45
+ .filter(Boolean) as [string, string][]
13
46
  );
14
47
 
15
- if (invalidKeys.length) {
48
+ if (Object.keys(validationErrors).length) {
16
49
  throw new Error(
17
50
  `Found invalid properties in the configuration:\n${formatToList(
18
- invalidKeys
19
- )}\n\nDid you forget to specify them under a 'screens' property?\n\nYou can only specify the following properties:\n${formatToList(
20
- validKeys
21
- )}\n\nSee https://reactnavigation.org/docs/configuring-links for more details on how to specify a linking configuration.`
51
+ validationErrors
52
+ )}\n\nYou can only specify the following properties:\n${formatToList(
53
+ validation
54
+ )}\n\nIf you want to specify configuration for screens, you need to specify them under a 'screens' property.\n\nSee https://reactnavigation.org/docs/configuring-links for more details on how to specify a linking configuration.`
55
+ );
56
+ }
57
+
58
+ if (
59
+ root &&
60
+ 'path' in config &&
61
+ typeof config.path === 'string' &&
62
+ config.path.includes(':')
63
+ ) {
64
+ throw new Error(
65
+ `Found invalid path '${config.path}'. The 'path' in the top-level configuration cannot contain patterns for params.`
22
66
  );
23
67
  }
24
68
 
25
- if (config.screens) {
69
+ if ('screens' in config && config.screens) {
26
70
  Object.entries(config.screens).forEach(([_, value]) => {
27
71
  if (typeof value !== 'string') {
28
72
  validatePathConfig(value, false);
@@ -1,18 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.fromEntries = fromEntries;
7
- // Object.fromEntries is not available in older iOS versions
8
- function fromEntries(entries) {
9
- return entries.reduce((acc, _ref) => {
10
- let [k, v] = _ref;
11
- if (acc.hasOwnProperty(k)) {
12
- throw new Error(`A value for key '${k}' already exists in the object.`);
13
- }
14
- acc[k] = v;
15
- return acc;
16
- }, {});
17
- }
18
- //# sourceMappingURL=fromEntries.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["fromEntries","entries","reduce","acc","k","v","hasOwnProperty","Error"],"sourceRoot":"../../src","sources":["fromEntries.tsx"],"mappings":";;;;;;AAAA;AACO,SAASA,WAAW,CAAsBC,OAA4B,EAAE;EAC7E,OAAOA,OAAO,CAACC,MAAM,CAAC,CAACC,GAAG,WAAa;IAAA,IAAX,CAACC,CAAC,EAAEC,CAAC,CAAC;IAChC,IAAIF,GAAG,CAACG,cAAc,CAACF,CAAC,CAAC,EAAE;MACzB,MAAM,IAAIG,KAAK,CAAE,oBAAmBH,CAAE,iCAAgC,CAAC;IACzE;IAEAD,GAAG,CAACC,CAAC,CAAC,GAAGC,CAAC;IACV,OAAOF,GAAG;EACZ,CAAC,EAAE,CAAC,CAAC,CAAiB;AACxB"}
@@ -1,12 +0,0 @@
1
- // Object.fromEntries is not available in older iOS versions
2
- export function fromEntries(entries) {
3
- return entries.reduce((acc, _ref) => {
4
- let [k, v] = _ref;
5
- if (acc.hasOwnProperty(k)) {
6
- throw new Error(`A value for key '${k}' already exists in the object.`);
7
- }
8
- acc[k] = v;
9
- return acc;
10
- }, {});
11
- }
12
- //# sourceMappingURL=fromEntries.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["fromEntries","entries","reduce","acc","k","v","hasOwnProperty","Error"],"sourceRoot":"../../src","sources":["fromEntries.tsx"],"mappings":"AAAA;AACA,OAAO,SAASA,WAAW,CAAsBC,OAA4B,EAAE;EAC7E,OAAOA,OAAO,CAACC,MAAM,CAAC,CAACC,GAAG,WAAa;IAAA,IAAX,CAACC,CAAC,EAAEC,CAAC,CAAC;IAChC,IAAIF,GAAG,CAACG,cAAc,CAACF,CAAC,CAAC,EAAE;MACzB,MAAM,IAAIG,KAAK,CAAE,oBAAmBH,CAAE,iCAAgC,CAAC;IACzE;IAEAD,GAAG,CAACC,CAAC,CAAC,GAAGC,CAAC;IACV,OAAOF,GAAG;EACZ,CAAC,EAAE,CAAC,CAAC,CAAiB;AACxB"}
@@ -1,2 +0,0 @@
1
- export declare function fromEntries<K extends string, V>(entries: (readonly [K, V])[]): Record<K, V>;
2
- //# sourceMappingURL=fromEntries.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fromEntries.d.ts","sourceRoot":"","sources":["../../../src/fromEntries.tsx"],"names":[],"mappings":"AACA,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,gBAS5E"}
@@ -1,11 +0,0 @@
1
- // Object.fromEntries is not available in older iOS versions
2
- export function fromEntries<K extends string, V>(entries: (readonly [K, V])[]) {
3
- return entries.reduce((acc, [k, v]) => {
4
- if (acc.hasOwnProperty(k)) {
5
- throw new Error(`A value for key '${k}' already exists in the object.`);
6
- }
7
-
8
- acc[k] = v;
9
- return acc;
10
- }, {} as Record<K, V>);
11
- }