Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

microrouter

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

microrouter - npm Package Compare versions

Comparing version 3.0.0 to 3.1.0

.eslintrc

16

dist/index.js
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
const UrlPattern = require('url-pattern');
const { send } = require('micro');
const { getParamsAndQuery } = require('../utils');
const { getParamsAndQuery, isPattern, patternOpts } = require('../utils');

@@ -13,5 +12,5 @@ const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'];

const route = new UrlPattern(path);
return (req, res, namespace) => {
const route = !isPattern(path) ? new UrlPattern(`${namespace}${path}`, patternOpts) : path;
return (req, res) => {
const { params, query } = getParamsAndQuery(route, req.url);

@@ -25,10 +24,10 @@

exports.router = (...funcs) => (() => {
const findRoute = (funcs, namespace = '') => (() => {
var _ref = _asyncToGenerator(function* (req, res) {
for (const fn of funcs) {
const result = yield fn(req, res);
const result = yield fn(req, res, namespace);
if (result || res.headersSent) return result;
}
send(res, 404, `Cannot ${req.method} ${req.url}`);
return null;
});

@@ -41,4 +40,7 @@

exports.router = (...funcs) => findRoute(funcs);
exports.withNamespace = namespace => (...funcs) => findRoute(funcs, namespace);
METHODS.forEach(method => {
exports[method === 'DELETE' ? 'del' : method.toLowerCase()] = methodFn(method);
});
{
"name": "microrouter",
"description": "🚉 A tiny and functional router for ZEIT's Micro",
"version": "3.0.0",
"version": "3.1.0",
"main": "dist/index.js",

@@ -9,5 +9,7 @@ "jsnext:main": "lib/index.js",

"release": "np",
"build": "rm -rf dist/* && babel lib --ignore *.test.js --out-dir dist --copy-files",
"test": "xo && nyc ava",
"coverage": "nyc report --reporter=text-lcov | coveralls"
"lint": "eslint ./src/**/*.js",
"build": "yarn lint && rm -rf dist/* && babel src/lib --ignore *.test.js --out-dir dist --copy-files",
"test": "nyc ava",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"format": "yarn lint && prettier --write \"src/**/*.js\""
},

@@ -21,27 +23,31 @@ "repository": "git@github.com:pedronauck/micro-router.git",

"devDependencies": {
"ava": "^0.22.0",
"ava": "^0.25.0",
"babel-cli": "^6.24.1",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"coveralls": "^3.0.0",
"micro": "^9.0.0",
"np": "^2.16.0",
"nyc": "^11.2.1",
"eslint": "^4.18.2",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-prettier": "^2.6.0",
"husky": "^0.15.0-rc.8",
"lint-staged": "^7.0.0",
"micro": "^9.1.0",
"np": "^2.20.1",
"nyc": "^11.4.1",
"prettier": "^1.11.1",
"request": "^2.83.0",
"request-promise": "^4.2.2",
"test-listen": "^1.0.2",
"xo": "^0.18.2"
"test-listen": "^1.1.0"
},
"xo": {
"space": 2,
"semicolon": false,
"rules": {
"curly": 0,
"object-curly-spacing": 0,
"unicorn/explicit-length-check": 0,
"no-await-in-loop": 0
}
},
"engines": {
"node": ">=6.10.0"
},
"lint-staged": {
"src/**/*.js": [
"yarn format",
"git add"
]
},
"husky": {
"pre-commit": "lint-staged && npm test"
}
}

@@ -1,2 +0,2 @@

:station: _**Micro Router -**_ A tiny and functional router for ZEIT's [micro](https://github.com/zeit/micro)
:station: _**Micro Router -**_ A tiny and functional router for ZEIT's [micro](https://github.com/zeit/micro)

@@ -6,10 +6,9 @@ [![GitHub release](https://img.shields.io/github/release/pedronauck/micro-router.svg)]()

[![Coveralls](https://img.shields.io/coveralls/pedronauck/micro-router.svg)]()
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/ebdcc3e942b14363a96438b41c770b32)](https://www.codacy.com/app/pedronauck/micro-router?utm_source=github.com&utm_medium=referral&utm_content=pedronauck/micro-router&utm_campaign=Badge_Grade)
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/ebdcc3e942b14363a96438b41c770b32)](https://www.codacy.com/app/pedronauck/micro-router?utm_source=github.com&utm_medium=referral&utm_content=pedronauck/micro-router&utm_campaign=Badge_Grade)
## 👌   Features
- **Tiny**. Just 23 lines of code.
- **Functional**. Write your http methods using functions.
- **Async**. Design to use with `async/await`
* **Tiny**. Just 23 lines of code.
* **Functional**. Write your http methods using functions.
* **Async**. Design to use with `async/await`

@@ -30,12 +29,7 @@ ## 💻   Usage

const hello = (req, res) =>
send(res, 200, `Hello ${req.params.who}`)
const hello = (req, res) => send(res, 200, `Hello ${req.params.who}`)
const notfound = (req, res) =>
send(res, 404, 'Not found route')
const notfound = (req, res) => send(res, 404, 'Not found route')
module.exports = router(
get('/hello/:who', hello),
get('/*', notfound)
)
module.exports = router(get('/hello/:who', hello), get('/*', notfound))
```

@@ -54,5 +48,3 @@

module.exports = router(
get('/hello/:who', hello)
)
module.exports = router(get('/hello/:who', hello))
```

@@ -64,9 +56,9 @@

- `get(path = String, handler = Function)`
- `post(path = String, handler = Function)`
- `put(path = String, handler = Function)`
- `patch(path = String, handler = Function)`
- `del(path = String, handler = Function)`
- `head(path = String, handler = Function)`
- `options(path = String, handler = Function)`
* `get(path = String, handler = Function)`
* `post(path = String, handler = Function)`
* `put(path = String, handler = Function)`
* `patch(path = String, handler = Function)`
* `del(path = String, handler = Function)`
* `head(path = String, handler = Function)`
* `options(path = String, handler = Function)`

@@ -124,3 +116,3 @@ #### path

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 _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:

@@ -147,7 +139,42 @@ ```js

### 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:
```js
const UrlPattern = require('url-pattern')
const { router, get } = require('microrouter')
const routes = router(
get(
new UrlPattern(/^\api/),
() => 'This will match all routes that start with "api"'
)
)
```
### Namespaced Routes
If you want to create nested routes, you can define a namespace for your routes using the `withNamespace` high order function:
```js
const { withNamespace, router, get } = require('microrouter')
const { json, send } = require('micro')
const oldApi = withNamespace('/api/v1')
const newApi = withNamespace('/api/v2')
const routes = router(
oldApi(get('/', () => 'My legacy api route')),
newApi(get('/', () => 'My new api route'))
)
```
_PS: The nested routes doesn't work if you pass a UrlPattern instance as path argument!_
## 🕺   Contribute
1. [Fork](https://help.github.com/articles/fork-a-repo/) this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device
2. Install dependencies using Yarn: `yarn install`
3. Make the necessary changes and ensure that the tests are passing using `yarn test`
4. Send a pull request 🙌
1. [Fork](https://help.github.com/articles/fork-a-repo/) this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device
2. Install dependencies using Yarn: `yarn install`
3. Make the necessary changes and ensure that the tests are passing using `yarn test`
4. Send a pull request 🙌
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc