Skip to content

Commit b897f46

Browse files
committed
feat: migrate to path-to-regexp-typed
1 parent 14e063c commit b897f46

4 files changed

Lines changed: 5 additions & 49 deletions

File tree

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"accepts": "^1.3.8",
4242
"cosmokit": "^1.8.0",
4343
"path-to-regexp": "^8.2.0",
44+
"path-to-regexp-typed": "^8.0.0",
4445
"ws": "^8.18.1"
4546
}
4647
}

packages/core/src/body.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class Request implements Body {
88
readonly url: string
99
readonly method: string
1010
readonly headers: Headers
11+
readonly query: URLSearchParams
1112

1213
private _accepts?: accepts.Accepts
1314
private _bodyImpl: globalThis.Response
@@ -16,6 +17,7 @@ export class Request implements Body {
1617
defineProperty(this, Service.tracker, { associate: 'server.request' })
1718
this.url = _req.url!
1819
this.method = _req.method!
20+
this.query = new URLSearchParams(_req.url!.split('?')[1])
1921
this.headers = new Headers()
2022
for (const [key, value] of Object.entries(_req.headers)) {
2123
if (Array.isArray(value)) {

packages/core/src/index.ts

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Context, DisposableList, Inject, Service, z } from 'cordis'
22
import { Awaitable, defineProperty, trimSlash } from 'cosmokit'
33
import type {} from '@cordisjs/plugin-logger'
44
import * as http from 'node:http'
5-
import { Keys, pathToRegexp } from 'path-to-regexp'
5+
import { ExtractParams, Keys, pathToRegexp } from 'path-to-regexp-typed'
66
import { WebSocket, WebSocketServer } from 'ws'
77
import { listen, ListenOptions } from './listen'
88
import { Request, Response } from './body'
@@ -21,54 +21,6 @@ declare module 'cordis' {
2121
}
2222
}
2323

24-
type Upper =
25-
| 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M'
26-
| 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z'
27-
28-
type Lower =
29-
| 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm'
30-
| 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z'
31-
32-
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
33-
34-
/* eslint-disable @typescript-eslint/no-unused-vars */
35-
36-
type Take<S extends string, D extends string, O extends string = ''> =
37-
| S extends `${infer C extends D}${infer S}`
38-
? Take<S, D, `${O}${C}`>
39-
: [O, S]
40-
41-
type TakeIdent<S extends string> =
42-
| S extends `"${infer P}"${infer S}`
43-
? [P, S]
44-
: Take<S, Upper | Lower | Digit | '_'>
45-
46-
// path-to-regexp v8 syntax
47-
export type ExtractParams<S extends string, O extends {} = {}, A extends 0[] = []> =
48-
| string extends S
49-
? any
50-
: S extends `${infer C}${infer S}`
51-
? | C extends '\\'
52-
? | S extends `${string}${infer S}`
53-
? ExtractParams<S, O, A>
54-
: O
55-
: C extends ':' | '*'
56-
? | TakeIdent<S> extends [infer P extends string, infer S extends string]
57-
? ExtractParams<S, O & (
58-
| A['length'] extends 0
59-
? { [K in P]: string }
60-
: { [K in P]?: string }
61-
), A>
62-
: never
63-
: C extends '{'
64-
? ExtractParams<S, O, [0, ...A]>
65-
: C extends '}'
66-
? | A extends [0, ...infer A extends 0[]]
67-
? ExtractParams<S, O, A>
68-
: ExtractParams<S, O, A>
69-
: ExtractParams<S, O, A>
70-
: O
71-
7224
export abstract class Route {
7325
regexp: RegExp
7426
keys?: Keys

tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"skipLibCheck": true,
1010
"esModuleInterop": true,
1111
"moduleResolution": "bundler",
12+
"allowImportingTsExtensions": true,
1213
"strict": true,
1314
"noImplicitAny": false,
1415
},

0 commit comments

Comments
 (0)