/
paramMapping.ts
109 lines (101 loc) 路 3.63 KB
/
paramMapping.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { createCustomParamDecorator, WEB_ROUTER_PARAM_KEY } from '../';
import {
IMidwayContext,
ParamDecoratorOptions,
PipeUnionTransform,
} from '../../interface';
export enum RouteParamTypes {
QUERY = 'query',
BODY = 'body',
PARAM = 'param',
HEADERS = 'headers',
SESSION = 'session',
FILESTREAM = 'file_stream',
FILESSTREAM = 'files_stream',
NEXT = 'next',
REQUEST_PATH = 'request_path',
REQUEST_IP = 'request_ip',
QUERIES = 'queries',
FIELDS = 'fields',
CUSTOM = 'custom',
}
export interface RouterParamValue {
index: number;
type: RouteParamTypes;
propertyData?: any;
}
const createParamMapping = function (type: RouteParamTypes) {
return (propertyOrPipes: any, options: ParamDecoratorOptions = {}) => {
let propertyData = propertyOrPipes;
if (Array.isArray(propertyOrPipes) && options.pipes === undefined) {
options.pipes = propertyOrPipes;
propertyData = undefined;
}
return createCustomParamDecorator(
WEB_ROUTER_PARAM_KEY,
{
type,
propertyData,
},
options
);
};
};
export declare type KoaLikeCustomParamDecorator<T = unknown> = (
ctx: IMidwayContext
) => T | Promise<T>;
export declare type ExpressLikeCustomParamDecorator<T = unknown> = (
req,
res
) => T | Promise<T>;
export declare type CustomParamDecorator<T = unknown> =
| KoaLikeCustomParamDecorator<T>
| ExpressLikeCustomParamDecorator<T>;
export const createRequestParamDecorator = function (
transform: CustomParamDecorator,
pipesOrOptions?: ParamDecoratorOptions | Array<PipeUnionTransform>
) {
pipesOrOptions = pipesOrOptions || {};
if (Array.isArray(pipesOrOptions)) {
pipesOrOptions = {
pipes: pipesOrOptions as Array<PipeUnionTransform>,
};
}
return createParamMapping(RouteParamTypes.CUSTOM)(transform, pipesOrOptions);
};
export const Session = (
propertyOrPipes?: string | PipeUnionTransform[],
pipes?: PipeUnionTransform[]
) => createParamMapping(RouteParamTypes.SESSION)(propertyOrPipes, { pipes });
export const Body = (
propertyOrPipes?: string | PipeUnionTransform[],
pipes?: PipeUnionTransform[]
) => createParamMapping(RouteParamTypes.BODY)(propertyOrPipes, { pipes });
export const Query = (
propertyOrPipes?: string | PipeUnionTransform[],
pipes?: PipeUnionTransform[]
) => createParamMapping(RouteParamTypes.QUERY)(propertyOrPipes, { pipes });
export const Param = (
propertyOrPipes?: string | PipeUnionTransform[],
pipes?: PipeUnionTransform[]
) => createParamMapping(RouteParamTypes.PARAM)(propertyOrPipes, { pipes });
export const Headers = (
propertyOrPipes?: string | PipeUnionTransform[],
pipes?: PipeUnionTransform[]
) => createParamMapping(RouteParamTypes.HEADERS)(propertyOrPipes, { pipes });
export const File = (propertyOrPipes?: any, pipes?: PipeUnionTransform[]) =>
createParamMapping(RouteParamTypes.FILESTREAM)(propertyOrPipes, { pipes });
export const Files = (propertyOrPipes?: any, pipes?: PipeUnionTransform[]) =>
createParamMapping(RouteParamTypes.FILESSTREAM)(propertyOrPipes, { pipes });
export const RequestPath = (pipes?: PipeUnionTransform[]) =>
createParamMapping(RouteParamTypes.REQUEST_PATH)(undefined, { pipes });
export const RequestIP = (pipes?: PipeUnionTransform[]) =>
createParamMapping(RouteParamTypes.REQUEST_IP)(undefined, { pipes });
export const Queries = (
propertyOrPipes?: string | PipeUnionTransform[],
pipes?: PipeUnionTransform[]
) => createParamMapping(RouteParamTypes.QUERIES)(propertyOrPipes, { pipes });
export const Fields = (
propertyOrPipes?: string | PipeUnionTransform[],
pipes?: PipeUnionTransform[]
) => createParamMapping(RouteParamTypes.FIELDS)(propertyOrPipes, { pipes });