];
this.logger = logger;
+
+ // Self reference added to each ctx.
+ const self = this;
+ this.selfProperty = {
+ get() {
+ return self;
+ },
+ configurable: false,
+ enumerable: false,
+ };
}
async dispatch(req, res, ctx = {}) {
const { handler, handlerArgs } = this._determineHandler(req, res, ctx);
try {
+ Object.defineProperty(ctx, 'dingus', this.selfProperty);
await this.preHandler(req, res, ctx);
return await handler(req, res, ctx, ...handlerArgs);
} catch (e) {
assert(!dingus.handlerMethodNotAllowed.called);
assert(!dingus.handlerNotFound.called);
});
+ it('references dingus in ctx', async function () {
+ let result;
+ const urlPath = '/:id';
+ const method = 'GET';
+ const stubHandler = async (req, res, ctx) => {
+ result = ctx.dingus;
+ };
+ dingus.on(method, urlPath, stubHandler);
+ req.url = '/abc';
+ req.method = method;
+
+ await dingus.dispatch(req, res);
+ assert(!dingus.handlerMethodNotAllowed.called);
+ assert(!dingus.handlerNotFound.called);
+ assert.strictEqual(result, dingus);
+ });
it('calls fallback handler', async function () {
const urlPath = '/abc/:id';
const method = 'GET';