-
Notifications
You must be signed in to change notification settings - Fork 93
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: extend support Class #276
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,15 @@ | ||||||||||||||||
import { MiddlewareFunc } from '../../src/index.js'; | ||||||||||||||||
|
||||||||||||||||
export const hello: MiddlewareFunc = async (ctx, next) => { | ||||||||||||||||
console.log('Hello middleware'); | ||||||||||||||||
console.log(ctx.app.BaseContextClass); | ||||||||||||||||
console.log(ctx.app.Service); | ||||||||||||||||
console.log(ctx.service); | ||||||||||||||||
console.log(ctx.app.timing); | ||||||||||||||||
console.log(ctx.app.lifecycle); | ||||||||||||||||
console.log(ctx.request.ctx.app.timing); | ||||||||||||||||
console.log(ctx.request.app.timing); | ||||||||||||||||
console.log(ctx.request.response.app.timing); | ||||||||||||||||
console.log(ctx.response.request.app.timing); | ||||||||||||||||
await next(); | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling around next() call The middleware should handle potential errors from downstream middleware. - await next();
+ try {
+ await next();
+ } catch (err) {
+ ctx.logger.error('Error in hello middleware chain', err);
+ throw err;
+ } 📝 Committable suggestion
Suggested change
|
||||||||||||||||
}; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,13 @@ | ||||||||||||||||||||||||||
export default async function() { | ||||||||||||||||||||||||||
const status = Number(this.query.status || 200); | ||||||||||||||||||||||||||
this.status = status; | ||||||||||||||||||||||||||
this.etag = '2.2.2.2'; | ||||||||||||||||||||||||||
Comment on lines
+1
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add input validation and error handling The status code parsing lacks validation and error handling. Add proper validation: export default async function() {
- const status = Number(this.query.status || 200);
+ const rawStatus = this.query.status;
+ const status = rawStatus ? parseInt(rawStatus, 10) : 200;
+ if (isNaN(status) || status < 100 || status > 599) {
+ this.throw(400, `Invalid status code: ${rawStatus}`);
+ }
this.status = status;
this.etag = '2.2.2.2'; 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
this.body = { | ||||||||||||||||||||||||||
returnAppContext: this.appContext, | ||||||||||||||||||||||||||
returnAppRequest: this.request.appRequest, | ||||||||||||||||||||||||||
returnAppResponse: this.response.appResponse, | ||||||||||||||||||||||||||
returnAppApplication: this.app.appApplication, | ||||||||||||||||||||||||||
status: this.status, | ||||||||||||||||||||||||||
etag: this.etag, | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { EggCore } from '../../../../../src/index.js' | ||
|
||
export default class Application extends EggCore { | ||
get appApplication() { | ||
return 'app application'; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Context } from '../../../../../src/index.js' | ||
|
||
export default class MyContext extends Context { | ||
get appContext() { | ||
return this.app ? 'app context' : 'no app context'; | ||
} | ||
|
||
ajax() { | ||
return 'app ajax patch'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Request } from '../../../../../src/index.js' | ||
|
||
export default class AppRequest extends Request { | ||
get appRequest() { | ||
return this.response.app.timing ? 'app request' : 'no app request'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,17 @@ | ||||||||||||||||||||||||||||
import { Response } from '../../../../../src/index.js' | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
export default class AppResponse extends Response { | ||||||||||||||||||||||||||||
get appResponse() { | ||||||||||||||||||||||||||||
return this.app.timing ? 'app response' : 'no app response'; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
set status(code) { | ||||||||||||||||||||||||||||
this._explicitStatus = true; | ||||||||||||||||||||||||||||
this.res.statusCode = code; | ||||||||||||||||||||||||||||
this.res.statusMessage = 'http status code ' + code; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+8
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add status code validation The status setter directly modifies response properties without validating the status code range. Add validation to ensure the status code is within valid HTTP range (100-599): set status(code) {
+ if (code < 100 || code > 599) {
+ throw new Error(`Invalid status code: ${code}`);
+ }
this._explicitStatus = true;
this.res.statusCode = code;
this.res.statusMessage = 'http status code ' + code;
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
get etag() { | ||||||||||||||||||||||||||||
return 'etag ok'; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default (app) => { | ||
app.get('/', app.controller.home); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "extend-with-class", | ||
"type": "module" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import request from 'supertest'; | ||
import mm from 'mm'; | ||
import { Application, createApp } from '../../helper.js'; | ||
|
||
describe('test/loader/mixin/load_extend_class.test.ts', () => { | ||
let app: Application; | ||
before(async () => { | ||
app = createApp('extend-with-class'); | ||
await app.loader.loadPlugin(); | ||
await app.loader.loadConfig(); | ||
await app.loader.loadRequestExtend(); | ||
await app.loader.loadResponseExtend(); | ||
await app.loader.loadApplicationExtend(); | ||
await app.loader.loadContextExtend(); | ||
await app.loader.loadController(); | ||
await app.loader.loadRouter(); | ||
await app.loader.loadMiddleware(); | ||
}); | ||
after(() => app.close()); | ||
afterEach(mm.restore); | ||
|
||
it('should load app.context app.request app.response', () => { | ||
assert((app as any).appApplication); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid type assertion with Using type assertions reduces type safety. - assert((app as any).appApplication);
+ interface ExtendedApp extends Application {
+ appApplication: unknown;
+ }
+ assert((app as ExtendedApp).appApplication);
|
||
|
||
return request(app.callback()) | ||
.get('/') | ||
.expect({ | ||
returnAppContext: 'app context', | ||
returnAppRequest: 'app request', | ||
returnAppResponse: 'app response', | ||
returnAppApplication: 'app application', | ||
status: 200, | ||
etag: 'etag ok', | ||
}) | ||
.expect(200); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove or replace console.log statements with proper logging
Multiple console.log statements appear to be for debugging purposes. This could impact performance in production and pollute logs.
Consider: