@bracketed/hono-router 1.0.0 → 1.0.1

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 CHANGED
@@ -5,24 +5,20 @@
5
5
 
6
6
  <br>
7
7
 
8
- <h2 align="center" >@bracketed/logger</h2>
8
+ <h2 align="center" >@bracketed/hono-router</h2>
9
9
 
10
- An alternative to your run-of-the-mill node console logging functions!
11
- This is a package built from the source code of [@sapphire/framework](https://www.npmjs.com/package/@sapphire/framework) & [@sapphire/plugin-logger](https://www.npmjs.com/package/@sapphire/plugin-logger) to allow usage of Sapphire's logger features in regular Node.js, full credit to the authors of the pieces of code that this package is made from.
10
+ A router plugin for the Hono http server based on `@bracketed/hono-router`.
12
11
 
13
- <h2>What is this?</h2>
14
-
15
- \- A Logger package built from [@sapphire/framework](https://www.npmjs.com/package/@sapphire/framework) & [@sapphire/plugin-logger](https://www.npmjs.com/package/@sapphire/plugin-logger) that uses [colorette](https://www.npmjs.com/package/colorette) for styling.
12
+ This package functions pretty much the same, but with minor changes and full Typescript support.
16
13
 
17
14
  <h2>Summary (Directory)</h2>
18
15
 
19
- - [Installation](#Installation)
16
+ - [Installation](#Installation)
20
17
  <!--truncate-->
21
- - [Yarn](#YarnInstall)
22
- - [Npm](#NpmInstall)
23
- - [Usage](#Usage)
24
- - [Contribution](#Contribution)
25
- - [ChangeLog](#versionlog)
18
+ - [Yarn](#YarnInstall)
19
+ - [Npm](#NpmInstall)
20
+ - [Usage](#Usage)
21
+ - [Contribution](#Contribution)
26
22
 
27
23
  <h2 id="Installation">Installation</h2>
28
24
 
@@ -31,43 +27,177 @@ Install via `yarn` or `npm`:
31
27
  <p id="YarnInstall">Yarn:</p>
32
28
 
33
29
  ```sh
34
- yarn add @bracketed/logger
30
+ yarn add @bracketed/hono-router
35
31
  ```
36
32
 
37
33
  <p id="NpmInstall">Npm:</p>
38
34
 
39
35
  ```sh
40
- npm install --save @bracketed/logger
36
+ npm install --save @bracketed/hono-router
41
37
  ```
42
38
 
43
39
  <h2 id="Usage">Usage</h2>
44
40
 
45
- ```ts
46
- // ESM
47
- import { Logger, LogLevel } from '@bracketed/logger';
48
- const console = new Logger();
49
-
50
- console.info('Hello World!');
51
- console.debug('Hello World!');
52
- console.warn('Hello World!');
53
- console.error('Hello World!');
54
- console.fatal('Hello World!');
55
- console.trace('Hello World!');
56
- console.write(LogLevel.Info, 'Hello World!');
41
+ 1. Configure the plugin with Rollup
42
+ 2. Create the routes directory with the desired routes
43
+ 3. Import the route directory into your code
44
+
45
+ The examples directory of the repo contains a NodeJS and a Cloudflare Workers
46
+ example that can be run, and a Bruno collection for each of the routes.
47
+
48
+ ### Importing in Code
49
+
50
+ Importing with the `@routes/` prefix on the directory to import will have
51
+ the plugin create a hono router from the routes in the directory. Normal import
52
+ resolution rules apply to the location (relative vs absolute file paths).
53
+
54
+ ```js
55
+ import router from "@routes/./routes"
56
+ ```
57
+
58
+ ### Rollup Config
59
+
60
+ ```js
61
+ import honoRouter from "@bracketed/hono-router"
62
+
63
+ export default {
64
+ input: "main.js",
65
+ output: {
66
+ file: "your-output-file",
67
+ format: "esm"
68
+ },
69
+ plugins: [
70
+ honoRouter({
71
+ debug: true
72
+ })
73
+ ]
74
+ }
75
+ ```
76
+
77
+ If `debug` is `true` the library will console.log information on what routes
78
+ are being imported and setup when the script begins.
79
+
80
+ ### File Routes
81
+
82
+ There are 4 route types that can be defined by the files and their locations.
83
+ An example of the order these run is provided after all the descriptions.
84
+
85
+ In order for a file to register a route action, it needs to export variables
86
+ named `$<method>` and / or `$any`. Specific methods will always match before
87
+ `$any` handlers (ex: if `$get` matches, `$any` in the same file won't).
88
+
89
+ The exported value for any handlers should be either a function, or an array of
90
+ functions that are used as [Hono routing](https://hono.dev/docs/api/routing)
91
+ functions. If the exported value is an array, all the handlers are added for the
92
+ same route/verb in the order they appear in the array. Additionally, if any of
93
+ them in the list returns it will make the route return and not go to subsequent
94
+ handlers, just like
95
+ [middleware in Hono](https://hono.dev/docs/guides/middleware).
96
+
97
+ > The library doesn't modify how any of the
98
+ > [Hono context](https://hono.dev/docs/api/context) functions work, it just
99
+ > makes it easier to setup routes based on file paths, so all context functions
100
+ > are available as-is per the Hono docs.
101
+
102
+ #### Middleware
103
+
104
+ Middleware Routes are files named `+Middleware.ts` in a folder. Middleware runs
105
+ before any routes in a folder, in a top down fashion (examples later).
106
+
107
+ #### Wildcard Routes
108
+
109
+ Wildcard routes match any route not defined in a directory and are defined by
110
+ naming a file as `[[<name>]].ts`.
111
+
112
+ #### Parameter Routes
113
+
114
+ Parameter routes are routes that have parameters in them and are defined by
115
+ naming a file or folder as `[<param name>]`. Any valid part of a path can match
116
+ against a parameter, and more than one parameter can exist on a route.
117
+
118
+ ### Static routes
119
+
120
+ Any route that isn't formatted as one of the previous types will be
121
+ treated as a static route. The files and directories don't have any special
122
+ syntax required (other than being `.js` or `.ts` files).
123
+
124
+ ### Routing Example
125
+
57
126
  ```
127
+ routes/
128
+ ├─ dir/
129
+ │ ├─ +Middleware.ts
130
+ │ ├─ static.ts
131
+ │ ├─ [[nested-wildcard]].ts
132
+ ├─ user/
133
+ │ ├─ _middleware.ts
134
+ │ ├─ logout.ts
135
+ │ ├─ [userID].ts
136
+ ├─ [param1]/
137
+ │ ├─ [param2]/
138
+ │ │ ├─ thing.ts
139
+ │ ├─ [file-param].ts
140
+ ├─ _middleware.ts
141
+ ├─ [[no-route]].ts
142
+ ```
143
+
144
+ This set of files will generate the following Hono routes:
58
145
 
59
- ```ts
60
- // CJS
61
- const { Logger, LogLevel } = require('@bracketed/logger');
62
- const console = new Logger();
63
-
64
- console.info('Hello World!');
65
- console.debug('Hello World!');
66
- console.warn('Hello World!');
67
- console.error('Hello World!');
68
- console.fatal('Hello World!');
69
- console.trace('Hello World!');
70
- console.write(LogLevel.Info, 'Hello World!');
146
+ ```
147
+ /dir/static
148
+ /dir/*
149
+ /user/logout
150
+ /user/:userID
151
+ /:param1/:file-param
152
+ /:param1/:param2/thing
153
+ /*
154
+ ```
155
+
156
+ Routes are added to Hono in such a way that the resolution order is
157
+ always the same:
158
+
159
+ 1. static routes
160
+ 2. parameterized routes
161
+ 3. wildcards
162
+
163
+ ### Routing Resolution
164
+
165
+ Using the previous set of routes, this is what the execution will look
166
+ like given some example requests.
167
+
168
+ ```
169
+ /dir/static
170
+ -> run /+middleware.js
171
+ -> run /dir/+middleware.js
172
+ -> match /dir/static.js
173
+
174
+ /dir/random-thing
175
+ -> run /+middleware.js
176
+ -> run /dir/+middleware.js
177
+ -> match /dir/[[nested-wildcard]].js
178
+
179
+ /user/logout
180
+ -> run /+middleware.js
181
+ -> run /user/+middleware.js
182
+ -> match /user/logout.js
183
+
184
+ /user/id-123
185
+ -> run /+middleware.js
186
+ -> run /user/+middleware.js
187
+ -> match /user/[userID].js
188
+
189
+ /user/not/valid
190
+ -> run /+middleware.js
191
+ -> run /user/+middleware.js
192
+ -> match /[[no-route]].js
193
+
194
+ /first/second/thing
195
+ -> run /+middleware.js
196
+ -> match /[param1]/[param2]/thing.js
197
+
198
+ /first/second
199
+ -> run /+middleware.js
200
+ -> match /[param1]/[file-param].js
71
201
  ```
72
202
 
73
203
  <h1 id="Contribution">Contribution & Help</h1>
package/lib/cjs/base.cjs CHANGED
@@ -2,19 +2,21 @@
2
2
  import { Hono } from "hono"
3
3
 
4
4
  const optlog = (...args) => {
5
- if (options.debug !== true) return
5
+ if (options.debug !== true) {
6
+ return
7
+ }
6
8
  console.log(...args)
7
9
  }
8
10
 
9
11
  const app = new Hono()
10
12
  const verbs = [
11
- "+GET",
12
- "+POST",
13
- "+PATCH",
14
- "+PUT",
15
- "+DELETE",
16
- "+OPTIONS",
17
- "+HEAD",
13
+ "$get",
14
+ "$post",
15
+ "$patch",
16
+ "$put",
17
+ "$delete",
18
+ "$options",
19
+ "$head",
18
20
  ]
19
21
  const addRoute = (pattern, module, routeType) => {
20
22
  for (const verb of verbs) {
@@ -22,14 +24,18 @@ const addRoute = (pattern, module, routeType) => {
22
24
  if (handler !== undefined) {
23
25
  const handlerList = Array.isArray(handler) ? handler : [handler]
24
26
  optlog("add", { pattern, verb, routeType, count: handlerList.length })
25
- for (const handler of handlerList) app.on(verb.slice(1), pattern, handler)
27
+ for (const handler of handlerList) {
28
+ app.on(verb.slice(1), pattern, handler)
29
+ }
26
30
  }
27
31
  }
28
-
29
- if (module.$any === undefined) return
30
-
32
+ if (module.$any === undefined) {
33
+ return
34
+ }
31
35
  const handlerList = Array.isArray(module.$any) ? module.$any : [module.$any]
32
36
  optlog("add", { pattern, verb: "any", routeType, count: handlerList.length })
33
- for (const handler of handlerList) app.use(pattern, handler)
37
+ for (const handler of handlerList) {
38
+ app.use(pattern, handler)
39
+ }
34
40
  }`;exports.base=e;//# sourceMappingURL=base.cjs.map
35
41
  //# sourceMappingURL=base.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/base.ts"],"names":["base"],"mappings":"6CAAA,IAAMA,CAAAA,CAAO","file":"base.cjs","sourcesContent":["const base = `\r\nimport { Hono } from \"hono\"\r\n\r\nconst optlog = (...args) => {\r\n if (options.debug !== true) return\r\n console.log(...args)\r\n}\r\n\r\nconst app = new Hono()\r\nconst verbs = [\r\n \"+GET\",\r\n \"+POST\",\r\n \"+PATCH\",\r\n \"+PUT\",\r\n \"+DELETE\",\r\n \"+OPTIONS\",\r\n \"+HEAD\",\r\n]\r\nconst addRoute = (pattern, module, routeType) => {\r\n for (const verb of verbs) {\r\n const handler = module[verb]\r\n if (handler !== undefined) {\r\n const handlerList = Array.isArray(handler) ? handler : [handler]\r\n optlog(\"add\", { pattern, verb, routeType, count: handlerList.length })\r\n for (const handler of handlerList) app.on(verb.slice(1), pattern, handler)\r\n }\r\n }\r\n\r\n if (module.$any === undefined) return\r\n \r\n const handlerList = Array.isArray(module.$any) ? module.$any : [module.$any]\r\n optlog(\"add\", { pattern, verb: \"any\", routeType, count: handlerList.length })\r\n for (const handler of handlerList) app.use(pattern, handler)\r\n}`;\r\n\r\nexport { base };\r\n"]}
1
+ {"version":3,"sources":["../../src/base.ts"],"names":["base"],"mappings":"6CAAA,IAAMA,CAAAA,CAAO","file":"base.cjs","sourcesContent":["const base = `\r\nimport { Hono } from \"hono\"\r\n\r\nconst optlog = (...args) => {\r\n if (options.debug !== true) {\r\n return\r\n }\r\n console.log(...args)\r\n}\r\n\r\nconst app = new Hono()\r\nconst verbs = [\r\n \"$get\",\r\n \"$post\",\r\n \"$patch\",\r\n \"$put\",\r\n \"$delete\",\r\n \"$options\",\r\n \"$head\",\r\n]\r\nconst addRoute = (pattern, module, routeType) => {\r\n for (const verb of verbs) {\r\n const handler = module[verb]\r\n if (handler !== undefined) {\r\n const handlerList = Array.isArray(handler) ? handler : [handler]\r\n optlog(\"add\", { pattern, verb, routeType, count: handlerList.length })\r\n for (const handler of handlerList) {\r\n app.on(verb.slice(1), pattern, handler)\r\n }\r\n }\r\n }\r\n if (module.$any === undefined) {\r\n return\r\n }\r\n const handlerList = Array.isArray(module.$any) ? module.$any : [module.$any]\r\n optlog(\"add\", { pattern, verb: \"any\", routeType, count: handlerList.length })\r\n for (const handler of handlerList) {\r\n app.use(pattern, handler)\r\n }\r\n}`;\r\n\r\nexport { base };\r\n"]}
package/lib/cjs/base.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- declare const base = "\nimport { Hono } from \"hono\"\n\nconst optlog = (...args) => {\n if (options.debug !== true) return\n console.log(...args)\n}\n\nconst app = new Hono()\nconst verbs = [\n \"+GET\",\n \"+POST\",\n \"+PATCH\",\n \"+PUT\",\n \"+DELETE\",\n \"+OPTIONS\",\n \"+HEAD\",\n]\nconst addRoute = (pattern, module, routeType) => {\n for (const verb of verbs) {\n const handler = module[verb]\n if (handler !== undefined) {\n const handlerList = Array.isArray(handler) ? handler : [handler]\n optlog(\"add\", { pattern, verb, routeType, count: handlerList.length })\n for (const handler of handlerList) app.on(verb.slice(1), pattern, handler)\n }\n }\n\n if (module.$any === undefined) return\n \n const handlerList = Array.isArray(module.$any) ? module.$any : [module.$any]\n optlog(\"add\", { pattern, verb: \"any\", routeType, count: handlerList.length })\n for (const handler of handlerList) app.use(pattern, handler)\n}";
1
+ declare const base = "\nimport { Hono } from \"hono\"\n\nconst optlog = (...args) => {\n if (options.debug !== true) {\n return\n }\n console.log(...args)\n}\n\nconst app = new Hono()\nconst verbs = [\n \"$get\",\n \"$post\",\n \"$patch\",\n \"$put\",\n \"$delete\",\n \"$options\",\n \"$head\",\n]\nconst addRoute = (pattern, module, routeType) => {\n for (const verb of verbs) {\n const handler = module[verb]\n if (handler !== undefined) {\n const handlerList = Array.isArray(handler) ? handler : [handler]\n optlog(\"add\", { pattern, verb, routeType, count: handlerList.length })\n for (const handler of handlerList) {\n app.on(verb.slice(1), pattern, handler)\n }\n }\n }\n if (module.$any === undefined) {\n return\n }\n const handlerList = Array.isArray(module.$any) ? module.$any : [module.$any]\n optlog(\"add\", { pattern, verb: \"any\", routeType, count: handlerList.length })\n for (const handler of handlerList) {\n app.use(pattern, handler)\n }\n}";
2
2
 
3
3
  export { base };
@@ -1,3 +1,3 @@
1
- declare const base = "\nimport { Hono } from \"hono\"\n\nconst optlog = (...args) => {\n if (options.debug !== true) return\n console.log(...args)\n}\n\nconst app = new Hono()\nconst verbs = [\n \"+GET\",\n \"+POST\",\n \"+PATCH\",\n \"+PUT\",\n \"+DELETE\",\n \"+OPTIONS\",\n \"+HEAD\",\n]\nconst addRoute = (pattern, module, routeType) => {\n for (const verb of verbs) {\n const handler = module[verb]\n if (handler !== undefined) {\n const handlerList = Array.isArray(handler) ? handler : [handler]\n optlog(\"add\", { pattern, verb, routeType, count: handlerList.length })\n for (const handler of handlerList) app.on(verb.slice(1), pattern, handler)\n }\n }\n\n if (module.$any === undefined) return\n \n const handlerList = Array.isArray(module.$any) ? module.$any : [module.$any]\n optlog(\"add\", { pattern, verb: \"any\", routeType, count: handlerList.length })\n for (const handler of handlerList) app.use(pattern, handler)\n}";
1
+ declare const base = "\nimport { Hono } from \"hono\"\n\nconst optlog = (...args) => {\n if (options.debug !== true) {\n return\n }\n console.log(...args)\n}\n\nconst app = new Hono()\nconst verbs = [\n \"$get\",\n \"$post\",\n \"$patch\",\n \"$put\",\n \"$delete\",\n \"$options\",\n \"$head\",\n]\nconst addRoute = (pattern, module, routeType) => {\n for (const verb of verbs) {\n const handler = module[verb]\n if (handler !== undefined) {\n const handlerList = Array.isArray(handler) ? handler : [handler]\n optlog(\"add\", { pattern, verb, routeType, count: handlerList.length })\n for (const handler of handlerList) {\n app.on(verb.slice(1), pattern, handler)\n }\n }\n }\n if (module.$any === undefined) {\n return\n }\n const handlerList = Array.isArray(module.$any) ? module.$any : [module.$any]\n optlog(\"add\", { pattern, verb: \"any\", routeType, count: handlerList.length })\n for (const handler of handlerList) {\n app.use(pattern, handler)\n }\n}";
2
2
 
3
3
  export { base };
package/lib/esm/base.mjs CHANGED
@@ -2,19 +2,21 @@ import'./chunk-QWPVIX2T.mjs';var e=`
2
2
  import { Hono } from "hono"
3
3
 
4
4
  const optlog = (...args) => {
5
- if (options.debug !== true) return
5
+ if (options.debug !== true) {
6
+ return
7
+ }
6
8
  console.log(...args)
7
9
  }
8
10
 
9
11
  const app = new Hono()
10
12
  const verbs = [
11
- "+GET",
12
- "+POST",
13
- "+PATCH",
14
- "+PUT",
15
- "+DELETE",
16
- "+OPTIONS",
17
- "+HEAD",
13
+ "$get",
14
+ "$post",
15
+ "$patch",
16
+ "$put",
17
+ "$delete",
18
+ "$options",
19
+ "$head",
18
20
  ]
19
21
  const addRoute = (pattern, module, routeType) => {
20
22
  for (const verb of verbs) {
@@ -22,14 +24,18 @@ const addRoute = (pattern, module, routeType) => {
22
24
  if (handler !== undefined) {
23
25
  const handlerList = Array.isArray(handler) ? handler : [handler]
24
26
  optlog("add", { pattern, verb, routeType, count: handlerList.length })
25
- for (const handler of handlerList) app.on(verb.slice(1), pattern, handler)
27
+ for (const handler of handlerList) {
28
+ app.on(verb.slice(1), pattern, handler)
29
+ }
26
30
  }
27
31
  }
28
-
29
- if (module.$any === undefined) return
30
-
32
+ if (module.$any === undefined) {
33
+ return
34
+ }
31
35
  const handlerList = Array.isArray(module.$any) ? module.$any : [module.$any]
32
36
  optlog("add", { pattern, verb: "any", routeType, count: handlerList.length })
33
- for (const handler of handlerList) app.use(pattern, handler)
37
+ for (const handler of handlerList) {
38
+ app.use(pattern, handler)
39
+ }
34
40
  }`;export{e as base};//# sourceMappingURL=base.mjs.map
35
41
  //# sourceMappingURL=base.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/base.ts"],"names":["base"],"mappings":"6BAAA,IAAMA,CAAAA,CAAO","file":"base.mjs","sourcesContent":["const base = `\r\nimport { Hono } from \"hono\"\r\n\r\nconst optlog = (...args) => {\r\n if (options.debug !== true) return\r\n console.log(...args)\r\n}\r\n\r\nconst app = new Hono()\r\nconst verbs = [\r\n \"+GET\",\r\n \"+POST\",\r\n \"+PATCH\",\r\n \"+PUT\",\r\n \"+DELETE\",\r\n \"+OPTIONS\",\r\n \"+HEAD\",\r\n]\r\nconst addRoute = (pattern, module, routeType) => {\r\n for (const verb of verbs) {\r\n const handler = module[verb]\r\n if (handler !== undefined) {\r\n const handlerList = Array.isArray(handler) ? handler : [handler]\r\n optlog(\"add\", { pattern, verb, routeType, count: handlerList.length })\r\n for (const handler of handlerList) app.on(verb.slice(1), pattern, handler)\r\n }\r\n }\r\n\r\n if (module.$any === undefined) return\r\n \r\n const handlerList = Array.isArray(module.$any) ? module.$any : [module.$any]\r\n optlog(\"add\", { pattern, verb: \"any\", routeType, count: handlerList.length })\r\n for (const handler of handlerList) app.use(pattern, handler)\r\n}`;\r\n\r\nexport { base };\r\n"]}
1
+ {"version":3,"sources":["../../src/base.ts"],"names":["base"],"mappings":"6BAAA,IAAMA,CAAAA,CAAO","file":"base.mjs","sourcesContent":["const base = `\r\nimport { Hono } from \"hono\"\r\n\r\nconst optlog = (...args) => {\r\n if (options.debug !== true) {\r\n return\r\n }\r\n console.log(...args)\r\n}\r\n\r\nconst app = new Hono()\r\nconst verbs = [\r\n \"$get\",\r\n \"$post\",\r\n \"$patch\",\r\n \"$put\",\r\n \"$delete\",\r\n \"$options\",\r\n \"$head\",\r\n]\r\nconst addRoute = (pattern, module, routeType) => {\r\n for (const verb of verbs) {\r\n const handler = module[verb]\r\n if (handler !== undefined) {\r\n const handlerList = Array.isArray(handler) ? handler : [handler]\r\n optlog(\"add\", { pattern, verb, routeType, count: handlerList.length })\r\n for (const handler of handlerList) {\r\n app.on(verb.slice(1), pattern, handler)\r\n }\r\n }\r\n }\r\n if (module.$any === undefined) {\r\n return\r\n }\r\n const handlerList = Array.isArray(module.$any) ? module.$any : [module.$any]\r\n optlog(\"add\", { pattern, verb: \"any\", routeType, count: handlerList.length })\r\n for (const handler of handlerList) {\r\n app.use(pattern, handler)\r\n }\r\n}`;\r\n\r\nexport { base };\r\n"]}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@bracketed/hono-router",
3
3
  "description": "A typed CF worker compatible hono file system router based on the hono router by @axel669.",
4
4
  "packageManager": "yarn@4.9.2",
5
- "version": "1.0.0",
5
+ "version": "1.0.1",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },