Skip to content

Commit

Permalink
Added request json as params to conditional routing
Browse files Browse the repository at this point in the history
  • Loading branch information
roh26it committed Feb 18, 2025
1 parent f35fddf commit 12390ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/handlers/handlerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/services/conditionalRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Query = {

interface RouterContext {
metadata?: Record<string, string>;
params?: Record<string, any>;
}

enum Operator {
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 12390ab

Please sign in to comment.