Skip to content

Commit

Permalink
feat: add http cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
akitaSummer committed Sep 10, 2024
1 parent fb6a17c commit 56d4af5
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/controller-decorator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions core/controller-decorator/src/decorator/http/HTTPParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
2 changes: 2 additions & 0 deletions core/controller-decorator/src/model/HTTPCookies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Cookies } from '@eggjs/cookies';
export class HTTPCookies extends (Cookies || Object) {}
12 changes: 12 additions & 0 deletions core/controller-decorator/src/model/HTTPMethodMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,6 +148,9 @@ export class ParamMetaUtil {
case HTTPParamType.REQUEST: {
return new RequestParamMeta();
}
case HTTPParamType.COOKIES: {
return new CookiesParamMeta();
}
default:
assert.fail('never arrive');
}
Expand Down
1 change: 1 addition & 0 deletions core/controller-decorator/src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './HTTPMethodMeta';
export * from './HTTPControllerMeta';
export * from './HTTPRequest';
export * from './HTTPResponse';
export * from './HTTPCookies';
1 change: 1 addition & 0 deletions core/types/controller-decorator/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ export enum HTTPParamType {
PARAM = 'PARAM',
REQUEST = 'REQUEST',
HEADERS = 'HEADERS',
COOKIES = 'COOKIES',
}
5 changes: 5 additions & 0 deletions plugin/controller/lib/impl/http/HTTPMethodRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PathParamMeta,
QueriesParamMeta,
QueryParamMeta,
HTTPCookies,
} from '@eggjs/tegg';
import { EggContainerFactory } from '@eggjs/tegg-runtime';
import { EggPrototype } from '@eggjs/tegg-metadata';
Expand Down Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
Middleware,
Request,
HTTPRequest,
Cookies,
HTTPCookies,
} from '@eggjs/tegg';
import { countMw } from '../middleware/count_mw';

Expand All @@ -20,14 +22,16 @@ 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,
traceId,
headers: Object.fromEntries(request.headers),
method: request.method,
requestBody: await request.text(),
cookies: cookies.get('test', { signed: false }),
};
}
}
2 changes: 2 additions & 0 deletions plugin/controller/test/http/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
}
Expand Down

0 comments on commit 56d4af5

Please sign in to comment.