microrouter
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -5,10 +5,12 @@ const { getParamsAndQuery } = require('../utils') | ||
const methodFn = method => (path, handler) => (req, res) => { | ||
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 { params, query } = getParamsAndQuery(path, req.url) | ||
return (req, res) => { | ||
const { params, query } = getParamsAndQuery(path, req.url) | ||
if (params && req.method === method) { | ||
return handler(Object.assign({}, req, { params, query }), res) | ||
if (params && req.method === method) { | ||
return handler(Object.assign({}, req, { params, query }), res) | ||
} | ||
} | ||
@@ -15,0 +17,0 @@ } |
@@ -50,1 +50,18 @@ const listen = require('test-listen') | ||
}) | ||
test('not found route', async t => { | ||
const hello = () => 'Hello world' | ||
const notfound = () => 'Not found' | ||
const routes = router( | ||
get('/hello', hello), | ||
get('/*', notfound) | ||
) | ||
const url = await server(routes) | ||
const { body: helloBody } = await got(`${url}/hello`) | ||
const { body: notfoundBody } = await got(`${url}/api`) | ||
t.is(helloBody, 'Hello world') | ||
t.is(notfoundBody, 'Not found') | ||
}) |
{ | ||
"name": "microrouter", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"main": "lib/index.js", | ||
@@ -12,8 +12,8 @@ "scripts": { | ||
"dependencies": { | ||
"route-parser": "^0.0.5" | ||
"url-pattern": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"micro": "^7.0.6", | ||
"ava": "^0.18.2", | ||
"got": "^6.7.1", | ||
"micro": "^7.0.6", | ||
"test-listen": "^1.0.1", | ||
@@ -20,0 +20,0 @@ "xo": "^0.17.1" |
@@ -10,3 +10,3 @@ :station: _**Micro Router -**_ A tiny and functional router for Zeit's [micro](https://github.com/zeit/micro) | ||
- **Tiny**. Just 23 lines of code. | ||
- **Functional**. Write your method using functions. | ||
- **Functional**. Write your http methods using functions. | ||
- **Async**. Design to use with `async/await` | ||
@@ -31,4 +31,8 @@ | ||
const notfound = () => | ||
send(res, 404, 'Not found route') | ||
module.exports = router( | ||
get('/hello/:who', hello) | ||
get('/hello/:who', hello), | ||
get('/*', notfound) | ||
) | ||
@@ -67,3 +71,3 @@ ``` | ||
A simple route path like that you can set any parameters using a `:` notation. | ||
A simple route path that you can set any parameters using a `:` notation. | ||
The `req` parameter from `handler` will return this parameters as a object. | ||
@@ -70,0 +74,0 @@ |
const { parse } = require('url') | ||
const Route = require('route-parser') | ||
const UrlPattern = require('url-pattern') | ||
const getParamsAndQuery = (path, url) => { | ||
const { query } = parse(url, true) | ||
const route = new Route(path) | ||
const params = route.match(url) | ||
const getParamsAndQuery = (pattern, url) => { | ||
const { query, pathname } = parse(url, true) | ||
const route = new UrlPattern(pattern) | ||
const params = route.match(pathname) | ||
@@ -9,0 +9,0 @@ return { query, params } |
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
8043
88
98
+ Addedurl-pattern@^1.0.3
+ Addedurl-pattern@1.0.3(transitive)
- Removedroute-parser@^0.0.5
- Removedroute-parser@0.0.5(transitive)