Skip to content

Commit 300d5e3

Browse files
committed
feat: add suport for routes with underline and remove default 404 handler
1 parent 2938f85 commit 300d5e3

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

lib/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
const UrlPattern = require('url-pattern')
2-
const { send } = require('micro')
3-
const { getParamsAndQuery } = require('../utils')
2+
const { getParamsAndQuery, patternOpts } = require('../utils')
43

54
const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']
65

76
const methodFn = method => (path, handler) => {
87
if (!path) throw new Error('You need to set a valid path')
98
if (!handler) throw new Error('You need to set a valid handler')
109

11-
const route = new UrlPattern(path)
10+
const route = new UrlPattern(path, patternOpts)
1211

1312
return (req, res) => {
1413
const { params, query } = getParamsAndQuery(route, req.url)
@@ -25,7 +24,7 @@ exports.router = (...funcs) => async (req, res) => {
2524
if (result || res.headersSent) return result
2625
}
2726

28-
send(res, 404, `Cannot ${req.method} ${req.url}`)
27+
return false
2928
}
3029

3130
METHODS.forEach(method => {

lib/index.test.js

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,54 +34,51 @@ test('routes with params and query', async t => {
3434
t.is(response, 'Hello world now')
3535
})
3636

37-
test('async handlers', async t => {
38-
const hello = async req =>
39-
await Promise.resolve(`Hello ${req.params.msg} ${req.query.time}`)
40-
37+
test('routes with underline', async t => {
4138
const routes = router(
42-
get('/hello/:msg', hello)
39+
get('/foo_bar', () => 'Hello with underline')
4340
)
4441

4542
const url = await server(routes)
46-
const response = await request(`${url}/hello/world?time=now`)
43+
const response = await request(`${url}/foo_bar`)
4744

48-
t.is(response, 'Hello world now')
45+
t.is(response, 'Hello with underline')
4946
})
5047

51-
test('default 404 handler', async t => {
52-
const hello = () => 'Hello world'
48+
test('async handlers', async t => {
49+
const hello = req =>
50+
Promise.resolve(`Hello ${req.params.msg} ${req.query.time}`)
5351

5452
const routes = router(
55-
get('/hello', hello)
53+
get('/hello/:msg', hello)
5654
)
5755

5856
const url = await server(routes)
59-
const helloResponse = await request(`${url}/hello`)
60-
t.is(helloResponse, 'Hello world')
61-
62-
const notfoundResponse = await request(`${url}/api`, {
63-
simple: false,
64-
resolveWithFullResponse: true
65-
})
66-
t.is(notfoundResponse.statusCode, 404)
67-
t.is(notfoundResponse.body, 'Cannot GET /api')
57+
const response = await request(`${url}/hello/world?time=now`)
58+
59+
t.is(response, 'Hello world now')
6860
})
6961

70-
test('custom 404 handler', async t => {
71-
const hello = () => 'Hello world'
72-
const notfound = () => 'Not found'
62+
test('composed routes', async t => {
63+
const fooRouter = router(
64+
get('/foo', () => `Hello foo`)
65+
)
66+
67+
const barRouter = router(
68+
get('/bar', () => `Hello bar`)
69+
)
7370

7471
const routes = router(
75-
get('/hello', hello),
76-
get('/*', notfound)
72+
fooRouter,
73+
barRouter
7774
)
7875

7976
const url = await server(routes)
80-
const helloResponse = await request(`${url}/hello`)
81-
const notfoundResponse = await request(`${url}/api`)
77+
const fooResponse = await request(`${url}/foo`)
78+
const barResponse = await request(`${url}/bar`)
8279

83-
t.is(helloResponse, 'Hello world')
84-
t.is(notfoundResponse, 'Not found')
80+
t.is(fooResponse, 'Hello foo')
81+
t.is(barResponse, 'Hello bar')
8582
})
8683

8784
test('multiple matching routes', async t => {
@@ -114,7 +111,7 @@ test('multiple matching async routes', async t => {
114111
t.is(pathResponse, 'Hello world')
115112
})
116113

117-
test('error without path and handler', async t => {
114+
test('error without path and handler', t => {
118115
const fn = () => {
119116
router(get())
120117
}
@@ -123,7 +120,7 @@ test('error without path and handler', async t => {
123120
t.is(error.message, 'You need to set a valid path')
124121
})
125122

126-
test('error without handler', async t => {
123+
test('error without handler', t => {
127124
const fn = () => {
128125
router(get('/hey'))
129126
}

utils/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
const { parse } = require('url')
22
const UrlPattern = require('url-pattern')
33

4+
const patternOpts = {
5+
segmentNameCharset: 'a-zA-Z0-9_-'
6+
}
7+
48
const getParamsAndQuery = (pattern, url) => {
59
const { query, pathname } = parse(url, true)
6-
const route = pattern instanceof UrlPattern ? pattern : new UrlPattern(pattern)
10+
const route = pattern instanceof UrlPattern ? pattern : new UrlPattern(pattern, patternOpts)
711
const params = route.match(pathname)
812

913
return { query, params }
1014
}
1115

1216
module.exports = {
13-
getParamsAndQuery
17+
getParamsAndQuery,
18+
patternOpts
1419
}

0 commit comments

Comments
 (0)