Browse Source

fix(deepLinks): improve route validation and type safety

- Add early validation for route paths to prevent undefined access
- Introduce INVALID_ROUTE error type with detailed error information
- Simplify parameter mapping using nullish coalescing operator
- Improve type safety in parseDeepLink method
- Add better error details for invalid route paths

This change prevents potential runtime errors from undefined route access
and provides clearer error messages for invalid deep links.
pull/137/head
Matthew Raymer 2 weeks ago
parent
commit
fd09c7e426
  1. 17
      src/services/deepLinks.ts

17
src/services/deepLinks.ts

@ -119,6 +119,15 @@ export class DeepLinkHandler {
const [path, queryString] = parts[1].split("?"); const [path, queryString] = parts[1].split("?");
const [routePath, param] = path.split("/"); const [routePath, param] = path.split("/");
// Validate route exists before proceeding
if (!this.ROUTE_MAP[routePath]) {
throw {
code: "INVALID_ROUTE",
message: `Invalid route path: ${routePath}`,
details: { routePath }
};
}
const query: Record<string, string> = {}; const query: Record<string, string> = {};
if (queryString) { if (queryString) {
new URLSearchParams(queryString).forEach((value, key) => { new URLSearchParams(queryString).forEach((value, key) => {
@ -128,11 +137,9 @@ export class DeepLinkHandler {
const params: Record<string, string> = {}; const params: Record<string, string> = {};
if (param) { if (param) {
if (this.ROUTE_MAP[routePath].paramKey) { // Now we know routePath exists in ROUTE_MAP
params[this.ROUTE_MAP[routePath].paramKey] = param; const routeConfig = this.ROUTE_MAP[routePath];
} else { params[routeConfig.paramKey ?? "id"] = param;
params["id"] = param;
}
} }
return { path: routePath, params, query }; return { path: routePath, params, query };
} }

Loading…
Cancel
Save