@vercel/config 0.0.18 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/router.js +66 -8
  2. package/package.json +1 -1
package/dist/router.js CHANGED
@@ -26,6 +26,30 @@ function runtimeEnv(name) {
26
26
  return `$${name}`;
27
27
  }
28
28
  exports.runtimeEnv = runtimeEnv;
29
+ /**
30
+ * Extract environment variable names from a string or array of strings
31
+ * Returns env var names without the $ prefix, excluding path parameters
32
+ */
33
+ function extractEnvVars(args, pathParams) {
34
+ if (!args)
35
+ return [];
36
+ const envVars = new Set();
37
+ const argsArray = Array.isArray(args) ? args : [args];
38
+ for (const arg of argsArray) {
39
+ // Find all $VAR patterns
40
+ const matches = arg.match(/\$([A-Z_][A-Z0-9_]*)/g);
41
+ if (matches) {
42
+ for (const match of matches) {
43
+ const varName = match.substring(1); // Remove the $
44
+ // Only add if it's not a path parameter
45
+ if (!pathParams.includes(varName)) {
46
+ envVars.add(varName);
47
+ }
48
+ }
49
+ }
50
+ }
51
+ return Array.from(envVars);
52
+ }
29
53
  /**
30
54
  * The main Router class for building a Vercel configuration object in code.
31
55
  * Supports synchronous or asynchronous addition of rewrites, redirects, headers,
@@ -149,34 +173,50 @@ class Router {
149
173
  if (hasTransforms) {
150
174
  // Build a Route object with transforms
151
175
  const transforms = [];
176
+ const pathParams = this.extractPathParams(source);
152
177
  if (requestHeaders) {
153
178
  for (const [key, value] of Object.entries(requestHeaders)) {
154
- transforms.push({
179
+ const transform = {
155
180
  type: 'request.headers',
156
181
  op: 'set',
157
182
  target: { key },
158
183
  args: value,
159
- });
184
+ };
185
+ const envVars = extractEnvVars(value, pathParams);
186
+ if (envVars.length > 0) {
187
+ transform.env = envVars;
188
+ }
189
+ transforms.push(transform);
160
190
  }
161
191
  }
162
192
  if (responseHeaders) {
163
193
  for (const [key, value] of Object.entries(responseHeaders)) {
164
- transforms.push({
194
+ const transform = {
165
195
  type: 'response.headers',
166
196
  op: 'set',
167
197
  target: { key },
168
198
  args: value,
169
- });
199
+ };
200
+ const envVars = extractEnvVars(value, pathParams);
201
+ if (envVars.length > 0) {
202
+ transform.env = envVars;
203
+ }
204
+ transforms.push(transform);
170
205
  }
171
206
  }
172
207
  if (requestQuery) {
173
208
  for (const [key, value] of Object.entries(requestQuery)) {
174
- transforms.push({
209
+ const transform = {
175
210
  type: 'request.query',
176
211
  op: 'set',
177
212
  target: { key },
178
213
  args: value,
179
- });
214
+ };
215
+ const envVars = extractEnvVars(value, pathParams);
216
+ if (envVars.length > 0) {
217
+ transform.env = envVars;
218
+ }
219
+ transforms.push(transform);
180
220
  }
181
221
  }
182
222
  const route = {
@@ -239,13 +279,19 @@ class Router {
239
279
  if (requestHeaders) {
240
280
  // Build a Route object with transforms
241
281
  const transforms = [];
282
+ const pathParams = this.extractPathParams(source);
242
283
  for (const [key, value] of Object.entries(requestHeaders)) {
243
- transforms.push({
284
+ const transform = {
244
285
  type: 'request.headers',
245
286
  op: 'set',
246
287
  target: { key },
247
288
  args: value,
248
- });
289
+ };
290
+ const envVars = extractEnvVars(value, pathParams);
291
+ if (envVars.length > 0) {
292
+ transform.env = envVars;
293
+ }
294
+ transforms.push(transform);
249
295
  }
250
296
  const route = {
251
297
  src: source,
@@ -331,6 +377,18 @@ class Router {
331
377
  */
332
378
  route(config) {
333
379
  this.validateSourcePattern(config.src);
380
+ // Auto-extract env vars from each transform if not already specified
381
+ if (config.transforms) {
382
+ const pathParams = this.extractPathParams(config.src);
383
+ for (const transform of config.transforms) {
384
+ if (!transform.env && transform.args) {
385
+ const envVars = extractEnvVars(transform.args, pathParams);
386
+ if (envVars.length > 0) {
387
+ transform.env = envVars;
388
+ }
389
+ }
390
+ }
391
+ }
334
392
  this.routeRules.push(config);
335
393
  return this;
336
394
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/config",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "A TypeScript SDK for programmatically configuring Vercel projects",
5
5
  "bugs": {
6
6
  "url": "https://github.com/vercel/config/issues"