Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should bind ctx to egg router controller this context #14

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/EggRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import assert from 'node:assert';
import { encodeURIComponent as safeEncodeURIComponent } from 'utility';
import inflection from 'inflection';
import methods from 'methods';
import { isGeneratorFunction } from 'is-type-of';
import { RegisterOptions, Router, RouterMethod, RouterOptions } from './Router.js';
import { MiddlewareFunc, ResourcesController } from './types.js';
import { MiddlewareFunc, Next, ResourcesController } from './types.js';

interface RestfulOptions {
suffix?: string;
Expand Down Expand Up @@ -146,8 +147,15 @@ export class EggRouter extends Router {
methods: string[],
middleware: MiddlewareFunc | string | (MiddlewareFunc | string | ResourcesController)[],
opts?: RegisterOptions) {
// patch register to support generator function middleware and string controller
// patch register to support bind ctx function middleware and string controller
middleware = Array.isArray(middleware) ? middleware : [ middleware ];
for (const mw of middleware) {
if (isGeneratorFunction(mw)) {
throw new TypeError(
methods.toString() + ' `' + path + '`: Please use async function instead of generator function',
);
}
}
const middlewares = convertMiddlewares(middleware, this.app);
return super.register(path, methods, middlewares, opts);
}
Expand Down Expand Up @@ -326,13 +334,16 @@ function resolveController(controller: string | MiddlewareFunc | ResourcesContro
* - [name, url(regexp), controller]: app.get('regRouter', /\/home\/index/, 'home.index');
* - [name, url, middleware, [...], controller]: `app.get(/user/:id', hasLogin, canGetUser, 'user.show');`
*
* 2. make middleware support generator function
* 2. bind ctx to controller `this`
*
* @param {Array} middlewares middlewares and controller(last middleware)
* @param {Application} app egg application instance
*/
function convertMiddlewares(middlewares: (MiddlewareFunc | string | ResourcesController)[], app: Application) {
// ensure controller is resolved
const controller = resolveController(middlewares.pop()!, app);
return [ ...middlewares as MiddlewareFunc[], controller ];
function wrappedController(ctx: any, next: Next) {
return controller.apply(ctx, [ ctx, next ]);
}
return [ ...middlewares as MiddlewareFunc[], wrappedController ];
}
19 changes: 19 additions & 0 deletions test/EggRouter.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import { strict as assert } from 'node:assert';
import is from 'is-type-of';
import Koa from '@eggjs/koa';
import request from 'supertest';
import { EggRouter } from '../src/index.js';

describe('test/EggRouter.test.ts', () => {
it('auto bind ctx to this on controller', async () => {
const app = new Koa();
const router = new EggRouter({}, app as any);
router.get('home', '/', function(this: any) {
this.body = {
url: this.router.url('home'),
method: this.method,
};
});
app.use(router.routes());
const res = await request(app.callback())
.get('/')
.expect(200);
assert.equal(res.body.url, '/');
assert.equal(res.body.method, 'GET');
});

it('creates new router with egg app', () => {
const app = { controller: {} };
const router = new EggRouter({}, app);
Expand Down
Loading