From 94169ecf0d7eafdca09ef9ef7b5b6f79667afee4 Mon Sep 17 00:00:00 2001 From: Rohit Agarwal Date: Wed, 19 Feb 2025 00:54:10 +0530 Subject: [PATCH] Added request json as params to conditional routing --- src/handlers/handlerUtils.ts | 13 ++++++++++++- src/services/conditionalRouter.ts | 7 ++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/handlers/handlerUtils.ts b/src/handlers/handlerUtils.ts index 8d5500b03..9dc2a2a84 100644 --- a/src/handlers/handlerUtils.ts +++ b/src/handlers/handlerUtils.ts @@ -750,10 +750,21 @@ export async function tryTargetsRecursively( } catch (err) { metadata = {}; } + + let params = + request instanceof FormData || + request instanceof ReadableStream || + request instanceof ArrayBuffer + ? {} // Send empty object if not JSON + : request; + let conditionalRouter: ConditionalRouter; let finalTarget: Targets; try { - conditionalRouter = new ConditionalRouter(currentTarget, { metadata }); + conditionalRouter = new ConditionalRouter(currentTarget, { + metadata, + params, + }); finalTarget = conditionalRouter.resolveTarget(); } catch (conditionalRouter: any) { throw new RouterError(conditionalRouter.message); diff --git a/src/services/conditionalRouter.ts b/src/services/conditionalRouter.ts index 43553591b..7bfcd55cf 100644 --- a/src/services/conditionalRouter.ts +++ b/src/services/conditionalRouter.ts @@ -6,6 +6,7 @@ type Query = { interface RouterContext { metadata?: Record; + params?: Record; } enum Operator { @@ -71,13 +72,13 @@ export class ConditionalRouter { ); } - const metadataValue = this.getContextValue(key); + const contextValue = this.getContextValue(key); if (typeof value === 'object' && value !== null) { - if (!this.evaluateOperator(value, metadataValue)) { + if (!this.evaluateOperator(value, contextValue)) { return false; } - } else if (metadataValue !== value) { + } else if (contextValue !== value) { return false; } }