expo-router 2.0.11 → 2.0.13
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/build/LocationProvider.d.ts +1 -0
- package/build/LocationProvider.d.ts.map +1 -1
- package/build/global-state/router-store.d.ts.map +1 -1
- package/build/global-state/routing.d.ts.map +1 -1
- package/build/loadStaticParamsAsync.d.ts +1 -0
- package/build/loadStaticParamsAsync.d.ts.map +1 -1
- package/package.json +4 -3
- package/src/LocationProvider.tsx +14 -0
- package/src/global-state/router-store.tsx +1 -0
- package/src/global-state/routing.ts +7 -2
- package/src/loadStaticParamsAsync.ts +149 -126
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LocationProvider.d.ts","sourceRoot":"","sources":["../src/LocationProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAEhD,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC;AAEtD,MAAM,MAAM,SAAS,GAAG;IACtB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC;IAC9B,QAAQ,EAAE,MAAM,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"LocationProvider.d.ts","sourceRoot":"","sources":["../src/LocationProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAEhD,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC;AAEtD,MAAM,MAAM,SAAS,GAAG;IACtB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC;IAC9B,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB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AE,WAAW,EAAE,GAAG,EAAE,IAAI,QAEhD;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,WAAW,EAAE,GAAG,EAAE,IAAI,QAEnD;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,WAAW,QAGvC;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAUpD;AAED,wBAAgB,SAAS,CACvB,IAAI,EAAE,WAAW,EACjB,MAAM,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAM,OAI7C;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"routing.d.ts","sourceRoot":"","sources":["../../src/global-state/routing.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,IAAI,EAAe,MAAM,cAAc,CAAC;AAGjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAUlD,wBAAgB,IAAI,CAAC,IAAI,EAAE,WAAW,EAAE,GAAG,EAAE,IAAI,QAEhD;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,WAAW,EAAE,GAAG,EAAE,IAAI,QAEnD;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,WAAW,QAGvC;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAUpD;AAED,wBAAgB,SAAS,CACvB,IAAI,EAAE,WAAW,EACjB,MAAM,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAM,OAI7C;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,QAuErE"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { RouteNode } from "./Route";
|
|
2
2
|
export declare function loadStaticParamsAsync(route: RouteNode): Promise<RouteNode>;
|
|
3
|
+
export declare function assertStaticParams(route: Pick<RouteNode, "contextKey" | "dynamic">, params: Record<string, string | string[]>): void;
|
|
3
4
|
//# sourceMappingURL=loadStaticParamsAsync.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loadStaticParamsAsync.d.ts","sourceRoot":"","sources":["../src/loadStaticParamsAsync.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAqB,SAAS,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"loadStaticParamsAsync.d.ts","sourceRoot":"","sources":["../src/loadStaticParamsAsync.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAqB,SAAS,EAAE,MAAM,SAAS,CAAC;AAE5D,wBAAsB,qBAAqB,CACzC,KAAK,EAAE,SAAS,GACf,OAAO,CAAC,SAAS,CAAC,CAQpB;AA+ID,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,IAAI,CAAC,SAAS,EAAE,YAAY,GAAG,SAAS,CAAC,EAChD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,QA6D1C"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "expo-router",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.13",
|
|
4
4
|
"main": "src/index.tsx",
|
|
5
5
|
"types": "build/index.d.ts",
|
|
6
|
+
"license": "MIT",
|
|
6
7
|
"files": [
|
|
7
8
|
"_error.js",
|
|
8
9
|
"_entry.tsx",
|
|
@@ -105,12 +106,12 @@
|
|
|
105
106
|
},
|
|
106
107
|
"dependencies": {
|
|
107
108
|
"@bacons/react-views": "^1.1.3",
|
|
108
|
-
"@expo/metro-runtime": "2.2.
|
|
109
|
+
"@expo/metro-runtime": "2.2.15",
|
|
109
110
|
"@radix-ui/react-slot": "1.0.1",
|
|
110
111
|
"@react-navigation/bottom-tabs": "~6.5.7",
|
|
111
112
|
"@react-navigation/native": "~6.1.6",
|
|
112
113
|
"@react-navigation/native-stack": "~6.9.12",
|
|
113
|
-
"expo-head": "0.0.
|
|
114
|
+
"expo-head": "0.0.19",
|
|
114
115
|
"expo-splash-screen": "~0.20.2",
|
|
115
116
|
"query-string": "7.1.3",
|
|
116
117
|
"react-helmet-async": "^1.3.0",
|
package/src/LocationProvider.tsx
CHANGED
|
@@ -7,6 +7,7 @@ export type UrlObject = {
|
|
|
7
7
|
pathname: string;
|
|
8
8
|
readonly params: SearchParams;
|
|
9
9
|
segments: string[];
|
|
10
|
+
isIndex: boolean;
|
|
10
11
|
};
|
|
11
12
|
|
|
12
13
|
export function getRouteInfoFromState(
|
|
@@ -21,11 +22,24 @@ export function getRouteInfoFromState(
|
|
|
21
22
|
return {
|
|
22
23
|
// TODO: This may have a predefined origin attached in the future.
|
|
23
24
|
unstable_globalHref: path,
|
|
25
|
+
isIndex: isIndexPath(state),
|
|
24
26
|
pathname: path.split("?")["0"],
|
|
25
27
|
...getNormalizedStatePath(qualified),
|
|
26
28
|
};
|
|
27
29
|
}
|
|
28
30
|
|
|
31
|
+
function isIndexPath(state: State): boolean {
|
|
32
|
+
const route = state.routes[state.index ?? state.routes.length - 1];
|
|
33
|
+
if (route.state) {
|
|
34
|
+
return isIndexPath(route.state);
|
|
35
|
+
}
|
|
36
|
+
// router.params is typed as 'object', so this usual syntax is to please TypeScript
|
|
37
|
+
if (route.params && "screen" in route.params) {
|
|
38
|
+
return route.params.screen === "index";
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
29
43
|
// TODO: Split up getPathFromState to return all this info at once.
|
|
30
44
|
export function getNormalizedStatePath({
|
|
31
45
|
path: statePath,
|
|
@@ -78,7 +78,8 @@ export function linkTo(this: RouterStore, href: string, event?: string) {
|
|
|
78
78
|
const rootState = navigationRef.getRootState();
|
|
79
79
|
|
|
80
80
|
if (href.startsWith(".")) {
|
|
81
|
-
|
|
81
|
+
// Resolve base path by merging the current segments with the params
|
|
82
|
+
let base =
|
|
82
83
|
this.routeInfo?.segments
|
|
83
84
|
?.map((segment) => {
|
|
84
85
|
if (!segment.startsWith("[")) return segment;
|
|
@@ -99,7 +100,11 @@ export function linkTo(this: RouterStore, href: string, event?: string) {
|
|
|
99
100
|
.filter(Boolean)
|
|
100
101
|
.join("/") ?? "/";
|
|
101
102
|
|
|
102
|
-
|
|
103
|
+
if (!this.routeInfo?.isIndex) {
|
|
104
|
+
base += "/..";
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
href = resolve(base, href);
|
|
103
108
|
}
|
|
104
109
|
|
|
105
110
|
const state = this.linking.getStateFromPath!(href, this.linking.config);
|
|
@@ -1,101 +1,17 @@
|
|
|
1
1
|
import type { DynamicConvention, RouteNode } from "./Route";
|
|
2
2
|
|
|
3
|
-
async function recurseAndFlattenNodes<
|
|
4
|
-
T,
|
|
5
|
-
TProps,
|
|
6
|
-
TProcess extends (node: T, props: any) => Promise<T[]>
|
|
7
|
-
>(nodes: T[], props: TProps, func: TProcess): Promise<T[]> {
|
|
8
|
-
const tarr = await Promise.all(nodes.map((node) => func(node, props)).flat());
|
|
9
|
-
return tarr.filter(Boolean) as T[];
|
|
10
|
-
}
|
|
11
|
-
|
|
12
3
|
export async function loadStaticParamsAsync(
|
|
13
4
|
route: RouteNode
|
|
14
5
|
): Promise<RouteNode> {
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
loadStaticParamsRecursive(route, { parentParams: {} })
|
|
19
|
-
)
|
|
6
|
+
const expandedChildren = await Promise.all(
|
|
7
|
+
route.children.map((route) =>
|
|
8
|
+
loadStaticParamsRecursive(route, { parentParams: {} })
|
|
20
9
|
)
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
route.children = processed;
|
|
10
|
+
);
|
|
11
|
+
route.children = expandedChildren.flat();
|
|
24
12
|
return route;
|
|
25
13
|
}
|
|
26
14
|
|
|
27
|
-
function assertStaticParams(
|
|
28
|
-
route: RouteNode,
|
|
29
|
-
params: Record<string, string | string[]>
|
|
30
|
-
) {
|
|
31
|
-
const matches = route.dynamic!.every((dynamic) => {
|
|
32
|
-
const value = params[dynamic.name];
|
|
33
|
-
return value !== undefined && value !== null;
|
|
34
|
-
});
|
|
35
|
-
if (!matches) {
|
|
36
|
-
throw new Error(
|
|
37
|
-
`generateStaticParams() must return an array of params that match the dynamic route. Received ${JSON.stringify(
|
|
38
|
-
params
|
|
39
|
-
)}`
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const validateSingleParam = (
|
|
44
|
-
dynamic: DynamicConvention,
|
|
45
|
-
value: any,
|
|
46
|
-
allowMultipleSegments?: boolean
|
|
47
|
-
) => {
|
|
48
|
-
if (typeof value !== "string") {
|
|
49
|
-
throw new Error(
|
|
50
|
-
`generateStaticParams() for route "${
|
|
51
|
-
route.contextKey
|
|
52
|
-
}" expected param "${
|
|
53
|
-
dynamic.name
|
|
54
|
-
}" to be of type string, instead found "${typeof value}" while parsing "${value}".`
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
const parts = value.split("/").filter(Boolean);
|
|
58
|
-
if (parts.length > 1 && !allowMultipleSegments) {
|
|
59
|
-
throw new Error(
|
|
60
|
-
`generateStaticParams() for route "${route.contextKey}" expected param "${dynamic.name}" to not contain "/" (multiple segments) while parsing "${value}".`
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
if (parts.length === 0) {
|
|
64
|
-
throw new Error(
|
|
65
|
-
`generateStaticParams() for route "${route.contextKey}" expected param "${dynamic.name}" not to be empty while parsing "${value}".`
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
route.dynamic!.forEach((dynamic) => {
|
|
71
|
-
const value = params[dynamic.name];
|
|
72
|
-
if (dynamic.deep) {
|
|
73
|
-
// TODO: We could split strings by `/` and use that too.
|
|
74
|
-
if (!Array.isArray(value)) {
|
|
75
|
-
validateSingleParam(dynamic, value, true);
|
|
76
|
-
} else {
|
|
77
|
-
validateSingleParam(dynamic, value.filter(Boolean).join("/"), true);
|
|
78
|
-
}
|
|
79
|
-
} else {
|
|
80
|
-
validateSingleParam(dynamic, value);
|
|
81
|
-
}
|
|
82
|
-
return value !== undefined && value !== null;
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** lodash.uniqBy */
|
|
87
|
-
function uniqBy<T>(array: T[], key: (item: T) => string): T[] {
|
|
88
|
-
const seen: { [key: string]: boolean } = {};
|
|
89
|
-
return array.filter((item) => {
|
|
90
|
-
const k = key(item);
|
|
91
|
-
if (seen[k]) {
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
seen[k] = true;
|
|
95
|
-
return true;
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
15
|
async function loadStaticParamsRecursive(
|
|
100
16
|
route: RouteNode,
|
|
101
17
|
props: { parentParams: any }
|
|
@@ -112,32 +28,36 @@ async function loadStaticParamsRecursive(
|
|
|
112
28
|
staticParams = await loaded.generateStaticParams({
|
|
113
29
|
params: props.parentParams || {},
|
|
114
30
|
});
|
|
115
|
-
if (!Array.isArray(staticParams)) {
|
|
116
|
-
throw new Error(
|
|
117
|
-
`generateStaticParams() must return an array of params, received ${staticParams}`
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
31
|
|
|
32
|
+
assertStaticParamsType(staticParams);
|
|
121
33
|
// Assert that at least one param from each matches the dynamic route.
|
|
122
34
|
staticParams.forEach((params) => assertStaticParams(route, params));
|
|
123
35
|
}
|
|
124
36
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
)
|
|
37
|
+
const traverseForNode = async (
|
|
38
|
+
nextParams: Record<string, string | string[]>
|
|
39
|
+
) => {
|
|
40
|
+
const nextChildren: RouteNode[] = [];
|
|
41
|
+
for (const child of route.children) {
|
|
42
|
+
const children = await loadStaticParamsRecursive(child, {
|
|
43
|
+
...props,
|
|
44
|
+
parentParams: nextParams,
|
|
45
|
+
});
|
|
46
|
+
nextChildren.push(...children);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return uniqBy(nextChildren, (i) => i.route);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
if (!staticParams.length) {
|
|
53
|
+
const nextParams = {
|
|
54
|
+
...props.parentParams,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
route.children = await traverseForNode(nextParams);
|
|
58
|
+
|
|
59
|
+
return [route];
|
|
60
|
+
}
|
|
141
61
|
|
|
142
62
|
const createParsedRouteName = (input: string, params: any) => {
|
|
143
63
|
let parsedRouteName = input;
|
|
@@ -159,6 +79,12 @@ async function loadStaticParamsRecursive(
|
|
|
159
79
|
|
|
160
80
|
const generatedRoutes = await Promise.all(
|
|
161
81
|
staticParams.map(async (params) => {
|
|
82
|
+
const nextParams = {
|
|
83
|
+
...props.parentParams,
|
|
84
|
+
...params,
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const dynamicChildren = await traverseForNode(nextParams);
|
|
162
88
|
const parsedRoute = createParsedRouteName(route.route, params);
|
|
163
89
|
const generatedContextKey = createParsedRouteName(
|
|
164
90
|
route.contextKey,
|
|
@@ -172,25 +98,122 @@ async function loadStaticParamsRecursive(
|
|
|
172
98
|
// Convert the dynamic route to a static route.
|
|
173
99
|
dynamic: null,
|
|
174
100
|
route: parsedRoute,
|
|
175
|
-
children:
|
|
176
|
-
(
|
|
177
|
-
await recurseAndFlattenNodes(
|
|
178
|
-
[...route.children],
|
|
179
|
-
{
|
|
180
|
-
...props,
|
|
181
|
-
parentParams: {
|
|
182
|
-
...props.parentParams,
|
|
183
|
-
...staticParams,
|
|
184
|
-
},
|
|
185
|
-
},
|
|
186
|
-
loadStaticParamsRecursive
|
|
187
|
-
)
|
|
188
|
-
).flat(),
|
|
189
|
-
(i) => i.route
|
|
190
|
-
),
|
|
101
|
+
children: dynamicChildren,
|
|
191
102
|
};
|
|
192
103
|
})
|
|
193
104
|
);
|
|
194
105
|
|
|
195
106
|
return [route, ...generatedRoutes];
|
|
196
107
|
}
|
|
108
|
+
|
|
109
|
+
/** lodash.uniqBy */
|
|
110
|
+
function uniqBy<T>(array: T[], key: (item: T) => string): T[] {
|
|
111
|
+
const seen: { [key: string]: boolean } = {};
|
|
112
|
+
return array.filter((item) => {
|
|
113
|
+
const k = key(item);
|
|
114
|
+
if (seen[k]) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
seen[k] = true;
|
|
118
|
+
return true;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function assertStaticParamsType(
|
|
123
|
+
params: any
|
|
124
|
+
): asserts params is Record<string, string | string[]> {
|
|
125
|
+
if (!Array.isArray(params)) {
|
|
126
|
+
throw new Error(
|
|
127
|
+
`generateStaticParams() must return an array of params, received ${params}`
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function formatExpected(
|
|
133
|
+
expected: string[],
|
|
134
|
+
received: Record<string, any>
|
|
135
|
+
): string {
|
|
136
|
+
const total = {
|
|
137
|
+
...received,
|
|
138
|
+
};
|
|
139
|
+
for (const item of expected) {
|
|
140
|
+
if (total[item] == null) {
|
|
141
|
+
total[item] = String(total[item]);
|
|
142
|
+
} else {
|
|
143
|
+
total[item] = `"${total[item]}"`;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return [
|
|
148
|
+
"{",
|
|
149
|
+
Object.entries(total)
|
|
150
|
+
.map(([key, value]) => ` "${key}": ${value}`)
|
|
151
|
+
.join(",\n"),
|
|
152
|
+
"}",
|
|
153
|
+
].join("\n");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function assertStaticParams(
|
|
157
|
+
route: Pick<RouteNode, "contextKey" | "dynamic">,
|
|
158
|
+
params: Record<string, string | string[]>
|
|
159
|
+
) {
|
|
160
|
+
// Type checking
|
|
161
|
+
if (!route.dynamic) {
|
|
162
|
+
throw new Error("assertStaticParams() must be called on a dynamic route.");
|
|
163
|
+
}
|
|
164
|
+
const matches = route.dynamic.every((dynamic) => {
|
|
165
|
+
const value = params[dynamic.name];
|
|
166
|
+
return value !== undefined && value !== null;
|
|
167
|
+
});
|
|
168
|
+
if (!matches) {
|
|
169
|
+
const plural = route.dynamic.length > 1 ? "s" : "";
|
|
170
|
+
const expected = route.dynamic.map((dynamic) => dynamic.name);
|
|
171
|
+
throw new Error(
|
|
172
|
+
`[${
|
|
173
|
+
route.contextKey
|
|
174
|
+
}]: generateStaticParams() must return an array of params that match the dynamic route${plural}. Expected non-nullish values for key${plural}: ${expected
|
|
175
|
+
.map((v) => `"${v}"`)
|
|
176
|
+
.join(", ")}.\nReceived:\n${formatExpected(expected, params)}`
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const validateSingleParam = (
|
|
181
|
+
dynamic: DynamicConvention,
|
|
182
|
+
value: any,
|
|
183
|
+
allowMultipleSegments?: boolean
|
|
184
|
+
) => {
|
|
185
|
+
if (typeof value !== "string") {
|
|
186
|
+
throw new Error(
|
|
187
|
+
`generateStaticParams() for route "${
|
|
188
|
+
route.contextKey
|
|
189
|
+
}" expected param "${
|
|
190
|
+
dynamic.name
|
|
191
|
+
}" to be of type string, instead found "${typeof value}" while parsing "${value}".`
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
const parts = value.split("/").filter(Boolean);
|
|
195
|
+
if (parts.length > 1 && !allowMultipleSegments) {
|
|
196
|
+
throw new Error(
|
|
197
|
+
`generateStaticParams() for route "${route.contextKey}" expected param "${dynamic.name}" to not contain "/" (multiple segments) while parsing "${value}".`
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
if (parts.length === 0) {
|
|
201
|
+
throw new Error(
|
|
202
|
+
`generateStaticParams() for route "${route.contextKey}" expected param "${dynamic.name}" not to be empty while parsing "${value}".`
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// `[shape]/bar/[...colors]` -> `[shape]`, `[...colors]`
|
|
208
|
+
for (const dynamic of route.dynamic) {
|
|
209
|
+
let parameter = params[dynamic.name];
|
|
210
|
+
if (dynamic.deep) {
|
|
211
|
+
if (Array.isArray(parameter)) {
|
|
212
|
+
parameter = parameter.filter(Boolean).join("/");
|
|
213
|
+
}
|
|
214
|
+
validateSingleParam(dynamic, parameter, true);
|
|
215
|
+
} else {
|
|
216
|
+
validateSingleParam(dynamic, parameter);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|