Skip to content

Commit

Permalink
feat: add property for web params
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Sep 15, 2020
1 parent b90c020 commit 5c19644
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 9 additions & 3 deletions packages/core/src/util/webRouterParam.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { RouteParamTypes } from "@midwayjs/decorator";
import { RouteParamTypes, ALL_VALUE } from "@midwayjs/decorator";

export const extractKoaLikeValue = (key, data) => {
if(ALL_VALUE === data) {
data = undefined;
}
return async function (ctx, next) {
switch (key) {
case RouteParamTypes.NEXT:
Expand All @@ -14,7 +17,7 @@ export const extractKoaLikeValue = (key, data) => {
case RouteParamTypes.HEADERS:
return data ? ctx.headers[data] : ctx.headers;
case RouteParamTypes.SESSION:
return ctx.session;
return data ? ctx.session[data] : ctx.session;
case RouteParamTypes.FILESTREAM:
return ctx.getFileStream && ctx.getFileStream(data);
case RouteParamTypes.FILESSTREAM:
Expand All @@ -26,6 +29,9 @@ export const extractKoaLikeValue = (key, data) => {
};

export const extractExpressLikeValue = (key, data) => {
if(ALL_VALUE === data) {
data = undefined;
}
return async function (req, res, next) {
switch (key) {
case RouteParamTypes.NEXT:
Expand All @@ -39,7 +45,7 @@ export const extractExpressLikeValue = (key, data) => {
case RouteParamTypes.HEADERS:
return data ? req.headers[data] : req.headers;
case RouteParamTypes.SESSION:
return req.session;
return data ? req.session[data] : req.session;
case RouteParamTypes.FILESTREAM:
return req.getFileStream && req.getFileStream(data);
case RouteParamTypes.FILESSTREAM:
Expand Down
10 changes: 7 additions & 3 deletions packages/decorator/src/web/paramMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface GetFileStreamOptions {
parts?: number;
headerPairs?: number;
};

checkFile?(
fieldname: string,
file: any,
Expand Down Expand Up @@ -44,6 +45,9 @@ export interface RouterParamValue {

const createParamMapping = function (type: RouteParamTypes) {
return (propertyData?: any) => (target, propertyName, index) => {
if (propertyData === undefined) {
propertyData = propertyName;
}
attachPropertyDataToClass(WEB_ROUTER_PARAM_KEY, {
index,
type,
Expand All @@ -52,10 +56,10 @@ const createParamMapping = function (type: RouteParamTypes) {
};
};

export const Session = () => createParamMapping(RouteParamTypes.SESSION)();
export const Session = (property?: string) => createParamMapping(RouteParamTypes.SESSION)(property);
export const Body = (property?: string) => createParamMapping(RouteParamTypes.BODY)(property);
export const Query = (property?: string) => createParamMapping(RouteParamTypes.QUERY)(property);
export const Param = (property?: string) => createParamMapping(RouteParamTypes.PARAM)(property);
export const Headers = (property?: string) => createParamMapping(RouteParamTypes.HEADERS)(property);
export const File = (property?: GetFileStreamOptions) => createParamMapping(RouteParamTypes.FILESTREAM)(property);
export const Files = (property?: GetFilesStreamOptions) => createParamMapping(RouteParamTypes.FILESSTREAM)(property);
export const File = (property: GetFileStreamOptions) => createParamMapping(RouteParamTypes.FILESTREAM)(property);
export const Files = (property: GetFilesStreamOptions) => createParamMapping(RouteParamTypes.FILESSTREAM)(property);

0 comments on commit 5c19644

Please sign in to comment.