@comity/router-path-to-regexp 0.9.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/LICENSE +21 -0
- package/README.md +56 -0
- package/dist/cjs/index.js +6 -0
- package/dist/cjs/router.js +47 -0
- package/dist/cjs/types.js +3 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/router.js +43 -0
- package/dist/esm/types.js +2 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/router.d.ts +19 -0
- package/dist/types/types.d.ts +15 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Filippo Bovo and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# @comity/router-path-to-regexp
|
|
2
|
+
|
|
3
|
+
Path-to-regexp router implementation for Comity.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Purpose
|
|
8
|
+
|
|
9
|
+
Provides a router implementation that uses `path-to-regexp` while conforming to the `@comity/router` contracts.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Scope
|
|
14
|
+
|
|
15
|
+
This package:
|
|
16
|
+
|
|
17
|
+
- ✅ implements `PathRouter`
|
|
18
|
+
- ✅ adapts routing behavior to `@comity/router`
|
|
19
|
+
|
|
20
|
+
This package does NOT:
|
|
21
|
+
|
|
22
|
+
- ❌ redefine router contracts
|
|
23
|
+
- ❌ own application routing policy
|
|
24
|
+
- ❌ render HTTP responses
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Public API
|
|
29
|
+
|
|
30
|
+
- `PathRouter`
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
No exhaustive reference; see docs for constraints.
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Documentation
|
|
37
|
+
|
|
38
|
+
- docs/overview.md
|
|
39
|
+
- docs/conventions.md
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## Related Packages
|
|
44
|
+
|
|
45
|
+
- @comity/router
|
|
46
|
+
- @comity/http
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Status
|
|
51
|
+
|
|
52
|
+
Stable
|
|
53
|
+
|
|
54
|
+
_Review Completed: 2026-07-25_
|
|
55
|
+
_Reviewer: Hobiri MAGI (DeepSeek v4 Pro)_
|
|
56
|
+
_Compliance Score: 99.5% (Green)_
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PathRouter = void 0;
|
|
4
|
+
var router_js_1 = require("./router.js");
|
|
5
|
+
Object.defineProperty(exports, "PathRouter", { enumerable: true, get: function () { return router_js_1.PathRouter; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PathRouter = void 0;
|
|
4
|
+
const path_to_regexp_1 = require("path-to-regexp");
|
|
5
|
+
/**
|
|
6
|
+
* PathRouter is a router implementation that matches incoming HTTP requests based on their URL path and HTTP method. It supports static paths and can be extended to support dynamic path parameters in the future.
|
|
7
|
+
*/
|
|
8
|
+
class PathRouter {
|
|
9
|
+
#routes;
|
|
10
|
+
/**
|
|
11
|
+
* @param routes - An array of Route objects that define the routing rules for this router.
|
|
12
|
+
*/
|
|
13
|
+
constructor(routes) {
|
|
14
|
+
this.#routes = routes
|
|
15
|
+
.filter((r) => r.path)
|
|
16
|
+
.map((r) => ({
|
|
17
|
+
route: r,
|
|
18
|
+
match: (0, path_to_regexp_1.match)(r.path, { decode: decodeURIComponent }),
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Matches an incoming HTTP request against the defined routes.
|
|
23
|
+
*
|
|
24
|
+
* @param ctx - The context of the incoming HTTP request.
|
|
25
|
+
*
|
|
26
|
+
* @returns A promise that resolves to a RouteMatch object if a matching route is found, or null if no match is found.
|
|
27
|
+
*/
|
|
28
|
+
async match(ctx) {
|
|
29
|
+
const method = ctx.http.request.method;
|
|
30
|
+
const pathname = ctx.url.pathname;
|
|
31
|
+
for (const r of this.#routes) {
|
|
32
|
+
if (r.route.method && r.route.method !== method) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const m = r.match(pathname);
|
|
36
|
+
if (!m)
|
|
37
|
+
continue;
|
|
38
|
+
return {
|
|
39
|
+
route: r.route,
|
|
40
|
+
params: m.params,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.PathRouter = PathRouter;
|
|
47
|
+
//# sourceMappingURL=router.js.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { match } from "path-to-regexp";
|
|
2
|
+
/**
|
|
3
|
+
* PathRouter is a router implementation that matches incoming HTTP requests based on their URL path and HTTP method. It supports static paths and can be extended to support dynamic path parameters in the future.
|
|
4
|
+
*/
|
|
5
|
+
export class PathRouter {
|
|
6
|
+
#routes;
|
|
7
|
+
/**
|
|
8
|
+
* @param routes - An array of Route objects that define the routing rules for this router.
|
|
9
|
+
*/
|
|
10
|
+
constructor(routes) {
|
|
11
|
+
this.#routes = routes
|
|
12
|
+
.filter((r) => r.path)
|
|
13
|
+
.map((r) => ({
|
|
14
|
+
route: r,
|
|
15
|
+
match: match(r.path, { decode: decodeURIComponent }),
|
|
16
|
+
}));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Matches an incoming HTTP request against the defined routes.
|
|
20
|
+
*
|
|
21
|
+
* @param ctx - The context of the incoming HTTP request.
|
|
22
|
+
*
|
|
23
|
+
* @returns A promise that resolves to a RouteMatch object if a matching route is found, or null if no match is found.
|
|
24
|
+
*/
|
|
25
|
+
async match(ctx) {
|
|
26
|
+
const method = ctx.http.request.method;
|
|
27
|
+
const pathname = ctx.url.pathname;
|
|
28
|
+
for (const r of this.#routes) {
|
|
29
|
+
if (r.route.method && r.route.method !== method) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const m = r.match(pathname);
|
|
33
|
+
if (!m)
|
|
34
|
+
continue;
|
|
35
|
+
return {
|
|
36
|
+
route: r.route,
|
|
37
|
+
params: m.params,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { PathRouter } from "./router.js";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Route, RouteMatch, Router, RouteResolutionContext } from "@comity/router";
|
|
2
|
+
/**
|
|
3
|
+
* PathRouter is a router implementation that matches incoming HTTP requests based on their URL path and HTTP method. It supports static paths and can be extended to support dynamic path parameters in the future.
|
|
4
|
+
*/
|
|
5
|
+
export declare class PathRouter implements Router {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* @param routes - An array of Route objects that define the routing rules for this router.
|
|
9
|
+
*/
|
|
10
|
+
constructor(routes: Route[]);
|
|
11
|
+
/**
|
|
12
|
+
* Matches an incoming HTTP request against the defined routes.
|
|
13
|
+
*
|
|
14
|
+
* @param ctx - The context of the incoming HTTP request.
|
|
15
|
+
*
|
|
16
|
+
* @returns A promise that resolves to a RouteMatch object if a matching route is found, or null if no match is found.
|
|
17
|
+
*/
|
|
18
|
+
match(ctx: RouteResolutionContext): Promise<RouteMatch | null>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Route } from "@comity/router";
|
|
2
|
+
import type { MatchFunction } from "path-to-regexp";
|
|
3
|
+
/**
|
|
4
|
+
* CompiledRoute represents a route that has been compiled with a path matching function.
|
|
5
|
+
*/
|
|
6
|
+
export interface CompiledRoute {
|
|
7
|
+
/**
|
|
8
|
+
* The original route object.
|
|
9
|
+
*/
|
|
10
|
+
route: Route;
|
|
11
|
+
/**
|
|
12
|
+
* The path matching function generated from the route's path.
|
|
13
|
+
*/
|
|
14
|
+
match: MatchFunction<Record<string, string>>;
|
|
15
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@comity/router-path-to-regexp",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "path-to-regexp adapter for Comity applications.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"private": false,
|
|
7
|
+
"author": "Filippo Bovo <hello@filippobovo.com>",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"comity": {
|
|
10
|
+
"layer": "technology-adapter",
|
|
11
|
+
"implements": "@comity/router"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/comityjs/framework#readme",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/comityjs/framework.git"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/comityjs/framework/issues"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=24.0.0"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"comity",
|
|
26
|
+
"comityjs",
|
|
27
|
+
"adapter",
|
|
28
|
+
"framework",
|
|
29
|
+
"path-to-regexp",
|
|
30
|
+
"router",
|
|
31
|
+
"routing",
|
|
32
|
+
"typescript"
|
|
33
|
+
],
|
|
34
|
+
"files": [
|
|
35
|
+
"./dist",
|
|
36
|
+
"!./dist/**/*.map"
|
|
37
|
+
],
|
|
38
|
+
"main": "./dist/cjs/index.js",
|
|
39
|
+
"module": "./dist/esm/index.js",
|
|
40
|
+
"types": "./dist/types/index.d.ts",
|
|
41
|
+
"exports": {
|
|
42
|
+
".": {
|
|
43
|
+
"import": {
|
|
44
|
+
"types": "./dist/types/index.d.ts",
|
|
45
|
+
"default": "./dist/esm/index.js"
|
|
46
|
+
},
|
|
47
|
+
"require": {
|
|
48
|
+
"types": "./dist/types/index.d.ts",
|
|
49
|
+
"default": "./dist/cjs/index.js"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"./package.json": "./package.json"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"registry": "https://registry.npmjs.org",
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"sideEffects": false,
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"path-to-regexp": "^8.0.0",
|
|
61
|
+
"@comity/router": "0.9.0"
|
|
62
|
+
},
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"@comity/router": "0.9.0"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@types/node": "^24.13.4",
|
|
68
|
+
"path-to-regexp": "^8.4.2",
|
|
69
|
+
"typescript": "^5.9.3"
|
|
70
|
+
},
|
|
71
|
+
"scripts": {
|
|
72
|
+
"build": "node ../../scripts/build.mjs",
|
|
73
|
+
"dev": "node ../../scripts/build.mjs --watch",
|
|
74
|
+
"test": "vitest run --coverage",
|
|
75
|
+
"type-check": "tsc -p tsconfig.json --noEmit",
|
|
76
|
+
"lint": "eslint --ext .ts src"
|
|
77
|
+
}
|
|
78
|
+
}
|