From fd09c7e426e883828a967a14e37d748dc5e7d879 Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Wed, 28 May 2025 08:40:49 +0000 Subject: [PATCH] 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. --- src/services/deepLinks.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/services/deepLinks.ts b/src/services/deepLinks.ts index f3202552..03e252e9 100644 --- a/src/services/deepLinks.ts +++ b/src/services/deepLinks.ts @@ -119,6 +119,15 @@ export class DeepLinkHandler { const [path, queryString] = parts[1].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 = {}; if (queryString) { new URLSearchParams(queryString).forEach((value, key) => { @@ -128,11 +137,9 @@ export class DeepLinkHandler { const params: Record = {}; if (param) { - if (this.ROUTE_MAP[routePath].paramKey) { - params[this.ROUTE_MAP[routePath].paramKey] = param; - } else { - params["id"] = param; - } + // Now we know routePath exists in ROUTE_MAP + const routeConfig = this.ROUTE_MAP[routePath]; + params[routeConfig.paramKey ?? "id"] = param; } return { path: routePath, params, query }; }