@ditojs/router 2.28.0 → 2.29.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.
- package/package.json +3 -3
- package/src/Node.js +5 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ditojs/router",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.29.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Dito.js Router – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js",
|
|
6
6
|
"repository": "https://github.com/ditojs/dito/tree/master/packages/router",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"node >= 18"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@ditojs/utils": "^2.
|
|
24
|
+
"@ditojs/utils": "^2.29.0"
|
|
25
25
|
},
|
|
26
26
|
"keywords": [
|
|
27
27
|
"route",
|
|
@@ -30,6 +30,6 @@
|
|
|
30
30
|
"koa",
|
|
31
31
|
"dito"
|
|
32
32
|
],
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "bdf4f66c3d6232f2322dacb088a0611f1e07dc7b",
|
|
34
34
|
"readme": "# Dito.js Router\n\nDito.js is a declarative and modern web framework with a focus on API driven\ndevelopment, based on Koa.js, Objection.js and Vue.js\n\nReleased in 2018 under the MIT license, with support by https://lineto.com/\n\nDito.js Router is a high performance, tree-based, framework agnostic HTTP\nrouter, based on [trek-router](https://github.com/trekjs/router), which in turn\nis inspired by [Echo](https://github.com/labstack/echo)'s Router.\n\n## How does it work?\n\nThe router relies on a tree structure which makes heavy use of common\nprefixes, essentially a [prefix tree](https://en.wikipedia.org/wiki/Trie).\n\n## Usage\n\n```js\nimport Koa from 'koa'\nimport Router from '@ditojs/router'\n\nconst app = new Koa()\nconst router = new Router()\n\n// static route\nrouter.get('/folders/files/bolt.gif', ctx => {\n ctx.body = `this ain't no GIF!`\n})\n\n// param route\nrouter.get('/users/:id', ctx => {\n ctx.body = `requesting user ${ctx.params.id}`\n})\n\n// match-any route\nrouter.get('/books/*', ctx => {\n ctx.body = `sub-route: ${ctx.params['*']}`\n})\n\n// Handler found\nlet { handler, params } = router.find('get', '/users/233')\nconsole.log(handler) // ctx => { ... }\n\n// Entry not Found\nlet { handler, params } = router.find('get', '/photos/233')\nconsole.log(handler) // null\n\n// Install router middleware\napp.use(async (ctx, next) => {\n const { method, path } = ctx\n const result = router.find(method, path)\n const { handler, params } = result\n if (handler) {\n ctx.params = params || {}\n return handler(ctx, next)\n } else {\n try {\n await next()\n } finally {\n if (ctx.body === undefined && ctx.status === 404) {\n ctx.status = result.status || 404\n if (ctx.status !== 404 && result.allowed) {\n ctx.set('Allow', result.allowed.join(', '))\n }\n }\n }\n }\n})\n\napp.listen(4040, () => console.log('Koa app listening on 4040'))\n```\n"
|
|
35
35
|
}
|
package/src/Node.js
CHANGED
|
@@ -67,7 +67,7 @@ export default class Node {
|
|
|
67
67
|
const start = pos
|
|
68
68
|
// Move pos to the next occurrence of the slash or the end:
|
|
69
69
|
pos += path.slice(pos).match(/^([^/]*)/)[1].length
|
|
70
|
-
parameters.
|
|
70
|
+
parameters.addKey(
|
|
71
71
|
isWildcard ? parameters.getPlaceholderKey() : path.slice(start, pos)
|
|
72
72
|
)
|
|
73
73
|
// Chop out param name from path, but keep colon.
|
|
@@ -242,7 +242,7 @@ class Parameters {
|
|
|
242
242
|
this.placeholderIndex = 0
|
|
243
243
|
}
|
|
244
244
|
|
|
245
|
-
|
|
245
|
+
addKey(key) {
|
|
246
246
|
this.keys.push(key)
|
|
247
247
|
}
|
|
248
248
|
|
|
@@ -271,15 +271,15 @@ class Parameters {
|
|
|
271
271
|
if (token === '**') {
|
|
272
272
|
const key = this.getMatchAnyKey()
|
|
273
273
|
pattern.push(`(?<${key}>.+?)`)
|
|
274
|
-
this.
|
|
274
|
+
this.addKey(key)
|
|
275
275
|
} else if (token === '*') {
|
|
276
276
|
const key = this.getPlaceholderKey()
|
|
277
277
|
pattern.push(`(?<${key}>[^/]+)`)
|
|
278
|
-
this.
|
|
278
|
+
this.addKey(key)
|
|
279
279
|
} else if (token.startsWith(':')) {
|
|
280
280
|
const key = token.slice(1)
|
|
281
281
|
pattern.push(`(?<${key}>[^/]+)`)
|
|
282
|
-
this.
|
|
282
|
+
this.addKey(key)
|
|
283
283
|
} else {
|
|
284
284
|
pattern.push(token)
|
|
285
285
|
}
|