-
Notifications
You must be signed in to change notification settings - Fork 567
/
filterManager.ts
161 lines (150 loc) 路 4.12 KB
/
filterManager.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {
CATCH_KEY,
getClassMetadata,
MATCH_KEY,
MatchPattern,
} from '@midwayjs/decorator';
import {
CommonFilterUnion,
IFilter,
IMidwayContainer,
IMidwayContext,
} from '../interface';
import { toPathMatch } from '../util';
export class FilterManager<
CTX extends IMidwayContext = IMidwayContext,
R = any,
N = any
> {
private errFilterList: Array<new (...args) => IFilter<CTX, R, N>> = [];
private successFilterList: Array<new (...args) => IFilter<CTX, R, N>> = [];
private exceptionMap: WeakMap<
Error,
{
filter: IFilter<CTX, R, N>;
catchOptions: {
matchPrototype?: boolean;
};
}
> = new WeakMap();
private defaultErrFilter = undefined;
private matchFnList = [];
private protoMatchList = [];
public useFilter(Filters: CommonFilterUnion<CTX, R, N>) {
if (!Array.isArray(Filters)) {
Filters = [Filters];
}
for (const Filter of Filters) {
if (getClassMetadata(CATCH_KEY, Filter)) {
this.errFilterList.push(Filter);
}
if (getClassMetadata(MATCH_KEY, Filter)) {
this.successFilterList.push(Filter);
}
}
}
public async init(applicationContext: IMidwayContainer) {
// for catch exception
for (const FilterClass of this.errFilterList) {
const filter = await applicationContext.getAsync(FilterClass);
const exceptionMetadata = getClassMetadata(CATCH_KEY, FilterClass);
if (exceptionMetadata && exceptionMetadata.catchTargets) {
exceptionMetadata.catchOptions = exceptionMetadata.catchOptions || {};
for (const Exception of exceptionMetadata.catchTargets) {
this.exceptionMap.set(Exception, {
filter,
catchOptions: exceptionMetadata.catchOptions,
});
if (exceptionMetadata.catchOptions['matchPrototype']) {
this.protoMatchList.push(err => {
if (err instanceof Exception) {
return Exception;
} else {
return false;
}
});
}
}
} else {
// default filter
this.defaultErrFilter = filter;
}
}
// for success return
for (const FilterClass of this.successFilterList) {
const filter = await applicationContext.getAsync(FilterClass);
const matchMetadata: {
matchPattern?: MatchPattern<CTX>;
} = getClassMetadata(MATCH_KEY, FilterClass);
if (matchMetadata && matchMetadata.matchPattern) {
this.matchFnList.push({
matchFn: toPathMatch(matchMetadata.matchPattern),
target: filter,
});
}
}
}
public async runErrorFilter(
err: Error,
ctx: CTX,
res?: R,
next?: N
): Promise<{
result: any;
error: any;
}> {
let result, error;
let matched = false;
if (this.exceptionMap.has((err as any).constructor)) {
matched = true;
const filterData = this.exceptionMap.get((err as any).constructor);
result = await filterData.filter.catch(err, ctx, res, next);
}
// match with prototype
if (!matched && this.protoMatchList.length) {
let protoException;
for (const matchPattern of this.protoMatchList) {
protoException = matchPattern(err);
if (protoException) {
break;
}
}
if (protoException) {
matched = true;
const filterData = this.exceptionMap.get(protoException);
result = await filterData.filter.catch(err, ctx, res, next);
}
}
if (!matched && this.defaultErrFilter) {
matched = true;
result = await this.defaultErrFilter.catch(err, ctx, res, next);
}
if (!matched) {
error = err;
}
return {
result,
error,
};
}
public async runResultFilter(
result: any,
ctx: CTX,
res?: R,
next?: N
): Promise<{
result: any;
error: any;
}> {
let returnValue = result;
for (const matchData of this.matchFnList) {
if (matchData.matchFn(ctx, res)) {
returnValue = await matchData.target.match(returnValue, ctx, res, next);
}
}
return {
result: returnValue,
error: undefined,
};
}
}