next-middleware-router 1.0.2 → 1.0.4

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/README.md CHANGED
@@ -1,5 +1,3 @@
1
1
  # Next.js Middleware Router
2
2
 
3
3
  **TODO**
4
-
5
- <sub><sub>(just gimme a sec. ill write the README over the weekend)</sub></sub>
@@ -4,39 +4,42 @@ exports.compilePath = void 0;
4
4
  const raise_1 = require("./raise");
5
5
  // Compile a RegEx that matches a specific route and extracts its params and wildcard
6
6
  const compilePath = (path, caseSensitive = false) => {
7
- const parts = path.split('/');
7
+ const parts = path.split("/");
8
8
  const params = [];
9
- let patt = '^', wildcard = Infinity;
9
+ let wildcard = Infinity;
10
+ let patt = "^";
10
11
  // Iterate over segments of the route
11
12
  for (let i = 0; i < parts.length; i++) {
12
- if (parts[i] === '*') {
13
+ if (parts[i] === "*") {
13
14
  // Wildcard
14
- if (i < parts.length - 1)
15
+ if (i < parts.length - 1) {
15
16
  (0, raise_1.raise)(`Invalid path '/${path}'. Wildcard use is only permitted at the very end.`);
17
+ }
16
18
  patt += "(/[a-zA-Z0-9.\\-/%_~!$&'()*+,;=:@]+)?";
17
19
  wildcard = i;
18
20
  }
19
- else if (parts[i].startsWith(':')) {
21
+ else if (parts[i].startsWith(":")) {
20
22
  // Param
21
23
  params.push([
22
- parts[i].endsWith('?') ? parts[i].slice(1, -1) : parts[i].slice(1),
24
+ parts[i].endsWith("?") ? parts[i].slice(1, -1) : parts[i].slice(1),
23
25
  i,
24
26
  ]);
25
27
  patt += "(/[a-zA-Z0-9.\\-%_~!$&'()*+,;=:@]+)";
26
- if (parts[i].endsWith('?'))
27
- patt += '?';
28
+ if (parts[i].endsWith("?"))
29
+ patt += "?";
28
30
  }
29
31
  else {
30
32
  // Exact
31
- let name = parts[i].endsWith('?') ? parts[i].slice(0, -1) : parts[i];
33
+ let name = parts[i].endsWith("?") ? parts[i].slice(0, -1) : parts[i];
32
34
  name = encodeURI(name);
33
- name = name.replace(/[\\.*+^$?{}|()[\]]/g, '\\$&'); // Escape RegExp special chars
35
+ // Escape RegExp special chars
36
+ name = name.replace(/[\\.*+^$?{}|()[\]]/g, "\\$&");
34
37
  patt += `(/${name})`;
35
- if (parts[i].endsWith('?'))
36
- patt += '?';
38
+ if (parts[i].endsWith("?"))
39
+ patt += "?";
37
40
  }
38
41
  }
39
- patt += '$';
40
- return [new RegExp(patt, caseSensitive ? '' : 'i'), params, wildcard];
42
+ patt += "$";
43
+ return [new RegExp(patt, caseSensitive ? "" : "i"), params, wildcard];
41
44
  };
42
45
  exports.compilePath = compilePath;
@@ -2,10 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.concatPaths = void 0;
4
4
  function concatPaths(...paths) {
5
- let res = '';
5
+ let res = "";
6
6
  for (const path of paths) {
7
7
  if (res && path)
8
- res += '/';
8
+ res += "/";
9
9
  res += path;
10
10
  }
11
11
  return res;
@@ -123,6 +123,11 @@ const handleRoutes = (pathname, { basename = "", children = [], } = {}) => {
123
123
  params[name] = decodeURIComponent(paramVal.slice(1));
124
124
  }
125
125
  }
126
+ // Extract splat
127
+ let splat;
128
+ if (route.wild < Infinity && (splat = match.at(route.wild + 1))) {
129
+ params["*"] = decodeURIComponent(splat.slice(1));
130
+ }
126
131
  // Traverse matching RouteNode's parents to build a complete
127
132
  // RouteNode chain with handler functions to be executed
128
133
  const routeChain = [route];
@@ -1 +1 @@
1
- export { handleRoutes } from './handleRoutes';
1
+ export { handleRoutes } from "./handleRoutes";
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.normalizePath = void 0;
4
4
  // Remove leading and trailing slashes on a path
5
5
  const normalizePath = (path) => {
6
- const normalized = path[0] === '/' ? path.slice(1) : path;
7
- return normalized.endsWith('/') ? normalized.slice(0, -1) : normalized;
6
+ const normalized = path[0] === "/" ? path.slice(1) : path;
7
+ return normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
8
8
  };
9
9
  exports.normalizePath = normalizePath;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.stripWild = exports.isWild = void 0;
4
- const isWild = (path) => path === '*' || path.endsWith('/*');
4
+ const isWild = (path) => path === "*" || path.endsWith("/*");
5
5
  exports.isWild = isWild;
6
6
  const stripWild = (path) => (0, exports.isWild)(path) ? path.slice(0, -2) : path;
7
7
  exports.stripWild = stripWild;
@@ -1,38 +1,41 @@
1
- import { raise } from './raise';
1
+ import { raise } from "./raise";
2
2
  // Compile a RegEx that matches a specific route and extracts its params and wildcard
3
3
  export const compilePath = (path, caseSensitive = false) => {
4
- const parts = path.split('/');
4
+ const parts = path.split("/");
5
5
  const params = [];
6
- let patt = '^', wildcard = Infinity;
6
+ let wildcard = Infinity;
7
+ let patt = "^";
7
8
  // Iterate over segments of the route
8
9
  for (let i = 0; i < parts.length; i++) {
9
- if (parts[i] === '*') {
10
+ if (parts[i] === "*") {
10
11
  // Wildcard
11
- if (i < parts.length - 1)
12
+ if (i < parts.length - 1) {
12
13
  raise(`Invalid path '/${path}'. Wildcard use is only permitted at the very end.`);
14
+ }
13
15
  patt += "(/[a-zA-Z0-9.\\-/%_~!$&'()*+,;=:@]+)?";
14
16
  wildcard = i;
15
17
  }
16
- else if (parts[i].startsWith(':')) {
18
+ else if (parts[i].startsWith(":")) {
17
19
  // Param
18
20
  params.push([
19
- parts[i].endsWith('?') ? parts[i].slice(1, -1) : parts[i].slice(1),
21
+ parts[i].endsWith("?") ? parts[i].slice(1, -1) : parts[i].slice(1),
20
22
  i,
21
23
  ]);
22
24
  patt += "(/[a-zA-Z0-9.\\-%_~!$&'()*+,;=:@]+)";
23
- if (parts[i].endsWith('?'))
24
- patt += '?';
25
+ if (parts[i].endsWith("?"))
26
+ patt += "?";
25
27
  }
26
28
  else {
27
29
  // Exact
28
- let name = parts[i].endsWith('?') ? parts[i].slice(0, -1) : parts[i];
30
+ let name = parts[i].endsWith("?") ? parts[i].slice(0, -1) : parts[i];
29
31
  name = encodeURI(name);
30
- name = name.replace(/[\\.*+^$?{}|()[\]]/g, '\\$&'); // Escape RegExp special chars
32
+ // Escape RegExp special chars
33
+ name = name.replace(/[\\.*+^$?{}|()[\]]/g, "\\$&");
31
34
  patt += `(/${name})`;
32
- if (parts[i].endsWith('?'))
33
- patt += '?';
35
+ if (parts[i].endsWith("?"))
36
+ patt += "?";
34
37
  }
35
38
  }
36
- patt += '$';
37
- return [new RegExp(patt, caseSensitive ? '' : 'i'), params, wildcard];
39
+ patt += "$";
40
+ return [new RegExp(patt, caseSensitive ? "" : "i"), params, wildcard];
38
41
  };
@@ -1,8 +1,8 @@
1
1
  export function concatPaths(...paths) {
2
- let res = '';
2
+ let res = "";
3
3
  for (const path of paths) {
4
4
  if (res && path)
5
- res += '/';
5
+ res += "/";
6
6
  res += path;
7
7
  }
8
8
  return res;
@@ -120,6 +120,11 @@ export const handleRoutes = (pathname, { basename = "", children = [], } = {}) =
120
120
  params[name] = decodeURIComponent(paramVal.slice(1));
121
121
  }
122
122
  }
123
+ // Extract splat
124
+ let splat;
125
+ if (route.wild < Infinity && (splat = match.at(route.wild + 1))) {
126
+ params["*"] = decodeURIComponent(splat.slice(1));
127
+ }
123
128
  // Traverse matching RouteNode's parents to build a complete
124
129
  // RouteNode chain with handler functions to be executed
125
130
  const routeChain = [route];
@@ -1 +1 @@
1
- export { handleRoutes } from './handleRoutes';
1
+ export { handleRoutes } from "./handleRoutes";
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- export { handleRoutes } from './handleRoutes';
1
+ export { handleRoutes } from "./handleRoutes";
@@ -1,5 +1,5 @@
1
1
  // Remove leading and trailing slashes on a path
2
2
  export const normalizePath = (path) => {
3
- const normalized = path[0] === '/' ? path.slice(1) : path;
4
- return normalized.endsWith('/') ? normalized.slice(0, -1) : normalized;
3
+ const normalized = path[0] === "/" ? path.slice(1) : path;
4
+ return normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
5
5
  };
@@ -1,2 +1,2 @@
1
- export const isWild = (path) => path === '*' || path.endsWith('/*');
1
+ export const isWild = (path) => path === "*" || path.endsWith("/*");
2
2
  export const stripWild = (path) => isWild(path) ? path.slice(0, -2) : path;
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "next-middleware-router",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
+ "type": "module",
4
5
  "author": "Fry",
5
6
  "license": "MIT",
6
7
  "description": "Router for middleware in Next.js",
7
8
  "files": [
8
9
  "dist"
9
10
  ],
11
+ "types": "dist/esm/index.d.ts",
10
12
  "exports": {
11
13
  ".": {
12
14
  "import": {