Skip to content

Commit

Permalink
feat: Add global variables for mapping (#47) (#48)
Browse files Browse the repository at this point in the history
* feat: Update API configuration and value mapping

* fix: Swap order of global and config variables in resolveValue function
  • Loading branch information
irensaltali committed Mar 8, 2024
1 parent 25db0a2 commit 5c0168a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
11 changes: 8 additions & 3 deletions worker/src/api-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"servers": [
{
"alias": "serverlessapigateway-api",
"url": "https://4e05-2a02-e0-665f-2400-e945-4e3-409c-d532.ngrok-free.app"
"url": "https://74ec-2a02-e0-665f-2400-4803-52e0-7bcf-8789.ngrok-free.app"
},
{
"alias": "serverlessapigateway-api-sub",
Expand All @@ -27,6 +27,9 @@
"audience": "opensourcecommunity",
"issuer": "serverlessapigw"
},
"variables": {
"global_variable": "global_variable_value"
},
"paths": [
{
"method": "GET",
Expand All @@ -47,7 +50,8 @@
"x-config-database-url": "$config.database-url",
"x-config-nested-config-key": "$config.nested.config.key",
"x-query-userId": "$request.query.userId",
"x-query-redirect_uri": "$request.query.redirect_uri"
"x-query-redirect_uri": "$request.query.redirect_uri",
"x-global-variable": "$config.global_variable"
},
"query": {
"jwt-sub": "$request.jwt.sub",
Expand All @@ -63,7 +67,8 @@
"variables": {
"api_key": "API_KEY_VALUE",
"database-url": "sqlite://db.sqlite",
"nested.config.key": "nested config value"
"nested.config.key": "nested config value",
"global_variable": "this-not-global-variable"
}
},
{
Expand Down
3 changes: 3 additions & 0 deletions worker/src/api-config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@
"path"
]
}
},
"variables": {
"type": "object"
}
},
"required": [
Expand Down
2 changes: 1 addition & 1 deletion worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default {
// console.log('Modified request:', modifiedRequest);
if (matchedPath.mapping) {
console.log('Applying mapping:', matchedPath.mapping);
modifiedRequest = await applyValueMapping(modifiedRequest, matchedPath.mapping, jwtPayload, matchedPath.variables);
modifiedRequest = await applyValueMapping(modifiedRequest, matchedPath.mapping, jwtPayload, matchedPath.variables, apiConfig.variables);
}
return fetch(modifiedRequest).then(response => setPoweredByHeader(setCorsHeaders(request, response)));
}
Expand Down
10 changes: 5 additions & 5 deletions worker/src/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
async function applyValueMapping(request: Request, mappingConfig: any, jwtPayload: any, configVariables: any): Promise<Request> {
async function applyValueMapping(request: Request, mappingConfig: any, jwtPayload: any, configVariables: any, globalVariables: any): Promise<Request> {
let newRequest = request.clone();
let url = new URL(newRequest.url);
let searchParams = new URLSearchParams(url.searchParams);
Expand All @@ -8,7 +8,7 @@ async function applyValueMapping(request: Request, mappingConfig: any, jwtPayloa
const newHeaders = new Headers(newRequest.headers);

for (const [key, value] of Object.entries(mappingConfig.headers)) {
const resolvedValue = resolveValue(String(value), request, jwtPayload, configVariables);
const resolvedValue = resolveValue(String(value), request, jwtPayload, configVariables, globalVariables);
console.log(`Resolved value for ${key}: ${resolvedValue}`);
if (resolvedValue !== null) {
newHeaders.set(key, resolvedValue);
Expand All @@ -21,7 +21,7 @@ async function applyValueMapping(request: Request, mappingConfig: any, jwtPayloa
// Apply mappings to query parameters
if (mappingConfig.query) {
for (const [key, value] of Object.entries(mappingConfig.query)) {
const resolvedValue = resolveValue(String(value), request, jwtPayload, configVariables);
const resolvedValue = resolveValue(String(value), request, jwtPayload, configVariables, globalVariables);
if (resolvedValue !== null) {
searchParams.set(key, resolvedValue);
}
Expand All @@ -34,7 +34,7 @@ async function applyValueMapping(request: Request, mappingConfig: any, jwtPayloa
return newRequest;
}

function resolveValue(template: string, request: Request, jwtPayload: any, configVariables: any): string | null {
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;;
Expand All @@ -47,7 +47,7 @@ function resolveValue(template: string, request: Request, jwtPayload: any, confi
case 'request.jwt':
return jwtPayload[match[2]] || null;
case 'config':
return configVariables[match[2]] || null;
return configVariables[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 5c0168a

Please sign in to comment.