Skip to content

Commit 2f67545

Browse files
authored
Merge pull request #9 from Bessonov/reintroduce-default-match
reintroduce default match
2 parents ad189c9 + ec252df commit 2f67545

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

.github/workflows/on-commit.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
# https://github.com/actions/setup-node/issues/32#issuecomment-1003854758
1515
node-version-file: '.nvmrc'
1616
- name: prepare dependencies
17-
run: npm install
17+
run: |
18+
npm install
19+
npm install -g [email protected]
1820
- name: test
19-
run: npm run precommit
21+
run: pnpm run precommit

.github/workflows/publish.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ jobs:
1717
# https://github.com/actions/setup-node/issues/32#issuecomment-1003854758
1818
node-version-file: '.nvmrc'
1919
- name: prepare dependencies
20-
run: npm install
20+
run: |
21+
npm install
22+
npm install -g [email protected]
2123
- name: test
22-
run: npm run precommit
24+
run: pnpm run precommit
2325
- name: publish
2426
run: |
2527
echo '//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}' >> .npmrc

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bessonovs/node-http-router",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "Extensible http router for node and micro",
55
"keywords": [
66
"router",
@@ -30,7 +30,7 @@
3030
"build": "tsc",
3131
"example-node-start": "tsc && node dist/examples/node.js",
3232
"example-micro-start": "tsc && node dist/examples/micro.js",
33-
"precommit": "$_ run test && $_ run lint && $_ run build",
33+
"precommit": "pnpm test && pnpm run lint && pnpm run build",
3434
"update": "pnpm update --interactive --recursive --latest"
3535
},
3636
"dependencies": {
@@ -39,20 +39,20 @@
3939
"devDependencies": {
4040
"@bessonovs/eslint-config": "0.0.7",
4141
"@types/express": "4.17.13",
42-
"@types/jest": "28.1.3",
42+
"@types/jest": "28.1.7",
4343
"@types/node": "18.0.0",
44-
"@typescript-eslint/eslint-plugin": "5.29.0",
45-
"@typescript-eslint/parser": "5.29.0",
46-
"eslint": "8.18.0",
44+
"@typescript-eslint/eslint-plugin": "5.33.1",
45+
"@typescript-eslint/parser": "5.33.1",
46+
"eslint": "8.22.0",
4747
"eslint-config-airbnb": "19.0.4",
4848
"eslint-plugin-import": "2.26.0",
49-
"eslint-plugin-jsx-a11y": "6.5.1",
50-
"eslint-plugin-react": "7.30.0",
51-
"jest": "28.1.1",
49+
"eslint-plugin-jsx-a11y": "6.6.1",
50+
"eslint-plugin-react": "7.30.1",
51+
"jest": "28.1.3",
5252
"micro": "9.3.5-canary.3",
5353
"node-mocks-http": "1.11.0",
5454
"path-to-regexp": "6.2.1",
55-
"ts-jest": "28.0.5",
55+
"ts-jest": "28.0.8",
5656
"ts-toolbelt": "9.6.0",
5757
"typescript": "4.7.4"
5858
},

src/Router.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
isMatched,
55
} from './matchers'
66
import {
7+
MatchResult,
78
MatchResultAny,
89
} from './matchers/MatchResult'
910

@@ -29,7 +30,7 @@ export interface Route<MR extends MatchResultAny, D> {
2930
export class Router<D> {
3031
private routes: Route<MatchResultAny, D>[] = []
3132

32-
constructor() {
33+
constructor(private defaultHandler?: Handler<MatchResult<unknown>, D>) {
3334
this.addRoute = this.addRoute.bind(this)
3435
this.exec = this.exec.bind(this)
3536
}
@@ -44,11 +45,14 @@ export class Router<D> {
4445
const match = route.matcher.match(params)
4546
if (isMatched(match)) {
4647
return route.handler({
47-
data: params,
4848
match,
49+
data: params,
4950
})
5051
}
5152
}
52-
return undefined
53+
return this.defaultHandler?.({
54+
match: { matched: true, result: undefined },
55+
data: params,
56+
})
5357
}
5458
}

src/__tests__/Router.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ it('match POST /group/123 endpoint', () => {
150150
expect(router.exec({ req: createRequest() })).toBe('not found')
151151
})
152152

153+
it('no match, but default handler', () => {
154+
router = new Router(() => 'default route')
155+
const req = createRequest({
156+
method: 'POST',
157+
url: '/dontcare',
158+
})
159+
160+
expect(router.exec({ req })).toBe('default route')
161+
})
162+
153163
class TestMatcherWithParams<
154164
T extends MatchResult<void>,
155165
D extends { test: string }

0 commit comments

Comments
 (0)