diff --git a/core/controller-decorator/package.json b/core/controller-decorator/package.json index 2083f085..678a2868 100644 --- a/core/controller-decorator/package.json +++ b/core/controller-decorator/package.json @@ -39,6 +39,7 @@ "dependencies": { "@eggjs/aop-decorator": "^3.42.0", "@eggjs/core-decorator": "^3.42.0", + "@eggjs/cookies": "^3.0.1", "@eggjs/tegg-common-util": "^3.42.0", "@eggjs/tegg-metadata": "^3.42.0", "@eggjs/tegg-types": "^3.42.0", diff --git a/core/controller-decorator/src/decorator/http/HTTPParam.ts b/core/controller-decorator/src/decorator/http/HTTPParam.ts index f3c14d50..4161decf 100644 --- a/core/controller-decorator/src/decorator/http/HTTPParam.ts +++ b/core/controller-decorator/src/decorator/http/HTTPParam.ts @@ -79,3 +79,13 @@ export function Request() { HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.REQUEST, parameterIndex, controllerClazz, methodName); }; } + +export function Cookies() { + return function(target: any, propertyKey: PropertyKey, parameterIndex: number) { + assert(typeof propertyKey === 'string', + `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`); + const methodName = propertyKey as string; + const controllerClazz = target.constructor as EggProtoImplClass; + HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.COOKIES, parameterIndex, controllerClazz, methodName); + }; +} diff --git a/core/controller-decorator/src/model/HTTPCookies.ts b/core/controller-decorator/src/model/HTTPCookies.ts new file mode 100644 index 00000000..38f59b28 --- /dev/null +++ b/core/controller-decorator/src/model/HTTPCookies.ts @@ -0,0 +1,2 @@ +import { Cookies } from '@eggjs/cookies'; +export class HTTPCookies extends Cookies {} diff --git a/core/controller-decorator/src/model/HTTPMethodMeta.ts b/core/controller-decorator/src/model/HTTPMethodMeta.ts index 9b6703a4..4642f828 100644 --- a/core/controller-decorator/src/model/HTTPMethodMeta.ts +++ b/core/controller-decorator/src/model/HTTPMethodMeta.ts @@ -78,6 +78,15 @@ export class PathParamMeta extends ParamMeta { } } +export class CookiesParamMeta extends ParamMeta { + type = HTTPParamType.COOKIES; + + validate() { + return; + } +} + + export class HTTPMethodMeta implements MethodMeta { public readonly name: string; public readonly path: string; @@ -139,6 +148,9 @@ export class ParamMetaUtil { case HTTPParamType.REQUEST: { return new RequestParamMeta(); } + case HTTPParamType.COOKIES: { + return new CookiesParamMeta(); + } default: assert.fail('never arrive'); } diff --git a/core/controller-decorator/src/model/index.ts b/core/controller-decorator/src/model/index.ts index b035fa43..1e1a2d0d 100644 --- a/core/controller-decorator/src/model/index.ts +++ b/core/controller-decorator/src/model/index.ts @@ -2,3 +2,4 @@ export * from './HTTPMethodMeta'; export * from './HTTPControllerMeta'; export * from './HTTPRequest'; export * from './HTTPResponse'; +export * from './HTTPCookies'; diff --git a/core/types/controller-decorator/model/types.ts b/core/types/controller-decorator/model/types.ts index 3e173649..dc138400 100644 --- a/core/types/controller-decorator/model/types.ts +++ b/core/types/controller-decorator/model/types.ts @@ -45,4 +45,5 @@ export enum HTTPParamType { PARAM = 'PARAM', REQUEST = 'REQUEST', HEADERS = 'HEADERS', + COOKIES = 'COOKIES', } diff --git a/plugin/controller/lib/impl/http/HTTPMethodRegister.ts b/plugin/controller/lib/impl/http/HTTPMethodRegister.ts index ad88e1fa..8450a6d8 100644 --- a/plugin/controller/lib/impl/http/HTTPMethodRegister.ts +++ b/plugin/controller/lib/impl/http/HTTPMethodRegister.ts @@ -9,6 +9,7 @@ import { PathParamMeta, QueriesParamMeta, QueryParamMeta, + HTTPCookies, } from '@eggjs/tegg'; import { EggContainerFactory } from '@eggjs/tegg-runtime'; import { EggPrototype } from '@eggjs/tegg-metadata'; @@ -97,6 +98,10 @@ export class HTTPMethodRegister { args[index] = new HTTPRequest(ctx); break; } + case HTTPParamType.COOKIES: { + args[index] = new HTTPCookies(ctx, []); + break; + } default: assert.fail('never arrive'); } diff --git a/plugin/controller/test/fixtures/apps/http-inject-app/app/controller/AppController.ts b/plugin/controller/test/fixtures/apps/http-inject-app/app/controller/AppController.ts index 93d04330..81ae65dc 100644 --- a/plugin/controller/test/fixtures/apps/http-inject-app/app/controller/AppController.ts +++ b/plugin/controller/test/fixtures/apps/http-inject-app/app/controller/AppController.ts @@ -7,6 +7,8 @@ import { Middleware, Request, HTTPRequest, + Cookies, + HTTPCookies, } from '@eggjs/tegg'; import { countMw } from '../middleware/count_mw'; @@ -20,7 +22,8 @@ export class AppController { method: HTTPMethodEnum.POST, path: '/testRequest', }) - async testRequest(@Context() ctx: EggContext, @Request() request: HTTPRequest) { + + async testRequest(@Context() ctx: EggContext, @Request() request: HTTPRequest, @Cookies() cookies: HTTPCookies) { const traceId = await ctx.tracer.traceId; return { success: true, @@ -28,6 +31,7 @@ export class AppController { headers: Object.fromEntries(request.headers), method: request.method, requestBody: await request.text(), + cookies: cookies.get('test', { signed: false }), }; } } diff --git a/plugin/controller/test/http/request.test.ts b/plugin/controller/test/http/request.test.ts index 6077a581..16bd6640 100644 --- a/plugin/controller/test/http/request.test.ts +++ b/plugin/controller/test/http/request.test.ts @@ -41,11 +41,13 @@ describe('plugin/controller/test/http/request.test.ts', () => { .post('/apps/testRequest') .send(param) .set('test', headerKey) + .set('cookie', 'test=foo') .expect(200) .expect(res => { assert(res.body.headers.test === headerKey); assert(res.body.method === 'POST'); assert(res.body.requestBody === JSON.stringify(param)); + assert(res.body.cookies === 'foo'); }); }); }