Skip to content

Commit

Permalink
first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
jonchurch committed Apr 8, 2020
1 parent 74e682a commit f44b331
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Layer = require('express/lib/router/layer');
const Router = require('express/lib/router');
const { version: expressVersion } = require('express/package.json');
const express = require('express');

const last = (arr = []) => arr[arr.length - 1];
const noop = Function.prototype;
Expand All @@ -10,7 +10,6 @@ function copyFnProps(oldFn, newFn) {
});
return newFn;
}

function wrap(fn) {
const newFn = function newFn(...args) {
const ret = fn.apply(this, args);
Expand All @@ -24,24 +23,32 @@ function wrap(fn) {
});
return copyFnProps(fn, newFn);
}

function patchRouterParam() {
const originalParam = Router.prototype.constructor.param;
Router.prototype.constructor.param = function param(name, fn) {
const originalParam = express.Router.prototype.constructor.param;
express.Router.prototype.constructor.param = function param(name, fn) {
fn = wrap(fn);
return originalParam.call(this, name, fn);
};
}

Object.defineProperty(Layer.prototype, 'handle', {
enumerable: true,
get() {
return this.__handle;
},
set(fn) {
fn = wrap(fn);
this.__handle = fn;
},
});

patchRouterParam();

if (expressVersion[0] > 4) {
// eslint-disable-next-line no-console
console.warn(`deprecated: Package express-async-errors works with version 4.x.x of Express, you are using ${expressVersion} which supports async route handlers natively`);
} else {
// setup module
// eslint-disable-next-line global-require
const Layer = require('express/lib/router/layer');

Object.defineProperty(Layer.prototype, 'handle', {
enumerable: true,
get() {
return this.__handle;
},
set(fn) {
fn = wrap(fn);
this.__handle = fn;
},
});
}

0 comments on commit f44b331

Please sign in to comment.