microrouter
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -18,4 +18,8 @@ const { getParamsAndQuery } = require('../utils') | ||
exports.router = (...funcs) => async (req, res) => | ||
funcs.map(fn => fn(req, res)).find(fn => fn) | ||
exports.router = (...funcs) => async (req, res) => { | ||
for (const fn of funcs) { | ||
const result = await fn(req, res) | ||
if (result || res.headersSent) return result | ||
} | ||
} | ||
@@ -22,0 +26,0 @@ METHODS.forEach(method => { |
@@ -68,2 +68,32 @@ const test = require('ava') | ||
test('multiple matching routes', async t => { | ||
const withPath = () => 'Hello world' | ||
const withParam = () => t.fail('Clashing route should not have been called') | ||
const routes = router( | ||
get('/path', withPath), | ||
get('/:param', withParam) | ||
) | ||
const url = await server(routes) | ||
const pathResponse = await request(`${url}/path`) | ||
t.is(pathResponse, 'Hello world') | ||
}) | ||
test('multiple matching async routes', async t => { | ||
const withPath = (req, res) => micro.send(res, 200, 'Hello world') | ||
const withParam = () => t.fail('Clashing route should not have been called') | ||
const routes = router( | ||
get('/path', withPath), | ||
get('/:param', withParam) | ||
) | ||
const url = await server(routes) | ||
const pathResponse = await request(`${url}/path`) | ||
t.is(pathResponse, 'Hello world') | ||
}) | ||
test('error without path and handler', async t => { | ||
@@ -70,0 +100,0 @@ const fn = () => { |
{ | ||
"name": "microrouter", | ||
"description": "🚉 A tiny and functional router for Zeit's Micro", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"main": "lib/index.js", | ||
@@ -32,3 +32,4 @@ "scripts": { | ||
"object-curly-spacing": 0, | ||
"unicorn/explicit-length-check": 0 | ||
"unicorn/explicit-length-check": 0, | ||
"no-await-in-loop": 0 | ||
} | ||
@@ -35,0 +36,0 @@ }, |
@@ -31,3 +31,3 @@ :station: _**Micro Router -**_ A tiny and functional router for Zeit's [micro](https://github.com/zeit/micro) | ||
const notfound = () => | ||
const notfound = (req, res) => | ||
send(res, 404, 'Not found route') | ||
@@ -34,0 +34,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11500
126