Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/on-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
# https://github.com/actions/setup-node/issues/32#issuecomment-1003854758
node-version-file: '.nvmrc'
- name: prepare dependencies
run: npm install
run: |
npm install
npm install -g [email protected]
- name: test
run: npm run precommit
run: pnpm run precommit
6 changes: 4 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ jobs:
# https://github.com/actions/setup-node/issues/32#issuecomment-1003854758
node-version-file: '.nvmrc'
- name: prepare dependencies
run: npm install
run: |
npm install
npm install -g [email protected]
- name: test
run: npm run precommit
run: pnpm run precommit
- name: publish
run: |
echo '//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}' >> .npmrc
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bessonovs/node-http-router",
"version": "2.1.0",
"version": "2.2.0",
"description": "Extensible http router for node and micro",
"keywords": [
"router",
Expand Down Expand Up @@ -30,7 +30,7 @@
"build": "tsc",
"example-node-start": "tsc && node dist/examples/node.js",
"example-micro-start": "tsc && node dist/examples/micro.js",
"precommit": "$_ run test && $_ run lint && $_ run build",
"precommit": "pnpm test && pnpm run lint && pnpm run build",
"update": "pnpm update --interactive --recursive --latest"
},
"dependencies": {
Expand All @@ -39,20 +39,20 @@
"devDependencies": {
"@bessonovs/eslint-config": "0.0.7",
"@types/express": "4.17.13",
"@types/jest": "28.1.3",
"@types/jest": "28.1.7",
"@types/node": "18.0.0",
"@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0",
"eslint": "8.18.0",
"@typescript-eslint/eslint-plugin": "5.33.1",
"@typescript-eslint/parser": "5.33.1",
"eslint": "8.22.0",
"eslint-config-airbnb": "19.0.4",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-jsx-a11y": "6.5.1",
"eslint-plugin-react": "7.30.0",
"jest": "28.1.1",
"eslint-plugin-jsx-a11y": "6.6.1",
"eslint-plugin-react": "7.30.1",
"jest": "28.1.3",
"micro": "9.3.5-canary.3",
"node-mocks-http": "1.11.0",
"path-to-regexp": "6.2.1",
"ts-jest": "28.0.5",
"ts-jest": "28.0.8",
"ts-toolbelt": "9.6.0",
"typescript": "4.7.4"
},
Expand Down
10 changes: 7 additions & 3 deletions src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isMatched,
} from './matchers'
import {
MatchResult,
MatchResultAny,
} from './matchers/MatchResult'

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

constructor() {
constructor(private defaultHandler?: Handler<MatchResult<unknown>, D>) {
this.addRoute = this.addRoute.bind(this)
this.exec = this.exec.bind(this)
}
Expand All @@ -44,11 +45,14 @@ export class Router<D> {
const match = route.matcher.match(params)
if (isMatched(match)) {
return route.handler({
data: params,
match,
data: params,
})
}
}
return undefined
return this.defaultHandler?.({
match: { matched: true, result: undefined },
data: params,
})
}
}
10 changes: 10 additions & 0 deletions src/__tests__/Router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ it('match POST /group/123 endpoint', () => {
expect(router.exec({ req: createRequest() })).toBe('not found')
})

it('no match, but default handler', () => {
router = new Router(() => 'default route')
const req = createRequest({
method: 'POST',
url: '/dontcare',
})

expect(router.exec({ req })).toBe('default route')
})

class TestMatcherWithParams<
T extends MatchResult<void>,
D extends { test: string }
Expand Down