Skip to content

Commit

Permalink
fix(types): add file/files opt types
Browse files Browse the repository at this point in the history
  • Loading branch information
yviscool committed Jun 28, 2019
1 parent 4ed5443 commit f40b03e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 63 deletions.
1 change: 1 addition & 0 deletions packages/midway-decorator/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const HANDLER_KEY = 'handler';
// web
export const CONTROLLER_KEY = 'controller';
export const WEB_ROUTER_KEY = 'web_router';
export const WEB_ROUTER_PARAM_KEY = 'web_router_param';

// framework
export const CONFIG_KEY = 'config';
Expand Down
86 changes: 52 additions & 34 deletions packages/midway-decorator/src/web/paramMapping.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
import { attachMethodDataToClass } from 'injection';
import { WEB_ROUTER_PARAM_KEY } from '../constant';

interface GetFileStreamOptions {
requireFile?: boolean; // required file submit, default is true
defCharset?: string;
limits?: {
fieldNameSize?: number;
fieldSize?: number;
fields?: number;
fileSize?: number;
files?: number;
parts?: number;
headerPairs?: number;
};
checkFile?(
fieldname: string,
file: any,
filename: string,
encoding: string,
mimetype: string
): void | Error;
}

interface GetFilesStreamOptions extends GetFileStreamOptions {
autoFields?: boolean;
}

export enum RouteParamTypes {
QUERY,
BODY,
PARAM,
CONTEXT,
HEADERS,
SESSION,
FILESTREAM,
FILESSTREAM,
NEXT,
QUERY,
BODY,
PARAM,
HEADERS,
SESSION,
FILESTREAM,
FILESSTREAM,
NEXT,
}

export interface RouterParamValue {
Expand All @@ -19,15 +44,11 @@ export interface RouterParamValue {
extractValue?: (ctx, next) => Promise<any>;
}

export const ROUTE_ARGS_METADATA = '__routeArgsMetadata__';

export const extractValue = function extractValue(key, data) {
return async function(ctx, next) {
return async function (ctx, next) {
switch (key) {
case RouteParamTypes.NEXT:
return next;
case RouteParamTypes.CONTEXT:
return ctx;
case RouteParamTypes.BODY:
return data && ctx.request.body ? ctx.request.body[data] : ctx.request.body;
case RouteParamTypes.PARAM:
Expand All @@ -48,24 +69,21 @@ export const extractValue = function extractValue(key, data) {
};
};

function createParamMapping(type: RouteParamTypes) {
return (data?: any) => {
return (target, key, index) => {
attachMethodDataToClass(ROUTE_ARGS_METADATA, {
index,
type,
data,
extractValue: extractValue(type, data)
}, target, key);
};
};
}
const createParamMapping = function (type: RouteParamTypes) {
return (data?: any) => (target, key, index) => {
attachMethodDataToClass(WEB_ROUTER_PARAM_KEY, {
index,
type,
data,
extractValue: extractValue(type, data)
}, target, key);
};
};

export const ctx = createParamMapping(RouteParamTypes.CONTEXT);
export const body = createParamMapping(RouteParamTypes.BODY);
export const param = createParamMapping(RouteParamTypes.PARAM);
export const query = createParamMapping(RouteParamTypes.QUERY);
export const session = createParamMapping(RouteParamTypes.SESSION);
export const headers = createParamMapping(RouteParamTypes.HEADERS);
export const file = createParamMapping(RouteParamTypes.FILESTREAM);
export const files = createParamMapping(RouteParamTypes.FILESSTREAM);
export const session = () => createParamMapping(RouteParamTypes.SESSION)();
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);
4 changes: 2 additions & 2 deletions packages/midway-web/src/loader/webLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
PRIORITY_KEY,
RouterOption,
WEB_ROUTER_KEY,
ROUTE_ARGS_METADATA,
WEB_ROUTER_PARAM_KEY,
RouterParamValue
} from '@midwayjs/decorator';
import * as extend from 'extend2';
Expand Down Expand Up @@ -213,7 +213,7 @@ export class MidwayWebLoader extends EggLoader {
});

// implement @body @query @param @body
const routeArgsInfo = getMethodDataFromClass(ROUTE_ARGS_METADATA, target, webRouter.method) || [];
const routeArgsInfo = getMethodDataFromClass(WEB_ROUTER_PARAM_KEY, target, webRouter.method) || [];

const routerArgs = [
webRouter.routerName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { provide } from 'injection';
import { controller, config, get, post, query, param, ctx, files, file, session, body, headers } from '../../../../../../../src';
import { provide, inject } from 'injection';
import { controller, config, get, post, query, param, files, file, session, body, headers } from '../../../../../../../src';
import * as path from 'path';
import * as fs from 'fs';

Expand All @@ -12,57 +12,60 @@ export class ParamController {
@config('baseDir')
baseDir: string;

@inject()
ctx: any;

@get('/query')
async query(@query() query, @ctx() ctx) {
ctx.body = query;
async query(@query() query) {
this.ctx.body = query;
}

@get('/:id/test')
async test(@query() query, @ctx() ctx, @param('id') id) {
const data = {
async test(@query() query, @param('id') id) {
const data = {
id,
...query
};
ctx.body = data;
this.ctx.body = data;
}

@get('/query_id')
async queryId(@query('id') id, @ctx() ctx) {
ctx.body = id;
async queryId(@query('id') id) {
this.ctx.body = id;
}

@get('/param/:id/test/:userId')
async param(@param() param, @ctx() ctx) {
async param(@param() param) {
// service,hello,a,b
ctx.body = param;
this.ctx.body = param;
}

@get('/param/:id')
async paramId(@param('id') id, @ctx() ctx) {
ctx.body = id;
async paramId(@param('id') id) {
this.ctx.body = id;
}

@post('/body')
async body(@body() body, @ctx() ctx) {
ctx.body = body;
async body(@body() body) {
this.ctx.body = body;
}

@get('/body_id')
async bodyId(@body('id') id, @ctx() ctx) {
ctx.body = id;
async bodyId(@body('id') id) {
this.ctx.body = id;
}

@post('/file')
async file(@file() stream, @ctx() ctx) {
async file(@file() stream) {
const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
const target = path.join(this.baseDir, 'app/public', filename);
const writeStream = fs.createWriteStream(target);
await pump(stream, writeStream);
ctx.body = 'ok';
this.ctx.body = 'ok';
}

@post('/files')
async files(@files({ autoFields: true }) parts, @ctx() ctx) {
async files(@files({ autoFields: true }) parts) {

let stream = await parts();

Expand All @@ -74,25 +77,25 @@ export class ParamController {
stream = await parts();
}

ctx.body = 'ok';
this.ctx.body = 'ok';
}

@get('/session')
async session(@session() session, @ctx() ctx) {
async session(@session() session) {
// service,hello,a,b
ctx.body = session;
this.ctx.body = session;
}

@get('/headers')
async header(@headers() headers, @ctx() ctx) {
async header(@headers() headers) {
// service,hello,a,b
ctx.body = headers.host.substring(0, 3);
this.ctx.body = headers.host.substring(0, 3);
}

@get('/headers_host')
async headerHost(@headers('host') host, @ctx() ctx) {
async headerHost(@headers('host') host) {
// service,hello,a,b
ctx.body = host.substring(0, 3);
this.ctx.body = host.substring(0, 3);
}

}

0 comments on commit f40b03e

Please sign in to comment.