hono 0.0.6 → 0.0.7

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.
@@ -0,0 +1,24 @@
1
+ name: ci
2
+ on:
3
+ push:
4
+ branches: [ master ]
5
+ pull_request:
6
+ branches: [ master ]
7
+
8
+ jobs:
9
+ ci:
10
+ runs-on: ubuntu-latest
11
+
12
+ strategy:
13
+ matrix:
14
+ node-version: [16.x]
15
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
16
+
17
+ steps:
18
+ - uses: actions/checkout@v2
19
+ - name: Use Node.js ${{ matrix.node-version }}
20
+ uses: actions/setup-node@v2
21
+ with:
22
+ node-version: ${{ matrix.node-version }}
23
+ - run: yarn install --frozen-lockfile
24
+ - run: npm test
package/README.md CHANGED
@@ -3,8 +3,8 @@
3
3
  Hono [炎] - Tiny web framework for Cloudflare Workers and others.
4
4
 
5
5
  ```js
6
- const Hono = require('Hono')
7
- const app = Hono()
6
+ const { Hono } = require('hono')
7
+ const app = new Hono()
8
8
 
9
9
  app.get('/', () => new Response('Hono!!'))
10
10
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "0.0.6",
4
- "description": "Minimal web framework for Cloudflare Workers",
3
+ "version": "0.0.7",
4
+ "description": "Minimal web framework for Cloudflare Workers and Fastly Compute@Edge",
5
5
  "main": "src/hono.js",
6
6
  "scripts": {
7
7
  "test": "jest"
package/src/hono.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ declare class FetchEvent {}
2
+ declare class Request {}
3
+ declare class Response {}
4
+ declare class Context {}
5
+
6
+ declare class Node {}
7
+
8
+ declare class Router {
9
+ tempPath: string
10
+ node: Node
11
+
12
+ add(method: string, path: string, handlers: any[]): Node
13
+ match(method: string, path: string): Node
14
+ }
15
+
16
+ export class Hono {
17
+ router: Router
18
+ middlewareRouters: Router[]
19
+
20
+ getRouter(): Router
21
+ addRoute(method: string, args: any[]): Hono
22
+ matchRoute(method: string, path: string): Node
23
+ createContext(req: Request, res: Response): Context
24
+ dispatch(req: Request, res: Response): Response
25
+ handleEvent(event: FetchEvent): Response
26
+ fire(): void
27
+
28
+ notFound(): Response
29
+
30
+ route(path: string): Hono
31
+
32
+ use(path: string, middleware: any): void
33
+
34
+ all(path: string, handler: any): Hono
35
+ get(path: string, handler: any): Hono
36
+ post(path: string, handler: any): Hono
37
+ put(path: string, handler: any): Hono
38
+ head(path: string, handler: any): Hono
39
+ delete(path: string, handler: any): Hono
40
+ }
41
+
42
+ export class Middleware {}
package/src/hono.js CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  const Node = require('./node')
4
4
  const compose = require('./compose')
5
+ const methods = require('./methods')
5
6
  const defaultFilter = require('./middleware/defaultFilter')
7
+ const Middleware = require('./middleware')
6
8
 
7
9
  const METHOD_NAME_OF_ALL = 'ALL'
8
10
 
@@ -27,23 +29,20 @@ const getPathFromURL = (url) => {
27
29
  return match[5]
28
30
  }
29
31
 
30
- const proxyHandler = {
31
- get:
32
- (target, prop) =>
33
- (...args) => {
34
- if (target.constructor.prototype.hasOwnProperty(prop)) {
35
- return target[prop](...args)
36
- } else {
37
- return target.addRoute(prop, ...args)
38
- }
39
- },
40
- }
41
-
42
32
  class Hono {
43
33
  constructor() {
44
34
  this.router = new Router()
45
- this.middlewareRouter = new Router()
46
35
  this.middlewareRouters = []
36
+
37
+ for (const method of methods) {
38
+ this[method] = (...args) => {
39
+ return this.addRoute(method, ...args)
40
+ }
41
+ }
42
+ }
43
+
44
+ all(...args) {
45
+ this.addRoute('ALL', ...args)
47
46
  }
48
47
 
49
48
  getRouter() {
@@ -57,12 +56,12 @@ class Hono {
57
56
  } else {
58
57
  this.router.add(method, ...args)
59
58
  }
60
- return WrappedApp(this)
59
+ return this
61
60
  }
62
61
 
63
62
  route(path) {
64
63
  this.router.tempPath = path
65
- return WrappedApp(this)
64
+ return this
66
65
  }
67
66
 
68
67
  use(path, middleware) {
@@ -134,8 +133,10 @@ class Hono {
134
133
  }
135
134
  }
136
135
 
137
- const WrappedApp = (hono = new Hono()) => {
138
- return new Proxy(hono, proxyHandler)
139
- }
136
+ // Default Export
137
+ module.exports = Hono
138
+ exports = module.exports
140
139
 
141
- module.exports = WrappedApp
140
+ // Named Export
141
+ exports.Hono = Hono
142
+ exports.Middleware = Middleware
package/src/hono.test.js CHANGED
@@ -1,8 +1,9 @@
1
- const Hono = require('./hono')
2
1
  const fetch = require('node-fetch')
2
+ const { Hono } = require('./hono')
3
3
 
4
4
  describe('GET Request', () => {
5
- const app = Hono()
5
+ const app = new Hono()
6
+
6
7
  app.get('/hello', () => {
7
8
  return new fetch.Response('hello', {
8
9
  status: 200,
@@ -29,7 +30,7 @@ describe('GET Request', () => {
29
30
  })
30
31
 
31
32
  describe('params and query', () => {
32
- const app = Hono()
33
+ const app = new Hono()
33
34
 
34
35
  app.get('/entry/:id', async (c) => {
35
36
  const id = await c.req.params('id')
@@ -60,7 +61,7 @@ describe('params and query', () => {
60
61
  })
61
62
 
62
63
  describe('Middleware', () => {
63
- const app = Hono()
64
+ const app = new Hono()
64
65
 
65
66
  const logger = async (c, next) => {
66
67
  console.log(`${c.req.method} : ${c.req.url}`)
package/src/methods.js ADDED
@@ -0,0 +1,30 @@
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
@@ -0,0 +1,3 @@
1
+ class Middleware {}
2
+
3
+ module.exports = Middleware
@@ -1,7 +1,7 @@
1
- const App = require('./hono')
1
+ const { Hono } = require('./hono')
2
2
 
3
3
  describe('Basic Usage', () => {
4
- const router = App()
4
+ const router = new Hono()
5
5
 
6
6
  it('get, post hello', async () => {
7
7
  router.get('/hello', 'get hello')
@@ -24,7 +24,7 @@ describe('Basic Usage', () => {
24
24
  })
25
25
 
26
26
  describe('Complex', () => {
27
- let router = App()
27
+ let router = new Hono()
28
28
 
29
29
  it('Named Param', async () => {
30
30
  router.get('/entry/:id', 'get entry')
@@ -56,7 +56,7 @@ describe('Complex', () => {
56
56
  })
57
57
 
58
58
  describe('Chained Route', () => {
59
- let router = App()
59
+ let router = new Hono()
60
60
 
61
61
  it('Return rooter object', async () => {
62
62
  router = router.patch('/hello', 'patch hello')