router-http 0.0.0 → 0.1.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.
Files changed (3) hide show
  1. package/README.md +10 -13
  2. package/package.json +4 -4
  3. package/src/index.js +18 -4
package/README.md CHANGED
@@ -1,15 +1,15 @@
1
- # http-router
1
+ # router-http
2
2
 
3
- ![Last version](https://img.shields.io/github/tag/Kikobeats/http-router.svg?style=flat-square)
4
- [![Coverage Status](https://img.shields.io/coveralls/Kikobeats/http-router.svg?style=flat-square)](https://coveralls.io/github/Kikobeats/http-router)
5
- [![NPM Status](https://img.shields.io/npm/dm/http-router.svg?style=flat-square)](https://www.npmjs.org/package/http-router)
3
+ ![Last version](https://img.shields.io/github/tag/Kikobeats/router-http.svg?style=flat-square)
4
+ [![Coverage Status](https://img.shields.io/coveralls/Kikobeats/router-http.svg?style=flat-square)](https://coveralls.io/github/Kikobeats/router-http)
5
+ [![NPM Status](https://img.shields.io/npm/dm/router-http.svg?style=flat-square)](https://www.npmjs.org/package/router-http)
6
6
 
7
7
  An HTTP router focused in only that, similar to [express@router](https://github.com/pillarjs/router), but:
8
8
 
9
9
  - Focused in just one thing.
10
10
  - Maintained and well tested.
11
- - Most of the API is supported.
12
- - Smaller and portable (less 50kbs).
11
+ - Smaller and portable (1.4 kB).
12
+ - Most of router API is supported.
13
13
 
14
14
  Don't get me wrong: The original Express router is a piece of art. I used it for years and I just considered create this library after experienced a bug that never was addressed in the stable version due to the [lack of maintenance](https://github.com/pillarjs/router/pull/60).
15
15
 
@@ -17,14 +17,11 @@ While I was evaluating the market for finding an alternative I found [polka](htt
17
17
 
18
18
  - This module doesn't take care about the http.Server.
19
19
  - This module doesn't use any of the Node.js built-in module, so it can be used in Vercel Edge Functions, Deno or CF Workers.
20
- - This module doesn't adds `req.query` nor `req.search` (check [to-query](https://github.com/Kikobeats/to-query) for that).
21
-
22
- In resume: this module does nothing beyond finding the correct path and matching the associated code.
23
20
 
24
21
  ## Install
25
22
 
26
23
  ```bash
27
- $ npm install http-router --save
24
+ $ npm install router-http --save
28
25
  ```
29
26
 
30
27
  ## Usage
@@ -32,7 +29,7 @@ $ npm install http-router --save
32
29
  First, you should to create a router:
33
30
 
34
31
  ```js
35
- const createRouter = require('http-router')
32
+ const createRouter = require('router-http')
36
33
 
37
34
  const router = createRouter((err, req, res) => {
38
35
  const hasError = err !== undefined
@@ -142,7 +139,7 @@ See [benchmark](/benchmark) sections.
142
139
 
143
140
  ## License
144
141
 
145
- **http-router** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/http-router/blob/master/LICENSE.md) License.<br>
146
- Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/Kikobeats/http-router/contributors).
142
+ **router-http** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/router-http/blob/master/LICENSE.md) License.<br>
143
+ Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/Kikobeats/router-http/contributors).
147
144
 
148
145
  > [kikobeats.com](https://kikobeats.com) · GitHub [Kiko Beats](https://github.com/Kikobeats) · Twitter [@Kikobeats](https://twitter.com/Kikobeats)
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "router-http",
3
3
  "description": "Simple HTTP router compatible with Express",
4
- "homepage": "https://nicedoc.io/Kikobeats/http-router",
5
- "version": "0.0.0",
4
+ "homepage": "https://nicedoc.io/Kikobeats/router-http",
5
+ "version": "0.1.0",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "josefrancisco.verdu@gmail.com",
@@ -11,10 +11,10 @@
11
11
  },
12
12
  "repository": {
13
13
  "type": "git",
14
- "url": "git+https://github.com/Kikobeats/http-router.git"
14
+ "url": "git+https://github.com/Kikobeats/router-http.git"
15
15
  },
16
16
  "bugs": {
17
- "url": "https://github.com/Kikobeats/http-router/issues"
17
+ "url": "https://github.com/Kikobeats/router-http/issues"
18
18
  },
19
19
  "keywords": [
20
20
  "app",
package/src/index.js CHANGED
@@ -16,6 +16,17 @@ const value = x => {
16
16
  return y > 1 ? x.substring(0, y) : x
17
17
  }
18
18
 
19
+ const parse = ({ url }) => {
20
+ const index = url.indexOf('?', 1)
21
+ const obj = { pathname: url, query: null, search: null }
22
+ if (index !== -1) {
23
+ obj.search = url.substring(index)
24
+ obj.query = obj.search.substring(1)
25
+ obj.pathname = url.substring(0, index)
26
+ }
27
+ return obj
28
+ }
29
+
19
30
  const mutate = (str, req) => {
20
31
  req.url = req.url.substring(str.length) || '/'
21
32
  req.path = req.path.substring(str.length) || '/'
@@ -53,6 +64,7 @@ class Router extends Trouter {
53
64
  base = lead(base)
54
65
  fns.forEach(fn => {
55
66
  const array = this.#middlewaresBy[base] || []
67
+ // eslint-disable-next-line no-sequences
56
68
  array.length > 0 || array.push((r, _, nxt) => (mutate(base, r), nxt()))
57
69
  this.#middlewaresBy[base] = array.concat(fn)
58
70
  })
@@ -60,12 +72,12 @@ class Router extends Trouter {
60
72
  return this
61
73
  }
62
74
 
63
- handler = (req, res, pathname) => {
64
- pathname = pathname || req.url
75
+ handler = (req, res, info) => {
76
+ info = info || parse(req)
65
77
  let fns = []
66
78
  let middlewares = this.#middlewares
67
- const route = this.find(req.method, pathname)
68
- const base = value((req.path = pathname))
79
+ const route = this.find(req.method, info.pathname)
80
+ const base = value((req.path = info.pathname))
69
81
  if (this.#middlewaresBy[base] !== undefined) {
70
82
  middlewares = middlewares.concat(this.#middlewaresBy[base])
71
83
  }
@@ -74,6 +86,8 @@ class Router extends Trouter {
74
86
  req.params = { ...req.params, ...route.params }
75
87
  }
76
88
  fns.push(this.unhandler)
89
+ req.search = info.search
90
+ req.query = info.query
77
91
  // Exit if only a single function
78
92
  let i = 0
79
93
  let len = middlewares.length