Skip to content

Commit

Permalink
feat: add suport for routes with underline and remove default 404 han…
Browse files Browse the repository at this point in the history
…dler
  • Loading branch information
pedronauck committed Oct 23, 2017
1 parent 2938f85 commit 300d5e3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
7 changes: 3 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const UrlPattern = require('url-pattern')
const { send } = require('micro')
const { getParamsAndQuery } = require('../utils')
const { getParamsAndQuery, patternOpts } = require('../utils')

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

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

const route = new UrlPattern(path)
const route = new UrlPattern(path, patternOpts)

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

send(res, 404, `Cannot ${req.method} ${req.url}`)
return false
}

METHODS.forEach(method => {
Expand Down
57 changes: 27 additions & 30 deletions lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,54 +34,51 @@ test('routes with params and query', async t => {
t.is(response, 'Hello world now')
})

test('async handlers', async t => {
const hello = async req =>
await Promise.resolve(`Hello ${req.params.msg} ${req.query.time}`)

test('routes with underline', async t => {
const routes = router(
get('/hello/:msg', hello)
get('/foo_bar', () => 'Hello with underline')
)

const url = await server(routes)
const response = await request(`${url}/hello/world?time=now`)
const response = await request(`${url}/foo_bar`)

t.is(response, 'Hello world now')
t.is(response, 'Hello with underline')
})

test('default 404 handler', async t => {
const hello = () => 'Hello world'
test('async handlers', async t => {
const hello = req =>
Promise.resolve(`Hello ${req.params.msg} ${req.query.time}`)

const routes = router(
get('/hello', hello)
get('/hello/:msg', hello)
)

const url = await server(routes)
const helloResponse = await request(`${url}/hello`)
t.is(helloResponse, 'Hello world')

const notfoundResponse = await request(`${url}/api`, {
simple: false,
resolveWithFullResponse: true
})
t.is(notfoundResponse.statusCode, 404)
t.is(notfoundResponse.body, 'Cannot GET /api')
const response = await request(`${url}/hello/world?time=now`)

t.is(response, 'Hello world now')
})

test('custom 404 handler', async t => {
const hello = () => 'Hello world'
const notfound = () => 'Not found'
test('composed routes', async t => {
const fooRouter = router(
get('/foo', () => `Hello foo`)
)

const barRouter = router(
get('/bar', () => `Hello bar`)
)

const routes = router(
get('/hello', hello),
get('/*', notfound)
fooRouter,
barRouter
)

const url = await server(routes)
const helloResponse = await request(`${url}/hello`)
const notfoundResponse = await request(`${url}/api`)
const fooResponse = await request(`${url}/foo`)
const barResponse = await request(`${url}/bar`)

t.is(helloResponse, 'Hello world')
t.is(notfoundResponse, 'Not found')
t.is(fooResponse, 'Hello foo')
t.is(barResponse, 'Hello bar')
})

test('multiple matching routes', async t => {
Expand Down Expand Up @@ -114,7 +111,7 @@ test('multiple matching async routes', async t => {
t.is(pathResponse, 'Hello world')
})

test('error without path and handler', async t => {
test('error without path and handler', t => {
const fn = () => {
router(get())
}
Expand All @@ -123,7 +120,7 @@ test('error without path and handler', async t => {
t.is(error.message, 'You need to set a valid path')
})

test('error without handler', async t => {
test('error without handler', t => {
const fn = () => {
router(get('/hey'))
}
Expand Down
9 changes: 7 additions & 2 deletions utils/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const { parse } = require('url')
const UrlPattern = require('url-pattern')

const patternOpts = {
segmentNameCharset: 'a-zA-Z0-9_-'
}

const getParamsAndQuery = (pattern, url) => {
const { query, pathname } = parse(url, true)
const route = pattern instanceof UrlPattern ? pattern : new UrlPattern(pattern)
const route = pattern instanceof UrlPattern ? pattern : new UrlPattern(pattern, patternOpts)
const params = route.match(pathname)

return { query, params }
}

module.exports = {
getParamsAndQuery
getParamsAndQuery,
patternOpts
}

0 comments on commit 300d5e3

Please sign in to comment.