0http-bun 1.0.2 → 1.0.4
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/common.d.ts +42 -0
- package/index.d.ts +7 -0
- package/lib/router/sequential.d.ts +3 -0
- package/lib/router/sequential.js +11 -7
- package/package.json +6 -2
package/common.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Pattern, Methods } from 'trouter'
|
|
2
|
+
|
|
3
|
+
export interface IRouterConfig {
|
|
4
|
+
defaultRoute?: RequestHandler
|
|
5
|
+
errorHandler?: (err: Error) => Response | Promise<Response>
|
|
6
|
+
port?: number
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type StepFunction = (error?: unknown) => Response | Promise<Response>
|
|
10
|
+
|
|
11
|
+
type ZeroRequest = Request & {
|
|
12
|
+
params: Record<string, string>
|
|
13
|
+
query: Record<string, string>
|
|
14
|
+
ctx?: Record<string, any>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type RequestHandler = (
|
|
18
|
+
req: ZeroRequest,
|
|
19
|
+
next: StepFunction
|
|
20
|
+
) => Response | Promise<Response>
|
|
21
|
+
|
|
22
|
+
export interface IRouter {
|
|
23
|
+
fetch: (req: Request) => Response | Promise<Response>
|
|
24
|
+
|
|
25
|
+
use(...handlers: RequestHandler[]): this
|
|
26
|
+
use(router: IRouter): this
|
|
27
|
+
use(pattern: Pattern, ...handlers: RequestHandler[]): this
|
|
28
|
+
use(prefix: Pattern, router: IRouter): this
|
|
29
|
+
|
|
30
|
+
on(method: Methods, pattern: Pattern, ...middlewares: RequestHandler[]): this
|
|
31
|
+
|
|
32
|
+
all(pattern: Pattern, ...handlers: RequestHandler[]): this
|
|
33
|
+
get(pattern: Pattern, ...handlers: RequestHandler[]): this
|
|
34
|
+
head(pattern: Pattern, ...handlers: RequestHandler[]): this
|
|
35
|
+
patch(pattern: Pattern, ...handlers: RequestHandler[]): this
|
|
36
|
+
options(pattern: Pattern, ...handlers: RequestHandler[]): this
|
|
37
|
+
connect(pattern: Pattern, ...handlers: RequestHandler[]): this
|
|
38
|
+
delete(pattern: Pattern, ...handlers: RequestHandler[]): this
|
|
39
|
+
trace(pattern: Pattern, ...handlers: RequestHandler[]): this
|
|
40
|
+
post(pattern: Pattern, ...handlers: RequestHandler[]): this
|
|
41
|
+
put(pattern: Pattern, ...handlers: RequestHandler[]): this
|
|
42
|
+
}
|
package/index.d.ts
ADDED
package/lib/router/sequential.js
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
const Trouter = require('trouter')
|
|
2
|
+
const qs = require('fast-querystring')
|
|
2
3
|
const next = require('./../next')
|
|
3
4
|
|
|
4
|
-
const
|
|
5
|
+
const STATUS_404 = {
|
|
5
6
|
status: 404
|
|
6
7
|
}
|
|
7
|
-
const
|
|
8
|
+
const STATUS_500 = {
|
|
8
9
|
status: 500
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
module.exports = (config = {}) => {
|
|
12
13
|
if (!config.defaultRoute) {
|
|
13
14
|
config.defaultRoute = () => {
|
|
14
|
-
return new Response(null,
|
|
15
|
+
return new Response(null, STATUS_404)
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
if (!config.errorHandler) {
|
|
18
19
|
config.errorHandler = (err) => {
|
|
19
|
-
return new Response(err.message,
|
|
20
|
+
return new Response(err.message, STATUS_500)
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
|
|
@@ -36,10 +37,13 @@ module.exports = (config = {}) => {
|
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
router.fetch = (req) => {
|
|
39
|
-
const url =
|
|
40
|
+
const url = req.url
|
|
41
|
+
const startIndex = url.indexOf('/', 11)
|
|
42
|
+
const queryIndex = url.indexOf('?', startIndex + 1)
|
|
43
|
+
const path = queryIndex === -1 ? url.substring(startIndex) : url.substring(startIndex, queryIndex)
|
|
40
44
|
|
|
41
|
-
req.path =
|
|
42
|
-
req.query = url.
|
|
45
|
+
req.path = path || '/'
|
|
46
|
+
req.query = queryIndex > 0 ? qs.parse(url.substring(queryIndex + 1)) : {}
|
|
43
47
|
|
|
44
48
|
const match = router.find(req.method, req.path)
|
|
45
49
|
if (match.handlers.length > 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "0http-bun",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "0http for Bun",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"format": "bun x standard --fix"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
+
"fast-querystring": "^1.1.2",
|
|
11
12
|
"trouter": "^3.2.1"
|
|
12
13
|
},
|
|
13
14
|
"repository": {
|
|
@@ -18,10 +19,13 @@
|
|
|
18
19
|
"LICENSE",
|
|
19
20
|
"README.md",
|
|
20
21
|
"index.js",
|
|
22
|
+
"index.d.ts",
|
|
23
|
+
"common.d.ts",
|
|
21
24
|
"lib/"
|
|
22
25
|
],
|
|
23
26
|
"devDependencies": {
|
|
24
|
-
"0http-bun": "^1.0.
|
|
27
|
+
"0http-bun": "^1.0.3",
|
|
28
|
+
"bun-types": "^1.1.8",
|
|
25
29
|
"mitata": "^0.1.11"
|
|
26
30
|
},
|
|
27
31
|
"keywords": [
|