Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pedronauck/micro-router
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.1.1
Choose a base ref
...
head repository: pedronauck/micro-router
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 14 commits
  • 8 files changed
  • 7 contributors

Commits on Mar 6, 2018

  1. chore: update readme

    pedronauck authored Mar 6, 2018
    Copy the full SHA
    f959b7f View commit details

Commits on Mar 21, 2018

  1. fix (res, res) typo in readme

    brendancwood authored and pedronauck committed Mar 21, 2018
    Copy the full SHA
    c39e97a View commit details

Commits on Apr 16, 2018

  1. Copy the full SHA
    40e478c View commit details
  2. Allow some more chars in url as pattern value

    Allow character @, dot, +, _ and - as url pattern value, so email address can be passed as url parameter.
    mingz-work authored and pedronauck committed Apr 16, 2018
    Copy the full SHA
    7a6fb64 View commit details
  3. Remove extra comma.

    Remove extra comma causing CI to fail.
    mingz-work authored and pedronauck committed Apr 16, 2018
    Copy the full SHA
    5dc2158 View commit details
  4. eslint-config-prettier Fix

    Remove back slash as recommanded by eslint-config-prettier.
    mingz-work authored and pedronauck committed Apr 16, 2018
    Copy the full SHA
    3cad5af View commit details
  5. Fix eslint-plugin-prettier

    Insert ending comma as suggested by eslint-plugin-prettier
    mingz-work authored and pedronauck committed Apr 16, 2018
    Copy the full SHA
    1404d8c View commit details
  6. chore: pr adjustments

    pedronauck committed Apr 16, 2018
    Copy the full SHA
    6db8c63 View commit details
  7. v3.1.2

    pedronauck committed Apr 16, 2018
    Copy the full SHA
    34bc280 View commit details

Commits on May 11, 2018

  1. fix: update dist

    pedronauck committed May 11, 2018
    Copy the full SHA
    e3ffac5 View commit details
  2. v3.1.3

    pedronauck committed May 11, 2018
    Copy the full SHA
    c9e7db7 View commit details

