Skip to content

Commit f40b03e

Browse files
committed
fix(types): add file/files opt types
1 parent 4ed5443 commit f40b03e

File tree

4 files changed

+85
-63
lines changed

4 files changed

+85
-63
lines changed

packages/midway-decorator/src/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const HANDLER_KEY = 'handler';
99
// web
1010
export const CONTROLLER_KEY = 'controller';
1111
export const WEB_ROUTER_KEY = 'web_router';
12+
export const WEB_ROUTER_PARAM_KEY = 'web_router_param';
1213

1314
// framework
1415
export const CONFIG_KEY = 'config';
Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
import { attachMethodDataToClass } from 'injection';
2+
import { WEB_ROUTER_PARAM_KEY } from '../constant';
3+
4+
interface GetFileStreamOptions {
5+
requireFile?: boolean; // required file submit, default is true
6+
defCharset?: string;
7+
limits?: {
8+
fieldNameSize?: number;
9+
fieldSize?: number;
10+
fields?: number;
11+
fileSize?: number;
12+
files?: number;
13+
parts?: number;
14+
headerPairs?: number;
15+
};
16+
checkFile?(
17+
fieldname: string,
18+
file: any,
19+
filename: string,
20+
encoding: string,
21+
mimetype: string
22+
): void | Error;
23+
}
24+
25+
interface GetFilesStreamOptions extends GetFileStreamOptions {
26+
autoFields?: boolean;
27+
}
228

329
export enum RouteParamTypes {
4-
QUERY,
5-
BODY,
6-
PARAM,
7-
CONTEXT,
8-
HEADERS,
9-
SESSION,
10-
FILESTREAM,
11-
FILESSTREAM,
12-
NEXT,
30+
QUERY,
31+
BODY,
32+
PARAM,
33+
HEADERS,
34+
SESSION,
35+
FILESTREAM,
36+
FILESSTREAM,
37+
NEXT,
1338
}
1439

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

