@vercel/routing-utils 5.3.3 → 6.0.0

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.
package/dist/index.js CHANGED
@@ -55,6 +55,35 @@ function isHandler(route) {
55
55
  function isValidHandleValue(handle) {
56
56
  return validHandleValues.has(handle);
57
57
  }
58
+ function convertRouteAliases(route, index) {
59
+ if (route.source !== void 0) {
60
+ if (route.src !== void 0) {
61
+ throw new Error(
62
+ `Route at index ${index} cannot define both \`src\` and \`source\`. Please use only one.`
63
+ );
64
+ }
65
+ route.src = route.source;
66
+ delete route.source;
67
+ }
68
+ if (route.destination !== void 0) {
69
+ if (route.dest !== void 0) {
70
+ throw new Error(
71
+ `Route at index ${index} cannot define both \`dest\` and \`destination\`. Please use only one.`
72
+ );
73
+ }
74
+ route.dest = route.destination;
75
+ delete route.destination;
76
+ }
77
+ if (route.statusCode !== void 0) {
78
+ if (route.status !== void 0) {
79
+ throw new Error(
80
+ `Route at index ${index} cannot define both \`status\` and \`statusCode\`. Please use only one.`
81
+ );
82
+ }
83
+ route.status = route.statusCode;
84
+ delete route.statusCode;
85
+ }
86
+ }
58
87
  function normalizeRoutes(inputRoutes) {
59
88
  if (!inputRoutes || inputRoutes.length === 0) {
60
89
  return { routes: inputRoutes, error: null };
@@ -65,6 +94,13 @@ function normalizeRoutes(inputRoutes) {
65
94
  inputRoutes.forEach((r, i) => {
66
95
  const route = { ...r };
67
96
  routes.push(route);
97
+ if (!isHandler(route)) {
98
+ try {
99
+ convertRouteAliases(route, i);
100
+ } catch (err) {
101
+ errors.push(err.message);
102
+ }
103
+ }
68
104
  const keys = Object.keys(route);
69
105
  if (isHandler(route)) {
70
106
  const { handle } = route;
@@ -100,12 +136,12 @@ function normalizeRoutes(inputRoutes) {
100
136
  if (handleValue === "hit") {
101
137
  if (route.dest) {
102
138
  errors.push(
103
- `Route at index ${i} cannot define \`dest\` after \`handle: hit\`.`
139
+ `Route at index ${i} cannot define \`dest\`/\`destination\` after \`handle: hit\`.`
104
140
  );
105
141
  }
106
142
  if (route.status) {
107
143
  errors.push(
108
- `Route at index ${i} cannot define \`status\` after \`handle: hit\`.`
144
+ `Route at index ${i} cannot define \`status\`/\`statusCode\` after \`handle: hit\`.`
109
145
  );
110
146
  }
111
147
  if (!route.continue) {
@@ -126,7 +162,7 @@ function normalizeRoutes(inputRoutes) {
126
162
  }
127
163
  } else {
128
164
  errors.push(
129
- `Route at index ${i} must define either \`handle\` or \`src\` property.`
165
+ `Route at index ${i} must define either \`src\` or \`source\` property.`
130
166
  );
131
167
  }
132
168
  });
@@ -142,7 +178,7 @@ function checkRegexSyntax(type, index, src) {
142
178
  try {
143
179
  new RegExp(src);
144
180
  } catch (err) {
145
- const prop = type === "Route" ? "src" : "source";
181
+ const prop = type === "Route" ? "src`/`source" : "source";
146
182
  return `${type} at index ${index} has invalid \`${prop}\` regular expression "${src}".`;
147
183
  }
148
184
  return null;
@@ -215,20 +251,8 @@ function notEmpty(value) {
215
251
  }
216
252
  function getTransformedRoutes(vercelConfig) {
217
253
  const { cleanUrls, rewrites, redirects, headers, trailingSlash } = vercelConfig;
218
- let { routes = null } = vercelConfig;
219
- if (routes) {
220
- const hasNewProperties = typeof cleanUrls !== "undefined" || typeof trailingSlash !== "undefined" || typeof redirects !== "undefined" || typeof headers !== "undefined" || typeof rewrites !== "undefined";
221
- if (hasNewProperties) {
222
- const error = createError(
223
- "invalid_mixed_routes",
224
- "If `rewrites`, `redirects`, `headers`, `cleanUrls` or `trailingSlash` are used, then `routes` cannot be present.",
225
- "https://vercel.link/mix-routing-props",
226
- "Learn More"
227
- );
228
- return { routes, error };
229
- }
230
- return normalizeRoutes(routes);
231
- }
254
+ const { routes: userRoutes = null } = vercelConfig;
255
+ let routes = null;
232
256
  if (typeof cleanUrls !== "undefined") {
233
257
  const normalized = normalizeRoutes(
234
258
  (0, import_superstatic.convertCleanUrls)(cleanUrls, trailingSlash)
@@ -249,6 +273,14 @@ function getTransformedRoutes(vercelConfig) {
249
273
  routes = routes || [];
250
274
  routes.push(...normalized.routes || []);
251
275
  }
276
+ if (userRoutes) {
277
+ const normalized = normalizeRoutes(userRoutes);
278
+ if (normalized.error) {
279
+ return { routes, error: normalized.error };
280
+ }
281
+ routes = routes || [];
282
+ routes.push(...normalized.routes || []);
283
+ }
252
284
  if (typeof redirects !== "undefined") {
253
285
  const code = "invalid_redirect";
254
286
  const regexErrorMessage = redirects.map((r, i) => checkRegexSyntax("Redirect", i, r.source)).find(notEmpty);
package/dist/schemas.d.ts CHANGED
@@ -190,7 +190,6 @@ export declare const hasSchema: {
190
190
  */
191
191
  export declare const routesSchema: {
192
192
  readonly type: "array";
193
- readonly deprecated: true;
194
193
  readonly description: "A list of routes objects used to rewrite paths to point towards other internal or external paths";
195
194
  readonly example: readonly [{
196
195
  readonly dest: "https://docs.example.com";
@@ -199,17 +198,29 @@ export declare const routesSchema: {
199
198
  readonly items: {
200
199
  readonly anyOf: readonly [{
201
200
  readonly type: "object";
202
- readonly required: readonly ["src"];
201
+ readonly anyOf: readonly [{
202
+ readonly required: readonly ["src"];
203
+ }, {
204
+ readonly required: readonly ["source"];
205
+ }];
203
206
  readonly additionalProperties: false;
204
207
  readonly properties: {
205
208
  readonly src: {
206
209
  readonly type: "string";
207
210
  readonly maxLength: 4096;
208
211
  };
212
+ readonly source: {
213
+ readonly type: "string";
214
+ readonly maxLength: 4096;
215
+ };
209
216
  readonly dest: {
210
217
  readonly type: "string";
211
218
  readonly maxLength: 4096;
212
219
  };
220
+ readonly destination: {
221
+ readonly type: "string";
222
+ readonly maxLength: 4096;
223
+ };
213
224
  readonly headers: {
214
225
  readonly type: "object";
215
226
  readonly additionalProperties: false;
@@ -234,6 +245,7 @@ export declare const routesSchema: {
234
245
  readonly type: "boolean";
235
246
  };
236
247
  readonly important: {
248
+ readonly deprecated: true;
237
249
  readonly type: "boolean";
238
250
  };
239
251
  readonly user: {
@@ -243,6 +255,7 @@ export declare const routesSchema: {
243
255
  readonly type: "boolean";
244
256
  };
245
257
  readonly override: {
258
+ readonly deprecated: true;
246
259
  readonly type: "boolean";
247
260
  };
248
261
  readonly check: {
@@ -256,6 +269,11 @@ export declare const routesSchema: {
256
269
  readonly minimum: 100;
257
270
  readonly maximum: 999;
258
271
  };
272
+ readonly statusCode: {
273
+ readonly type: "integer";
274
+ readonly minimum: 100;
275
+ readonly maximum: 999;
276
+ };
259
277
  readonly locale: {
260
278
  readonly type: "object";
261
279
  readonly additionalProperties: false;
@@ -887,6 +905,7 @@ export declare const routesSchema: {
887
905
  };
888
906
  }, {
889
907
  readonly type: "object";
908
+ readonly deprecated: true;
890
909
  readonly required: readonly ["handle"];
891
910
  readonly additionalProperties: false;
892
911
  readonly properties: {
package/dist/schemas.js CHANGED
@@ -364,24 +364,31 @@ const transformsSchema = {
364
364
  };
365
365
  const routesSchema = {
366
366
  type: "array",
367
- deprecated: true,
368
367
  description: "A list of routes objects used to rewrite paths to point towards other internal or external paths",
369
368
  example: [{ dest: "https://docs.example.com", src: "/docs" }],
370
369
  items: {
371
370
  anyOf: [
372
371
  {
373
372
  type: "object",
374
- required: ["src"],
373
+ anyOf: [{ required: ["src"] }, { required: ["source"] }],
375
374
  additionalProperties: false,
376
375
  properties: {
377
376
  src: {
378
377
  type: "string",
379
378
  maxLength: 4096
380
379
  },
380
+ source: {
381
+ type: "string",
382
+ maxLength: 4096
383
+ },
381
384
  dest: {
382
385
  type: "string",
383
386
  maxLength: 4096
384
387
  },
388
+ destination: {
389
+ type: "string",
390
+ maxLength: 4096
391
+ },
385
392
  headers: {
386
393
  type: "object",
387
394
  additionalProperties: false,
@@ -406,6 +413,7 @@ const routesSchema = {
406
413
  type: "boolean"
407
414
  },
408
415
  important: {
416
+ deprecated: true,
409
417
  type: "boolean"
410
418
  },
411
419
  user: {
@@ -415,6 +423,7 @@ const routesSchema = {
415
423
  type: "boolean"
416
424
  },
417
425
  override: {
426
+ deprecated: true,
418
427
  type: "boolean"
419
428
  },
420
429
  check: {
@@ -428,6 +437,11 @@ const routesSchema = {
428
437
  minimum: 100,
429
438
  maximum: 999
430
439
  },
440
+ statusCode: {
441
+ type: "integer",
442
+ minimum: 100,
443
+ maximum: 999
444
+ },
431
445
  locale: {
432
446
  type: "object",
433
447
  additionalProperties: false,
@@ -493,6 +507,7 @@ const routesSchema = {
493
507
  },
494
508
  {
495
509
  type: "object",
510
+ deprecated: true,
496
511
  required: ["handle"],
497
512
  additionalProperties: false,
498
513
  properties: {
package/dist/types.d.ts CHANGED
@@ -57,9 +57,11 @@ export type RouteWithSrc = {
57
57
  };
58
58
  methods?: string[];
59
59
  continue?: boolean;
60
+ /** @deprecated */
60
61
  override?: boolean;
61
62
  caseSensitive?: boolean;
62
63
  check?: boolean;
64
+ /** @deprecated */
63
65
  important?: boolean;
64
66
  status?: number;
65
67
  has?: HasField;
@@ -73,6 +75,15 @@ export type RouteWithSrc = {
73
75
  redirect?: Record<string, string>;
74
76
  cookie?: string;
75
77
  };
78
+ /**
79
+ * Aliases for `src`, `dest`, and `status`. These provide consistency with the
80
+ * `rewrites`, `redirects`, and `headers` fields which use `source`, `destination`,
81
+ * and `statusCode`. During normalization, these are converted to their canonical
82
+ * forms (`src`, `dest`, `status`) and stripped from the route object.
83
+ */
84
+ source?: string;
85
+ destination?: string;
86
+ statusCode?: number;
76
87
  /**
77
88
  * A middleware key within the `output` key under the build result.
78
89
  * Overrides a `middleware` definition.
@@ -89,6 +100,7 @@ export type RouteWithSrc = {
89
100
  respectOriginCacheControl?: boolean;
90
101
  };
91
102
  export type RouteWithHandle = {
103
+ /** @deprecated Internal use only. Do not use in vercel.json. */
92
104
  handle: HandleValue;
93
105
  src?: string;
94
106
  dest?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/routing-utils",
3
- "version": "5.3.3",
3
+ "version": "6.0.0",
4
4
  "description": "Vercel routing utilities",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",