router-http 0.2.0 → 1.0.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/LICENSE.md +0 -0
- package/README.md +4 -2
- package/package.json +18 -17
- package/src/index.js +12 -12
package/LICENSE.md
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
@@ -29,7 +29,7 @@ const createRouter = require('router-http')
|
|
|
29
29
|
|
|
30
30
|
const router = createRouter((error, req, res) => {
|
|
31
31
|
const hasError = error !== undefined
|
|
32
|
-
res.statusCode = hasError ? error.
|
|
32
|
+
res.statusCode = hasError ? error.statusCode ?? 500 : 404
|
|
33
33
|
res.end(hasError ? error.message ?? 'Internal Server Error' : 'Not Found')
|
|
34
34
|
})
|
|
35
35
|
```
|
|
@@ -118,7 +118,7 @@ routes.get('/', (req, res) => res.end('Welcome to my API!'))
|
|
|
118
118
|
/**
|
|
119
119
|
* Prefix all routes with the API version
|
|
120
120
|
*/
|
|
121
|
-
const router =
|
|
121
|
+
const router = createRouter(final)
|
|
122
122
|
router
|
|
123
123
|
.use('/latest', routes)
|
|
124
124
|
.use('/v1', routes)
|
|
@@ -170,6 +170,8 @@ See more details, check [benchmark](/benchmark) section.
|
|
|
170
170
|
|
|
171
171
|
## License
|
|
172
172
|
|
|
173
|
+
Full credits to [Luke Edwards](https://github.com/lukeed) for writing [Polka](https://github.com/lukeed/polka) and inspired this project.
|
|
174
|
+
|
|
173
175
|
**router-http** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/router-http/blob/master/LICENSE.md) License.<br>
|
|
174
176
|
Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/Kikobeats/router-http/contributors).
|
|
175
177
|
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "router-http",
|
|
3
3
|
"description": "Simple HTTP router compatible with Express",
|
|
4
4
|
"homepage": "https://nicedoc.io/Kikobeats/router-http",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "1.0.0",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "josefrancisco.verdu@gmail.com",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"@commitlint/config-conventional": "latest",
|
|
36
36
|
"ava": "latest",
|
|
37
37
|
"c8": "latest",
|
|
38
|
+
"ci-publish": "latest",
|
|
38
39
|
"conventional-github-releaser": "latest",
|
|
39
40
|
"finepack": "latest",
|
|
40
41
|
"git-authors-cli": "latest",
|
|
@@ -53,6 +54,21 @@
|
|
|
53
54
|
"files": [
|
|
54
55
|
"src"
|
|
55
56
|
],
|
|
57
|
+
"scripts": {
|
|
58
|
+
"clean": "rm -rf node_modules",
|
|
59
|
+
"contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
|
|
60
|
+
"coverage": "c8 report --reporter=text-lcov > coverage/lcov.info",
|
|
61
|
+
"lint": "standard-markdown README.md && standard",
|
|
62
|
+
"postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
|
|
63
|
+
"prerelease": "npm run update:check && npm run contributors",
|
|
64
|
+
"pretest": "npm run lint",
|
|
65
|
+
"release": "standard-version -a",
|
|
66
|
+
"release:github": "conventional-github-releaser -p angular",
|
|
67
|
+
"release:tags": "git push --follow-tags origin HEAD:master",
|
|
68
|
+
"test": "c8 ava",
|
|
69
|
+
"update": "ncu -u",
|
|
70
|
+
"update:check": "ncu -- --error-level 2"
|
|
71
|
+
},
|
|
56
72
|
"license": "MIT",
|
|
57
73
|
"commitlint": {
|
|
58
74
|
"extends": [
|
|
@@ -73,20 +89,5 @@
|
|
|
73
89
|
"simple-git-hooks": {
|
|
74
90
|
"commit-msg": "npx commitlint --edit",
|
|
75
91
|
"pre-commit": "npx nano-staged"
|
|
76
|
-
},
|
|
77
|
-
"scripts": {
|
|
78
|
-
"clean": "rm -rf node_modules",
|
|
79
|
-
"contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
|
|
80
|
-
"coverage": "c8 report --reporter=text-lcov > coverage/lcov.info",
|
|
81
|
-
"lint": "standard-markdown README.md && standard",
|
|
82
|
-
"postrelease": "npm run release:tags && npm run release:github && npm publish",
|
|
83
|
-
"prerelease": "npm run update:check",
|
|
84
|
-
"pretest": "npm run lint",
|
|
85
|
-
"release": "standard-version -a",
|
|
86
|
-
"release:github": "conventional-github-releaser -p angular",
|
|
87
|
-
"release:tags": "git push --follow-tags origin HEAD:master",
|
|
88
|
-
"test": "c8 ava",
|
|
89
|
-
"update": "ncu -u",
|
|
90
|
-
"update:check": "ncu -- --error-level 2"
|
|
91
92
|
}
|
|
92
|
-
}
|
|
93
|
+
}
|
package/src/index.js
CHANGED
|
@@ -49,24 +49,24 @@ class Router extends Trouter {
|
|
|
49
49
|
#middlewaresBy = []
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
|
-
* Middleware declaration, where the
|
|
52
|
+
* Middleware declaration, where the page is optional
|
|
53
53
|
* .use(one)
|
|
54
54
|
* .use('/v1', one)
|
|
55
55
|
* .use(one, two)
|
|
56
56
|
* .use('/v2', two)
|
|
57
57
|
*/
|
|
58
|
-
use = (
|
|
59
|
-
if (typeof
|
|
60
|
-
this.#middlewares = this.#middlewares.concat(
|
|
61
|
-
} else if (
|
|
58
|
+
use = (page = '/', ...fns) => {
|
|
59
|
+
if (typeof page === 'function' || typeof page === 'boolean') {
|
|
60
|
+
this.#middlewares = this.#middlewares.concat(page, fns).filter(Boolean)
|
|
61
|
+
} else if (page === '/') {
|
|
62
62
|
this.#middlewares = this.#middlewares.concat(fns).filter(Boolean)
|
|
63
63
|
} else {
|
|
64
|
-
|
|
64
|
+
page = lead(page)
|
|
65
65
|
fns.filter(Boolean).forEach(fn => {
|
|
66
|
-
const array = this.#middlewaresBy[
|
|
66
|
+
const array = this.#middlewaresBy[page] ?? []
|
|
67
67
|
// eslint-disable-next-line no-sequences
|
|
68
|
-
array.length > 0 || array.push((r, _, nxt) => (mutate(
|
|
69
|
-
this.#middlewaresBy[
|
|
68
|
+
array.length > 0 || array.push((r, _, nxt) => (mutate(page, r), nxt()))
|
|
69
|
+
this.#middlewaresBy[page] = array.concat(fn)
|
|
70
70
|
})
|
|
71
71
|
}
|
|
72
72
|
return this
|
|
@@ -77,9 +77,9 @@ class Router extends Trouter {
|
|
|
77
77
|
let fns = []
|
|
78
78
|
let middlewares = this.#middlewares
|
|
79
79
|
const route = this.find(req.method, info.pathname)
|
|
80
|
-
const
|
|
81
|
-
if (this.#middlewaresBy[
|
|
82
|
-
middlewares = middlewares.concat(this.#middlewaresBy[
|
|
80
|
+
const page = value((req.path = info.pathname))
|
|
81
|
+
if (this.#middlewaresBy[page] !== undefined) {
|
|
82
|
+
middlewares = middlewares.concat(this.#middlewaresBy[page])
|
|
83
83
|
}
|
|
84
84
|
if (route) {
|
|
85
85
|
fns = route.handlers
|