22-
export const ROUTE_ARGS_METADATA = '__routeArgsMetadata__';
23-
2447
export const extractValue = function extractValue(key, data) {
25-
return async function(ctx, next) {
48+
return async function (ctx, next) {
2649
switch (key) {
2750
case RouteParamTypes.NEXT:
2851
return next;
29-
case RouteParamTypes.CONTEXT:
30-
return ctx;
3152
case RouteParamTypes.BODY:
3253
return data && ctx.request.body ? ctx.request.body[data] : ctx.request.body;
3354
case RouteParamTypes.PARAM:
@@ -48,24 +69,21 @@ export const extractValue = function extractValue(key, data) {
4869
};
4970
};
5071

51-
function createParamMapping(type: RouteParamTypes) {
52-
return (data?: any) => {
53-
return (target, key, index) => {
54-
attachMethodDataToClass(ROUTE_ARGS_METADATA, {
55-
index,
56-
type,
57-
data,
58-
extractValue: extractValue(type, data)
59-
}, target, key);
60-
};
61-
};
62-
}
72+
const createParamMapping = function (type: RouteParamTypes) {
73+
return (data?: any) => (target, key, index) => {
74+
attachMethodDataToClass(WEB_ROUTER_PARAM_KEY, {
75+
index,
76+
type,
77+
data,
78+
extractValue: extractValue(type, data)
79+
}, target, key);
80+
};
81+
};
6382

64-
export const ctx = createParamMapping(RouteParamTypes.CONTEXT);
65-
export const body = createParamMapping(RouteParamTypes.BODY);
66-
export const param = createParamMapping(RouteParamTypes.PARAM);
67-
export const query = createParamMapping(RouteParamTypes.QUERY);
68-
export const session = createParamMapping(RouteParamTypes.SESSION);
69-
export const headers = createParamMapping(RouteParamTypes.HEADERS);
70-
export const file = createParamMapping(RouteParamTypes.FILESTREAM);
71-
export const files = createParamMapping(RouteParamTypes.FILESSTREAM);
83+
export const session = () => createParamMapping(RouteParamTypes.SESSION)();
84+
export const body = (property?: string) => createParamMapping(RouteParamTypes.BODY)(property);
85+
export const query = (property?: string) => createParamMapping(RouteParamTypes.QUERY)(property);
86+
export const param = (property?: string) => createParamMapping(RouteParamTypes.PARAM)(property);
87+
export const headers = (property?: string) => createParamMapping(RouteParamTypes.HEADERS)(property);
88+
export const file = (property?: GetFileStreamOptions) => createParamMapping(RouteParamTypes.FILESTREAM)(property);
89+
export const files = (property?: GetFilesStreamOptions) => createParamMapping(RouteParamTypes.FILESSTREAM)(property);

packages/midway-web/src/loader/webLoader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
PRIORITY_KEY,
77
RouterOption,
88
WEB_ROUTER_KEY,
9-
ROUTE_ARGS_METADATA,
9+
WEB_ROUTER_PARAM_KEY,
1010
RouterParamValue
1111
} from '@midwayjs/decorator';
1212
import * as extend from 'extend2';
@@ -213,7 +213,7 @@ export class MidwayWebLoader extends EggLoader {
213213
});
214214

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

218218
const routerArgs = [
219219
webRouter.routerName,
Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { provide } from 'injection';
2-
import { controller, config, get, post, query, param, ctx, files, file, session, body, headers } from '../../../../../../../src';
1+
import { provide, inject } from 'injection';
2+
import { controller, config, get, post, query, param, files, file, session, body, headers } from '../../../../../../../src';
33
import * as path from 'path';
44
import * as fs from 'fs';
55

@@ -12,57 +12,60 @@ export class ParamController {
1212
@config('baseDir')
1313
baseDir: string;
1414

15+
@inject()
16+
ctx: any;
17+
1518
@get('/query')
16-
async query(@query() query, @ctx() ctx) {
17-
ctx.body = query;
19+
async query(@query() query) {
20+
this.ctx.body = query;
1821
}
1922

2023
@get('/:id/test')
21-
async test(@query() query, @ctx() ctx, @param('id') id) {
22-
const data = {
24+
async test(@query() query, @param('id') id) {
25+
const data = {
2326
id,
2427
...query
2528
};
26-
ctx.body = data;
29+
this.ctx.body = data;
2730
}
2831

2932
@get('/query_id')
30-
async queryId(@query('id') id, @ctx() ctx) {
31-
ctx.body = id;
33+
async queryId(@query('id') id) {
34+
this.ctx.body = id;
3235
}
3336

3437
@get('/param/:id/test/:userId')
35-
async param(@param() param, @ctx() ctx) {
38+
async param(@param() param) {
3639
// service,hello,a,b
37-
ctx.body = param;
40+
this.ctx.body = param;
3841
}
3942

4043
@get('/param/:id')
41-
async paramId(@param('id') id, @ctx() ctx) {
42-
ctx.body = id;
44+
async paramId(@param('id') id) {
45+
this.ctx.body = id;
4346
}
4447

4548
@post('/body')
46-
async body(@body() body, @ctx() ctx) {
47-
ctx.body = body;
49+
async body(@body() body) {
50+
this.ctx.body = body;
4851
}
4952

5053
@get('/body_id')
51-
async bodyId(@body('id') id, @ctx() ctx) {
52-
ctx.body = id;
54+
async bodyId(@body('id') id) {
55+
this.ctx.body = id;
5356
}
5457

5558
@post('/file')
56-
async file(@file() stream, @ctx() ctx) {
59+
async file(@file() stream) {
5760
const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
5861
const target = path.join(this.baseDir, 'app/public', filename);
5962
const writeStream = fs.createWriteStream(target);
6063
await pump(stream, writeStream);
61-
ctx.body = 'ok';
64+
this.ctx.body = 'ok';
6265
}
6366

6467
@post('/files')
65-
async files(@files({ autoFields: true }) parts, @ctx() ctx) {
68+
async files(@files({ autoFields: true }) parts) {
6669

6770
let stream = await parts();
6871

@@ -74,25 +77,25 @@ export class ParamController {
7477
stream = await parts();
7578
}
7679

77-
ctx.body = 'ok';
80+
this.ctx.body = 'ok';
7881
}
7982

8083
@get('/session')
81-
async session(@session() session, @ctx() ctx) {
84+
async session(@session() session) {
8285
// service,hello,a,b
83-
ctx.body = session;
86+
this.ctx.body = session;
8487
}
8588

8689
@get('/headers')
87-
async header(@headers() headers, @ctx() ctx) {
90+
async header(@headers() headers) {
8891
// service,hello,a,b
89-
ctx.body = headers.host.substring(0, 3);
92+
this.ctx.body = headers.host.substring(0, 3);
9093
}
9194

9295
@get('/headers_host')
93-
async headerHost(@headers('host') host, @ctx() ctx) {
96+
async headerHost(@headers('host') host) {
9497
// service,hello,a,b
95-
ctx.body = host.substring(0, 3);
98+
this.ctx.body = host.substring(0, 3);
9699
}
97100

98101
}

0 commit comments

Comments
 (0)