expo-router 2.0.12 → 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.
|
@@ -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",
|
|
@@ -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
|
+
}
|