@vercel/config
Advanced tools
+66
-8
@@ -30,2 +30,26 @@ "use strict"; | ||
| /** | ||
| * Extract environment variable names from a string or array of strings | ||
| * Returns env var names without the $ prefix, excluding path parameters | ||
| */ | ||
| function extractEnvVars(args, pathParams) { | ||
| if (!args) | ||
| return []; | ||
| const envVars = new Set(); | ||
| const argsArray = Array.isArray(args) ? args : [args]; | ||
| for (const arg of argsArray) { | ||
| // Find all $VAR patterns | ||
| const matches = arg.match(/\$([A-Z_][A-Z0-9_]*)/g); | ||
| if (matches) { | ||
| for (const match of matches) { | ||
| const varName = match.substring(1); // Remove the $ | ||
| // Only add if it's not a path parameter | ||
| if (!pathParams.includes(varName)) { | ||
| envVars.add(varName); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return Array.from(envVars); | ||
| } | ||
| /** | ||
| * The main Router class for building a Vercel configuration object in code. | ||
@@ -153,5 +177,6 @@ * Supports synchronous or asynchronous addition of rewrites, redirects, headers, | ||
| const transforms = []; | ||
| const pathParams = this.extractPathParams(source); | ||
| if (requestHeaders) { | ||
| for (const [key, value] of Object.entries(requestHeaders)) { | ||
| transforms.push({ | ||
| const transform = { | ||
| type: 'request.headers', | ||
@@ -161,3 +186,8 @@ op: 'set', | ||
| args: value, | ||
| }); | ||
| }; | ||
| const envVars = extractEnvVars(value, pathParams); | ||
| if (envVars.length > 0) { | ||
| transform.env = envVars; | ||
| } | ||
| transforms.push(transform); | ||
| } | ||
@@ -167,3 +197,3 @@ } | ||
| for (const [key, value] of Object.entries(responseHeaders)) { | ||
| transforms.push({ | ||
| const transform = { | ||
| type: 'response.headers', | ||
@@ -173,3 +203,8 @@ op: 'set', | ||
| args: value, | ||
| }); | ||
| }; | ||
| const envVars = extractEnvVars(value, pathParams); | ||
| if (envVars.length > 0) { | ||
| transform.env = envVars; | ||
| } | ||
| transforms.push(transform); | ||
| } | ||
@@ -179,3 +214,3 @@ } | ||
| for (const [key, value] of Object.entries(requestQuery)) { | ||
| transforms.push({ | ||
| const transform = { | ||
| type: 'request.query', | ||
@@ -185,3 +220,8 @@ op: 'set', | ||
| args: value, | ||
| }); | ||
| }; | ||
| const envVars = extractEnvVars(value, pathParams); | ||
| if (envVars.length > 0) { | ||
| transform.env = envVars; | ||
| } | ||
| transforms.push(transform); | ||
| } | ||
@@ -249,4 +289,5 @@ } | ||
| const transforms = []; | ||
| const pathParams = this.extractPathParams(source); | ||
| for (const [key, value] of Object.entries(requestHeaders)) { | ||
| transforms.push({ | ||
| const transform = { | ||
| type: 'request.headers', | ||
@@ -256,3 +297,8 @@ op: 'set', | ||
| args: value, | ||
| }); | ||
| }; | ||
| const envVars = extractEnvVars(value, pathParams); | ||
| if (envVars.length > 0) { | ||
| transform.env = envVars; | ||
| } | ||
| transforms.push(transform); | ||
| } | ||
@@ -343,2 +389,14 @@ const route = { | ||
| this.validateSourcePattern(config.src); | ||
| // Auto-extract env vars from each transform if not already specified | ||
| if (config.transforms) { | ||
| const pathParams = this.extractPathParams(config.src); | ||
| for (const transform of config.transforms) { | ||
| if (!transform.env && transform.args) { | ||
| const envVars = extractEnvVars(transform.args, pathParams); | ||
| if (envVars.length > 0) { | ||
| transform.env = envVars; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| this.routeRules.push(config); | ||
@@ -345,0 +403,0 @@ return this; |
+1
-1
| { | ||
| "name": "@vercel/config", | ||
| "version": "0.0.18", | ||
| "version": "0.0.19", | ||
| "description": "A TypeScript SDK for programmatically configuring Vercel projects", | ||
@@ -5,0 +5,0 @@ "bugs": { |
77604
3.19%2057
2.9%