@craigcollie/universal-route-middleware
Advanced tools
Comparing version 0.0.10 to 0.0.13
127
lib/index.js
@@ -6,2 +6,25 @@ 'use strict'; | ||
}); | ||
var _regenerator = require('babel-runtime/regenerator'); | ||
var _regenerator2 = _interopRequireDefault(_regenerator); | ||
var _promise = require('babel-runtime/core-js/promise'); | ||
var _promise2 = _interopRequireDefault(_promise); | ||
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); | ||
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); | ||
var _stringify = require('babel-runtime/core-js/json/stringify'); | ||
var _stringify2 = _interopRequireDefault(_stringify); | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var normalizePathname = function normalizePathname(url) { | ||
@@ -11,21 +34,97 @@ return url.split('?')[0]; | ||
var isPromise = function isPromise(fn) { | ||
return typeof fn.then === 'function'; | ||
}; | ||
var parseMetaData = function parseMetaData(html, meta) { | ||
if (meta == null) { | ||
return html; | ||
} | ||
var htmlStr = html; | ||
(0, _keys2.default)(meta).forEach(function (key) { | ||
if (meta && meta[key]) { | ||
htmlStr = htmlStr.replace('%' + key + '%', meta[key]); | ||
} | ||
}); | ||
return htmlStr; | ||
}; | ||
var parseInitialProps = function parseInitialProps(html, props) { | ||
if (props == null) { | ||
return html; | ||
} | ||
var htmlStr = html; | ||
var serverProps = (0, _stringify2.default)(props); | ||
htmlStr = htmlStr.replace('%serverProps%', serverProps); | ||
return htmlStr; | ||
}; | ||
var matchRoute = function matchRoute(routes, pathname) { | ||
var matchedRoutes = routes.filter(function (route) { | ||
return route.path === pathname; | ||
}); | ||
var notFoundRoute = routes.filter(function (route) { | ||
return !route.path; | ||
})[0]; | ||
switch (matchedRoutes.length) { | ||
case 0: | ||
return { activeRoute: notFoundRoute, status: 404 }; | ||
default: | ||
return { activeRoute: matchedRoutes[0], status: 200 }; | ||
} | ||
}; | ||
exports.default = function (app, config) { | ||
return function (req, res, next) { | ||
var routes = config.routes; | ||
var originalUrl = req.originalUrl; | ||
return function () { | ||
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(req, res, next) { | ||
var routes, originalUrl, _matchRoute, activeRoute, status, meta, resolve, _html, _props; | ||
var normalizedPathname = normalizePathname(originalUrl); | ||
return _regenerator2.default.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
routes = config.routes; | ||
originalUrl = req.originalUrl; | ||
_matchRoute = matchRoute(routes, normalizePathname(originalUrl)), activeRoute = _matchRoute.activeRoute, status = _matchRoute.status; | ||
meta = activeRoute.meta, resolve = activeRoute.resolve; | ||
_html = String(app.locals.html); | ||
var activeRoute = routes.filter(function (route) { | ||
return route.path === normalizedPathname; | ||
}); | ||
if (activeRoute.length > 0) { | ||
res.status(200); | ||
} else { | ||
res.status(404); | ||
} | ||
if (meta) { | ||
_html = parseMetaData(_html, meta); | ||
} | ||
next(); | ||
}; | ||
if (!(resolve && isPromise(resolve))) { | ||
_context.next = 11; | ||
break; | ||
} | ||
_context.next = 9; | ||
return _promise2.default.resolve(resolve); | ||
case 9: | ||
_props = _context.sent; | ||
_html = parseInitialProps(_html, _props); | ||
case 11: | ||
app.locals.html = _html; | ||
res.status(status); | ||
next(); | ||
case 14: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, undefined); | ||
})); | ||
return function (_x, _x2, _x3) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
}; |
{ | ||
"name": "@craigcollie/universal-route-middleware", | ||
"version": "0.0.10", | ||
"version": "0.0.13", | ||
"main": "lib/index.js", | ||
@@ -15,2 +15,3 @@ "license": "MIT", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-flow": "^6.23.0", | ||
"chai": "^4.1.2", | ||
@@ -22,2 +23,4 @@ "chai-http": "^3.0.0", | ||
"eslint-plugin-jsx-a11y": "^6.0.3", | ||
"express": "^4.16.2", | ||
"flow-bin": "^0.63.1", | ||
"mocha": "^4.1.0", | ||
@@ -24,0 +27,0 @@ "sinon": "^4.1.4" |
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
4553
97
16