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 +0 -2
- package/dist/cjs/compilePath.js +17 -14
- package/dist/cjs/concatPaths.js +2 -2
- package/dist/cjs/handleRoutes.js +5 -0
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/normalizePath.js +2 -2
- package/dist/cjs/stripWild.js +1 -1
- package/dist/esm/compilePath.js +18 -15
- package/dist/esm/concatPaths.js +2 -2
- package/dist/esm/handleRoutes.js +5 -0
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/normalizePath.js +2 -2
- package/dist/esm/stripWild.js +1 -1
- package/package.json +3 -1
package/README.md
CHANGED
package/dist/cjs/compilePath.js
CHANGED
|
@@ -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
|
|
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(
|
|
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(
|
|
33
|
+
let name = parts[i].endsWith("?") ? parts[i].slice(0, -1) : parts[i];
|
|
32
34
|
name = encodeURI(name);
|
|
33
|
-
|
|
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 ?
|
|
42
|
+
patt += "$";
|
|
43
|
+
return [new RegExp(patt, caseSensitive ? "" : "i"), params, wildcard];
|
|
41
44
|
};
|
|
42
45
|
exports.compilePath = compilePath;
|
package/dist/cjs/concatPaths.js
CHANGED
|
@@ -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;
|
package/dist/cjs/handleRoutes.js
CHANGED
|
@@ -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];
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { handleRoutes } from
|
|
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] ===
|
|
7
|
-
return normalized.endsWith(
|
|
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;
|
package/dist/cjs/stripWild.js
CHANGED
|
@@ -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 ===
|
|
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;
|
package/dist/esm/compilePath.js
CHANGED
|
@@ -1,38 +1,41 @@
|
|
|
1
|
-
import { raise } from
|
|
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
|
|
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(
|
|
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(
|
|
30
|
+
let name = parts[i].endsWith("?") ? parts[i].slice(0, -1) : parts[i];
|
|
29
31
|
name = encodeURI(name);
|
|
30
|
-
|
|
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 ?
|
|
39
|
+
patt += "$";
|
|
40
|
+
return [new RegExp(patt, caseSensitive ? "" : "i"), params, wildcard];
|
|
38
41
|
};
|
package/dist/esm/concatPaths.js
CHANGED
package/dist/esm/handleRoutes.js
CHANGED
|
@@ -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];
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { handleRoutes } from
|
|
1
|
+
export { handleRoutes } from "./handleRoutes";
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { handleRoutes } from
|
|
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] ===
|
|
4
|
-
return normalized.endsWith(
|
|
3
|
+
const normalized = path[0] === "/" ? path.slice(1) : path;
|
|
4
|
+
return normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
|
|
5
5
|
};
|
package/dist/esm/stripWild.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const isWild = (path) => path ===
|
|
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.
|
|
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": {
|