Skip to content

Commit

Permalink
fix: update dist
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Mar 5, 2018
1 parent 879a7b3 commit 1917c51
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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');

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');
if (!handler) throw new Error('You need to set a valid handler');

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);

if (params && req.method === method) {
Expand All @@ -21,21 +20,24 @@ const methodFn = method => (path, handler) => {
};
};

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;
});

return function (_x, _x2) {
return _ref.apply(this, arguments);
};
})();

exports.router = (...funcs) => findRoute(funcs);
exports.withNamespace = namespace => (...funcs) => findRoute(funcs, namespace);

METHODS.forEach(method => {
exports[method === 'DELETE' ? 'del' : method.toLowerCase()] = methodFn(method);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"release": "np",
"lint": "eslint ./src/**/*.js",
"build":
"rm -rf dist/* && babel src/lib --ignore *.test.js --out-dir dist --copy-files",
"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\""
Expand Down

0 comments on commit 1917c51

Please sign in to comment.