microrouter
Advanced tools
Comparing version 1.2.2 to 2.0.0
@@ -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) | ||
[![Build Status](https://travis-ci.org/pedronauck/micro-router.svg?branch=master)](https://travis-ci.org/pedronauck/micro-router) | ||
[![Coveralls](https://img.shields.io/coveralls/pedronauck/micro-router.svg)]() | ||
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](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) | ||
}) |
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
1
10
147
10591
8
100
- Removedmicro@^7.0.6
- Removedansi-align@2.0.0(transitive)
- Removedansi-regex@2.1.13.0.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedargs@2.6.1(transitive)
- Removedasync-to-gen@1.3.3(transitive)
- Removedbabylon@6.18.0(transitive)
- Removedbluebird@3.5.0(transitive)
- Removedboxen@1.1.0(transitive)
- Removedbytes@2.4.0(transitive)
- Removedcamelcase@4.1.0(transitive)
- Removedcapture-stack-trace@1.0.2(transitive)
- Removedchalk@1.1.3(transitive)
- Removedcli-boxes@1.0.0(transitive)
- Removedclipboardy@1.1.1(transitive)
- Removedcode-point-at@1.1.0(transitive)
- Removedconfigstore@3.1.5(transitive)
- Removedcreate-error-class@3.0.2(transitive)
- Removedcross-spawn@5.1.0(transitive)
- Removedcross-spawn-async@2.2.5(transitive)
- Removedcrypto-random-string@1.0.0(transitive)
- Removeddeep-extend@0.6.0(transitive)
- Removeddot-prop@4.2.1(transitive)
- Removedduplexer3@0.1.5(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedexeca@0.4.00.6.3(transitive)
- Removedget-port@3.1.0(transitive)
- Removedget-stream@3.0.0(transitive)
- Removedgot@6.7.1(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removediconv-lite@0.4.15(transitive)
- Removedimurmurhash@0.1.4(transitive)
- Removedini@1.3.8(transitive)
- Removedip@1.1.5(transitive)
- Removedis-async-supported@1.2.0(transitive)
- Removedis-fullwidth-code-point@1.0.02.0.0(transitive)
- Removedis-npm@1.0.0(transitive)
- Removedis-obj@1.0.1(transitive)
- Removedis-redirect@1.0.0(transitive)
- Removedis-retry-allowed@1.2.0(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedisexe@2.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedlatest-version@3.1.0(transitive)
- Removedlazy-req@2.0.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removedlowercase-keys@1.0.1(transitive)
- Removedlru-cache@4.1.5(transitive)
- Removedmagic-string@0.19.1(transitive)
- Removedmake-dir@1.3.0(transitive)
- Removedmedia-typer@0.3.0(transitive)
- Removedmicro@7.3.3(transitive)
- Removedminimist@1.2.0(transitive)
- Removednode-version@1.0.0(transitive)
- Removednpm-run-path@1.0.02.0.2(transitive)
- Removednumber-is-nan@1.0.1(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedp-finally@1.0.0(transitive)
- Removedpackage-json@4.0.1(transitive)
- Removedpath-key@1.0.02.0.1(transitive)
- Removedpify@3.0.0(transitive)
- Removedpkginfo@0.4.0(transitive)
- Removedprepend-http@1.0.4(transitive)
- Removedpseudomap@1.0.2(transitive)
- Removedraw-body@2.2.0(transitive)
- Removedrc@1.2.8(transitive)
- Removedregistry-auth-token@3.4.0(transitive)
- Removedregistry-url@3.1.0(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsemver@5.7.2(transitive)
- Removedsemver-diff@2.1.0(transitive)
- Removedshebang-command@1.2.0(transitive)
- Removedshebang-regex@1.0.0(transitive)
- Removedsignal-exit@3.0.7(transitive)
- Removedstring-similarity@1.1.0(transitive)
- Removedstring-width@1.0.22.1.1(transitive)
- Removedstrip-ansi@3.0.14.0.0(transitive)
- Removedstrip-eof@1.0.0(transitive)
- Removedstrip-json-comments@2.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedterm-size@0.1.1(transitive)
- Removedtimed-out@4.0.1(transitive)
- Removedunique-string@1.0.0(transitive)
- Removedunpipe@1.0.0(transitive)
- Removedunzip-response@2.0.1(transitive)
- Removedupdate-notifier@2.1.0(transitive)
- Removedurl-parse-lax@1.0.0(transitive)
- Removedvlq@0.2.3(transitive)
- Removedwhich@1.3.1(transitive)
- Removedwidest-line@1.0.0(transitive)
- Removedwrite-file-atomic@2.4.3(transitive)
- Removedxdg-basedir@3.0.0(transitive)
- Removedyallist@2.1.2(transitive)