-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support Request decorators for HTTPController (#159)
- Loading branch information
Showing
19 changed files
with
199 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import undici from 'undici'; | ||
// https://github.com/nodejs/undici/blob/main/index.js#L118 | ||
// 只有 nodejs >= 16 才支持 Request | ||
export class HTTPRequest extends (undici.Request || Object) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import undici from 'undici'; | ||
// https://github.com/nodejs/undici/blob/main/index.js#L118 | ||
// 只有 nodejs >= 16 才支持 Request | ||
export class HTTPResponse extends (undici.Response || Object) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,5 @@ export enum HTTPParamType { | |
QUERIES = 'QUERIES', | ||
BODY = 'BODY', | ||
PARAM = 'PARAM', | ||
REQUEST = 'REQUEST', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { Context } from 'egg'; | ||
import { HTTPRequest as BaseHTTPRequest } from '@eggjs/tegg'; | ||
export class HTTPRequest extends BaseHTTPRequest { | ||
constructor(ctx:Context) { | ||
const request = ctx.request; | ||
// href: https://github.com/eggjs/koa/blob/master/src/request.ts#L90C1-L98C4 | ||
super(request.href, { | ||
method: request.method, | ||
headers: request.headers as Record<string, string | string[]>, | ||
body: (ctx.request as any).rawBody, | ||
}); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
plugin/controller/test/fixtures/apps/http-inject-app/app/controller/AppController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Context as EggContext } from 'egg'; | ||
import { | ||
Context, | ||
HTTPController, | ||
HTTPMethod, | ||
HTTPMethodEnum, | ||
Middleware, | ||
Request, | ||
HTTPRequest, | ||
} from '@eggjs/tegg'; | ||
import { countMw } from '../middleware/count_mw'; | ||
|
||
@HTTPController({ | ||
path: '/apps', | ||
}) | ||
@Middleware(countMw) | ||
export class AppController { | ||
|
||
@HTTPMethod({ | ||
method: HTTPMethodEnum.POST, | ||
path: '/testRequest', | ||
}) | ||
async testRequest(@Context() ctx: EggContext, @Request() request: HTTPRequest) { | ||
const traceId = await ctx.tracer.traceId; | ||
return { | ||
success: true, | ||
traceId, | ||
headers: Object.fromEntries(request.headers), | ||
method: request.method, | ||
requestBody: await request.text(), | ||
}; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
plugin/controller/test/fixtures/apps/http-inject-app/app/middleware/call_module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Context } from 'egg'; | ||
import { Next } from '@eggjs/tegg'; | ||
|
||
export async function callModuleCtx(ctx: Context, next: Next) { | ||
await (ctx.module as any).multiModuleService.appService.findApp('foo'); | ||
await next(); | ||
} |
9 changes: 9 additions & 0 deletions
9
plugin/controller/test/fixtures/apps/http-inject-app/app/middleware/count_mw.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Context } from 'egg'; | ||
import { Next } from '@eggjs/tegg'; | ||
|
||
let index = 0; | ||
|
||
export async function countMw(ctx: Context, next: Next) { | ||
await next(); | ||
if (ctx.body) ctx.body.count = index++; | ||
} |
9 changes: 9 additions & 0 deletions
9
plugin/controller/test/fixtures/apps/http-inject-app/app/middleware/log_mw.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Context } from 'egg'; | ||
import { Next } from '@eggjs/tegg'; | ||
|
||
export function logMwFactory(log: string) { | ||
return async function logMw(ctx: Context, next: Next) { | ||
await next(); | ||
ctx.body.log = log; | ||
}; | ||
} |
13 changes: 13 additions & 0 deletions
13
plugin/controller/test/fixtures/apps/http-inject-app/config/config.default.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
module.exports = function() { | ||
const config = { | ||
keys: 'test key', | ||
security: { | ||
csrf: { | ||
ignoreJSON: false, | ||
} | ||
}, | ||
}; | ||
return config; | ||
}; |
1 change: 1 addition & 0 deletions
1
plugin/controller/test/fixtures/apps/http-inject-app/config/module.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
16 changes: 16 additions & 0 deletions
16
plugin/controller/test/fixtures/apps/http-inject-app/config/plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
exports.tracer = { | ||
package: 'egg-tracer', | ||
enable: true, | ||
}; | ||
|
||
exports.tegg = { | ||
package: '@eggjs/tegg-plugin', | ||
enable: true, | ||
}; | ||
|
||
exports.teggConfig = { | ||
package: '@eggjs/tegg-config', | ||
enable: true, | ||
}; |
3 changes: 3 additions & 0 deletions
3
plugin/controller/test/fixtures/apps/http-inject-app/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "http-inject-app" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,4 +98,5 @@ describe('test/params.test.ts', () => { | |
}); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import mm from 'egg-mock'; | ||
import path from 'path'; | ||
import assert from 'assert'; | ||
|
||
describe('test/params.test.ts', () => { | ||
let app; | ||
|
||
beforeEach(() => { | ||
mm(process.env, 'EGG_TYPESCRIPT', true); | ||
}); | ||
|
||
afterEach(() => { | ||
mm.restore(); | ||
}); | ||
|
||
before(async () => { | ||
mm(process.env, 'EGG_TYPESCRIPT', true); | ||
mm(process, 'cwd', () => { | ||
return path.join(__dirname, '../..'); | ||
}); | ||
app = mm.app({ | ||
baseDir: path.join(__dirname, '../fixtures/apps/http-inject-app'), | ||
framework: require.resolve('egg'), | ||
}); | ||
await app.ready(); | ||
}); | ||
|
||
after(() => { | ||
return app.close(); | ||
}); | ||
const [ nodeMajor ] = process.versions.node.split('.').map(v => Number(v)); | ||
if (nodeMajor >= 16) { | ||
it('Request should work', async () => { | ||
app.mockCsrf(); | ||
const param = { | ||
name: 'foo', | ||
desc: 'mock-desc', | ||
}; | ||
const headerKey = 'test-header'; | ||
await app.httpRequest() | ||
.post('/apps/testRequest') | ||
.send(param) | ||
.set('test', headerKey) | ||
.expect(200) | ||
.expect(res => { | ||
assert(res.body.headers.test === headerKey); | ||
assert(res.body.method === 'POST'); | ||
assert(res.body.requestBody === JSON.stringify(param)); | ||
}); | ||
}); | ||
} | ||
|
||
}); |