Skip to content

Commit

Permalink
fix: @match empty args (#1384)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Nov 25, 2021
1 parent 02e2144 commit 6f90fc9
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 10 deletions.
7 changes: 3 additions & 4 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Thank you for reporting an issue.
感谢您向我们反馈问题。
1. 提交问题前,请先阅读 https://eggjs.org/zh-cn/faq.html
1. 提交问题前,请先阅读文档,善用搜索
2. 我们推荐如果是小问题(错别字修改,小的 bug fix)直接提交 PR。
3. 如果是一个新需求,请提供:详细需求描述,最好是有伪代码实现。
4. 如果是一个 BUG,请提供:复现步骤,错误日志以及相关配置,并尽量填写下面的模板中的条目。
Expand All @@ -17,9 +17,8 @@ Thank you for reporting an issue.
-->

* **Node Version**:
* **Egg Version**:
* **Plugin Name**:
* **Plugin Version**:
* **Midway Version(Decorator/Core)**:
* **Component Name/Version**:
* **Platform**:
* **Mini Showcase Repository**:

Expand Down
4 changes: 2 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
Contributors guide: https://github.com/midwayjs/midway/blob/master/CONTRIBUTING.md
感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
Contributors guide: https://github.com/midwayjs/midway/blob/master/CONTRIBUTING.md
-->

##### Checklist
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/filterManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class FilterManager<
let returnValue = result;

for (const matchData of this.matchFnList) {
if (matchData.matchFn(ctx)) {
if (matchData.matchFn(ctx, res)) {
returnValue = await matchData.target.match(returnValue, ctx, res, next);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type ServiceFactoryConfigOption<OPTIONS> = {
};
}

type ConfigType<T> = T extends (args: any[]) => any ? PowerPartial<ReturnType<T>> : PowerPartial<T>;
type ConfigType<T> = T extends (...args: any[]) => any ? PowerPartial<ReturnType<T>> : PowerPartial<T>;

export type FileConfigOption<T, K = unknown> = K extends keyof ConfigType<T> ? Pick<ConfigType<T>, K> : ConfigType<T>;

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ export const transformRequestObjectByType = (originValue: any, targetType?) => {
};

export function toPathMatch(pattern) {
if (typeof pattern === 'boolean') {
return ctx => pattern;
}
if (typeof pattern === 'string') {
const reg = pathToRegexp(pattern, [], { end: false });
if (reg.global) reg.lastIndex = 0;
Expand Down
21 changes: 21 additions & 0 deletions packages/core/test/common/filterManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,27 @@ describe('/test/common/filterManager.test.ts', function () {
expect(error2).toBeUndefined();
});

it('should test all match with empty args', async () => {
const container = new MidwayContainer();
const filterManager = new FilterManager();

@Match()
class TestFilter {
match(result, ctx) {
return result + ', midway';
}
}

container.bindClass(TestFilter);
filterManager.useFilter(TestFilter);
await filterManager.init(container);

const { result, error } = await filterManager.runResultFilter('hello world', {} as any);

expect(result).toEqual('hello world, midway');
expect(error).toBeUndefined();
});

it('should test match path', async () => {
const container = new MidwayContainer();
const filterManager = new FilterManager();
Expand Down
8 changes: 6 additions & 2 deletions packages/decorator/src/decorator/common/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ export function Catch(catchTarget?: any | any[]) {
};
}

export type MatchPattern<T = any> = ((ctx: T) => boolean) | string | string[];
export type MatchPattern<CtxOrReq = any, Res = any> =
| ((ctxOrReq: CtxOrReq, res: Res) => boolean)
| string
| string[]
| boolean;

export function Match(matchPattern: MatchPattern) {
export function Match(matchPattern: MatchPattern = true) {
return function (target) {
saveClassMetadata(
MATCH_KEY,
Expand Down

0 comments on commit 6f90fc9

Please sign in to comment.