Skip to content

Commit

Permalink
fix: Fix global variable reachability issue (#49)
Browse files Browse the repository at this point in the history
* feat: Add global variables for mapping (#47)

* feat: Update API configuration and value mapping

* fix: Swap order of global and config variables in resolveValue function

* fix: If local variables are none then I couldn't rearch global variables.
  • Loading branch information
irensaltali committed Mar 10, 2024
1 parent 5c0168a commit 665cb90
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions worker/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,17 @@ async function applyValueMapping(request: Request, mappingConfig: any, jwtPayloa

function resolveValue(template: string, request: Request, jwtPayload: any, configVariables: any, globalVariables: any): string | null {
try {

const templateMatcher = /\$(request\.header|request\.jwt|config|request\.query)\.([a-zA-Z0-9-_.]+)/g;;
let match = templateMatcher.exec(template);

if (match) {
switch (match[1]) {
case 'request.header':
return request.headers.get(match[2]) || null;
return (request && request.headers && request.headers.hasOwnProperty(match[2])) ? request.headers.get(match[2]) : null;
case 'request.jwt':
return jwtPayload[match[2]] || null;
return (jwtPayload && jwtPayload.hasOwnProperty(match[2])) ? jwtPayload[match[2]] : null;
case 'config':
return configVariables[match[2]] || globalVariables[match[2]] || null;
return (configVariables && configVariables.hasOwnProperty(match[2])) ? configVariables[match[2]] : (globalVariables && globalVariables.hasOwnProperty(match[2])) ? globalVariables[match[2]] : null;
case 'request.query':
const url = new URL(request.url);
return url.searchParams.get(match[2]) || null;
Expand Down

0 comments on commit 665cb90

Please sign in to comment.