@midwayjs/core 3.10.15 → 3.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -619,13 +619,14 @@ export interface Context {
619
619
  }
620
620
  export type IMidwayContext<FrameworkContext = unknown> = Context & FrameworkContext;
621
621
  export type NextFunction = () => Promise<any>;
622
+ export type IgnoreMatcher<CTX> = string | RegExp | ((ctx: CTX) => boolean);
622
623
  /**
623
624
  * Common middleware definition
624
625
  */
625
626
  export interface IMiddleware<CTX, R, N = unknown> {
626
627
  resolve: (app: IMidwayApplication) => FunctionMiddleware<CTX, R, N> | Promise<FunctionMiddleware<CTX, R, N>>;
627
- match?: (ctx: CTX) => boolean;
628
- ignore?: (ctx: CTX) => boolean;
628
+ match?: IgnoreMatcher<CTX> | IgnoreMatcher<CTX>[];
629
+ ignore?: IgnoreMatcher<CTX> | IgnoreMatcher<CTX>[];
629
630
  }
630
631
  export type FunctionMiddleware<CTX, R, N = unknown> = N extends true ? (req: CTX, res: R, next: N) => any : (context: CTX, next: R, options?: any) => any;
631
632
  export type ClassMiddleware<CTX, R, N> = new (...args: any[]) => IMiddleware<CTX, R, N>;
@@ -20,7 +20,6 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
20
20
  this.applicationContext = applicationContext;
21
21
  }
22
22
  async compose(middleware, app, name) {
23
- var _a, _b;
24
23
  if (!Array.isArray(middleware)) {
25
24
  throw new error_1.MidwayParameterError('Middleware stack must be an array');
26
25
  }
@@ -49,8 +48,9 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
49
48
  // wrap ignore and match
50
49
  const mw = fn;
51
50
  const match = (0, util_1.pathMatching)({
52
- match: (_a = classMiddleware.match) === null || _a === void 0 ? void 0 : _a.bind(classMiddleware),
53
- ignore: (_b = classMiddleware.ignore) === null || _b === void 0 ? void 0 : _b.bind(classMiddleware),
51
+ match: classMiddleware.match,
52
+ ignore: classMiddleware.ignore,
53
+ thisResolver: classMiddleware,
54
54
  });
55
55
  fn = (ctx, next, options) => {
56
56
  if (!match(ctx))
@@ -1,4 +1,4 @@
1
- import { FunctionMiddleware } from '../interface';
1
+ import { FunctionMiddleware, IgnoreMatcher } from '../interface';
2
2
  import { camelCase, pascalCase } from './camelCase';
3
3
  import { randomUUID } from './uuid';
4
4
  import { safeParse, safeStringify } from './flatted';
@@ -85,7 +85,11 @@ export declare const deprecatedOutput: (message: string) => void;
85
85
  */
86
86
  export declare const transformRequestObjectByType: (originValue: any, targetType?: any) => any;
87
87
  export declare function toPathMatch(pattern: any): any;
88
- export declare function pathMatching(options: any): (ctx?: any) => any;
88
+ export declare function pathMatching(options: {
89
+ match?: IgnoreMatcher<any> | IgnoreMatcher<any>[];
90
+ ignore?: IgnoreMatcher<any> | IgnoreMatcher<any>[];
91
+ thisResolver?: any;
92
+ }): (ctx?: any) => boolean;
89
93
  /**
90
94
  * wrap function middleware with match and ignore
91
95
  * @param mw
@@ -270,9 +270,32 @@ function pathMatching(options) {
270
270
  throw new error_1.MidwayCommonError('options.match and options.ignore can not both present');
271
271
  if (!options.match && !options.ignore)
272
272
  return () => true;
273
+ if (options.match && !Array.isArray(options.match)) {
274
+ options.match = [options.match];
275
+ }
276
+ if (options.ignore && !Array.isArray(options.ignore)) {
277
+ options.ignore = [options.ignore];
278
+ }
279
+ const createMatch = (ignoreMatcherArr) => {
280
+ const matchedArr = ignoreMatcherArr.map(item => {
281
+ if (options.thisResolver) {
282
+ return toPathMatch(item).bind(options.thisResolver);
283
+ }
284
+ return toPathMatch(item);
285
+ });
286
+ return ctx => {
287
+ for (let i = 0; i < matchedArr.length; i++) {
288
+ const matched = matchedArr[i](ctx);
289
+ if (matched) {
290
+ return true;
291
+ }
292
+ }
293
+ return false;
294
+ };
295
+ };
273
296
  const matchFn = options.match
274
- ? toPathMatch(options.match)
275
- : toPathMatch(options.ignore);
297
+ ? createMatch(options.match)
298
+ : createMatch(options.ignore);
276
299
  return function pathMatch(ctx) {
277
300
  const matched = matchFn(ctx);
278
301
  return options.match ? matched : !matched;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.10.15",
3
+ "version": "3.11.0",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -25,7 +25,7 @@
25
25
  "koa": "2.14.1",
26
26
  "mm": "3.2.1",
27
27
  "raw-body": "2.5.2",
28
- "sinon": "15.0.1"
28
+ "sinon": "15.0.3"
29
29
  },
30
30
  "dependencies": {
31
31
  "@midwayjs/glob": "^1.0.2",
@@ -42,5 +42,5 @@
42
42
  "engines": {
43
43
  "node": ">=12"
44
44
  },
45
- "gitHead": "8d5fd0e58156612c675821134377208e4da710ad"
45
+ "gitHead": "eadb977e7fddcd4287c099fc32b601cd51702514"
46
46
  }