Commits on May 12, 2018

  1. chore: use yarn cache

    DanielRuf authored and pedronauck committed May 12, 2018
    Copy the full SHA
    46aac81 View commit details
  2. chore: clone last 5 commits (#50)

    DanielRuf authored and pedronauck committed May 12, 2018
    Copy the full SHA
    a41fe90 View commit details

Commits on Jun 13, 2018

  1. fix: typo (#51)

    shidhincr authored and pedronauck committed Jun 13, 2018
    Copy the full SHA
    b551f42 View commit details
Showing with 45 additions and 15 deletions.
  1. +3 −0 .travis.yml
  2. +4 −3 dist/lib/index.js
  3. +2 −1 dist/utils/index.js
  4. +2 −1 package.json
  5. +5 −5 readme.md
  6. +6 −5 src/lib/index.js
  7. +22 −0 src/lib/index.test.js
  8. +1 −0 src/utils/index.js
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -4,3 +4,6 @@ after_success: npm run coverage
branches:
only:
- master
git:
depth: 5
cache: yarn
7 changes: 4 additions & 3 deletions dist/lib/index.js
Original file line number Diff line number Diff line change
@@ -5,12 +5,13 @@ const { getParamsAndQuery, isPattern, patternOpts } = require('../utils');

const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'];

const methodFn = method => (path, handler) => {
if (!path) throw new Error('You need to set a valid path');
const methodFn = method => (givenPath, handler) => {
if (!givenPath) throw new Error('You need to set a valid path');
if (!handler) throw new Error('You need to set a valid handler');

return (req, res, namespace) => {
const route = !isPattern(path) ? new UrlPattern(`${namespace}${path}`, patternOpts) : path;
const path = givenPath === '/' ? '(/)' : givenPath;
const route = isPattern(path) ? path : new UrlPattern(`${namespace}${path}`, patternOpts);

const { params, query } = getParamsAndQuery(route, req.url);

3 changes: 2 additions & 1 deletion dist/utils/index.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@ const { parse } = require('url');
const UrlPattern = require('url-pattern');

const patternOpts = {
segmentNameCharset: 'a-zA-Z0-9_-'
segmentNameCharset: 'a-zA-Z0-9_-',
segmentValueCharset: 'a-zA-Z0-9@.+-_'
};

const isPattern = pattern => pattern instanceof UrlPattern;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "microrouter",
"description": "🚉 A tiny and functional router for ZEIT's Micro",
"version": "3.1.1",
"version": "3.1.3",
"main": "dist/lib/index.js",
"jsnext:main": "src/lib/index.js",
"scripts": {
"prerelease": "yarn test && yarn lint && yarn build",
"release": "np",
"lint": "eslint ./src/**/*.js",
"build": "yarn lint && rm -rf dist/* && babel src --ignore *.test.js --out-dir dist --copy-files",
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

## 👌   Features

* **Tiny**. Just 23 lines of code.
* **Tiny**. Just couple lines of code.
* **Functional**. Write your http methods using functions.
* **Async**. Design to use with `async/await`

@@ -60,14 +60,14 @@ Each route is a single basic http method that you import from `microrouter` and

#### path

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.
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 these parameters as an object.

For more information about how you can define your path, see [url-pattern](https://github.com/snd/url-pattern) that's the package that we're using to match paths.

#### handler

The `handler` method is a simple function that will make some action base on your path.
The format of this function is `(res, res) => {}`
The format of this function is `(req, res) => {}`

##### `req.params`

@@ -109,7 +109,7 @@ console.log(response) // { id: 1 }

### Parsing Body

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:
By default, router _doesn'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')
@@ -133,7 +133,7 @@ const response = await request.post('/user', { body })

### UrlPattern instance as path

The package [url-pattern](https://github.com/snd/url-pattern) has a lot of options inside it to match url. If you has a different need for some of your paths, like make pattern from a regexp, you can pass a instance of `UrlPattern` as the path parameter:
The package [url-pattern](https://github.com/snd/url-pattern) has a lot of options inside it to match url. If you have a different need for some of your paths, like a make pattern from a regexp, you can pass an instance of `UrlPattern` as the path parameter:

```js
const UrlPattern = require('url-pattern')
11 changes: 6 additions & 5 deletions src/lib/index.js
Original file line number Diff line number Diff line change
@@ -3,14 +3,15 @@ const { getParamsAndQuery, isPattern, patternOpts } = require('../utils')

const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']

const methodFn = method => (path, handler) => {
if (!path) throw new Error('You need to set a valid path')
const methodFn = method => (givenPath, handler) => {
if (!givenPath) throw new Error('You need to set a valid path')
if (!handler) throw new Error('You need to set a valid handler')

return (req, res, namespace) => {
const route = !isPattern(path)
? new UrlPattern(`${namespace}${path}`, patternOpts)
: path
const path = givenPath === '/' ? '(/)' : givenPath
const route = isPattern(path)
? path
: new UrlPattern(`${namespace}${path}`, patternOpts)

const { params, query } = getParamsAndQuery(route, req.url)

22 changes: 22 additions & 0 deletions src/lib/index.test.js
Original file line number Diff line number Diff line change
@@ -177,3 +177,25 @@ test('routes with namespace', async t => {
t.is(fooResponse, 'foo')
t.is(barResponse, 'bar')
})

test('route with namespace without trailing slash', async t => {
const fooRoutes = withNamespace('/foo')

const routes = router(fooRoutes(get('/', () => 'foo')))

const url = await server(routes)
const fooResponse = await request(`${url}/foo`)

t.is(fooResponse, 'foo')
})

test('route with namespace with trailing slash', async t => {
const fooRoutes = withNamespace('/foo')

const routes = router(fooRoutes(get('/', () => 'foo')))

const url = await server(routes)
const fooResponse = await request(`${url}/foo/`)

t.is(fooResponse, 'foo')
})
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ const UrlPattern = require('url-pattern')

const patternOpts = {
segmentNameCharset: 'a-zA-Z0-9_-',
segmentValueCharset: 'a-zA-Z0-9@.+-_',
}

const isPattern = pattern => pattern instanceof UrlPattern