koa2-router
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -24,2 +24,3 @@ /*! | ||
var setPrototypeOf = require('setprototypeof') | ||
var delegates = require('delegates') | ||
@@ -48,2 +49,33 @@ /** | ||
/** | ||
* Add baseUrl/params to the | ||
* prototype of request and context in app | ||
* | ||
* We only need to patch once for a better | ||
* performance | ||
* | ||
* @param {*} app | ||
* @private | ||
*/ | ||
function patchPrototype(app) { | ||
const context = app.context | ||
const request = app.request | ||
if (!request.hasOwnProperty('baseUrl')) { | ||
Object.defineProperties(request, { | ||
baseUrl: { | ||
get: function() { return this.req.baseUrl }, | ||
set: function(baseUrl) { this.req.baseUrl = baseUrl } | ||
}, | ||
params: { | ||
get: function() { return this.req.params }, | ||
set: function(params) { this.req.params = params } | ||
} | ||
}) | ||
delegates(context, 'request') | ||
.access('baseUrl') | ||
.access('params') | ||
} | ||
} | ||
/** | ||
* Initialize a new `Router` with the given `options`. | ||
@@ -64,30 +96,9 @@ * | ||
function router(ctx, next) { | ||
// set ctx.originalUrl and ctx.req.originalUrl | ||
if (typeof ctx.originalUrl !== 'string') { | ||
Object.defineProperties(ctx, { | ||
originalUrl: { | ||
get: function() { return ctx.req.originalUrl }, | ||
set: function(originalUrl) { return ctx.req.originalUrl = originalUrl }, | ||
enumerable: true, | ||
configurable: true | ||
}, | ||
baseUrl: { | ||
get: function() { return ctx.req.baseUrl }, | ||
set: function(baseUrl) { return ctx.req.baseUrl = baseUrl }, | ||
enumerable: true, | ||
configurable: true | ||
}, | ||
params: { | ||
get: function() { return ctx.params.params }, | ||
set: function(params) { return ctx.req.params = params }, | ||
enumerable: true, | ||
configurable: true | ||
} | ||
}) | ||
// initialize ctx.req with `originalUrl`, `baseUrl`, `params` | ||
ctx.req.originalUrl = ctx.req.originalUrl || ctx.req.url | ||
ctx.req.baseUrl = ctx.req.baseUrl || '' | ||
ctx.req.params = ctx.req.params || {} | ||
} | ||
// patch context/request prototype | ||
patchPrototype(ctx.app) | ||
// initialize ctx.req with `originalUrl`, `baseUrl`, `params` | ||
const req = ctx.req | ||
req.originalUrl = req.originalUrl || req.url | ||
req.baseUrl = req.baseUrl || '' | ||
req.params = req.params || {} | ||
return router.handle(ctx, next) | ||
@@ -94,0 +105,0 @@ } |
{ | ||
"name": "koa2-router", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "A express-liked router component for koa2", | ||
@@ -21,2 +21,3 @@ "main": "index.js", | ||
"debug": "^4.1.1", | ||
"delegates": "^1.0.0", | ||
"depd": "^2.0.0", | ||
@@ -23,0 +24,0 @@ "methods": "^1.1.2", |
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
27715
877
9
4
+ Addeddelegates@^1.0.0
+ Addeddelegates@1.0.0(transitive)