hono 0.4.2 → 0.5.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.
- package/README.md +32 -25
- package/dist/compose.d.ts +2 -1
- package/dist/compose.js +9 -6
- package/dist/hono.d.ts +6 -2
- package/dist/hono.js +22 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -71,6 +71,8 @@ An instance of `Hono` has these methods.
|
|
|
71
71
|
- app.**all**(path, handler)
|
|
72
72
|
- app.**route**(path)
|
|
73
73
|
- app.**use**(path, middleware)
|
|
74
|
+
- app.**notFound**(handler)
|
|
75
|
+
- app.**onError**(err, handler)
|
|
74
76
|
- app.**fire**()
|
|
75
77
|
- app.**fetch**(request, env, event)
|
|
76
78
|
|
|
@@ -126,10 +128,10 @@ book.post('/', (c) => c.text('Create Book')) // POST /book
|
|
|
126
128
|
|
|
127
129
|
### no strict
|
|
128
130
|
|
|
129
|
-
If `strict` is set
|
|
131
|
+
If `strict` is set false, `/hello`and`/hello/` are treated the same.
|
|
130
132
|
|
|
131
133
|
```js
|
|
132
|
-
const app = new Hono({ strict: false })
|
|
134
|
+
const app = new Hono({ strict: false }) // Default is true
|
|
133
135
|
|
|
134
136
|
app.get('/hello', (c) => c.text('/hello or /hello/'))
|
|
135
137
|
```
|
|
@@ -188,6 +190,29 @@ app.use('/message/*', async (c, next) => {
|
|
|
188
190
|
app.get('/message/hello', (c) => c.text('Hello Middleware!'))
|
|
189
191
|
```
|
|
190
192
|
|
|
193
|
+
## Error
|
|
194
|
+
|
|
195
|
+
### Not Found
|
|
196
|
+
|
|
197
|
+
`app.notFound` for customizing Not Found Response.
|
|
198
|
+
|
|
199
|
+
```js
|
|
200
|
+
app.notFound((c) => {
|
|
201
|
+
return c.text('Custom 404 Message', 404)
|
|
202
|
+
})
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Error Handling
|
|
206
|
+
|
|
207
|
+
`app.onError` handle the error and return the customized Response.
|
|
208
|
+
|
|
209
|
+
```js
|
|
210
|
+
app.onError((err, c) => {
|
|
211
|
+
console.error(`${err}`)
|
|
212
|
+
return c.text('Custom Error Message', 500)
|
|
213
|
+
})
|
|
214
|
+
```
|
|
215
|
+
|
|
191
216
|
## Context
|
|
192
217
|
|
|
193
218
|
To handle Request and Reponse, you can use Context object.
|
|
@@ -224,10 +249,12 @@ app.get('/entry/:id', (c) => {
|
|
|
224
249
|
|
|
225
250
|
```js
|
|
226
251
|
app.get('/welcome', (c) => {
|
|
252
|
+
// Set headers
|
|
227
253
|
c.header('X-Message', 'Hello!')
|
|
228
254
|
c.header('Content-Type', 'text/plain')
|
|
255
|
+
// Set HTTP status code
|
|
229
256
|
c.status(201)
|
|
230
|
-
|
|
257
|
+
// Return the response body
|
|
231
258
|
return c.body('Thank you for comming')
|
|
232
259
|
})
|
|
233
260
|
```
|
|
@@ -278,7 +305,7 @@ app.get('/', (c) => {
|
|
|
278
305
|
|
|
279
306
|
### c.notFound()
|
|
280
307
|
|
|
281
|
-
Return the
|
|
308
|
+
Return the `404 Not Found` Response.
|
|
282
309
|
|
|
283
310
|
```js
|
|
284
311
|
app.get('/notfound', (c) => {
|
|
@@ -327,26 +354,6 @@ app.get('*', async c => {
|
|
|
327
354
|
})
|
|
328
355
|
```
|
|
329
356
|
|
|
330
|
-
## Not Found
|
|
331
|
-
|
|
332
|
-
You can write the default `404 Not Found` Response.
|
|
333
|
-
|
|
334
|
-
```js
|
|
335
|
-
app.notFound = (c) => {
|
|
336
|
-
return c.text('This is default 404 Not Found', 404)
|
|
337
|
-
}
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
## Error handling
|
|
341
|
-
|
|
342
|
-
You can handle errors in your way.
|
|
343
|
-
|
|
344
|
-
```js
|
|
345
|
-
app.onError = (err, c) => {
|
|
346
|
-
return c.text(`This is error message: ${err.mssage}`, 500)
|
|
347
|
-
}
|
|
348
|
-
```
|
|
349
|
-
|
|
350
357
|
## fire
|
|
351
358
|
|
|
352
359
|
`app.fire()` do this.
|
|
@@ -359,7 +366,7 @@ addEventListener('fetch', (event) => {
|
|
|
359
366
|
|
|
360
367
|
## fetch
|
|
361
368
|
|
|
362
|
-
`app.fetch
|
|
369
|
+
`app.fetch` for Cloudflare Module Worker syntax.
|
|
363
370
|
|
|
364
371
|
```js
|
|
365
372
|
export default {
|
package/dist/compose.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import type { ErrorHandler } from './hono';
|
|
2
|
+
export declare const compose: <T>(middleware: Function[], onError?: ErrorHandler) => (context: T, next?: Function) => Promise<T>;
|
package/dist/compose.js
CHANGED
|
@@ -4,7 +4,6 @@ exports.compose = void 0;
|
|
|
4
4
|
const context_1 = require("./context");
|
|
5
5
|
// Based on the code in the MIT licensed `koa-compose` package.
|
|
6
6
|
const compose = (middleware, onError) => {
|
|
7
|
-
const errors = [];
|
|
8
7
|
return function (context, next) {
|
|
9
8
|
let index = -1;
|
|
10
9
|
return dispatch(0);
|
|
@@ -16,15 +15,19 @@ const compose = (middleware, onError) => {
|
|
|
16
15
|
if (i === middleware.length)
|
|
17
16
|
fn = next;
|
|
18
17
|
if (!fn)
|
|
19
|
-
return
|
|
18
|
+
return context;
|
|
20
19
|
try {
|
|
21
|
-
return Promise.resolve(fn(context, dispatch.bind(null, i + 1)))
|
|
22
|
-
|
|
20
|
+
return Promise.resolve(fn(context, dispatch.bind(null, i + 1)))
|
|
21
|
+
.then(() => {
|
|
22
|
+
return context;
|
|
23
|
+
})
|
|
24
|
+
.catch((err) => {
|
|
23
25
|
if (onError && context instanceof context_1.Context) {
|
|
24
|
-
context.res = onError(
|
|
26
|
+
context.res = onError(err, context);
|
|
27
|
+
return context;
|
|
25
28
|
}
|
|
26
29
|
else {
|
|
27
|
-
throw
|
|
30
|
+
throw err;
|
|
28
31
|
}
|
|
29
32
|
});
|
|
30
33
|
}
|
package/dist/hono.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ declare global {
|
|
|
12
12
|
}
|
|
13
13
|
export declare type Handler<RequestParamKeyType = string> = (c: Context<RequestParamKeyType>, next?: Function) => Response | Promise<Response>;
|
|
14
14
|
export declare type MiddlewareHandler = (c: Context, next: Function) => Promise<void>;
|
|
15
|
+
export declare type NotFoundHandler = (c: Context) => Response;
|
|
16
|
+
export declare type ErrorHandler = (err: Error, c: Context) => Response;
|
|
15
17
|
declare type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}` ? Name : NameWithPattern;
|
|
16
18
|
declare type ParamKey<Component> = Component extends `:${infer NameWithPattern}` ? ParamKeyName<NameWithPattern> : never;
|
|
17
19
|
declare type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}` ? ParamKey<Component> | ParamKeys<Rest> : ParamKey<Path>;
|
|
@@ -24,6 +26,8 @@ export declare class Hono {
|
|
|
24
26
|
middlewareRouters: Router<MiddlewareHandler>[];
|
|
25
27
|
tempPath: string;
|
|
26
28
|
constructor(init?: Partial<Pick<Hono, 'routerClass' | 'strict'>>);
|
|
29
|
+
notFoundHandler: NotFoundHandler;
|
|
30
|
+
errorHandler: ErrorHandler;
|
|
27
31
|
get<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono;
|
|
28
32
|
post<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono;
|
|
29
33
|
put<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono;
|
|
@@ -34,13 +38,13 @@ export declare class Hono {
|
|
|
34
38
|
all<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono;
|
|
35
39
|
route(path: string): Hono;
|
|
36
40
|
use(path: string, middleware: MiddlewareHandler): void;
|
|
41
|
+
onError(handler: ErrorHandler): Hono;
|
|
42
|
+
notFound(handler: NotFoundHandler): Hono;
|
|
37
43
|
addRoute(method: string, path: string, handler: Handler): Hono;
|
|
38
44
|
matchRoute(method: string, path: string): Promise<Result<Handler>>;
|
|
39
45
|
dispatch(request: Request, env?: Env, event?: FetchEvent): Promise<Response>;
|
|
40
46
|
handleEvent(event: FetchEvent): Promise<Response>;
|
|
41
47
|
fetch(request: Request, env?: Env, event?: FetchEvent): Promise<Response>;
|
|
42
48
|
fire(): void;
|
|
43
|
-
onError(err: Error, c: Context): Response;
|
|
44
|
-
notFound(c: Context): Response;
|
|
45
49
|
}
|
|
46
50
|
export {};
|
package/dist/hono.js
CHANGED
|
@@ -10,6 +10,15 @@ class Hono {
|
|
|
10
10
|
constructor(init = {}) {
|
|
11
11
|
this.routerClass = trie_router_1.TrieRouter;
|
|
12
12
|
this.strict = true; // strict routing - default is true
|
|
13
|
+
this.notFoundHandler = (c) => {
|
|
14
|
+
const message = '404 Not Found';
|
|
15
|
+
return c.text(message, 404);
|
|
16
|
+
};
|
|
17
|
+
this.errorHandler = (err, c) => {
|
|
18
|
+
console.error(`${err.message}`);
|
|
19
|
+
const message = 'Internal Server Error';
|
|
20
|
+
return c.text(message, 500);
|
|
21
|
+
};
|
|
13
22
|
Object.assign(this, init);
|
|
14
23
|
this.router = new this.routerClass();
|
|
15
24
|
this.middlewareRouters = [];
|
|
@@ -53,6 +62,14 @@ class Hono {
|
|
|
53
62
|
router.add(router_1.METHOD_NAME_OF_ALL, path, middleware);
|
|
54
63
|
this.middlewareRouters.push(router);
|
|
55
64
|
}
|
|
65
|
+
onError(handler) {
|
|
66
|
+
this.errorHandler = handler;
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
notFound(handler) {
|
|
70
|
+
this.notFoundHandler = handler;
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
56
73
|
// addRoute('get', '/', handler)
|
|
57
74
|
addRoute(method, path, handler) {
|
|
58
75
|
method = method.toUpperCase();
|
|
@@ -82,7 +99,7 @@ class Hono {
|
|
|
82
99
|
const url = new URL(c.req.url);
|
|
83
100
|
return url.searchParams.get(key);
|
|
84
101
|
};
|
|
85
|
-
const handler = result ? result.handler : this.
|
|
102
|
+
const handler = result ? result.handler : this.notFoundHandler;
|
|
86
103
|
const middleware = [];
|
|
87
104
|
for (const mr of this.middlewareRouters) {
|
|
88
105
|
const mwResult = mr.match(router_1.METHOD_NAME_OF_ALL, path);
|
|
@@ -99,11 +116,11 @@ class Hono {
|
|
|
99
116
|
await next();
|
|
100
117
|
};
|
|
101
118
|
middleware.push(wrappedHandler);
|
|
102
|
-
const composed = (0, compose_1.compose)(middleware, this.
|
|
119
|
+
const composed = (0, compose_1.compose)(middleware, this.errorHandler);
|
|
103
120
|
const c = new context_1.Context(request, { env: env, event: event, res: null });
|
|
104
|
-
c.notFound = () => this.
|
|
105
|
-
await composed(c);
|
|
106
|
-
return
|
|
121
|
+
c.notFound = () => this.notFoundHandler(c);
|
|
122
|
+
const context = await composed(c);
|
|
123
|
+
return context.res;
|
|
107
124
|
}
|
|
108
125
|
async handleEvent(event) {
|
|
109
126
|
return this.dispatch(event.request, {}, event);
|
|
@@ -116,16 +133,5 @@ class Hono {
|
|
|
116
133
|
event.respondWith(this.handleEvent(event));
|
|
117
134
|
});
|
|
118
135
|
}
|
|
119
|
-
// Default error Response
|
|
120
|
-
onError(err, c) {
|
|
121
|
-
console.error(`${err.message}`);
|
|
122
|
-
const message = 'Internal Server Error';
|
|
123
|
-
return c.text(message, 500);
|
|
124
|
-
}
|
|
125
|
-
// Default 404 not found Response
|
|
126
|
-
notFound(c) {
|
|
127
|
-
const message = 'Not Found';
|
|
128
|
-
return c.text(message, 404);
|
|
129
|
-
}
|
|
130
136
|
}
|
|
131
137
|
exports.Hono = Hono;
|