@ditojs/router
Advanced tools
Comparing version 1.25.0 to 1.25.1
{ | ||
"name": "@ditojs/router", | ||
"version": "1.25.0", | ||
"version": "1.25.1", | ||
"type": "module", | ||
@@ -33,4 +33,4 @@ "description": "Dito.js Router – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js", | ||
], | ||
"gitHead": "29e7f8d8c613c48a856d90d1ee876e85c8d84c08", | ||
"gitHead": "bebbf44ddf15a565e50ed8b2cbe0bf8a463c276d", | ||
"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 compose from 'koa-compose'\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" | ||
} |
@@ -136,3 +136,3 @@ import { getCommonOffset } from '@ditojs/utils' | ||
find(path, paramValues = []) { | ||
find(path, parameterValues = []) { | ||
const { prefix } = this | ||
@@ -143,3 +143,3 @@ if (!path || path === prefix) { | ||
if (handler) { | ||
const params = this.parameters.getObject(paramValues) | ||
const params = this.parameters.getObject(parameterValues) | ||
// Support HTTP status on found entries. | ||
@@ -170,3 +170,3 @@ return { handler, params, status: 200 } | ||
if (staticChild) { | ||
const result = staticChild.find(path, paramValues) | ||
const result = staticChild.find(path, parameterValues) | ||
if (result) { | ||
@@ -191,8 +191,8 @@ return result | ||
} | ||
paramValues.push(path.slice(0, pos)) | ||
const result = paramChild.find(path.slice(pos), paramValues) | ||
parameterValues.push(path.slice(0, pos)) | ||
const result = paramChild.find(path.slice(pos), parameterValues) | ||
if (result) { | ||
return result | ||
} | ||
paramValues.pop() | ||
parameterValues.pop() | ||
} | ||
@@ -203,6 +203,6 @@ | ||
for (const child of this.children) { | ||
const match = child.parameters?.matchPathPattern(path) | ||
if (match) { | ||
paramValues.push(...Object.values(match.groups)) | ||
return child.find('', paramValues) // '' == End | ||
const values = child.parameters?.matchPathPattern(path) | ||
if (values) { | ||
parameterValues.push(...values) | ||
return child.find('', parameterValues) // '' == End | ||
} | ||
@@ -248,4 +248,4 @@ } | ||
add(...keys) { | ||
this.keys.push(...keys) | ||
add(key) { | ||
this.keys.push(key) | ||
} | ||
@@ -266,3 +266,4 @@ | ||
matchPathPattern(path) { | ||
return this.pathPattern?.exec(path) | ||
const match = this.pathPattern?.exec(path) | ||
return match ? Object.values(match.groups) : null | ||
} | ||
@@ -274,3 +275,2 @@ | ||
const pattern = [] | ||
const keys = [] | ||
for (const token of path.split('/')) { | ||
@@ -280,11 +280,11 @@ if (token === '**') { | ||
pattern.push(`(?<${key}>.+?)`) | ||
keys.push(key) | ||
this.add(key) | ||
} else if (token === '*') { | ||
const key = this.getPlaceholderKey() | ||
pattern.push(`(?<${key}>[^/]+)`) | ||
keys.push(key) | ||
this.add(key) | ||
} else if (token.startsWith(':')) { | ||
const key = token.slice(1) | ||
pattern.push(`(?<${key}>[^/]+)`) | ||
keys.push(key) | ||
this.add(key) | ||
} else { | ||
@@ -295,3 +295,2 @@ pattern.push(token) | ||
this.pathPattern = new RegExp(`^${pattern.join('/')}$`) | ||
this.add(...keys) | ||
} | ||
@@ -302,3 +301,3 @@ | ||
// groups back to param names | ||
const params = {} | ||
const object = {} | ||
let i = 0 | ||
@@ -315,6 +314,6 @@ for (const key of this.keys) { | ||
: key | ||
params[name] = values[i++] | ||
object[name] = values[i++] | ||
} | ||
return params | ||
return object | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46887
1044