/
getPathFromState.js
466 lines (466 loc) · 17.9 KB
/
getPathFromState.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.appendBaseUrl = exports.deepEqual = exports.getPathDataFromState = void 0;
const core_1 = require("@react-navigation/core");
const matchers_1 = require("../matchers");
const DEFAULT_SCREENS = {};
const getActiveRoute = (state) => {
const route = typeof state.index === 'number'
? state.routes[state.index]
: state.routes[state.routes.length - 1];
if (route.state) {
return getActiveRoute(route.state);
}
if (route && isInvalidParams(route.params)) {
return getActiveRoute(createFakeState(route.params));
}
return route;
};
function createFakeState(params) {
return {
stale: false,
type: 'UNKNOWN',
key: 'UNKNOWN',
index: 0,
routeNames: [],
routes: [
{
key: 'UNKNOWN',
name: params.screen,
params: params.params,
path: params.path,
},
],
};
}
function segmentMatchesConvention(segment) {
return (segment === 'index' ||
(0, matchers_1.matchDynamicName)(segment) != null ||
(0, matchers_1.matchGroupName)(segment) != null ||
(0, matchers_1.matchDeepDynamicRouteName)(segment) != null);
}
function encodeURIComponentPreservingBrackets(str) {
return encodeURIComponent(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}
/**
* Utility to serialize a navigation state object to a path string.
*
* @example
* ```js
* getPathFromState(
* {
* routes: [
* {
* name: 'Chat',
* params: { author: 'Jane', id: 42 },
* },
* ],
* },
* {
* screens: {
* Chat: {
* path: 'chat/:author/:id',
* stringify: { author: author => author.toLowerCase() }
* }
* }
* }
* )
* ```
*
* @param state Navigation state to serialize.
* @param options Extra options to fine-tune how to serialize the path.
* @returns Path representing the state, e.g. /foo/bar?count=42.
*/
function getPathFromState(state, _options) {
return getPathDataFromState(state, _options).path;
}
exports.default = getPathFromState;
function getPathDataFromState(state, _options = { screens: DEFAULT_SCREENS }) {
if (state == null) {
throw Error("Got 'undefined' for the navigation state. You must pass a valid state object.");
}
const { preserveGroups, preserveDynamicRoutes, ...options } = _options;
(0, core_1.validatePathConfig)(options);
// Expo Router disallows usage without a linking config.
if (Object.is(options.screens, DEFAULT_SCREENS)) {
throw Error("You must pass a 'screens' object to 'getPathFromState' to generate a path.");
}
return getPathFromResolvedState(JSON.parse(JSON.stringify(state)),
// Create a normalized configs object which will be easier to use
createNormalizedConfigs(options.screens), { preserveGroups, preserveDynamicRoutes });
}
exports.getPathDataFromState = getPathDataFromState;
function processParamsWithUserSettings(configItem, params) {
const stringify = configItem?.stringify;
return Object.fromEntries(Object.entries(params).map(([key, value]) => [
key,
// TODO: Strip nullish values here.
stringify?.[key]
? stringify[key](value)
: // Preserve rest params
Array.isArray(value)
? value
: String(value),
]));
}
function deepEqual(a, b) {
if (a === b) {
return true;
}
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (!deepEqual(a[i], b[i])) {
return false;
}
}
return true;
}
if (typeof a === 'object' && typeof b === 'object') {
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) {
return false;
}
for (const key of keysA) {
if (!deepEqual(a[key], b[key])) {
return false;
}
}
return true;
}
return false;
}
exports.deepEqual = deepEqual;
function walkConfigItems(route, focusedRoute, configs, { preserveDynamicRoutes, }) {
// NOTE(EvanBacon): Fill in current route using state that was passed as params.
if (!route.state && isInvalidParams(route.params)) {
route.state = createFakeState(route.params);
}
let pattern = null;
let focusedParams;
let hash;
const collectedParams = {};
while (route.name in configs) {
const configItem = configs[route.name];
const inputPattern = configItem.pattern;
if (inputPattern == null) {
// This should never happen in Expo Router.
throw new Error('Unexpected: No pattern found for route ' + route.name);
}
pattern = inputPattern;
if (route.params) {
if (route.params['#']) {
hash = route.params['#'];
delete route.params['#'];
}
const params = processParamsWithUserSettings(configItem, route.params);
if (pattern !== undefined && pattern !== null) {
Object.assign(collectedParams, params);
}
if (deepEqual(focusedRoute, route)) {
if (preserveDynamicRoutes) {
focusedParams = params;
}
else {
// If this is the focused route, keep the params for later use
// We save it here since it's been stringified already
focusedParams = getParamsWithConventionsCollapsed({
params,
pattern,
routeName: route.name,
});
}
}
}
if (!route.state && isInvalidParams(route.params)) {
route.state = createFakeState(route.params);
}
// If there is no `screens` property or no nested state, we return pattern
if (!configItem.screens || route.state === undefined) {
if (configItem.initialRouteName &&
configItem.screens &&
configItem.initialRouteName in configItem.screens &&
configItem.screens[configItem.initialRouteName]?.pattern) {
const initialRouteConfig = configItem.screens[configItem.initialRouteName];
// NOTE(EvanBacon): Big hack to support initial route changes in tab bars.
pattern = initialRouteConfig.pattern;
if (focusedParams) {
if (!preserveDynamicRoutes) {
// If this is the focused route, keep the params for later use
// We save it here since it's been stringified already
focusedParams = getParamsWithConventionsCollapsed({
params: focusedParams,
pattern,
routeName: route.name,
});
}
}
}
break;
}
const index = route.state.index ?? route.state.routes.length - 1;
const nextRoute = route.state.routes[index];
const nestedScreens = configItem.screens;
// if there is config for next route name, we go deeper
if (nestedScreens && nextRoute.name in nestedScreens) {
route = nextRoute;
configs = nestedScreens;
}
else {
// If not, there is no sense in going deeper in config
break;
}
}
if (pattern == null) {
throw new Error(`No pattern found for route "${route.name}". Options are: ${Object.keys(configs).join(', ')}.`);
}
if (pattern && !focusedParams && focusedRoute.params) {
if (preserveDynamicRoutes) {
focusedParams = focusedRoute.params;
}
else {
// If this is the focused route, keep the params for later use
// We save it here since it's been stringified already
focusedParams = getParamsWithConventionsCollapsed({
params: focusedRoute.params,
pattern,
routeName: route.name,
});
}
Object.assign(focusedParams, collectedParams);
}
return {
pattern,
nextRoute: route,
focusedParams,
hash,
params: collectedParams,
};
}
function getPathFromResolvedState(state, configs, { preserveGroups, preserveDynamicRoutes, }) {
let path = '';
let current = state;
let hash;
const allParams = {};
while (current) {
path += '/';
// Make mutable copies to ensure we don't leak state outside of the function.
const route = current.routes[current.index ?? 0];
// NOTE(EvanBacon): Fill in current route using state that was passed as params.
// if (isInvalidParams(route.params)) {
if (!route.state && isInvalidParams(route.params)) {
route.state = createFakeState(route.params);
}
const { pattern, params, nextRoute, focusedParams, hash: $hash, } = walkConfigItems(route, getActiveRoute(current), { ...configs }, { preserveDynamicRoutes });
if ($hash) {
hash = $hash;
}
Object.assign(allParams, params);
path += getPathWithConventionsCollapsed({
pattern,
routePath: nextRoute.path,
params: allParams,
initialRouteName: configs[nextRoute.name]?.initialRouteName,
preserveGroups,
preserveDynamicRoutes,
});
if (nextRoute.state &&
// NOTE(EvanBacon): The upstream implementation allows for sending in synthetic states (states that weren't generated by `getStateFromPath`)
// and any invalid routes will simply be ignored.
// Because of this, we need to check if the next route is valid before continuing, otherwise our more strict
// implementation will throw an error.
configs[nextRoute.state.routes?.[nextRoute.state?.index ?? 0]?.name]) {
// Continue looping with the next state if available.
current = nextRoute.state;
}
else {
// Finished crawling state.
// Check for query params before exiting.
if (focusedParams) {
for (const param in focusedParams) {
// TODO: This is not good. We shouldn't squat strings named "undefined".
if (focusedParams[param] === 'undefined') {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete focusedParams[param];
}
}
const query = new URLSearchParams(focusedParams).toString();
if (query) {
path += `?${query}`;
}
}
break;
}
}
if (hash) {
allParams['#'] = hash;
path += `#${hash}`;
}
const params = decodeParams(allParams);
return { path: appendBaseUrl(basicSanitizePath(path)), params };
}
function decodeParams(params) {
const parsed = {};
for (const [key, value] of Object.entries(params)) {
try {
if (Array.isArray(value)) {
parsed[key] = value.map((v) => decodeURIComponent(v));
}
else {
parsed[key] = decodeURIComponent(value);
}
}
catch {
parsed[key] = value;
}
}
return parsed;
}
function getPathWithConventionsCollapsed({ pattern, routePath, params, preserveGroups, preserveDynamicRoutes, initialRouteName, }) {
const segments = pattern.split('/');
return segments
.map((p, i) => {
const name = getParamName(p);
// We don't know what to show for wildcard patterns
// Showing the route name seems ok, though whatever we show here will be incorrect
// Since the page doesn't actually exist
if (p.startsWith('*')) {
if (preserveDynamicRoutes) {
if (name === 'not-found') {
return '+not-found';
}
return `[...${name}]`;
}
if (params[name]) {
if (Array.isArray(params[name])) {
return params[name].join('/');
}
return params[name];
}
if (i === 0) {
// This can occur when a wildcard matches all routes and the given path was `/`.
return routePath;
}
// remove existing segments from route.path and return it
// this is used for nested wildcard routes. Without this, the path would add
// all nested segments to the beginning of the wildcard route.
return routePath
?.split('/')
.slice(i + 1)
.join('/');
}
// If the path has a pattern for a param, put the param in the path
if (p.startsWith(':')) {
if (preserveDynamicRoutes) {
return `[${name}]`;
}
// Optional params without value assigned in route.params should be ignored
return params[name];
}
if (!preserveGroups && (0, matchers_1.matchGroupName)(p) != null) {
// When the last part is a group it could be a shared URL
// if the route has an initialRouteName defined, then we should
// use that as the component path as we can assume it will be shown.
if (segments.length - 1 === i) {
if (initialRouteName) {
// Return an empty string if the init route is ambiguous.
if (segmentMatchesConvention(initialRouteName)) {
return '';
}
return encodeURIComponentPreservingBrackets(initialRouteName);
}
}
return '';
}
// Preserve dynamic syntax for rehydration
return encodeURIComponentPreservingBrackets(p);
})
.map((v) => v ?? '')
.join('/');
}
/** Given a set of query params and a pattern with possible conventions, collapse the conventions and return the remaining params. */
function getParamsWithConventionsCollapsed({ pattern, routeName, params, }) {
const processedParams = { ...params };
// Remove the params present in the pattern since we'll only use the rest for query string
const segments = pattern.split('/');
// Dynamic Routes
segments
.filter((segment) => segment.startsWith(':'))
.forEach((segment) => {
const name = getParamName(segment);
delete processedParams[name];
});
// Deep Dynamic Routes
if (segments.some((segment) => segment.startsWith('*'))) {
// NOTE(EvanBacon): Drop the param name matching the wildcard route name -- this is specific to Expo Router.
const name = (0, matchers_1.testNotFound)(routeName)
? 'not-found'
: (0, matchers_1.matchDeepDynamicRouteName)(routeName) ?? routeName;
delete processedParams[name];
}
return processedParams;
}
// Remove multiple as well as trailing slashes
function basicSanitizePath(path) {
// Remove duplicate slashes like `foo//bar` -> `foo/bar`
const simplifiedPath = path.replace(/\/+/g, '/');
if (simplifiedPath.length <= 1) {
return simplifiedPath;
}
// Remove trailing slash like `foo/bar/` -> `foo/bar`
return simplifiedPath.replace(/\/$/, '');
}
// TODO: Make StackRouter not do this...
// Detect if the params came from StackRouter using `params` to pass around internal state.
function isInvalidParams(params) {
if (!params) {
return false;
}
if ('params' in params && typeof params.params === 'object' && !!params.params) {
return true;
}
return ('initial' in params &&
typeof params.initial === 'boolean' &&
// "path" in params &&
'screen' in params);
}
const getParamName = (pattern) => pattern.replace(/^[:*]/, '').replace(/\?$/, '');
const joinPaths = (...paths) => []
.concat(...paths.map((p) => p.split('/')))
.filter(Boolean)
.join('/');
const createConfigItem = (config, parentPattern) => {
if (typeof config === 'string') {
// If a string is specified as the value of the key(e.g. Foo: '/path'), use it as the pattern
const pattern = parentPattern ? joinPaths(parentPattern, config) : config;
return { pattern };
}
if (config.exact && config.path === undefined) {
throw new Error("A 'path' needs to be specified when specifying 'exact: true'. If you don't want this screen in the URL, specify it as empty string, e.g. `path: ''`.");
}
// If an object is specified as the value (e.g. Foo: { ... }),
// It can have `path` property and `screens` prop which has nested configs
const pattern = config.exact !== true ? joinPaths(parentPattern || '', config.path || '') : config.path || '';
const screens = config.screens ? createNormalizedConfigs(config.screens, pattern) : undefined;
return {
// Normalize pattern to remove any leading, trailing slashes, duplicate slashes etc.
pattern: pattern?.split('/').filter(Boolean).join('/'),
stringify: config.stringify,
screens,
initialRouteName: config.initialRouteName,
};
};
const createNormalizedConfigs = (options, pattern) => Object.fromEntries(Object.entries(options).map(([name, c]) => [name, createConfigItem(c, pattern)]));
function appendBaseUrl(path, baseUrl = process.env.EXPO_BASE_URL) {
if (process.env.NODE_ENV !== 'development') {
if (baseUrl) {
return `/${baseUrl.replace(/^\/+/, '').replace(/\/$/, '')}${path}`;
}
}
return path;
}
exports.appendBaseUrl = appendBaseUrl;
//# sourceMappingURL=getPathFromState.js.map