hono 0.0.10 → 0.0.14
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/README.md +212 -75
- package/dist/compose.d.ts +1 -0
- package/dist/compose.js +27 -0
- package/dist/context.d.ts +23 -0
- package/dist/context.js +65 -0
- package/dist/hono.d.ts +44 -0
- package/dist/hono.js +147 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +9 -0
- package/dist/middleware/basic-auth/basic-auth.d.ts +6 -0
- package/dist/middleware/basic-auth/basic-auth.js +50 -0
- package/dist/middleware/body-parse/body-parse.d.ts +2 -0
- package/dist/middleware/body-parse/body-parse.js +27 -0
- package/dist/middleware/default.d.ts +2 -0
- package/dist/middleware/default.js +16 -0
- package/dist/middleware/logger/logger.d.ts +6 -0
- package/dist/middleware/logger/logger.js +58 -0
- package/dist/middleware/powered-by/powered-by.d.ts +2 -0
- package/dist/middleware/powered-by/powered-by.js +11 -0
- package/dist/middleware.d.ts +15 -0
- package/dist/middleware.js +16 -0
- package/dist/node.d.ts +24 -0
- package/dist/node.js +104 -0
- package/dist/util.d.ts +5 -0
- package/dist/util.js +72 -0
- package/package.json +34 -5
- package/CODE_OF_CONDUCT.md +0 -128
- package/src/compose.js +0 -21
- package/src/compose.test.js +0 -42
- package/src/hono.d.ts +0 -67
- package/src/hono.js +0 -141
- package/src/hono.test.js +0 -158
- package/src/methods.js +0 -30
- package/src/middleware/defaultFilter.js +0 -19
- package/src/middleware/poweredBy.js +0 -6
- package/src/middleware/poweredBy.test.js +0 -17
- package/src/middleware.js +0 -9
- package/src/middleware.test.js +0 -17
- package/src/node.js +0 -97
- package/src/node.test.js +0 -135
- package/src/router.test.js +0 -88
- package/src/util.js +0 -33
- package/src/util.test.js +0 -44
package/CODE_OF_CONDUCT.md
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
# Contributor Covenant Code of Conduct
|
|
2
|
-
|
|
3
|
-
## Our Pledge
|
|
4
|
-
|
|
5
|
-
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
-
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
-
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
-
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
-
nationality, personal appearance, race, religion, or sexual identity
|
|
10
|
-
and orientation.
|
|
11
|
-
|
|
12
|
-
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
-
diverse, inclusive, and healthy community.
|
|
14
|
-
|
|
15
|
-
## Our Standards
|
|
16
|
-
|
|
17
|
-
Examples of behavior that contributes to a positive environment for our
|
|
18
|
-
community include:
|
|
19
|
-
|
|
20
|
-
* Demonstrating empathy and kindness toward other people
|
|
21
|
-
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
-
* Giving and gracefully accepting constructive feedback
|
|
23
|
-
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
-
and learning from the experience
|
|
25
|
-
* Focusing on what is best not just for us as individuals, but for the
|
|
26
|
-
overall community
|
|
27
|
-
|
|
28
|
-
Examples of unacceptable behavior include:
|
|
29
|
-
|
|
30
|
-
* The use of sexualized language or imagery, and sexual attention or
|
|
31
|
-
advances of any kind
|
|
32
|
-
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
-
* Public or private harassment
|
|
34
|
-
* Publishing others' private information, such as a physical or email
|
|
35
|
-
address, without their explicit permission
|
|
36
|
-
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
-
professional setting
|
|
38
|
-
|
|
39
|
-
## Enforcement Responsibilities
|
|
40
|
-
|
|
41
|
-
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
-
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
-
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
-
or harmful.
|
|
45
|
-
|
|
46
|
-
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
-
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
-
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
-
decisions when appropriate.
|
|
50
|
-
|
|
51
|
-
## Scope
|
|
52
|
-
|
|
53
|
-
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
-
an individual is officially representing the community in public spaces.
|
|
55
|
-
Examples of representing our community include using an official e-mail address,
|
|
56
|
-
posting via an official social media account, or acting as an appointed
|
|
57
|
-
representative at an online or offline event.
|
|
58
|
-
|
|
59
|
-
## Enforcement
|
|
60
|
-
|
|
61
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
-
reported to the community leaders responsible for enforcement at
|
|
63
|
-
yusuke@kamawada.com.
|
|
64
|
-
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
-
|
|
66
|
-
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
-
reporter of any incident.
|
|
68
|
-
|
|
69
|
-
## Enforcement Guidelines
|
|
70
|
-
|
|
71
|
-
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
-
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
-
|
|
74
|
-
### 1. Correction
|
|
75
|
-
|
|
76
|
-
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
-
unprofessional or unwelcome in the community.
|
|
78
|
-
|
|
79
|
-
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
-
clarity around the nature of the violation and an explanation of why the
|
|
81
|
-
behavior was inappropriate. A public apology may be requested.
|
|
82
|
-
|
|
83
|
-
### 2. Warning
|
|
84
|
-
|
|
85
|
-
**Community Impact**: A violation through a single incident or series
|
|
86
|
-
of actions.
|
|
87
|
-
|
|
88
|
-
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
-
interaction with the people involved, including unsolicited interaction with
|
|
90
|
-
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
-
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
-
like social media. Violating these terms may lead to a temporary or
|
|
93
|
-
permanent ban.
|
|
94
|
-
|
|
95
|
-
### 3. Temporary Ban
|
|
96
|
-
|
|
97
|
-
**Community Impact**: A serious violation of community standards, including
|
|
98
|
-
sustained inappropriate behavior.
|
|
99
|
-
|
|
100
|
-
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
-
communication with the community for a specified period of time. No public or
|
|
102
|
-
private interaction with the people involved, including unsolicited interaction
|
|
103
|
-
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
-
Violating these terms may lead to a permanent ban.
|
|
105
|
-
|
|
106
|
-
### 4. Permanent Ban
|
|
107
|
-
|
|
108
|
-
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
-
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
-
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
-
|
|
112
|
-
**Consequence**: A permanent ban from any sort of public interaction within
|
|
113
|
-
the community.
|
|
114
|
-
|
|
115
|
-
## Attribution
|
|
116
|
-
|
|
117
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
-
version 2.0, available at
|
|
119
|
-
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
|
120
|
-
|
|
121
|
-
Community Impact Guidelines were inspired by [Mozilla's code of conduct
|
|
122
|
-
enforcement ladder](https://github.com/mozilla/diversity).
|
|
123
|
-
|
|
124
|
-
[homepage]: https://www.contributor-covenant.org
|
|
125
|
-
|
|
126
|
-
For answers to common questions about this code of conduct, see the FAQ at
|
|
127
|
-
https://www.contributor-covenant.org/faq. Translations are available at
|
|
128
|
-
https://www.contributor-covenant.org/translations.
|
package/src/compose.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// Based on the code in the MIT licensed `koa-compose` package.
|
|
2
|
-
const compose = (middleware) => {
|
|
3
|
-
return function (context, next) {
|
|
4
|
-
let index = -1
|
|
5
|
-
return dispatch(0)
|
|
6
|
-
function dispatch(i) {
|
|
7
|
-
if (i <= index) return Promise.reject(new Error('next() called multiple times'))
|
|
8
|
-
index = i
|
|
9
|
-
let fn = middleware[i]
|
|
10
|
-
if (i === middleware.length) fn = next
|
|
11
|
-
if (!fn) return Promise.resolve()
|
|
12
|
-
try {
|
|
13
|
-
return Promise.resolve(fn(context, dispatch.bind(null, i + 1)))
|
|
14
|
-
} catch (err) {
|
|
15
|
-
return Promise.reject(err)
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
module.exports = compose
|
package/src/compose.test.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
const compose = require('./compose')
|
|
2
|
-
|
|
3
|
-
describe('compose middleware', () => {
|
|
4
|
-
const middleware = []
|
|
5
|
-
|
|
6
|
-
const a = async (c, next) => {
|
|
7
|
-
c.req['log'] = 'log'
|
|
8
|
-
await next()
|
|
9
|
-
}
|
|
10
|
-
middleware.push(a)
|
|
11
|
-
|
|
12
|
-
const b = async (c, next) => {
|
|
13
|
-
await next()
|
|
14
|
-
c.res['header'] = `${c.res.header}-custom-header`
|
|
15
|
-
}
|
|
16
|
-
middleware.push(b)
|
|
17
|
-
|
|
18
|
-
const handler = async (c, next) => {
|
|
19
|
-
c.req['log'] = `${c.req.log} message`
|
|
20
|
-
await next()
|
|
21
|
-
c.res = { message: 'new response' }
|
|
22
|
-
}
|
|
23
|
-
middleware.push(handler)
|
|
24
|
-
|
|
25
|
-
const request = {}
|
|
26
|
-
const response = {}
|
|
27
|
-
|
|
28
|
-
it('Request', async () => {
|
|
29
|
-
const c = { req: request, res: response }
|
|
30
|
-
const composed = compose(middleware)
|
|
31
|
-
await composed(c)
|
|
32
|
-
expect(c.req['log']).not.toBeNull()
|
|
33
|
-
expect(c.req['log']).toBe('log message')
|
|
34
|
-
})
|
|
35
|
-
it('Response', async () => {
|
|
36
|
-
const c = { req: request, res: response }
|
|
37
|
-
const composed = compose(middleware)
|
|
38
|
-
await composed(c)
|
|
39
|
-
expect(c.res['header']).not.toBeNull()
|
|
40
|
-
expect(c.res['message']).toBe('new response')
|
|
41
|
-
})
|
|
42
|
-
})
|
package/src/hono.d.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
type Result = {
|
|
2
|
-
handler: any
|
|
3
|
-
params: {}
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
declare class Node {
|
|
7
|
-
method: string
|
|
8
|
-
handler: any
|
|
9
|
-
children: Node[]
|
|
10
|
-
middlewares: any[]
|
|
11
|
-
|
|
12
|
-
insert(method: string, path: string, handler: any): Node
|
|
13
|
-
search(method: string, path: string): Result
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
declare class Context {
|
|
17
|
-
req: Request
|
|
18
|
-
res: Response
|
|
19
|
-
newResponse(params: {}): Response
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
type Handler = (c: Context, next: () => void) => Response | void
|
|
23
|
-
|
|
24
|
-
declare class Router {
|
|
25
|
-
tempPath: string
|
|
26
|
-
node: Node
|
|
27
|
-
|
|
28
|
-
add(method: string, path: string, handler: Handler): Node
|
|
29
|
-
match(method: string, path: string): Node
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class Hono {
|
|
33
|
-
router: Router
|
|
34
|
-
middlewareRouters: Router[]
|
|
35
|
-
|
|
36
|
-
use(path: string, middleware: Handler): void
|
|
37
|
-
|
|
38
|
-
route(path: string): Hono
|
|
39
|
-
fire(): void
|
|
40
|
-
|
|
41
|
-
all(path: string, handler: Handler): Hono
|
|
42
|
-
get(path: string, handler: Handler): Hono
|
|
43
|
-
post(path: string, handler: Handler): Hono
|
|
44
|
-
put(path: string, handler: Handler): Hono
|
|
45
|
-
head(path: string, handler: Handler): Hono
|
|
46
|
-
delete(path: string, handler: Handler): Hono
|
|
47
|
-
|
|
48
|
-
notFound(): Response
|
|
49
|
-
|
|
50
|
-
getRouter(): Router
|
|
51
|
-
addRoute(method: string, args: any[]): Hono
|
|
52
|
-
matchRoute(method: string, path: string): Promise<Node>
|
|
53
|
-
createContext(req: Request, res: Response): Promise<Context>
|
|
54
|
-
dispatch(req: Request, res: Response): Promise<Response>
|
|
55
|
-
handleEvent(event: FetchEvent): Promise<Response>
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export class Middleware {
|
|
59
|
-
static defaultFilter: Handler
|
|
60
|
-
// Add builtin middlewares
|
|
61
|
-
static poweredBy: Handler
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
interface FetchEvent extends Event {
|
|
65
|
-
request: Request
|
|
66
|
-
respondWith(response: Promise<Response> | Response): Promise<Response>
|
|
67
|
-
}
|
package/src/hono.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const Node = require('./node')
|
|
4
|
-
const Middleware = require('./middleware')
|
|
5
|
-
const compose = require('./compose')
|
|
6
|
-
const methods = require('./methods')
|
|
7
|
-
const { getPathFromURL } = require('./util')
|
|
8
|
-
|
|
9
|
-
const METHOD_NAME_OF_ALL = 'ALL'
|
|
10
|
-
|
|
11
|
-
class Router {
|
|
12
|
-
constructor() {
|
|
13
|
-
this.node = new Node()
|
|
14
|
-
this.tempPath = '/'
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
add(method, path, ...handlers) {
|
|
18
|
-
this.node.insert(method, path, handlers)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
match(method, path) {
|
|
22
|
-
return this.node.search(method, path)
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
class Hono {
|
|
27
|
-
constructor() {
|
|
28
|
-
this.router = new Router()
|
|
29
|
-
this.middlewareRouters = []
|
|
30
|
-
|
|
31
|
-
for (const method of methods) {
|
|
32
|
-
this[method] = (...args) => {
|
|
33
|
-
return this.addRoute(method, ...args)
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
all(...args) {
|
|
39
|
-
this.addRoute('ALL', ...args)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
getRouter() {
|
|
43
|
-
return this.router
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
addRoute(method, ...args) {
|
|
47
|
-
method = method.toUpperCase()
|
|
48
|
-
if (args.length === 1) {
|
|
49
|
-
this.router.add(method, this.router.tempPath, ...args)
|
|
50
|
-
} else {
|
|
51
|
-
this.router.add(method, ...args)
|
|
52
|
-
}
|
|
53
|
-
return this
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
route(path) {
|
|
57
|
-
this.router.tempPath = path
|
|
58
|
-
return this
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
use(path, middleware) {
|
|
62
|
-
if (middleware.constructor.name !== 'AsyncFunction') {
|
|
63
|
-
throw new TypeError('middleware must be a async function!')
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const router = new Router()
|
|
67
|
-
router.add(METHOD_NAME_OF_ALL, path, middleware)
|
|
68
|
-
this.middlewareRouters.push(router)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async matchRoute(method, path) {
|
|
72
|
-
return this.router.match(method, path)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// XXX
|
|
76
|
-
async createContext(req, res) {
|
|
77
|
-
return {
|
|
78
|
-
req: req,
|
|
79
|
-
res: res,
|
|
80
|
-
newResponse: (params) => {
|
|
81
|
-
return new Response(params)
|
|
82
|
-
},
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async dispatch(request, response) {
|
|
87
|
-
const [method, path] = [request.method, getPathFromURL(request.url)]
|
|
88
|
-
|
|
89
|
-
const result = await this.matchRoute(method, path)
|
|
90
|
-
|
|
91
|
-
request.params = (key) => result.params[key]
|
|
92
|
-
|
|
93
|
-
let handler = result ? result.handler[0] : this.notFound // XXX
|
|
94
|
-
|
|
95
|
-
const middleware = []
|
|
96
|
-
|
|
97
|
-
for (const mr of this.middlewareRouters) {
|
|
98
|
-
const mwResult = mr.match(METHOD_NAME_OF_ALL, path)
|
|
99
|
-
if (mwResult) {
|
|
100
|
-
middleware.push(...mwResult.handler)
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
let wrappedHandler = async (context, next) => {
|
|
105
|
-
context.res = await handler(context)
|
|
106
|
-
await next()
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
middleware.push(Middleware.defaultFilter)
|
|
110
|
-
middleware.push(wrappedHandler)
|
|
111
|
-
|
|
112
|
-
const composed = compose(middleware)
|
|
113
|
-
const c = await this.createContext(request, response)
|
|
114
|
-
|
|
115
|
-
await composed(c)
|
|
116
|
-
|
|
117
|
-
return c.res
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async handleEvent(event) {
|
|
121
|
-
return this.dispatch(event.request, {}) // XXX
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
fire() {
|
|
125
|
-
addEventListener('fetch', (event) => {
|
|
126
|
-
event.respondWith(this.handleEvent(event))
|
|
127
|
-
})
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
notFound() {
|
|
131
|
-
return new Response('Not Found', { status: 404 })
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Default Export
|
|
136
|
-
module.exports = Hono
|
|
137
|
-
exports = module.exports
|
|
138
|
-
|
|
139
|
-
// Named Export
|
|
140
|
-
exports.Hono = Hono
|
|
141
|
-
exports.Middleware = Middleware
|
package/src/hono.test.js
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
const fetch = require('node-fetch')
|
|
2
|
-
const { Hono } = require('./hono')
|
|
3
|
-
|
|
4
|
-
describe('GET Request', () => {
|
|
5
|
-
const app = new Hono()
|
|
6
|
-
|
|
7
|
-
app.get('/hello', () => {
|
|
8
|
-
return new fetch.Response('hello', {
|
|
9
|
-
status: 200,
|
|
10
|
-
})
|
|
11
|
-
})
|
|
12
|
-
app.notFound = () => {
|
|
13
|
-
return new fetch.Response('not found', {
|
|
14
|
-
status: 404,
|
|
15
|
-
})
|
|
16
|
-
}
|
|
17
|
-
it('GET /hello is ok', async () => {
|
|
18
|
-
let req = new fetch.Request('https://example.com/hello')
|
|
19
|
-
let res = await app.dispatch(req, new fetch.Response())
|
|
20
|
-
expect(res).not.toBeNull()
|
|
21
|
-
expect(res.status).toBe(200)
|
|
22
|
-
expect(await res.text()).toBe('hello')
|
|
23
|
-
})
|
|
24
|
-
it('GET / is not found', async () => {
|
|
25
|
-
let req = new fetch.Request('https://example.com/')
|
|
26
|
-
let res = await app.dispatch(req, new fetch.Response())
|
|
27
|
-
expect(res).not.toBeNull()
|
|
28
|
-
expect(res.status).toBe(404)
|
|
29
|
-
})
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
describe('params and query', () => {
|
|
33
|
-
const app = new Hono()
|
|
34
|
-
|
|
35
|
-
app.get('/entry/:id', async (c) => {
|
|
36
|
-
const id = await c.req.params('id')
|
|
37
|
-
return new fetch.Response(`id is ${id}`, {
|
|
38
|
-
status: 200,
|
|
39
|
-
})
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
app.get('/search', async (c) => {
|
|
43
|
-
const name = await c.req.query('name')
|
|
44
|
-
return new fetch.Response(`name is ${name}`, {
|
|
45
|
-
status: 200,
|
|
46
|
-
})
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('params of /entry/:id is found', async () => {
|
|
50
|
-
let req = new fetch.Request('https://example.com/entry/123')
|
|
51
|
-
let res = await app.dispatch(req)
|
|
52
|
-
expect(res.status).toBe(200)
|
|
53
|
-
expect(await res.text()).toBe('id is 123')
|
|
54
|
-
})
|
|
55
|
-
it('query of /search?name=sam is found', async () => {
|
|
56
|
-
let req = new fetch.Request('https://example.com/search?name=sam')
|
|
57
|
-
let res = await app.dispatch(req)
|
|
58
|
-
expect(res.status).toBe(200)
|
|
59
|
-
expect(await res.text()).toBe('name is sam')
|
|
60
|
-
})
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
describe('Middleware', () => {
|
|
64
|
-
const app = new Hono()
|
|
65
|
-
|
|
66
|
-
const logger = async (c, next) => {
|
|
67
|
-
console.log(`${c.req.method} : ${c.req.url}`)
|
|
68
|
-
await next()
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const rootHeader = async (c, next) => {
|
|
72
|
-
await next()
|
|
73
|
-
await c.res.headers.append('x-custom', 'root')
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const customHeader = async (c, next) => {
|
|
77
|
-
await next()
|
|
78
|
-
await c.res.headers.append('x-message', 'custom-header')
|
|
79
|
-
}
|
|
80
|
-
const customHeader2 = async (c, next) => {
|
|
81
|
-
await next()
|
|
82
|
-
c.res.headers.append('x-message-2', 'custom-header-2')
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
app.use('*', logger)
|
|
86
|
-
app.use('*', rootHeader)
|
|
87
|
-
app.use('/hello', customHeader)
|
|
88
|
-
app.use('/hello/*', customHeader2)
|
|
89
|
-
|
|
90
|
-
app.get('/hello', () => {
|
|
91
|
-
return new fetch.Response('hello')
|
|
92
|
-
})
|
|
93
|
-
app.get('/hello/:message', (c) => {
|
|
94
|
-
const message = c.req.params('message')
|
|
95
|
-
return new fetch.Response(`${message}`)
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
it('logging and custom header', async () => {
|
|
99
|
-
let req = new fetch.Request('https://example.com/hello')
|
|
100
|
-
let res = await app.dispatch(req)
|
|
101
|
-
expect(res.status).toBe(200)
|
|
102
|
-
expect(await res.text()).toBe('hello')
|
|
103
|
-
expect(await res.headers.get('x-custom')).toBe('root')
|
|
104
|
-
expect(await res.headers.get('x-message')).toBe('custom-header')
|
|
105
|
-
expect(await res.headers.get('x-message-2')).toBe('custom-header-2')
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
it('logging and custom header with named params', async () => {
|
|
109
|
-
let req = new fetch.Request('https://example.com/hello/message')
|
|
110
|
-
let res = await app.dispatch(req)
|
|
111
|
-
expect(res.status).toBe(200)
|
|
112
|
-
expect(await res.text()).toBe('message')
|
|
113
|
-
expect(await res.headers.get('x-custom')).toBe('root')
|
|
114
|
-
expect(await res.headers.get('x-message-2')).toBe('custom-header-2')
|
|
115
|
-
})
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
describe('Custom 404', () => {
|
|
119
|
-
const app = new Hono()
|
|
120
|
-
|
|
121
|
-
const customNotFound = async (c, next) => {
|
|
122
|
-
await next()
|
|
123
|
-
if (c.res.status === 404) {
|
|
124
|
-
c.res = new fetch.Response('Custom 404 Not Found', { status: 404 })
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
app.notFound = () => {
|
|
129
|
-
return new fetch.Response('Default 404 Nout Found', { status: 404 })
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
app.use('*', customNotFound)
|
|
133
|
-
app.get('/hello', () => {
|
|
134
|
-
return new fetch.Response('hello')
|
|
135
|
-
})
|
|
136
|
-
it('Custom 404 Not Found', async () => {
|
|
137
|
-
let req = new fetch.Request('https://example.com/hello')
|
|
138
|
-
let res = await app.dispatch(req)
|
|
139
|
-
expect(res.status).toBe(200)
|
|
140
|
-
req = new fetch.Request('https://example.com/foo')
|
|
141
|
-
res = await app.dispatch(req)
|
|
142
|
-
expect(res.status).toBe(404)
|
|
143
|
-
expect(await res.text()).toBe('Custom 404 Not Found')
|
|
144
|
-
})
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
describe('Error Handling', () => {
|
|
148
|
-
const app = new Hono()
|
|
149
|
-
|
|
150
|
-
it('Middleware must be async function', () => {
|
|
151
|
-
expect(() => {
|
|
152
|
-
app.use('*', {})
|
|
153
|
-
}).toThrow(TypeError)
|
|
154
|
-
expect(() => {
|
|
155
|
-
app.use('*', () => '')
|
|
156
|
-
}).toThrow(TypeError)
|
|
157
|
-
})
|
|
158
|
-
})
|
package/src/methods.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
const methods = [
|
|
2
|
-
'get',
|
|
3
|
-
'post',
|
|
4
|
-
'put',
|
|
5
|
-
'head',
|
|
6
|
-
'delete',
|
|
7
|
-
'options',
|
|
8
|
-
'trace',
|
|
9
|
-
'copy',
|
|
10
|
-
'lock',
|
|
11
|
-
'mkcol',
|
|
12
|
-
'move',
|
|
13
|
-
'patch',
|
|
14
|
-
'purge',
|
|
15
|
-
'propfind',
|
|
16
|
-
'proppatch',
|
|
17
|
-
'unlock',
|
|
18
|
-
'report',
|
|
19
|
-
'mkactivity',
|
|
20
|
-
'checkout',
|
|
21
|
-
'merge',
|
|
22
|
-
'm-search',
|
|
23
|
-
'notify',
|
|
24
|
-
'subscribe',
|
|
25
|
-
'unsubscribe',
|
|
26
|
-
'search',
|
|
27
|
-
'connect',
|
|
28
|
-
]
|
|
29
|
-
|
|
30
|
-
module.exports = methods
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
const defaultFilter = async (c, next) => {
|
|
2
|
-
c.req.query = (key) => {
|
|
3
|
-
const url = new URL(c.req.url)
|
|
4
|
-
return url.searchParams.get(key)
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
await next()
|
|
8
|
-
|
|
9
|
-
if (typeof c.res === 'string') {
|
|
10
|
-
c.res = new Response(c.res, {
|
|
11
|
-
status: 200,
|
|
12
|
-
headers: {
|
|
13
|
-
'Conten-Type': 'text/plain',
|
|
14
|
-
},
|
|
15
|
-
})
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
module.exports = defaultFilter
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
const fetch = require('node-fetch')
|
|
2
|
-
const { Hono, Middleware } = require('../hono')
|
|
3
|
-
|
|
4
|
-
describe('Powered by Middleware', () => {
|
|
5
|
-
const app = new Hono()
|
|
6
|
-
|
|
7
|
-
app.use('*', Middleware.poweredBy)
|
|
8
|
-
app.get('/', () => new fetch.Response('root'))
|
|
9
|
-
|
|
10
|
-
it('Response headers include X-Powered-By', async () => {
|
|
11
|
-
let req = new fetch.Request('https://example.com/')
|
|
12
|
-
let res = await app.dispatch(req, new fetch.Response())
|
|
13
|
-
expect(res).not.toBeNull()
|
|
14
|
-
expect(res.status).toBe(200)
|
|
15
|
-
expect(res.headers.get('X-Powered-By')).toBe('Hono')
|
|
16
|
-
})
|
|
17
|
-
})
|
package/src/middleware.js
DELETED
package/src/middleware.test.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
const fetch = require('node-fetch')
|
|
2
|
-
const { Hono, Middleware } = require('./hono')
|
|
3
|
-
|
|
4
|
-
describe('Builtin Middleware', () => {
|
|
5
|
-
const app = new Hono()
|
|
6
|
-
|
|
7
|
-
app.use('*', Middleware.poweredBy)
|
|
8
|
-
app.get('/', () => new fetch.Response('root'))
|
|
9
|
-
|
|
10
|
-
it('Builtin Powered By Middleware', async () => {
|
|
11
|
-
let req = new fetch.Request('https://example.com/')
|
|
12
|
-
let res = await app.dispatch(req, new fetch.Response())
|
|
13
|
-
expect(res).not.toBeNull()
|
|
14
|
-
expect(res.status).toBe(200)
|
|
15
|
-
expect(res.headers.get('X-Powered-By')).toBe('Hono')
|
|
16
|
-
})
|
|
17
|
-
})
|