@jderstd/hono 0.7.1 → 0.8.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 +3 -1
- package/dist/body-limit.d.ts +54 -54
- package/dist/ip-limit.d.ts +74 -74
- package/dist/not-found.d.ts +28 -28
- package/dist/not-found.js.map +1 -1
- package/dist/not-found.mjs.map +1 -1
- package/dist/on-error.d.ts +56 -56
- package/dist/on-error.js.map +1 -1
- package/dist/on-error.mjs.map +1 -1
- package/dist/response/common/index.d.ts +67 -67
- package/dist/response/common/index.js +2 -2
- package/dist/response/common/index.js.map +1 -1
- package/dist/response/error/http.d.ts +3 -0
- package/dist/response/error/http.js +7 -0
- package/dist/response/error/http.js.map +1 -1
- package/dist/response/error/http.mjs +7 -0
- package/dist/response/error/http.mjs.map +1 -1
- package/dist/response/error/index.d.ts +36 -32
- package/dist/response/error/index.js +4 -0
- package/dist/response/error/index.js.map +1 -1
- package/dist/response/error/index.mjs +4 -0
- package/dist/response/error/index.mjs.map +1 -1
- package/dist/response/json/index.d.ts +95 -95
- package/dist/response/json/index.js +2 -2
- package/dist/response/json/index.js.map +1 -1
- package/dist/response.d.ts +2 -2
- package/dist/time-limit.d.ts +50 -50
- package/package.json +3 -3
package/README.md
CHANGED
package/dist/body-limit.d.ts
CHANGED
|
@@ -4,63 +4,63 @@ declare const BODY_LIMIT_MAX_DEFAULT: 10485760;
|
|
|
4
4
|
/** Options for `bodyLimit` middleware. */
|
|
5
5
|
type BodyLimitOptions = {
|
|
6
6
|
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
* Maximum body size in bytes.
|
|
8
|
+
*
|
|
9
|
+
* By default, it is `BODY_LIMIT_MAX_DEFAULT`.
|
|
10
|
+
*/
|
|
11
11
|
max?: number;
|
|
12
12
|
};
|
|
13
13
|
/**
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
14
|
+
* Body limit middleware.
|
|
15
|
+
*
|
|
16
|
+
* Following error will be returned if the body size is over the limit:
|
|
17
|
+
*
|
|
18
|
+
* ```jsonc
|
|
19
|
+
* // Status: 413
|
|
20
|
+
* {
|
|
21
|
+
* "success": false,
|
|
22
|
+
* "errors": [
|
|
23
|
+
* {
|
|
24
|
+
* "code": "too_large",
|
|
25
|
+
* "path": [
|
|
26
|
+
* "request",
|
|
27
|
+
* "body"
|
|
28
|
+
* ],
|
|
29
|
+
* "message": "Request body is too large"
|
|
30
|
+
* }
|
|
31
|
+
* ]
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* For more information, please refer to
|
|
36
|
+
* [Body Limit](https://hono.dev/docs/middleware/builtin/body-limit).
|
|
37
|
+
*
|
|
38
|
+
* ### Examples
|
|
39
|
+
*
|
|
40
|
+
* A example of using `bodyLimit` middleware:
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { Hono } from "hono";
|
|
44
|
+
* import { bodyLimit } from "@jderstd/hono/body-limit";
|
|
45
|
+
*
|
|
46
|
+
* const app: Hono = new Hono();
|
|
47
|
+
*
|
|
48
|
+
* app.use(bodyLimit());
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* A example of using `bodyLimit` middleware with options:
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { Hono } from "hono";
|
|
55
|
+
* import { bodyLimit } from "@jderstd/hono/body-limit";
|
|
56
|
+
*
|
|
57
|
+
* const app: Hono = new Hono();
|
|
58
|
+
*
|
|
59
|
+
* app.use(bodyLimit({
|
|
60
|
+
* max: 20 * 1024 * 1024, // 20MiB
|
|
61
|
+
* }));
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
64
|
declare const bodyLimit: (options?: BodyLimitOptions) => MiddlewareHandler;
|
|
65
65
|
export { BODY_LIMIT_MAX_DEFAULT, type BodyLimitOptions, bodyLimit };
|
|
66
66
|
//# sourceMappingURL=body-limit.d.ts.map
|
package/dist/ip-limit.d.ts
CHANGED
|
@@ -10,9 +10,9 @@ type IpLimitBaseOptions = {
|
|
|
10
10
|
/** Denied IP addresses. */
|
|
11
11
|
denyList?: IPRestrictionRule[];
|
|
12
12
|
/**
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
* Whether show more information.
|
|
14
|
+
* By default, it's `false`.
|
|
15
|
+
*/
|
|
16
16
|
verbose?: boolean;
|
|
17
17
|
};
|
|
18
18
|
/** Options for `ipLimit` middleware. */
|
|
@@ -21,79 +21,79 @@ type IpLimitOptions = Format<{
|
|
|
21
21
|
getConnInfo: GetIPAddr;
|
|
22
22
|
} & IpLimitBaseOptions>;
|
|
23
23
|
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
24
|
+
* IP limit middleware.
|
|
25
|
+
*
|
|
26
|
+
* Following error will be returned if the IP address is not allowed:
|
|
27
|
+
*
|
|
28
|
+
* ```jsonc
|
|
29
|
+
* // Status: 403
|
|
30
|
+
* {
|
|
31
|
+
* "success": false,
|
|
32
|
+
* "errors": [
|
|
33
|
+
* {
|
|
34
|
+
* "code": "forbidden"
|
|
35
|
+
* }
|
|
36
|
+
* ]
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* When `verbose` is `true`, the error will be like:
|
|
41
|
+
*
|
|
42
|
+
* ```jsonc
|
|
43
|
+
* // Status: 403
|
|
44
|
+
* {
|
|
45
|
+
* "success": false,
|
|
46
|
+
* "errors": [
|
|
47
|
+
* {
|
|
48
|
+
* "code": "forbidden",
|
|
49
|
+
* "path": [
|
|
50
|
+
* "request",
|
|
51
|
+
* "ip"
|
|
52
|
+
* ],
|
|
53
|
+
* "message": "Forbidden IP address: x.x.x.x"
|
|
54
|
+
* }
|
|
55
|
+
* ]
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* For more information, please refer to
|
|
60
|
+
* [IP Restriction](https://hono.dev/docs/middleware/builtin/ip-restriction).
|
|
61
|
+
*
|
|
62
|
+
* For `getConnInfo`, please refer to
|
|
63
|
+
* [ConnInfo helper](https://hono.dev/docs/helpers/conninfo).
|
|
64
|
+
*
|
|
65
|
+
* ### Example
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* import { Hono } from "hono";
|
|
69
|
+
* import { ipLimit } from "@jderstd/hono/ip-limit";
|
|
70
|
+
*
|
|
71
|
+
* // getConnInfo helper for Node.js
|
|
72
|
+
* import { getConnInfo } from "@hono/node-server/conninfo";
|
|
73
|
+
*
|
|
74
|
+
* const app: Hono = new Hono();
|
|
75
|
+
*
|
|
76
|
+
* app.use(
|
|
77
|
+
* ipLimit({
|
|
78
|
+
* getConnInfo,
|
|
79
|
+
* allowList: [],
|
|
80
|
+
* denyList: [],
|
|
81
|
+
* })
|
|
82
|
+
* );
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
85
|
declare function ipLimit(options: IpLimitOptions): MiddlewareHandler;
|
|
86
86
|
/**
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
87
|
+
* IP limit middleware for compatibility with `hono/ip-restriction`.
|
|
88
|
+
*
|
|
89
|
+
* This is functionally equivalent to:
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* ipLimit({ getConnInfo, ...options });
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* And it behaves the same as the main `ipLimit` function.
|
|
96
|
+
*/
|
|
97
97
|
declare function ipLimit(getConnInfo: GetIPAddr, options?: IpLimitBaseOptions): MiddlewareHandler;
|
|
98
98
|
export { type AddressType, type ConnInfo, type GetConnInfo, type GetIPAddr, type IPRestrictionRule, type IpLimitBaseOptions, type IpLimitOptions, type NetAddrInfo, ipLimit };
|
|
99
99
|
//# sourceMappingURL=ip-limit.d.ts.map
|
package/dist/not-found.d.ts
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
import { Context } from "hono";
|
|
2
2
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
3
|
+
* Not found handler.
|
|
4
|
+
*
|
|
5
|
+
* Following response will be returned on route not found:
|
|
6
|
+
*
|
|
7
|
+
* ```jsonc
|
|
8
|
+
* // Status: 404
|
|
9
|
+
* {
|
|
10
|
+
* "success": false,
|
|
11
|
+
* "errors": [
|
|
12
|
+
* {
|
|
13
|
+
* "code": "not_found",
|
|
14
|
+
* "message": "Content not found"
|
|
15
|
+
* }
|
|
16
|
+
* ]
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* ### Example
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { Hono } from "hono";
|
|
24
|
+
* import { notFoundHandler } from "@jderstd/hono/not-found";
|
|
25
|
+
*
|
|
26
|
+
* const app: Hono = new Hono();
|
|
27
|
+
*
|
|
28
|
+
* app.notFound(notFoundHandler());
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
31
|
declare const notFoundHandler: () => ((c: Context) => Response);
|
|
32
32
|
export { notFoundHandler };
|
|
33
33
|
//# sourceMappingURL=not-found.d.ts.map
|
package/dist/not-found.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"not-found.js","names":["code: ResponseErrorCode","ResponseErrorCode","createJsonResponse","getResponseErrorMessage"],"sources":["../src/handlers/not-found.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"not-found.js","names":["code: ResponseErrorCode","ResponseErrorCode","createJsonResponse","getResponseErrorMessage"],"sources":["../src/handlers/not-found.ts"],"sourcesContent":["/**\n * Not found handler module\n * @module handlers/not-found\n */\n\nimport type { Context } from \"hono\";\n\nimport { createJsonResponse } from \"#/response\";\nimport { getResponseErrorMessage, ResponseErrorCode } from \"#/response/error\";\n\n/**\n * Not found handler.\n *\n * Following response will be returned on route not found:\n *\n * ```jsonc\n * // Status: 404\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"not_found\",\n * \"message\": \"Content not found\"\n * }\n * ]\n * }\n * ```\n *\n * ### Example\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { notFoundHandler } from \"@jderstd/hono/not-found\";\n *\n * const app: Hono = new Hono();\n *\n * app.notFound(notFoundHandler());\n * ```\n */\nconst notFoundHandler = (): ((c: Context) => Response) => {\n const code: ResponseErrorCode = ResponseErrorCode.NotFound;\n\n return (c: Context): Response => {\n return createJsonResponse(c, {\n status: 404,\n errors: [\n {\n code,\n message: getResponseErrorMessage(code),\n },\n ],\n });\n };\n};\n\nexport { notFoundHandler };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,MAAM,wBAAoD;CACtD,MAAMA,OAA0BC,+CAAkB;AAElD,SAAQ,MAAyB;AAC7B,SAAOC,iCAAmB,GAAG;GACzB,QAAQ;GACR,QAAQ,CACJ;IACI;IACA,SAASC,qDAAwB,KAAK;IACzC,CACJ;GACJ,CAAC"}
|
package/dist/not-found.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"not-found.mjs","names":["code: ResponseErrorCode"],"sources":["../src/handlers/not-found.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"not-found.mjs","names":["code: ResponseErrorCode"],"sources":["../src/handlers/not-found.ts"],"sourcesContent":["/**\n * Not found handler module\n * @module handlers/not-found\n */\n\nimport type { Context } from \"hono\";\n\nimport { createJsonResponse } from \"#/response\";\nimport { getResponseErrorMessage, ResponseErrorCode } from \"#/response/error\";\n\n/**\n * Not found handler.\n *\n * Following response will be returned on route not found:\n *\n * ```jsonc\n * // Status: 404\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"not_found\",\n * \"message\": \"Content not found\"\n * }\n * ]\n * }\n * ```\n *\n * ### Example\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { notFoundHandler } from \"@jderstd/hono/not-found\";\n *\n * const app: Hono = new Hono();\n *\n * app.notFound(notFoundHandler());\n * ```\n */\nconst notFoundHandler = (): ((c: Context) => Response) => {\n const code: ResponseErrorCode = ResponseErrorCode.NotFound;\n\n return (c: Context): Response => {\n return createJsonResponse(c, {\n status: 404,\n errors: [\n {\n code,\n message: getResponseErrorMessage(code),\n },\n ],\n });\n };\n};\n\nexport { notFoundHandler };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,MAAM,wBAAoD;CACtD,MAAMA,OAA0B,kBAAkB;AAElD,SAAQ,MAAyB;AAC7B,SAAO,mBAAmB,GAAG;GACzB,QAAQ;GACR,QAAQ,CACJ;IACI;IACA,SAAS,wBAAwB,KAAK;IACzC,CACJ;GACJ,CAAC"}
|
package/dist/on-error.d.ts
CHANGED
|
@@ -3,64 +3,64 @@ import { HTTPResponseError } from "hono/types";
|
|
|
3
3
|
/** Options for `onErrorHandler` function. */
|
|
4
4
|
type OnErrorHandlerOptions = {
|
|
5
5
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
* Whether show more information.
|
|
7
|
+
* By default, it's `false`.
|
|
8
|
+
*/
|
|
9
9
|
verbose?: boolean;
|
|
10
10
|
};
|
|
11
11
|
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
12
|
+
* On error handler.
|
|
13
|
+
*
|
|
14
|
+
* Following responses could be returned on error:
|
|
15
|
+
*
|
|
16
|
+
* ```jsonc
|
|
17
|
+
* // Status: 400
|
|
18
|
+
* {
|
|
19
|
+
* "success": false,
|
|
20
|
+
* "errors": [
|
|
21
|
+
* {
|
|
22
|
+
* "code": "bad_request"
|
|
23
|
+
* }
|
|
24
|
+
* ]
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* ```jsonc
|
|
29
|
+
* // Status: 500
|
|
30
|
+
* {
|
|
31
|
+
* "success": false,
|
|
32
|
+
* "errors": [
|
|
33
|
+
* {
|
|
34
|
+
* "code": "server"
|
|
35
|
+
* }
|
|
36
|
+
* ]
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* ### Examples
|
|
41
|
+
*
|
|
42
|
+
* Basic example of using `onErrorHandler` handler:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { Hono } from "hono";
|
|
46
|
+
* import { onErrorHandler } from "@jderstd/hono/on-error";
|
|
47
|
+
*
|
|
48
|
+
* const app: Hono = new Hono();
|
|
49
|
+
*
|
|
50
|
+
* app.onError(onErrorHandler());
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* Show more information with `verbose` option:
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { Hono } from "hono";
|
|
57
|
+
* import { onErrorHandler } from "@jderstd/hono/on-error";
|
|
58
|
+
*
|
|
59
|
+
* const app: Hono = new Hono();
|
|
60
|
+
*
|
|
61
|
+
* app.onError(onErrorHandler({ verbose: true }));
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
64
|
declare const onErrorHandler: (options?: OnErrorHandlerOptions) => ((err: Error | HTTPResponseError, c: Context) => Response);
|
|
65
|
-
export { onErrorHandler };
|
|
65
|
+
export { type OnErrorHandlerOptions, onErrorHandler };
|
|
66
66
|
//# sourceMappingURL=on-error.d.ts.map
|
package/dist/on-error.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"on-error.js","names":["JderHttpException","HTTPException","res: Response","code: ResponseErrorCode","ResponseErrorCode","createJsonResponse"],"sources":["../src/handlers/on-error.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"on-error.js","names":["JderHttpException","HTTPException","res: Response","code: ResponseErrorCode","ResponseErrorCode","createJsonResponse"],"sources":["../src/handlers/on-error.ts"],"sourcesContent":["/**\n * On error handler module\n * @module handlers/on-error\n */\n\nimport type { Context } from \"hono\";\nimport type { HTTPResponseError } from \"hono/types\";\nimport type { StatusCode } from \"hono/utils/http-status\";\n\nimport { HTTPException } from \"hono/http-exception\";\n\nimport { createJsonResponse } from \"#/response\";\nimport { ResponseErrorCode } from \"#/response/error\";\nimport { JderHttpException } from \"#/response/error/http\";\n\n/** Options for `onErrorHandler` function. */\ntype OnErrorHandlerOptions = {\n /**\n * Whether show more information.\n * By default, it's `false`.\n */\n verbose?: boolean;\n};\n\n/**\n * On error handler.\n *\n * Following responses could be returned on error:\n *\n * ```jsonc\n * // Status: 400\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"bad_request\"\n * }\n * ]\n * }\n * ```\n *\n * ```jsonc\n * // Status: 500\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"server\"\n * }\n * ]\n * }\n * ```\n *\n * ### Examples\n *\n * Basic example of using `onErrorHandler` handler:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { onErrorHandler } from \"@jderstd/hono/on-error\";\n *\n * const app: Hono = new Hono();\n *\n * app.onError(onErrorHandler());\n * ```\n *\n * Show more information with `verbose` option:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { onErrorHandler } from \"@jderstd/hono/on-error\";\n *\n * const app: Hono = new Hono();\n *\n * app.onError(onErrorHandler({ verbose: true }));\n * ```\n */\nconst onErrorHandler = (\n options?: OnErrorHandlerOptions,\n): ((err: Error | HTTPResponseError, c: Context) => Response) => {\n return (err: Error | HTTPResponseError, c: Context): Response => {\n // HTTP Exception for JDER format response\n if (err instanceof JderHttpException) return err.getResponse();\n\n // HTTP Exception\n if (err instanceof HTTPException) {\n const res: Response = err.getResponse();\n\n const status: StatusCode = res.status as StatusCode;\n\n const code: ResponseErrorCode =\n status >= 500\n ? ResponseErrorCode.Server\n : ResponseErrorCode.BadRequest;\n\n return createJsonResponse(c, {\n status: res.status as StatusCode,\n errors: [\n {\n code,\n ...(options?.verbose && {\n message: err.message,\n }),\n },\n ],\n });\n }\n\n // Internal server error\n return createJsonResponse(c, {\n status: 500,\n errors: [\n {\n code: ResponseErrorCode.Server,\n ...(options?.verbose && {\n message: err.message,\n }),\n },\n ],\n });\n };\n};\n\nexport type { OnErrorHandlerOptions };\nexport { onErrorHandler };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6EA,MAAM,kBACF,YAC6D;AAC7D,SAAQ,KAAgC,MAAyB;AAE7D,MAAI,eAAeA,8CAAmB,QAAO,IAAI,aAAa;AAG9D,MAAI,eAAeC,mCAAe;GAC9B,MAAMC,MAAgB,IAAI,aAAa;GAIvC,MAAMC,OAFqB,IAAI,UAGjB,MACJC,+CAAkB,SAClBA,+CAAkB;AAE5B,UAAOC,iCAAmB,GAAG;IACzB,QAAQ,IAAI;IACZ,QAAQ,CACJ;KACI;KACA,GAAI,SAAS,WAAW,EACpB,SAAS,IAAI,SAChB;KACJ,CACJ;IACJ,CAAC;;AAIN,SAAOA,iCAAmB,GAAG;GACzB,QAAQ;GACR,QAAQ,CACJ;IACI,MAAMD,+CAAkB;IACxB,GAAI,SAAS,WAAW,EACpB,SAAS,IAAI,SAChB;IACJ,CACJ;GACJ,CAAC"}
|
package/dist/on-error.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"on-error.mjs","names":["res: Response","code: ResponseErrorCode"],"sources":["../src/handlers/on-error.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"on-error.mjs","names":["res: Response","code: ResponseErrorCode"],"sources":["../src/handlers/on-error.ts"],"sourcesContent":["/**\n * On error handler module\n * @module handlers/on-error\n */\n\nimport type { Context } from \"hono\";\nimport type { HTTPResponseError } from \"hono/types\";\nimport type { StatusCode } from \"hono/utils/http-status\";\n\nimport { HTTPException } from \"hono/http-exception\";\n\nimport { createJsonResponse } from \"#/response\";\nimport { ResponseErrorCode } from \"#/response/error\";\nimport { JderHttpException } from \"#/response/error/http\";\n\n/** Options for `onErrorHandler` function. */\ntype OnErrorHandlerOptions = {\n /**\n * Whether show more information.\n * By default, it's `false`.\n */\n verbose?: boolean;\n};\n\n/**\n * On error handler.\n *\n * Following responses could be returned on error:\n *\n * ```jsonc\n * // Status: 400\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"bad_request\"\n * }\n * ]\n * }\n * ```\n *\n * ```jsonc\n * // Status: 500\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"server\"\n * }\n * ]\n * }\n * ```\n *\n * ### Examples\n *\n * Basic example of using `onErrorHandler` handler:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { onErrorHandler } from \"@jderstd/hono/on-error\";\n *\n * const app: Hono = new Hono();\n *\n * app.onError(onErrorHandler());\n * ```\n *\n * Show more information with `verbose` option:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { onErrorHandler } from \"@jderstd/hono/on-error\";\n *\n * const app: Hono = new Hono();\n *\n * app.onError(onErrorHandler({ verbose: true }));\n * ```\n */\nconst onErrorHandler = (\n options?: OnErrorHandlerOptions,\n): ((err: Error | HTTPResponseError, c: Context) => Response) => {\n return (err: Error | HTTPResponseError, c: Context): Response => {\n // HTTP Exception for JDER format response\n if (err instanceof JderHttpException) return err.getResponse();\n\n // HTTP Exception\n if (err instanceof HTTPException) {\n const res: Response = err.getResponse();\n\n const status: StatusCode = res.status as StatusCode;\n\n const code: ResponseErrorCode =\n status >= 500\n ? ResponseErrorCode.Server\n : ResponseErrorCode.BadRequest;\n\n return createJsonResponse(c, {\n status: res.status as StatusCode,\n errors: [\n {\n code,\n ...(options?.verbose && {\n message: err.message,\n }),\n },\n ],\n });\n }\n\n // Internal server error\n return createJsonResponse(c, {\n status: 500,\n errors: [\n {\n code: ResponseErrorCode.Server,\n ...(options?.verbose && {\n message: err.message,\n }),\n },\n ],\n });\n };\n};\n\nexport type { OnErrorHandlerOptions };\nexport { onErrorHandler };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6EA,MAAM,kBACF,YAC6D;AAC7D,SAAQ,KAAgC,MAAyB;AAE7D,MAAI,eAAe,kBAAmB,QAAO,IAAI,aAAa;AAG9D,MAAI,eAAe,eAAe;GAC9B,MAAMA,MAAgB,IAAI,aAAa;GAIvC,MAAMC,OAFqB,IAAI,UAGjB,MACJ,kBAAkB,SAClB,kBAAkB;AAE5B,UAAO,mBAAmB,GAAG;IACzB,QAAQ,IAAI;IACZ,QAAQ,CACJ;KACI;KACA,GAAI,SAAS,WAAW,EACpB,SAAS,IAAI,SAChB;KACJ,CACJ;IACJ,CAAC;;AAIN,SAAO,mBAAmB,GAAG;GACzB,QAAQ;GACR,QAAQ,CACJ;IACI,MAAM,kBAAkB;IACxB,GAAI,SAAS,WAAW,EACpB,SAAS,IAAI,SAChB;IACJ,CACJ;GACJ,CAAC"}
|