@eggjs/path-matching 3.0.0-beta.20
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.
- package/LICENSE +21 -0
- package/README.md +64 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +36 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016-present Alibaba Group Holding Limited and other contributors.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# egg-path-matching
|
|
2
|
+
|
|
3
|
+
[![NPM version][npm-image]][npm-url]
|
|
4
|
+
[](https://github.com/eggjs/egg-path-matching/actions/workflows/nodejs.yml)
|
|
5
|
+
[](https://codecov.io/gh/eggjs/egg-path-matching)
|
|
6
|
+
[![Known Vulnerabilities][snyk-image]][snyk-url]
|
|
7
|
+
[![npm download][download-image]][download-url]
|
|
8
|
+
|
|
9
|
+
[npm-image]: https://img.shields.io/npm/v/egg-path-matching.svg?style=flat-square
|
|
10
|
+
[npm-url]: https://npmjs.org/package/egg-path-matching
|
|
11
|
+
[snyk-image]: https://snyk.io/test/npm/egg-path-matching/badge.svg?style=flat-square
|
|
12
|
+
[snyk-url]: https://snyk.io/test/npm/egg-path-matching
|
|
13
|
+
[download-image]: https://img.shields.io/npm/dm/egg-path-matching.svg?style=flat-square
|
|
14
|
+
[download-url]: https://npmjs.org/package/egg-path-matching
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install egg-path-matching
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { pathMatching } from 'egg-path-matching';
|
|
26
|
+
|
|
27
|
+
const options = {
|
|
28
|
+
ignore: '/api', // string will use parsed by path-to-regexp
|
|
29
|
+
// support regexp
|
|
30
|
+
ignore: /^\/api/,
|
|
31
|
+
// support function
|
|
32
|
+
ignore: ctx => ctx.path.startsWith('/api'),
|
|
33
|
+
// support Array
|
|
34
|
+
ignore: [ctx => ctx.path.startsWith('/api'), /^\/foo$/, '/bar'],
|
|
35
|
+
// support match or ignore
|
|
36
|
+
match: '/api',
|
|
37
|
+
// custom path-to-regexp module, default is `path-to-regexp@6`
|
|
38
|
+
// pathToRegexpModule: customPathToRegexp,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const match = pathMatching(options);
|
|
42
|
+
assert.equal(match({ path: '/api' }), true);
|
|
43
|
+
assert.equal(match({ path: '/api/hello' }), true);
|
|
44
|
+
assert.equal(match({ path: '/api' }), true);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### options
|
|
48
|
+
|
|
49
|
+
- `match` {String | RegExp | Function | Array} - if request path hit `options.match`, will return `true`, otherwise will return `false`.
|
|
50
|
+
- `ignore` {String | RegExp | Function | Array} - if request path hit `options.ignore`, will return `false`, otherwise will return `true`.
|
|
51
|
+
- `pathToRegexpModule` {Object | Function} - custom path-to-regexp module. Default is `path-to-regexp@6`. Supports both `path-to-regexp@6` and `path-to-regexp@8+` formats.
|
|
52
|
+
|
|
53
|
+
`ignore` and `match` can not both be presented.
|
|
54
|
+
and if neither `ignore` nor `match` presented, the new function will always return `true`.
|
|
55
|
+
|
|
56
|
+
### License
|
|
57
|
+
|
|
58
|
+
[MIT](LICENSE)
|
|
59
|
+
|
|
60
|
+
## Contributors
|
|
61
|
+
|
|
62
|
+
[](https://github.com/eggjs/egg-path-matching/graphs/contributors)
|
|
63
|
+
|
|
64
|
+
Made with [contributors-img](https://contrib.rocks).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
type PathMatchingFun = (ctx: any) => boolean;
|
|
3
|
+
type PathMatchingPattern = string | RegExp | PathMatchingFun | (string | RegExp | PathMatchingFun)[];
|
|
4
|
+
interface PathMatchingOptions {
|
|
5
|
+
ignore?: PathMatchingPattern;
|
|
6
|
+
match?: PathMatchingPattern;
|
|
7
|
+
pathToRegexpModule?: any;
|
|
8
|
+
}
|
|
9
|
+
declare function pathMatching(options: PathMatchingOptions): PathMatchingFun;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { PathMatchingFun, PathMatchingOptions, PathMatchingPattern, pathMatching };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { pathToRegexp } from "path-to-regexp";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
function pathMatching(options) {
|
|
5
|
+
options = options || {};
|
|
6
|
+
if (options.match && options.ignore) throw new Error("options.match and options.ignore can not both present");
|
|
7
|
+
if (!options.match && !options.ignore) return () => true;
|
|
8
|
+
const pathToRegexpModule = options.pathToRegexpModule || { pathToRegexp };
|
|
9
|
+
const pathToRegexpFn = pathToRegexpModule.pathToRegexp || pathToRegexpModule;
|
|
10
|
+
const matchFn = options.match ? toPathMatch(options.match, pathToRegexpFn) : toPathMatch(options.ignore, pathToRegexpFn);
|
|
11
|
+
return function pathMatch(ctx) {
|
|
12
|
+
const matched = matchFn(ctx);
|
|
13
|
+
return options.match ? matched : !matched;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function toPathMatch(pattern, pathToRegexpFn) {
|
|
17
|
+
if (typeof pattern === "string") {
|
|
18
|
+
let reg = pathToRegexpFn(pattern, [], { end: false });
|
|
19
|
+
if (reg.regexp) reg = reg.regexp;
|
|
20
|
+
if (reg.global) reg.lastIndex = 0;
|
|
21
|
+
return (ctx) => reg.test(ctx.path);
|
|
22
|
+
}
|
|
23
|
+
if (pattern instanceof RegExp) return (ctx) => {
|
|
24
|
+
if (pattern.global) pattern.lastIndex = 0;
|
|
25
|
+
return pattern.test(ctx.path);
|
|
26
|
+
};
|
|
27
|
+
if (typeof pattern === "function") return pattern;
|
|
28
|
+
if (Array.isArray(pattern)) {
|
|
29
|
+
const matchFns = pattern.map((item) => toPathMatch(item, pathToRegexpFn));
|
|
30
|
+
return (ctx) => matchFns.some((matchFn) => matchFn(ctx));
|
|
31
|
+
}
|
|
32
|
+
throw new Error(`match/ignore pattern must be RegExp, Array or String, but got ${pattern}`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
export { pathMatching };
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@eggjs/path-matching",
|
|
3
|
+
"version": "3.0.0-beta.20",
|
|
4
|
+
"description": "match or ignore url path",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./dist/index.js",
|
|
8
|
+
"./package.json": "./package.json"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"keywords": [
|
|
17
|
+
"url",
|
|
18
|
+
"match",
|
|
19
|
+
"ignore"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"homepage": "https://github.com/eggjs/egg/tree/next/packages/path-matching",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/eggjs/egg/issues"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/eggjs/egg.git",
|
|
29
|
+
"directory": "packages/path-matching"
|
|
30
|
+
},
|
|
31
|
+
"author": {
|
|
32
|
+
"name": "dead-horse",
|
|
33
|
+
"email": "dead_horse@qq.com",
|
|
34
|
+
"url": "http://deadhorse.me"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">= 22.18.0"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"path-to-regexp": "^6.3.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^24.6.2",
|
|
44
|
+
"oxlint": "^1.19.0",
|
|
45
|
+
"path-to-regexp-v8": "npm:path-to-regexp@^8.3.0",
|
|
46
|
+
"rimraf": "^6.0.1",
|
|
47
|
+
"tsdown": "^0.15.4",
|
|
48
|
+
"typescript": "^5.9.3",
|
|
49
|
+
"vitest": "4.0.0-beta.17",
|
|
50
|
+
"@eggjs/tsconfig": "3.1.0-beta.27"
|
|
51
|
+
},
|
|
52
|
+
"main": "./dist/index.js",
|
|
53
|
+
"module": "./dist/index.js",
|
|
54
|
+
"types": "./dist/index.d.ts",
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsdown",
|
|
57
|
+
"clean": "rimraf dist",
|
|
58
|
+
"typecheck": "tsc --noEmit",
|
|
59
|
+
"lint": "oxlint --type-aware",
|
|
60
|
+
"lint:fix": "npm run lint -- --fix",
|
|
61
|
+
"test": "npm run lint:fix && vitest"
|
|
62
|
+
}
|
|
63
|
+
}
|