microrouter
Advanced tools
Comparing version 2.2.3 to 3.0.0
const UrlPattern = require('url-pattern') | ||
const { send } = require('micro') | ||
const { getParamsAndQuery } = require('../utils') | ||
const { getParamsAndQuery, patternOpts } = require('../utils') | ||
@@ -11,3 +10,3 @@ const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'] | ||
const route = new UrlPattern(path) | ||
const route = new UrlPattern(path, patternOpts) | ||
@@ -29,3 +28,3 @@ return (req, res) => { | ||
send(res, 404, `Cannot ${req.method} ${req.url}`) | ||
return false | ||
} | ||
@@ -32,0 +31,0 @@ |
@@ -37,50 +37,47 @@ const test = require('ava') | ||
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 response = await request(`${url}/hello/world?time=now`) | ||
const notfoundResponse = await request(`${url}/api`, { | ||
simple: false, | ||
resolveWithFullResponse: true | ||
}) | ||
t.is(notfoundResponse.statusCode, 404) | ||
t.is(notfoundResponse.body, 'Cannot GET /api') | ||
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') | ||
}) | ||
@@ -118,3 +115,3 @@ | ||
test('error without path and handler', async t => { | ||
test('error without path and handler', t => { | ||
const fn = () => { | ||
@@ -128,3 +125,3 @@ router(get()) | ||
test('error without handler', async t => { | ||
test('error without handler', t => { | ||
const fn = () => { | ||
@@ -131,0 +128,0 @@ router(get('/hey')) |
{ | ||
"name": "microrouter", | ||
"description": "🚉 A tiny and functional router for ZEIT's Micro", | ||
"version": "2.2.3", | ||
"version": "3.0.0", | ||
"main": "dist/index.js", | ||
@@ -20,17 +20,14 @@ "jsnext:main": "lib/index.js", | ||
"devDependencies": { | ||
"ava": "^0.18.2", | ||
"ava": "^0.22.0", | ||
"babel-cli": "^6.24.1", | ||
"babel-plugin-transform-async-to-generator": "^6.24.1", | ||
"coveralls": "^2.11.16", | ||
"coveralls": "^3.0.0", | ||
"micro": "^9.0.0", | ||
"np": "^2.16.0", | ||
"nyc": "^10.1.2", | ||
"request": "^2.79.0", | ||
"request-promise": "^4.1.1", | ||
"test-listen": "^1.0.1", | ||
"xo": "^0.17.1" | ||
"nyc": "^11.2.1", | ||
"request": "^2.83.0", | ||
"request-promise": "^4.2.2", | ||
"test-listen": "^1.0.2", | ||
"xo": "^0.18.2" | ||
}, | ||
"peerDependencies": { | ||
"micro": "^9.0.0" | ||
}, | ||
"xo": { | ||
@@ -37,0 +34,0 @@ "space": 2, |
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) | ||
@@ -13,3 +17,4 @@ | ||
module.exports = { | ||
getParamsAndQuery | ||
getParamsAndQuery, | ||
patternOpts | ||
} |
Sorry, the diff of this file is not supported yet
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
173402
1
12