Skip to content

Commit

Permalink
feat: add request path and ip decorator (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Oct 2, 2020
1 parent 3319872 commit 3354c26
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
8 changes: 8 additions & 0 deletions packages/core/src/util/webRouterParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export const extractKoaLikeValue = (key, data) => {
return ctx.getFileStream && ctx.getFileStream(data);
case RouteParamTypes.FILESSTREAM:
return ctx.multipart && ctx.multipart(data);
case RouteParamTypes.REQUEST_PATH:
return ctx['path'];
case RouteParamTypes.REQUEST_IP:
return ctx['ip'];
default:
return null;
}
Expand Down Expand Up @@ -52,6 +56,10 @@ export const extractExpressLikeValue = (key, data) => {
return req.getFileStream && req.getFileStream(data);
case RouteParamTypes.FILESSTREAM:
return req.multipart && req.multipart(data);
case RouteParamTypes.REQUEST_PATH:
return req['baseUrl'];
case RouteParamTypes.REQUEST_IP:
return req['ip'];
default:
return null;
}
Expand Down
63 changes: 58 additions & 5 deletions packages/core/test/paramMapping.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { expect } from 'chai';
import { Param, Session, Query, Body, Headers, File, Files, WEB_ROUTER_PARAM_KEY, RouteParamTypes } from '@midwayjs/decorator';
import { getPropertyDataFromClass, extractKoaLikeValue } from '../src';
import { Param, Session, Query, Body, Headers, File, Files, WEB_ROUTER_PARAM_KEY, RouteParamTypes, RequestIP, RequestPath } from '@midwayjs/decorator';
import { getPropertyDataFromClass, extractKoaLikeValue, extractExpressLikeValue } from '../src';

class Test {
async doget(@Param('aa')aa: any,
Expand All @@ -10,18 +10,20 @@ class Test {
@Headers('tt') tt: any,
@File({requireFile: true}) f: any,
@Files() files: any,
@Session() bb: any) {
@Session() bb: any,
@RequestIP() requestIp: any,
@RequestPath() requestPath: any) {

}
}

describe('/test/web/paramMapping.test.ts', () => {
it('paramMapping decorator should be ok', () => {
const meta = getPropertyDataFromClass(WEB_ROUTER_PARAM_KEY, Test, 'doget');
expect(meta.length).eq(7);
expect(meta.length).eq(9);
});

it('extractValue shoule be ok', async () => {
it('extract koa value shoule be ok', async () => {
let fn = extractKoaLikeValue(RouteParamTypes.NEXT, {});
expect(await fn({}, 'next')).eq('next');

Expand Down Expand Up @@ -58,5 +60,56 @@ describe('/test/web/paramMapping.test.ts', () => {
}, 'next')).deep.eq({ body : {aaa: 111}, data: 'filesstream'});
fn = extractKoaLikeValue('ttt', 'any');
expect(await fn('', '')).null;

fn = extractKoaLikeValue(RouteParamTypes.REQUEST_PATH, null);
expect(await fn({path: '/base/api/a.html'}, 'next')).deep.eq('/base/api/a.html');

fn = extractKoaLikeValue(RouteParamTypes.REQUEST_IP, null);
expect(await fn({ip: '127.0.0.1'}, 'next')).deep.eq('127.0.0.1');
});

it('extract express value shoule be ok', async () => {
let fn = extractExpressLikeValue(RouteParamTypes.NEXT, {});
expect(await fn({}, {}, 'next')).eq('next');

fn = extractExpressLikeValue(RouteParamTypes.BODY, 'aaa');
expect(await fn({ body : {aaa: 111}}, {}, 'next')).eq(111);
fn = extractExpressLikeValue(RouteParamTypes.BODY, null);
expect(await fn({ body : {aaa: 111}}, {}, 'next')).deep.eq({aaa: 111});
fn = extractExpressLikeValue(RouteParamTypes.PARAM, 'body');
expect(await fn({params: { body : {aaa: 111}}}, {}, 'next')).deep.eq({aaa: 111});
fn = extractExpressLikeValue(RouteParamTypes.PARAM, null);
expect(await fn({params: { body : {aaa: 111}}}, {}, 'next')).deep.eq({ body : {aaa: 111}});
fn = extractExpressLikeValue(RouteParamTypes.QUERY, 'body');
expect(await fn({query: { body : {aaa: 111}}}, {}, 'next')).deep.eq({aaa: 111});
fn = extractExpressLikeValue(RouteParamTypes.QUERY, null);
expect(await fn({query: { body : {aaa: 111}}}, {}, 'next')).deep.eq({ body : {aaa: 111}});
fn = extractExpressLikeValue(RouteParamTypes.HEADERS, 'body');
expect(await fn({headers: { body : {aaa: 111}}}, {}, 'next')).deep.eq({aaa: 111});
fn = extractExpressLikeValue(RouteParamTypes.HEADERS, null);
expect(await fn({headers: { body : {aaa: 111}}}, {}, 'next')).deep.eq({ body : {aaa: 111}});
fn = extractExpressLikeValue(RouteParamTypes.SESSION, null);
expect(await fn({session: { body : {aaa: 111}}}, {}, 'next')).deep.eq({ body : {aaa: 111}});
fn = extractExpressLikeValue(RouteParamTypes.FILESTREAM, 'filestream');
expect(await fn({
getFileStream(data) {
return {body: {aaa: 111}, data};
}
}, {}, 'next')).deep.eq({ body : {aaa: 111}, data: 'filestream'});

fn = extractExpressLikeValue(RouteParamTypes.FILESSTREAM, 'filesstream');
expect(await fn({
multipart(data) {
return {body: {aaa: 111}, data};
}
}, {}, 'next')).deep.eq({ body : {aaa: 111}, data: 'filesstream'});
fn = extractExpressLikeValue('ttt', 'any');
expect(await fn('', {}, '')).null;

fn = extractExpressLikeValue(RouteParamTypes.REQUEST_PATH, null);
expect(await fn({baseUrl: '/base/api/a.html'}, {}, 'next')).deep.eq('/base/api/a.html');

fn = extractExpressLikeValue(RouteParamTypes.REQUEST_IP, null);
expect(await fn({ip: '127.0.0.1'}, {}, 'next')).deep.eq('127.0.0.1');
});
});
6 changes: 6 additions & 0 deletions packages/decorator/src/web/paramMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export enum RouteParamTypes {
FILESTREAM,
FILESSTREAM,
NEXT,
REQUEST_PATH,
REQUEST_IP,
}

export interface RouterParamValue {
Expand Down Expand Up @@ -79,3 +81,7 @@ export const File = (property?: GetFileStreamOptions) =>
createParamMapping(RouteParamTypes.FILESTREAM)(property);
export const Files = (property?: GetFilesStreamOptions) =>
createParamMapping(RouteParamTypes.FILESSTREAM)(property);
export const RequestPath = () =>
createParamMapping(RouteParamTypes.REQUEST_PATH)();
export const RequestIP = () =>
createParamMapping(RouteParamTypes.REQUEST_IP)();

0 comments on commit 3354c26

Please sign in to comment.