feat: support custom pathToRegexpModule option (#13)
Description
This PR picks the custom
pathToRegexpModulefeature from PR #11 to the master branch, adapting it for the TypeScript codebase.Changes
Added
pathToRegexpModuleoptionUsers can now provide a custom
path-to-regexpmodule via thepathToRegexpModuleoption inPathMatchingOptions. This enables compatibility with different versions ofpath-to-regexpand allows users to use newer versions (like v8+) with their preferred syntax.Example usage:
import { pathMatching } from 'egg-path-matching'; import { pathToRegexp } from 'path-to-regexp'; // v8+ // Use custom path-to-regexp module with v8 syntax const match = pathMatching({ match: '/api{/*path}', pathToRegexpModule: pathToRegexp }); match({ path: '/api/hello' }); // true match({ path: '/api' }); // true match({ path: '/api1' }); // falseBackwards Compatibility
- Defaults to the built-in
path-to-regexp@6when no custom module is provided- All existing tests pass without modification
- Supports both module export formats:
- Object with
pathToRegexpproperty:{ pathToRegexp: fn }- Direct function export:
pathToRegexpVersion Support
The implementation automatically detects and handles both
path-to-regexp@6andpath-to-regexp@8+return formats:
- v6: Returns a
RegExpdirectly- v8+: Returns an object with
regexpandkeyspropertiesTesting
- Added new test case validating custom module functionality with
path-to-regexp@8- All 13 tests pass successfully
- Test coverage maintained
Documentation
- Updated README with usage example
- Documented the new
pathToRegexpModuleoption in the options sectionFixes the feature request to pick custom pathToRegexpModule support from the 1.x branch to master.
Fixes eggjs/egg-path-matching#12Original prompt
This section details on the original issue you should resolve
Pick custom pathToRegexpModule feature to master branch Track picking the custom pathToRegexpModule feature from eggjs/egg-path-matching#11 to master branch.
Context:
- Original PR: eggjs/egg-path-matching#11
- Feature: Support custom pathToRegexpModule
This issue tracks the work needed to pick this feature to the master branch.
Comments on the Issue (you are @copilot in this section)
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
Summary by CodeRabbit
- New Features
- Added optional pathToRegexpModule option to supply a custom path-to-regexp implementation, enabling compatibility with v6 and v8+.
- Documentation
- Updated usage and options to describe the new pathToRegexpModule, defaults, and version compatibility.
- Tests
- Added tests verifying matching behavior when using a custom pathToRegexp module, including v8 compatibility.
- Chores
- Added a development dependency alias for path-to-regexp v8 to support testing and local development.
Co-authored-by: copilot-swe-agent[bot] 198982749+Copilot@users.noreply.github.com Co-authored-by: fengmk2 156269+fengmk2@users.noreply.github.com
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802032778号
egg-path-matching
Installation
Usage
options
match{String | RegExp | Function | Array} - if request path hitoptions.match, will returntrue, otherwise will returnfalse.ignore{String | RegExp | Function | Array} - if request path hitoptions.ignore, will returnfalse, otherwise will returntrue.pathToRegexpModule{Object | Function} - custom path-to-regexp module. Default ispath-to-regexp@6. Supports bothpath-to-regexp@6andpath-to-regexp@8+formats.ignoreandmatchcan not both be presented. and if neitherignorenormatchpresented, the new function will always returntrue.License
MIT
Contributors
Made with contributors-img.