diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 003b101..d876c2e 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -12,6 +12,6 @@ jobs: uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest' - version: '18.7.0, 18, 20, 22' + version: '18.19.0, 18, 20, 22' secrets: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 9159bd4..d118bb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,10 +12,10 @@ ### ⚠ BREAKING CHANGES -* drop Node.js < 18.7.0 support +* drop Node.js < 18.19.0 support - Drop generator function support -- Drop Node.js < 18.7.0 support +- Drop Node.js < 18.19.0 support diff --git a/README.md b/README.md index bc089ad..717870c 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Router core component for [Egg.js](https://github.com/eggjs). - [Tests](#tests) - [Breaking changes on v3](#breaking-changes-on-v3) - [License](#license) + - [Contributors](#contributors) @@ -450,8 +451,26 @@ Run tests using `npm test`. ## Breaking changes on v3 - Drop generator function support -- Drop Node.js < 18.7.0 support +- Drop Node.js < 18.19.0 support ## License [MIT](LICENSE) + + + +## Contributors + +|[
alexmingoia](https://github.com/alexmingoia)
|[
jbielick](https://github.com/jbielick)
|[
dead-horse](https://github.com/dead-horse)
|[
fengmk2](https://github.com/fengmk2)
|[
wachunei](https://github.com/wachunei)
|[
dominicbarnes](https://github.com/dominicbarnes)
| +| :---: | :---: | :---: | :---: | :---: | :---: | +|[
tj](https://github.com/tj)
|[
aheckmann](https://github.com/aheckmann)
|[
kilianc](https://github.com/kilianc)
|[
secretfader](https://github.com/secretfader)
|[
ilkkao](https://github.com/ilkkao)
|[
HeavenDuke](https://github.com/HeavenDuke)
| +|[
XadillaX](https://github.com/XadillaX)
|[
yiminghe](https://github.com/yiminghe)
|[
semantic-release-bot](https://github.com/semantic-release-bot)
|[
vkhv](https://github.com/vkhv)
|[
vikramdurai](https://github.com/vikramdurai)
|[
Tankenstein](https://github.com/Tankenstein)
| +|[
richardprior](https://github.com/richardprior)
|[
joesonw](https://github.com/joesonw)
|[
ifroz](https://github.com/ifroz)
|[
jeynish](https://github.com/jeynish)
|[
jergason](https://github.com/jergason)
|[
atian25](https://github.com/atian25)
| +|[
lagden](https://github.com/lagden)
|[
fixe](https://github.com/fixe)
|[
viliam-jobko](https://github.com/viliam-jobko)
|[
mzyy94](https://github.com/mzyy94)
|[
jeromew](https://github.com/jeromew)
|[
killagu](https://github.com/killagu)
| +|[
RobertHerhold](https://github.com/RobertHerhold)
|[
yudppp](https://github.com/yudppp)
|[
thedark1337](https://github.com/thedark1337)
|[
x-cold](https://github.com/x-cold)
|[
zzuieliyaoli](https://github.com/zzuieliyaoli)
|[
ryankask](https://github.com/ryankask)
| +|[
pschwyter](https://github.com/pschwyter)
|[
mikefrey](https://github.com/mikefrey)
|[
dizlexik](https://github.com/dizlexik)
|[
jeffijoe](https://github.com/jeffijoe)
|[
iliakan](https://github.com/iliakan)
|[
frederickfogerty](https://github.com/frederickfogerty)
| +[
t3chnoboy](https://github.com/t3chnoboy)
|[
bitinn](https://github.com/bitinn)
|[
drGrove](https://github.com/drGrove)
|[
CreativeCactus](https://github.com/CreativeCactus)
|[
bguiz](https://github.com/bguiz)
+ +This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Sun Jun 16 2024 12:28:11 GMT+0800`. + + diff --git a/package.json b/package.json index e40cb7d..a08f5b6 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@eggjs/router", "version": "3.0.1", "engines": { - "node": ">= 18.7.0" + "node": ">= 18.19.0" }, "publishConfig": { "access": "public" diff --git a/src/Router.ts b/src/Router.ts index 5400b0e..116cc60 100644 --- a/src/Router.ts +++ b/src/Router.ts @@ -601,7 +601,25 @@ export class Router { middlewares: (MiddlewareFunc | string | ResourcesController)[]) { const options: RegisterOptions = {}; let path: string | RegExp | (string | RegExp)[]; - if (typeof pathOrMiddleware === 'string' || pathOrMiddleware instanceof RegExp) { + if (typeof nameOrPath === 'string' && nameOrPath.startsWith('/')) { + // verb(method, path, ...middlewares) + path = nameOrPath; + middlewares = [ pathOrMiddleware as string, ...middlewares ]; + if (typeof pathOrMiddleware === 'string') { + // verb(method, path, controllerString) + // set controller name to router name + options.name = pathOrMiddleware; + } + } else if (nameOrPath instanceof RegExp) { + // verb(method, pathRegex, ...middlewares) + path = nameOrPath; + middlewares = [ pathOrMiddleware as string, ...middlewares ]; + if (typeof pathOrMiddleware === 'string') { + // verb(method, pathRegex, controllerString) + // set controller name to router name + options.name = pathOrMiddleware; + } + } else if (typeof pathOrMiddleware === 'string' || pathOrMiddleware instanceof RegExp) { // verb(method, name, path, ...middlewares) path = pathOrMiddleware; assert(typeof nameOrPath === 'string', 'route name should be string'); diff --git a/test/EggRouter.test.ts b/test/EggRouter.test.ts index 353df64..2680b74 100644 --- a/test/EggRouter.test.ts +++ b/test/EggRouter.test.ts @@ -135,6 +135,63 @@ describe('test/EggRouter.test.ts', () => { assert(router.stack[1].stack.length === 1); }); + it('should app.verb(url, controllerString) work', () => { + const app = { + controller: { + async foo() { return; }, + hello: { + world() { return; }, + }, + }, + }; + + const router = new EggRouter({}, app); + router.get('/foo', 'foo'); + router.post('/hello/world', 'hello.world'); + + assert.equal(router.stack[0].name, 'foo'); + assert.equal(router.stack[0].path, '/foo'); + assert.deepEqual(router.stack[0].methods, [ 'HEAD', 'GET' ]); + assert.equal(router.stack[0].stack.length, 1); + assert.equal(router.stack[1].name, 'hello.world'); + assert.equal(router.stack[1].path, '/hello/world'); + assert.deepEqual(router.stack[1].methods, [ 'POST' ]); + assert.equal(router.stack[1].stack.length, 1); + }); + + it('should app.verb(urlRegex, controllerString) work', () => { + const app = { + controller: { + async foo() { return; }, + hello: { + world() { return; }, + }, + }, + }; + + const router = new EggRouter({}, app); + router.get(/^\/foo/, 'foo'); + router.post(/^\/hello\/world/, 'hello.world'); + router.post(/^\/hello\/world2/, () => {}, 'hello.world'); + + assert.equal(router.stack[0].name, 'foo'); + assert(router.stack[0].path instanceof RegExp); + assert.equal(router.stack[0].path.toString(), String(/^\/foo/)); + assert.deepEqual(router.stack[0].methods, [ 'HEAD', 'GET' ]); + assert.equal(router.stack[0].stack.length, 1); + assert.equal(router.stack[1].name, 'hello.world'); + assert(router.stack[1].path instanceof RegExp); + assert.equal(router.stack[1].path.toString(), String(/^\/hello\/world/)); + assert.deepEqual(router.stack[1].methods, [ 'POST' ]); + assert.equal(router.stack[1].stack.length, 1); + + assert.equal(router.stack[2].name, undefined); + assert(router.stack[2].path instanceof RegExp); + assert.equal(router.stack[2].path.toString(), String(/^\/hello\/world2/)); + assert.deepEqual(router.stack[2].methods, [ 'POST' ]); + assert.equal(router.stack[2].stack.length, 2); + }); + it('should app.verb() throw if not found controller', () => { const app = { controller: {