-
Notifications
You must be signed in to change notification settings - Fork 576
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
277 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { attachMethodDataToClass } from 'injection'; | ||
|
||
export enum RouteParamTypes { | ||
QUERY, | ||
BODY, | ||
PARAM, | ||
CONTEXT, | ||
HEADERS, | ||
SESSION, | ||
FILESTREAM, | ||
FILESSTREAM, | ||
NEXT, | ||
} | ||
|
||
export interface RouterParamValue { | ||
index?: number; | ||
type?: RouteParamTypes; | ||
data?: any; | ||
extractValue?: (ctx, next) => Promise<any>; | ||
} | ||
|
||
export const ROUTE_ARGS_METADATA = '__routeArgsMetadata__'; | ||
|
||
export const extractValue = function extractValue(key, data) { | ||
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: | ||
return data ? ctx.params[data] : ctx.params; | ||
case RouteParamTypes.QUERY: | ||
return data ? ctx.query[data] : ctx.query; | ||
case RouteParamTypes.HEADERS: | ||
return data ? ctx.headers[data] : ctx.headers; | ||
case RouteParamTypes.SESSION: | ||
return ctx.session; | ||
case RouteParamTypes.FILESTREAM: | ||
return ctx.getFileStream && ctx.getFileStream(data); | ||
case RouteParamTypes.FILESSTREAM: | ||
return ctx.multipart && ctx.multipart(data); | ||
default: | ||
return null; | ||
} | ||
}; | ||
}; | ||
|
||
function createParamMapping(type: RouteParamTypes) { | ||
return (data?: any) => { | ||
return (target, key, index) => { | ||
attachMethodDataToClass(ROUTE_ARGS_METADATA, { | ||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions
89
packages/midway-web/test/fixtures/enhance/base-app-decorator/src/app/controller/param.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { provide } from 'injection'; | ||
import { controller, config, get, post, query, param, ctx, files, file, session, body, headers } from '../../../../../../../src'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
import * as pump from 'mz-modules/pump'; | ||
|
||
@provide() | ||
@controller('/param') | ||
export class ParamController { | ||
|
||
@config('baseDir') | ||
baseDir: string; | ||
|
||
@get('/query') | ||
async query(@query() query, @ctx() ctx) { | ||
ctx.body = query; | ||
} | ||
|
||
@get('/query_id') | ||
async queryId(@query('id') id, @ctx() ctx) { | ||
ctx.body = id; | ||
} | ||
|
||
@get('/param/:id/test/:userId') | ||
async param(@param() param, @ctx() ctx) { | ||
// service,hello,a,b | ||
ctx.body = param; | ||
} | ||
|
||
@get('/param/:id') | ||
async paramId(@param('id') id, @ctx() ctx) { | ||
ctx.body = id; | ||
} | ||
|
||
@post('/body') | ||
async body(@body() body, @ctx() ctx) { | ||
ctx.body = body; | ||
} | ||
|
||
@get('/body_id') | ||
async bodyId(@body('id') id, @ctx() ctx) { | ||
ctx.body = id; | ||
} | ||
|
||
@post('/file') | ||
async file(@file() stream, @ctx() ctx) { | ||
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'; | ||
} | ||
|
||
@post('/files') | ||
async files(@files({ autoFields: true }) parts, @ctx() ctx) { | ||
|
||
let stream = await parts(); | ||
|
||
while (stream) { | ||
const filename = stream.filename.toLowerCase(); | ||
const target = path.join(this.baseDir, 'app/public', filename); | ||
const writeStream = fs.createWriteStream(target); | ||
await pump(stream, writeStream); | ||
stream = await parts(); | ||
} | ||
|
||
ctx.body = 'ok'; | ||
} | ||
|
||
@get('/session') | ||
async session(@session() session, @ctx() ctx) { | ||
// service,hello,a,b | ||
ctx.body = session; | ||
} | ||
|
||
@get('/headers') | ||
async header(@headers() headers, @ctx() ctx) { | ||
// service,hello,a,b | ||
ctx.body = headers.host.substring(0, 3); | ||
} | ||
|
||
@get('/headers_host') | ||
async headerHost(@headers('host') host, @ctx() ctx) { | ||
// service,hello,a,b | ||
ctx.body = host.substring(0, 3); | ||
} | ||
|
||
} |