Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb3f7d2
commit bffefe0
Showing
90 changed files
with
1,352 additions
and
503 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
File renamed without changes.
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,116 @@ | ||
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, IFilter<CTX, R, N>> = new WeakMap(); | ||
private defaultErrFilter = undefined; | ||
private matchFnList = []; | ||
|
||
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) { | ||
for (const Exception of exceptionMetadata.catchTargets) { | ||
this.exceptionMap.set(Exception, filter); | ||
} | ||
} 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; | ||
if (this.exceptionMap.has((err as any).constructor)) { | ||
const filter = this.exceptionMap.get((err as any).constructor); | ||
result = await filter.catch(err, ctx, res, next); | ||
} else if (this.defaultErrFilter) { | ||
result = await this.defaultErrFilter.catch(err, ctx, res, next); | ||
} else { | ||
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)) { | ||
returnValue = await matchData.target.match(returnValue, ctx, res, next); | ||
} | ||
} | ||
|
||
return { | ||
result: returnValue, | ||
error: undefined, | ||
}; | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
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
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
Oops, something went wrong.