Skip to content

Commit c15c4e9

Browse files
authored
Merge pull request #4 from Bessonov/fix-export-type
Fix export type
2 parents 622e185 + ceedfe9 commit c15c4e9

File tree

6 files changed

+31
-16
lines changed

6 files changed

+31
-16
lines changed

src/examples/micro.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ server.once('listening', () => {
2929

3030
router.addRoute({
3131
// it's not necessary to type the matcher, but it give you a confidence
32-
matcher: new EndpointMatcher<{groups: {name: string}}>('GET', /^\/hello\/(?<name>[^/]+)$/),
32+
matcher: new EndpointMatcher<{name: string}>('GET', /^\/hello\/(?<name>[^/]+)$/),
3333
handler: (req, res, match) => {
3434
return `Hello ${match.match.groups.name}!`
3535
},

src/examples/node.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import http from 'http'
22
import { Router } from '../router'
33
import {
4-
EndpointMatcher, ExactUrlPathnameMatcher,
4+
EndpointMatcher,
5+
ExactUrlPathnameMatcher,
56
} from '../matchers'
67

78
/*
@@ -31,7 +32,7 @@ server.once('listening', () => {
3132

3233
router.addRoute({
3334
// it's not necessary to type the matcher, but it give you a confidence
34-
matcher: new EndpointMatcher<{groups: {name: string}}>('GET', /^\/hello\/(?<name>[^/]+)$/),
35+
matcher: new EndpointMatcher<{ name: string }>('GET', /^\/hello\/(?<name>[^/]+)$/),
3536
handler: (req, res, match) => {
3637
res.write(`Hello ${match.match.groups.name}!`)
3738
res.end()

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './matchers'
2-
export type { Route, Router, MatchedHandler } from './router'
2+
export type { Route, MatchedHandler } from './router'
3+
export { Router } from './router'

src/matchers/EndpointMatcher.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
11
import {
2-
IncomingMessage, ServerResponse,
2+
IncomingMessage,
3+
ServerResponse,
34
} from 'http'
45
import { Matcher } from './Matcher'
56
import { MatchResult } from './MatchResult'
67
import {
7-
Method, MethodMatchResult, MethodMatcher,
8+
Method,
9+
MethodMatchResult,
10+
MethodMatcher,
811
} from './MethodMatcher'
912
import { AndMatcher } from './AndMatcher'
1013
import {
11-
RegExpUrlMatchResult, RegExpUrlMatcher,
14+
RegExpExecGroupArray,
15+
RegExpUrlMatchResult,
16+
RegExpUrlMatcher,
1217
} from './RegExpUrlMatcher'
1318

1419
// waiting for
1520
// https://github.com/microsoft/TypeScript/issues/10571
1621
// https://github.com/microsoft/TypeScript/pull/26349
1722
// to resolve http method
1823

19-
export type EndpointMatchResult<R extends Partial<RegExpExecArray>> =
24+
export type EndpointMatchResult<R extends object> =
2025
MatchResult<{
2126
method: Method
22-
match: RegExpExecArray & R
27+
match: RegExpExecGroupArray<R>
2328
}>
2429

2530
/**
2631
* higher order matcher which is combine matching of method
2732
* with regular expression
2833
*/
29-
export class EndpointMatcher<R extends Partial<RegExpExecArray>>
34+
export class EndpointMatcher<R extends object>
3035
implements Matcher<EndpointMatchResult<R>> {
3136
private readonly matcher: AndMatcher<MethodMatchResult<[Method]>, RegExpUrlMatchResult<R>>
3237
constructor(method: Method, url: RegExp) {
3338
this.matcher = new AndMatcher([
3439
new MethodMatcher([method]),
35-
new RegExpUrlMatcher([url]),
40+
new RegExpUrlMatcher<R>([url]),
3641
])
3742
}
3843

src/matchers/RegExpUrlMatcher.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@ import { IncomingMessage } from 'http'
22
import { Matcher } from './Matcher'
33
import { MatchResult } from './MatchResult'
44

5-
export type RegExpUrlMatchResult<R extends Partial<RegExpExecArray>> = MatchResult<{
6-
match: RegExpExecArray & R
5+
export interface RegExpExecGroupArray<
6+
T extends object
7+
> extends Array<string> {
8+
index: number
9+
input: string
10+
groups: T
11+
}
12+
13+
export type RegExpUrlMatchResult<R extends object> = MatchResult<{
14+
match: RegExpExecGroupArray<R>
715
}>
816

9-
export class RegExpUrlMatcher<R extends Partial<RegExpExecArray>>
17+
export class RegExpUrlMatcher<R extends object>
1018
implements Matcher<RegExpUrlMatchResult<R>> {
1119
constructor(private readonly urls: [RegExp, ...RegExp[]]) {
1220
}
1321

1422
match(req: IncomingMessage): RegExpUrlMatchResult<R> {
1523
if (req.url) {
1624
for (const url of this.urls) {
17-
const result = url.exec(req.url) as never as RegExpExecArray & R
25+
const result = url.exec(req.url) as never as RegExpExecGroupArray<R>
1826
if (result !== null) {
1927
return {
2028
matched: true,

src/matchers/__tests__/RegExpUrlMatcher.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ it('match second', () => {
4343
})
4444

4545
it('match group', () => {
46-
const result = new RegExpUrlMatcher<{groups: {groupId: string}}>([/^\/group\/(?<groupId>[^/]+)$/])
46+
const result = new RegExpUrlMatcher<{ groupId: string }>([/^\/group\/(?<groupId>[^/]+)$/])
4747
.match(httpMocks.createRequest({
4848
url: '/group/123',
4949
}))

0 commit comments

Comments
 (0)