microrouter
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
const { getParamsAndQuery, parseBody } = require('../utils') | ||
const { getParamsAndQuery } = require('../utils') | ||
@@ -9,7 +9,7 @@ const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'] | ||
return (req, res, body) => { | ||
return (req, res) => { | ||
const { params, query } = getParamsAndQuery(path, req.url) | ||
if (params && req.method === method) { | ||
return handler(Object.assign({}, req, { params, query, body }), res) | ||
return handler(Object.assign({}, req, { params, query }), res) | ||
} | ||
@@ -19,6 +19,4 @@ } | ||
exports.router = (...funcs) => async (req, res) => { | ||
const body = await parseBody(req) | ||
return funcs.map(fn => fn(req, res, body)).filter(fn => fn)[0] | ||
} | ||
exports.router = (...funcs) => async (req, res) => | ||
funcs.map(fn => fn(req, res)).find(fn => fn) | ||
@@ -25,0 +23,0 @@ METHODS.forEach(method => { |
@@ -6,3 +6,3 @@ const test = require('ava') | ||
const { router, get, post } = require('./') | ||
const { router, get } = require('./') | ||
@@ -69,15 +69,18 @@ const server = fn => listen(micro(fn)) | ||
test('body request', async t => { | ||
const hello = req => `Hello ${req.body.who}` | ||
test('error without path and handler', async t => { | ||
const fn = () => { | ||
router(get()) | ||
} | ||
const routes = router( | ||
post('/hello', hello) | ||
) | ||
const error = t.throws(fn, Error) | ||
t.is(error.message, 'You need to set a valid path') | ||
}) | ||
const body = { who: 'world' } | ||
test('error without handler', async t => { | ||
const fn = () => { | ||
router(get('/hey')) | ||
} | ||
const url = await server(routes) | ||
const response = await request.post(`${url}/hello`, { body, json: true }) | ||
t.is(response, 'Hello world') | ||
const error = t.throws(fn, Error) | ||
t.is(error.message, 'You need to set a valid handler') | ||
}) |
{ | ||
"name": "microrouter", | ||
"version": "1.2.2", | ||
"description": "🚉 A tiny and functional router for Zeit's Micro", | ||
"version": "2.0.0", | ||
"main": "lib/index.js", | ||
@@ -13,3 +14,2 @@ "scripts": { | ||
"dependencies": { | ||
"micro": "^7.0.6", | ||
"url-pattern": "^1.0.3" | ||
@@ -20,2 +20,3 @@ }, | ||
"coveralls": "^2.11.16", | ||
"micro": "^7.0.6", | ||
"nyc": "^10.1.2", | ||
@@ -22,0 +23,0 @@ "request": "^2.79.0", |
@@ -5,2 +5,3 @@ :station: _**Micro Router -**_ A tiny and functional router for Zeit's [micro](https://github.com/zeit/micro) | ||
[](https://travis-ci.org/pedronauck/micro-router) | ||
[]() | ||
[](https://github.com/sindresorhus/xo) | ||
@@ -60,11 +61,11 @@ | ||
- `get(path, handler)` | ||
- `post(path, handler)` | ||
- `put(path, handler)` | ||
- `patch(path, handler)` | ||
- `delete(path, handler)` | ||
- `head(path, handler)` | ||
- `options(path, handler)` | ||
- `get(path = String, handler = Function)` | ||
- `post(path = String, handler = Function)` | ||
- `put(path = String, handler = Function)` | ||
- `patch(path = String, handler = Function)` | ||
- `delete(path = String, handler = Function)` | ||
- `head(path = String, handler = Function)` | ||
- `options(path = String, handler = Function)` | ||
#### `path` | ||
#### path | ||
@@ -78,3 +79,3 @@ A simple url pattern that you can define your path. In this path you can set your parameters using a `:` notation. The `req` parameter from `handler` will return this parameters as an object. | ||
The `handler` method is a simple function that will make some action base on your path. | ||
The format of this method is `(res, res) => {}` | ||
The format of this function is `(res, res) => {}` | ||
@@ -119,20 +120,24 @@ ##### `req.params` | ||
##### `req.body` | ||
### Parsing Body | ||
Finally, the last property of `req` parameter that you might use a lot is `body` that representsfix your body requisition passed through your method. Note that `body` result is the same that you will sent, here the body hasn't any parser, if you can parse it you need to do that by yourself | ||
By default, router *doens't parse anything* from your requisition, it's just match your paths and execute a specific handler. So, if you want to parse your body requisition you can do something like that: | ||
```js | ||
``` | ||
const { router, post } = require('microrouter') | ||
const { json, send } = require('micro') | ||
const request = require('some-request-lib') | ||
// service.js | ||
const user = async (req, res) => { | ||
const body = await json(req) | ||
send(res, 200, body) | ||
} | ||
module.exports = router( | ||
post('/user', (req, res) => req.body) | ||
post('/user', user) | ||
) | ||
// test.js | ||
const body = { name: 'John' } | ||
const response = await request.post('/user', { body, json: true }) | ||
console.log(response) // { name: 'John' } | ||
const body = { id: 1 } | ||
const response = await request.post('/user', { body }) | ||
``` | ||
@@ -139,0 +144,0 @@ |
const { parse } = require('url') | ||
const { json } = require('micro') | ||
const UrlPattern = require('url-pattern') | ||
@@ -13,13 +12,4 @@ | ||
const parseBody = async req => { | ||
try { | ||
return await json(req) | ||
} catch (err) { | ||
return {} | ||
} | ||
} | ||
module.exports = { | ||
getParamsAndQuery, | ||
parseBody | ||
getParamsAndQuery | ||
} |
const test = require('ava') | ||
const micro = require('micro') | ||
const listen = require('test-listen') | ||
const request = require('request-promise') | ||
const { getParamsAndQuery, parseBody } = require('./') | ||
const { getParamsAndQuery } = require('./') | ||
const server = fn => listen(micro(fn)) | ||
test('getParamsAndQuery()', t => { | ||
@@ -19,13 +14,1 @@ const path = '/hello/:msg' | ||
}) | ||
test('parseBody()', async t => { | ||
const url = await server(async (req, res) => { | ||
const body = await parseBody(req) | ||
micro.send(res, 200, body) | ||
}) | ||
const body = { foo: 'bar' } | ||
const response = await request.post(url, { body, json: true }) | ||
t.deepEqual(response, body) | ||
}) |
1
-50%10
11.11%147
3.52%10591
-0.74%8
14.29%100
-16.67%- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed