Skip to content

Commit

Permalink
feat(router): allow registering multiple methods at once (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
seho-dev authored Apr 12, 2022
1 parent 8b2e1bc commit c26bd46
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { CompatibilityEventHandler } from './event'
export type RouterMethod = Lowercase<HTTPMethod>
const RouterMethods: RouterMethod[] = ['connect', 'delete', 'get', 'head', 'options', 'post', 'put', 'trace']

export type RouterUse = (path: string, handler: CompatibilityEventHandler, method?: RouterMethod) => Router
export type RouterUse = (path: string, handler: CompatibilityEventHandler, method?: RouterMethod | RouterMethod[]) => Router
export type AddRouteShortcuts = Record<RouterMethod, RouterUse>

export interface Router extends AddRouteShortcuts {
Expand All @@ -17,7 +17,7 @@ export interface Router extends AddRouteShortcuts {
}

interface RouteNode {
handlers: Partial<Record<RouterMethod| 'all', EventHandler>>
handlers: Partial<Record<RouterMethod | 'all', EventHandler>>
}

export function createRouter (): Router {
Expand All @@ -27,13 +27,17 @@ export function createRouter (): Router {
const router: Router = {} as Router

// Utilities to add a new route
const addRoute = (path: string, handler: CompatibilityEventHandler, method: RouterMethod| 'all') => {
const addRoute = (path: string, handler: CompatibilityEventHandler, method: RouterMethod | RouterMethod[] | 'all') => {
let route = routes[path]
if (!route) {
routes[path] = route = { handlers: {} }
_router.insert(path, route)
}
route.handlers[method] = toEventHandler(handler)
if (Array.isArray(method)) {
method.forEach(m => addRoute(path, handler, m))
} else {
route.handlers[method] = toEventHandler(handler)
}
return router
}
router.use = router.add = (path, handler, method) => addRoute(path, handler, method || 'all')
Expand Down
11 changes: 11 additions & 0 deletions test/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('router', () => {
router = createRouter()
.add('/', () => 'Hello')
.add('/test/?/a', () => '/test/?/a')
.add('/many/routes', () => 'many routes', ['get', 'post'])
.get('/test', () => 'Test (GET)')
.post('/test', () => 'Test (POST)')

Expand Down Expand Up @@ -40,6 +41,16 @@ describe('router', () => {
expect(res.status).toEqual(200)
})

it('Handle many methods (get)', async () => {
const res = await request.get('/many/routes')
expect(res.status).toEqual(200)
})

it('Handle many methods (post)', async () => {
const res = await request.post('/many/routes')
expect(res.status).toEqual(200)
})

it('Not matching route', async () => {
const res = await request.get('/404')
expect(res.status).toEqual(404)
Expand Down

0 comments on commit c26bd46

Please sign in to comment.