Skip to content

Commit fc0e4e5

Browse files
committed
feat: add http cookies
1 parent fb6a17c commit fc0e4e5

File tree

9 files changed

+39
-1
lines changed

9 files changed

+39
-1
lines changed

core/controller-decorator/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"dependencies": {
4040
"@eggjs/aop-decorator": "^3.42.0",
4141
"@eggjs/core-decorator": "^3.42.0",
42+
"@eggjs/cookies": "^3.0.1",
4243
"@eggjs/tegg-common-util": "^3.42.0",
4344
"@eggjs/tegg-metadata": "^3.42.0",
4445
"@eggjs/tegg-types": "^3.42.0",

core/controller-decorator/src/decorator/http/HTTPParam.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,13 @@ export function Request() {
7979
HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.REQUEST, parameterIndex, controllerClazz, methodName);
8080
};
8181
}
82+
83+
export function Cookies() {
84+
return function(target: any, propertyKey: PropertyKey, parameterIndex: number) {
85+
assert(typeof propertyKey === 'string',
86+
`[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
87+
const methodName = propertyKey as string;
88+
const controllerClazz = target.constructor as EggProtoImplClass;
89+
HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.COOKIES, parameterIndex, controllerClazz, methodName);
90+
};
91+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { Cookies } from '@eggjs/cookies';
2+
export class HTTPCookies extends (Cookies || Object) {}

core/controller-decorator/src/model/HTTPMethodMeta.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ export class PathParamMeta extends ParamMeta {
7878
}
7979
}
8080

81+
export class CookiesParamMeta extends ParamMeta {
82+
type = HTTPParamType.COOKIES;
83+
84+
validate() {
85+
return;
86+
}
87+
}
88+
89+
8190
export class HTTPMethodMeta implements MethodMeta {
8291
public readonly name: string;
8392
public readonly path: string;
@@ -139,6 +148,9 @@ export class ParamMetaUtil {
139148
case HTTPParamType.REQUEST: {
140149
return new RequestParamMeta();
141150
}
151+
case HTTPParamType.COOKIES: {
152+
return new CookiesParamMeta();
153+
}
142154
default:
143155
assert.fail('never arrive');
144156
}

core/controller-decorator/src/model/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './HTTPMethodMeta';
22
export * from './HTTPControllerMeta';
33
export * from './HTTPRequest';
44
export * from './HTTPResponse';
5+
export * from './HTTPCookies';

core/types/controller-decorator/model/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ export enum HTTPParamType {
4545
PARAM = 'PARAM',
4646
REQUEST = 'REQUEST',
4747
HEADERS = 'HEADERS',
48+
COOKIES = 'COOKIES',
4849
}

plugin/controller/lib/impl/http/HTTPMethodRegister.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
PathParamMeta,
1010
QueriesParamMeta,
1111
QueryParamMeta,
12+
HTTPCookies,
1213
} from '@eggjs/tegg';
1314
import { EggContainerFactory } from '@eggjs/tegg-runtime';
1415
import { EggPrototype } from '@eggjs/tegg-metadata';
@@ -97,6 +98,10 @@ export class HTTPMethodRegister {
9798
args[index] = new HTTPRequest(ctx);
9899
break;
99100
}
101+
case HTTPParamType.COOKIES: {
102+
args[index] = new HTTPCookies(ctx, []);
103+
break;
104+
}
100105
default:
101106
assert.fail('never arrive');
102107
}

plugin/controller/test/fixtures/apps/http-inject-app/app/controller/AppController.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
Middleware,
88
Request,
99
HTTPRequest,
10+
Cookies,
11+
HTTPCookies,
1012
} from '@eggjs/tegg';
1113
import { countMw } from '../middleware/count_mw';
1214

@@ -20,14 +22,16 @@ export class AppController {
2022
method: HTTPMethodEnum.POST,
2123
path: '/testRequest',
2224
})
23-
async testRequest(@Context() ctx: EggContext, @Request() request: HTTPRequest) {
25+
26+
async testRequest(@Context() ctx: EggContext, @Request() request: HTTPRequest, @Cookies() cookies: HTTPCookies) {
2427
const traceId = await ctx.tracer.traceId;
2528
return {
2629
success: true,
2730
traceId,
2831
headers: Object.fromEntries(request.headers),
2932
method: request.method,
3033
requestBody: await request.text(),
34+
cookies: cookies.get('test', { signed: false }),
3135
};
3236
}
3337
}

plugin/controller/test/http/request.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ describe('plugin/controller/test/http/request.test.ts', () => {
4141
.post('/apps/testRequest')
4242
.send(param)
4343
.set('test', headerKey)
44+
.set('cookie', 'test=foo')
4445
.expect(200)
4546
.expect(res => {
4647
assert(res.body.headers.test === headerKey);
4748
assert(res.body.method === 'POST');
4849
assert(res.body.requestBody === JSON.stringify(param));
50+
assert(res.body.cookies === 'foo');
4951
});
5052
});
5153
}

0 commit comments

Comments
 (0)