Skip to content

Commit 35f9535

Browse files
authored
🤖 Merge PR DefinitelyTyped#74805 Add types for route-matcher by @mchevestrier
1 parent f0dfa30 commit 35f9535

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

types/route-matcher/.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts

types/route-matcher/index.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export interface StringRoute {
2+
parse: (url: string) => Record<string, string> | null;
3+
stringify: (params: Record<string, string>) => string;
4+
}
5+
6+
export interface RegExpRoute {
7+
parse: (url: string) => { captures: Array<string | undefined> } | null;
8+
}
9+
10+
export type StringRouteRule = RegExp | ((value: string) => boolean);
11+
export type StringRouteRules = Record<string, StringRouteRule>;
12+
13+
export function routeMatcher(route: string, rules?: StringRouteRules): StringRoute;
14+
export function routeMatcher(route: RegExp): RegExpRoute;

types/route-matcher/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/route-matcher",
4+
"version": "0.1.9999",
5+
"projects": [
6+
"https://github.com/cowboy/javascript-route-matcher"
7+
],
8+
"devDependencies": {
9+
"@types/route-matcher": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "mchevestrier",
14+
"githubUsername": "mchevestrier"
15+
}
16+
]
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { routeMatcher } from "route-matcher";
2+
3+
// $ExpectType StringRoute
4+
const search = routeMatcher("search/:query/p:page");
5+
6+
// $ExpectType Record<string, string> | null
7+
search.parse("search/cowboy/p5");
8+
9+
// $ExpectType StringRoute
10+
const user = routeMatcher("user/:id/:other", {
11+
id: /^\d+$/,
12+
other: function(value) {
13+
return value === "" || value === "foo";
14+
},
15+
});
16+
17+
// $ExpectType string
18+
user.stringify({ id: "abc", other: "xyz" });
19+
20+
// $ExpectType RegExpRoute
21+
const users = routeMatcher(/^(users?)(?:\/(\d+)(?:\.\.(\d+))?)?/);
22+
23+
// $ExpectType { captures: (string | undefined)[]; } | null
24+
users.parse("user/123");
25+
26+
// @ts-expect-error - stringification isn't supported for RegExp routes
27+
users.stringify({ id: "abc", other: "xyz" });

types/route-matcher/tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictFunctionTypes": true,
10+
"strictNullChecks": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"route-matcher-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)