目录
Copilot

feat: support custom pathToRegexpModule option (#13)

Description

This PR picks the custom pathToRegexpModule feature from PR #11 to the master branch, adapting it for the TypeScript codebase.

Changes

Added pathToRegexpModule option

Users can now provide a custom path-to-regexp module via the pathToRegexpModule option in PathMatchingOptions. This enables compatibility with different versions of path-to-regexp and 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' }); // false

Backwards Compatibility

  • Defaults to the built-in path-to-regexp@6 when no custom module is provided
  • All existing tests pass without modification
  • Supports both module export formats:
    • Object with pathToRegexp property: { pathToRegexp: fn }
    • Direct function export: pathToRegexp

Version Support

The implementation automatically detects and handles both path-to-regexp@6 and path-to-regexp@8+ return formats:

  • v6: Returns a RegExp directly
  • v8+: Returns an object with regexp and keys properties

Testing

  • 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 pathToRegexpModule option in the options section

Fixes the feature request to pick custom pathToRegexpModule support from the 1.x branch to master.

Original 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)

Fixes eggjs/egg-path-matching#12

✨ 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

6个月前21次提交

egg-path-matching

NPM version CI Test coverage Known Vulnerabilities npm download

Installation

npm install egg-path-matching

Usage

import { pathMatching } from 'egg-path-matching';

const options = {
  ignore: '/api', // string will use parsed by path-to-regexp
  // support regexp
  ignore: /^\/api/,
  // support function
  ignore: ctx => ctx.path.startsWith('/api'),
  // support Array
  ignore: [ ctx => ctx.path.startsWith('/api'), /^\/foo$/, '/bar'],
  // support match or ignore
  match: '/api',
  // custom path-to-regexp module, default is `path-to-regexp@6`
  // pathToRegexpModule: customPathToRegexp,
};

const match = pathMatching(options);
assert.equal(match({ path: '/api' }), true);
assert.equal(match({ path: '/api/hello' }), true);
assert.equal(match({ path: '/api' }), true);

options

  • match {String | RegExp | Function | Array} - if request path hit options.match, will return true, otherwise will return false.
  • ignore {String | RegExp | Function | Array} - if request path hit options.ignore, will return false, otherwise will return true.
  • 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.

ignore and match can not both be presented. and if neither ignore nor match presented, the new function will always return true.

License

MIT

Contributors

Contributors

Made with contributors-img.

关于
235.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802032778号