@jderstd/hono 0.6.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/LICENSE +21 -0
- package/README.md +29 -0
- package/dist/_virtual/rolldown_runtime.js +23 -0
- package/dist/body-limit.d.ts +66 -0
- package/dist/body-limit.js +79 -0
- package/dist/body-limit.js.map +1 -0
- package/dist/body-limit.mjs +76 -0
- package/dist/body-limit.mjs.map +1 -0
- package/dist/ip-limit.d.ts +99 -0
- package/dist/ip-limit.js +30 -0
- package/dist/ip-limit.js.map +1 -0
- package/dist/ip-limit.mjs +28 -0
- package/dist/ip-limit.mjs.map +1 -0
- package/dist/not-found.d.ts +33 -0
- package/dist/not-found.js +48 -0
- package/dist/not-found.js.map +1 -0
- package/dist/not-found.mjs +48 -0
- package/dist/not-found.mjs.map +1 -0
- package/dist/on-error.d.ts +66 -0
- package/dist/on-error.js +85 -0
- package/dist/on-error.js.map +1 -0
- package/dist/on-error.mjs +83 -0
- package/dist/on-error.mjs.map +1 -0
- package/dist/response/common/index.d.ts +82 -0
- package/dist/response/common/index.js +21 -0
- package/dist/response/common/index.js.map +1 -0
- package/dist/response/common/index.mjs +19 -0
- package/dist/response/common/index.mjs.map +1 -0
- package/dist/response/error.d.ts +53 -0
- package/dist/response/error.js +67 -0
- package/dist/response/error.js.map +1 -0
- package/dist/response/error.mjs +65 -0
- package/dist/response/error.mjs.map +1 -0
- package/dist/response/json/index.d.ts +110 -0
- package/dist/response/json/index.js +23 -0
- package/dist/response/json/index.js.map +1 -0
- package/dist/response/json/index.mjs +20 -0
- package/dist/response/json/index.mjs.map +1 -0
- package/dist/response.d.ts +4 -0
- package/dist/response.js +5 -0
- package/dist/response.mjs +4 -0
- package/dist/time-limit.d.ts +62 -0
- package/dist/time-limit.js +74 -0
- package/dist/time-limit.js.map +1 -0
- package/dist/time-limit.mjs +70 -0
- package/dist/time-limit.mjs.map +1 -0
- package/package.json +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present, Alpheus
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# JDER Hono
|
|
2
|
+
|
|
3
|
+
A response builder for Hono.
|
|
4
|
+
|
|
5
|
+
This package includes different response builders based on the JSON response structure specified in [JSON Data Errors Response (JDER)](https://github.com/jderstd/spec). With the builders, various kinds of responses can be created easily instead of sending plain text responses.
|
|
6
|
+
|
|
7
|
+
## Quick Start
|
|
8
|
+
|
|
9
|
+
To create a JSON response, use the following code:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createJsonResponse } from "@jderstd/hono/response";
|
|
13
|
+
|
|
14
|
+
const route = (): Response => {
|
|
15
|
+
return createJsonResponse();
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
And the response will be shown as below:
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{
|
|
23
|
+
"success": true
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## License
|
|
28
|
+
|
|
29
|
+
This project is licensed under the terms of the MIT license.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __copyProps = (to, from, except, desc) => {
|
|
8
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
9
|
+
key = keys[i];
|
|
10
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
11
|
+
get: ((k) => from[k]).bind(null, key),
|
|
12
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
18
|
+
value: mod,
|
|
19
|
+
enumerable: true
|
|
20
|
+
}) : target, mod));
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
exports.__toESM = __toESM;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { MiddlewareHandler } from "hono";
|
|
2
|
+
/** Default maximum body size in bytes. */
|
|
3
|
+
declare const BODY_LIMIT_MAX_DEFAULT: 10485760;
|
|
4
|
+
/** Options for `bodyLimit` middleware. */
|
|
5
|
+
type BodyLimitOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Maximum body size in bytes.
|
|
8
|
+
*
|
|
9
|
+
* By default, it is `BODY_LIMIT_MAX_DEFAULT`.
|
|
10
|
+
*/
|
|
11
|
+
max?: number;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
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
|
+
declare const bodyLimit: (options?: BodyLimitOptions) => MiddlewareHandler;
|
|
65
|
+
export { BODY_LIMIT_MAX_DEFAULT, type BodyLimitOptions, bodyLimit };
|
|
66
|
+
//# sourceMappingURL=body-limit.d.ts.map
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
|
|
2
|
+
const require_index = require('./response/json/index.js');
|
|
3
|
+
const require_response_error = require('./response/error.js');
|
|
4
|
+
let hono_body_limit = require("hono/body-limit");
|
|
5
|
+
hono_body_limit = require_rolldown_runtime.__toESM(hono_body_limit);
|
|
6
|
+
|
|
7
|
+
/** Default maximum body size in bytes. */
|
|
8
|
+
const BODY_LIMIT_MAX_DEFAULT = 10 * 1024 * 1024;
|
|
9
|
+
/**
|
|
10
|
+
* Body limit middleware.
|
|
11
|
+
*
|
|
12
|
+
* Following error will be returned if the body size is over the limit:
|
|
13
|
+
*
|
|
14
|
+
* ```jsonc
|
|
15
|
+
* // Status: 413
|
|
16
|
+
* {
|
|
17
|
+
* "success": false,
|
|
18
|
+
* "errors": [
|
|
19
|
+
* {
|
|
20
|
+
* "code": "too_large",
|
|
21
|
+
* "path": [
|
|
22
|
+
* "request",
|
|
23
|
+
* "body"
|
|
24
|
+
* ],
|
|
25
|
+
* "message": "Request body is too large"
|
|
26
|
+
* }
|
|
27
|
+
* ]
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* For more information, please refer to
|
|
32
|
+
* [Body Limit](https://hono.dev/docs/middleware/builtin/body-limit).
|
|
33
|
+
*
|
|
34
|
+
* ### Examples
|
|
35
|
+
*
|
|
36
|
+
* A example of using `bodyLimit` middleware:
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { Hono } from "hono";
|
|
40
|
+
* import { bodyLimit } from "@jderstd/hono/body-limit";
|
|
41
|
+
*
|
|
42
|
+
* const app: Hono = new Hono();
|
|
43
|
+
*
|
|
44
|
+
* app.use(bodyLimit());
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* A example of using `bodyLimit` middleware with options:
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { Hono } from "hono";
|
|
51
|
+
* import { bodyLimit } from "@jderstd/hono/body-limit";
|
|
52
|
+
*
|
|
53
|
+
* const app: Hono = new Hono();
|
|
54
|
+
*
|
|
55
|
+
* app.use(bodyLimit({
|
|
56
|
+
* max: 20 * 1024 * 1024, // 20MiB
|
|
57
|
+
* }));
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
const bodyLimit = (options) => {
|
|
61
|
+
const code = require_response_error.ResponseErrorCode.TooLarge;
|
|
62
|
+
return (0, hono_body_limit.bodyLimit)({
|
|
63
|
+
maxSize: options?.max ?? BODY_LIMIT_MAX_DEFAULT,
|
|
64
|
+
onError: (c) => {
|
|
65
|
+
return require_index.createJsonResponse(c, {
|
|
66
|
+
status: 413,
|
|
67
|
+
errors: [{
|
|
68
|
+
code,
|
|
69
|
+
path: ["request", "body"],
|
|
70
|
+
message: require_response_error.getResponseErrorMessage(code)
|
|
71
|
+
}]
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
exports.BODY_LIMIT_MAX_DEFAULT = BODY_LIMIT_MAX_DEFAULT;
|
|
78
|
+
exports.bodyLimit = bodyLimit;
|
|
79
|
+
//# sourceMappingURL=body-limit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body-limit.js","names":["code: ResponseErrorCode","ResponseErrorCode","createJsonResponse","getResponseErrorMessage"],"sources":["../src/middlewares/body-limit.ts"],"sourcesContent":["/**\n * Body limit module\n * @module middlewares/body-limit\n */\n\nimport type { Context, MiddlewareHandler } from \"hono\";\n\nimport { bodyLimit as _bodyLimit } from \"hono/body-limit\";\n\nimport { getResponseErrorMessage, ResponseErrorCode } from \"#/response/error\";\nimport { createJsonResponse } from \"#/response/json\";\n\n/** Default maximum body size in bytes. */\nconst BODY_LIMIT_MAX_DEFAULT = (10 * 1024 * 1024) as 10485760;\n\n/** Options for `bodyLimit` middleware. */\ntype BodyLimitOptions = {\n /**\n * Maximum body size in bytes.\n *\n * By default, it is `BODY_LIMIT_MAX_DEFAULT`.\n */\n max?: number;\n};\n\n/**\n * Body limit middleware.\n *\n * Following error will be returned if the body size is over the limit:\n *\n * ```jsonc\n * // Status: 413\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"too_large\",\n * \"path\": [\n * \"request\",\n * \"body\"\n * ],\n * \"message\": \"Request body is too large\"\n * }\n * ]\n * }\n * ```\n *\n * For more information, please refer to\n * [Body Limit](https://hono.dev/docs/middleware/builtin/body-limit).\n *\n * ### Examples\n *\n * A example of using `bodyLimit` middleware:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { bodyLimit } from \"@jderstd/hono/body-limit\";\n *\n * const app: Hono = new Hono();\n *\n * app.use(bodyLimit());\n * ```\n *\n * A example of using `bodyLimit` middleware with options:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { bodyLimit } from \"@jderstd/hono/body-limit\";\n *\n * const app: Hono = new Hono();\n *\n * app.use(bodyLimit({\n * max: 20 * 1024 * 1024, // 20MiB\n * }));\n * ```\n */\nconst bodyLimit = (options?: BodyLimitOptions): MiddlewareHandler => {\n const code: ResponseErrorCode = ResponseErrorCode.TooLarge;\n\n return _bodyLimit({\n maxSize: options?.max ?? BODY_LIMIT_MAX_DEFAULT,\n onError: (c: Context): Response => {\n return createJsonResponse(c, {\n status: 413,\n errors: [\n {\n code,\n path: [\n \"request\",\n \"body\",\n ],\n message: getResponseErrorMessage(code),\n },\n ],\n });\n },\n });\n};\n\nexport type { BodyLimitOptions };\nexport { bodyLimit, BODY_LIMIT_MAX_DEFAULT };\n"],"mappings":";;;;;;;AAaA,MAAM,yBAA0B,KAAK,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+D5C,MAAM,aAAa,YAAkD;CACjE,MAAMA,OAA0BC,yCAAkB;AAElD,uCAAkB;EACd,SAAS,SAAS,OAAO;EACzB,UAAU,MAAyB;AAC/B,UAAOC,iCAAmB,GAAG;IACzB,QAAQ;IACR,QAAQ,CACJ;KACI;KACA,MAAM,CACF,WACA,OACH;KACD,SAASC,+CAAwB,KAAK;KACzC,CACJ;IACJ,CAAC;;EAET,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { createJsonResponse } from "./response/json/index.mjs";
|
|
2
|
+
import { ResponseErrorCode, getResponseErrorMessage } from "./response/error.mjs";
|
|
3
|
+
import { bodyLimit as bodyLimit$1 } from "hono/body-limit";
|
|
4
|
+
|
|
5
|
+
/** Default maximum body size in bytes. */
|
|
6
|
+
const BODY_LIMIT_MAX_DEFAULT = 10 * 1024 * 1024;
|
|
7
|
+
/**
|
|
8
|
+
* Body limit middleware.
|
|
9
|
+
*
|
|
10
|
+
* Following error will be returned if the body size is over the limit:
|
|
11
|
+
*
|
|
12
|
+
* ```jsonc
|
|
13
|
+
* // Status: 413
|
|
14
|
+
* {
|
|
15
|
+
* "success": false,
|
|
16
|
+
* "errors": [
|
|
17
|
+
* {
|
|
18
|
+
* "code": "too_large",
|
|
19
|
+
* "path": [
|
|
20
|
+
* "request",
|
|
21
|
+
* "body"
|
|
22
|
+
* ],
|
|
23
|
+
* "message": "Request body is too large"
|
|
24
|
+
* }
|
|
25
|
+
* ]
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* For more information, please refer to
|
|
30
|
+
* [Body Limit](https://hono.dev/docs/middleware/builtin/body-limit).
|
|
31
|
+
*
|
|
32
|
+
* ### Examples
|
|
33
|
+
*
|
|
34
|
+
* A example of using `bodyLimit` middleware:
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { Hono } from "hono";
|
|
38
|
+
* import { bodyLimit } from "@jderstd/hono/body-limit";
|
|
39
|
+
*
|
|
40
|
+
* const app: Hono = new Hono();
|
|
41
|
+
*
|
|
42
|
+
* app.use(bodyLimit());
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* A example of using `bodyLimit` middleware with options:
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { Hono } from "hono";
|
|
49
|
+
* import { bodyLimit } from "@jderstd/hono/body-limit";
|
|
50
|
+
*
|
|
51
|
+
* const app: Hono = new Hono();
|
|
52
|
+
*
|
|
53
|
+
* app.use(bodyLimit({
|
|
54
|
+
* max: 20 * 1024 * 1024, // 20MiB
|
|
55
|
+
* }));
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
const bodyLimit = (options) => {
|
|
59
|
+
const code = ResponseErrorCode.TooLarge;
|
|
60
|
+
return bodyLimit$1({
|
|
61
|
+
maxSize: options?.max ?? BODY_LIMIT_MAX_DEFAULT,
|
|
62
|
+
onError: (c) => {
|
|
63
|
+
return createJsonResponse(c, {
|
|
64
|
+
status: 413,
|
|
65
|
+
errors: [{
|
|
66
|
+
code,
|
|
67
|
+
path: ["request", "body"],
|
|
68
|
+
message: getResponseErrorMessage(code)
|
|
69
|
+
}]
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export { BODY_LIMIT_MAX_DEFAULT, bodyLimit };
|
|
76
|
+
//# sourceMappingURL=body-limit.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body-limit.mjs","names":["code: ResponseErrorCode","_bodyLimit"],"sources":["../src/middlewares/body-limit.ts"],"sourcesContent":["/**\n * Body limit module\n * @module middlewares/body-limit\n */\n\nimport type { Context, MiddlewareHandler } from \"hono\";\n\nimport { bodyLimit as _bodyLimit } from \"hono/body-limit\";\n\nimport { getResponseErrorMessage, ResponseErrorCode } from \"#/response/error\";\nimport { createJsonResponse } from \"#/response/json\";\n\n/** Default maximum body size in bytes. */\nconst BODY_LIMIT_MAX_DEFAULT = (10 * 1024 * 1024) as 10485760;\n\n/** Options for `bodyLimit` middleware. */\ntype BodyLimitOptions = {\n /**\n * Maximum body size in bytes.\n *\n * By default, it is `BODY_LIMIT_MAX_DEFAULT`.\n */\n max?: number;\n};\n\n/**\n * Body limit middleware.\n *\n * Following error will be returned if the body size is over the limit:\n *\n * ```jsonc\n * // Status: 413\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"too_large\",\n * \"path\": [\n * \"request\",\n * \"body\"\n * ],\n * \"message\": \"Request body is too large\"\n * }\n * ]\n * }\n * ```\n *\n * For more information, please refer to\n * [Body Limit](https://hono.dev/docs/middleware/builtin/body-limit).\n *\n * ### Examples\n *\n * A example of using `bodyLimit` middleware:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { bodyLimit } from \"@jderstd/hono/body-limit\";\n *\n * const app: Hono = new Hono();\n *\n * app.use(bodyLimit());\n * ```\n *\n * A example of using `bodyLimit` middleware with options:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { bodyLimit } from \"@jderstd/hono/body-limit\";\n *\n * const app: Hono = new Hono();\n *\n * app.use(bodyLimit({\n * max: 20 * 1024 * 1024, // 20MiB\n * }));\n * ```\n */\nconst bodyLimit = (options?: BodyLimitOptions): MiddlewareHandler => {\n const code: ResponseErrorCode = ResponseErrorCode.TooLarge;\n\n return _bodyLimit({\n maxSize: options?.max ?? BODY_LIMIT_MAX_DEFAULT,\n onError: (c: Context): Response => {\n return createJsonResponse(c, {\n status: 413,\n errors: [\n {\n code,\n path: [\n \"request\",\n \"body\",\n ],\n message: getResponseErrorMessage(code),\n },\n ],\n });\n },\n });\n};\n\nexport type { BodyLimitOptions };\nexport { bodyLimit, BODY_LIMIT_MAX_DEFAULT };\n"],"mappings":";;;;;AAaA,MAAM,yBAA0B,KAAK,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+D5C,MAAM,aAAa,YAAkD;CACjE,MAAMA,OAA0B,kBAAkB;AAElD,QAAOC,YAAW;EACd,SAAS,SAAS,OAAO;EACzB,UAAU,MAAyB;AAC/B,UAAO,mBAAmB,GAAG;IACzB,QAAQ;IACR,QAAQ,CACJ;KACI;KACA,MAAM,CACF,WACA,OACH;KACD,SAAS,wBAAwB,KAAK;KACzC,CACJ;IACJ,CAAC;;EAET,CAAC"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { Context, MiddlewareHandler } from "hono";
|
|
2
|
+
import { AddressType, ConnInfo, GetConnInfo, NetAddrInfo } from "hono/conninfo";
|
|
3
|
+
import { IPRestrictionRule } from "hono/ip-restriction";
|
|
4
|
+
import { Format } from "ts-vista";
|
|
5
|
+
type GetIPAddr = GetConnInfo | ((c: Context) => string);
|
|
6
|
+
/** Base options for `ipLimit` middleware. */
|
|
7
|
+
type IpLimitBaseOptions = {
|
|
8
|
+
/** Allowed IP addresses. */
|
|
9
|
+
allowList?: IPRestrictionRule[];
|
|
10
|
+
/** Denied IP addresses. */
|
|
11
|
+
denyList?: IPRestrictionRule[];
|
|
12
|
+
/**
|
|
13
|
+
* Whether show more information.
|
|
14
|
+
* By default, it's `false`.
|
|
15
|
+
*/
|
|
16
|
+
verbose?: boolean;
|
|
17
|
+
};
|
|
18
|
+
/** Options for `ipLimit` middleware. */
|
|
19
|
+
type IpLimitOptions = Format<{
|
|
20
|
+
/** Function to get IP address. */
|
|
21
|
+
getConnInfo: GetIPAddr;
|
|
22
|
+
} & IpLimitBaseOptions>;
|
|
23
|
+
/**
|
|
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
|
+
declare function ipLimit(options: IpLimitOptions): MiddlewareHandler;
|
|
86
|
+
/**
|
|
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
|
+
declare function ipLimit(getConnInfo: GetIPAddr, options?: IpLimitBaseOptions): MiddlewareHandler;
|
|
98
|
+
export { type AddressType, type ConnInfo, type GetConnInfo, type GetIPAddr, type IPRestrictionRule, type IpLimitBaseOptions, type IpLimitOptions, type NetAddrInfo, ipLimit };
|
|
99
|
+
//# sourceMappingURL=ip-limit.d.ts.map
|
package/dist/ip-limit.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
|
|
2
|
+
const require_index = require('./response/json/index.js');
|
|
3
|
+
require('./response.js');
|
|
4
|
+
const require_response_error = require('./response/error.js');
|
|
5
|
+
let hono_ip_restriction = require("hono/ip-restriction");
|
|
6
|
+
hono_ip_restriction = require_rolldown_runtime.__toESM(hono_ip_restriction);
|
|
7
|
+
|
|
8
|
+
function ipLimit(getConnInfoOrOptions, options) {
|
|
9
|
+
const code = require_response_error.ResponseErrorCode.Forbidden;
|
|
10
|
+
const getConnInfo = typeof getConnInfoOrOptions === "function" ? getConnInfoOrOptions : getConnInfoOrOptions.getConnInfo;
|
|
11
|
+
const { denyList, allowList, verbose } = (typeof getConnInfoOrOptions === "function" ? options : getConnInfoOrOptions) ?? {};
|
|
12
|
+
return (0, hono_ip_restriction.ipRestriction)(getConnInfo, {
|
|
13
|
+
denyList,
|
|
14
|
+
allowList
|
|
15
|
+
}, ({ addr }, c) => {
|
|
16
|
+
return require_index.createJsonResponse(c, {
|
|
17
|
+
status: 403,
|
|
18
|
+
errors: [{
|
|
19
|
+
code,
|
|
20
|
+
...verbose ? {
|
|
21
|
+
path: ["request", "ip"],
|
|
22
|
+
message: `${require_response_error.getResponseErrorMessage(code)}: ${addr}`
|
|
23
|
+
} : {}
|
|
24
|
+
}]
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
exports.ipLimit = ipLimit;
|
|
30
|
+
//# sourceMappingURL=ip-limit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ip-limit.js","names":["code: ResponseErrorCode","ResponseErrorCode","getConnInfo: GetIPAddr","createJsonResponse","getResponseErrorMessage"],"sources":["../src/middlewares/ip-limit.ts"],"sourcesContent":["/**\n * IP limit module\n * @module middlewares/ip-limit\n */\n\nimport type { Context, MiddlewareHandler } from \"hono\";\nimport type {\n AddressType,\n ConnInfo,\n GetConnInfo,\n NetAddrInfo,\n} from \"hono/conninfo\";\nimport type { IPRestrictionRule } from \"hono/ip-restriction\";\nimport type { Format } from \"ts-vista\";\n\nimport { ipRestriction } from \"hono/ip-restriction\";\n\nimport { createJsonResponse } from \"#/response\";\nimport { getResponseErrorMessage, ResponseErrorCode } from \"#/response/error\";\n\ntype GetIPAddr = GetConnInfo | ((c: Context) => string);\n\n/** Base options for `ipLimit` middleware. */\ntype IpLimitBaseOptions = {\n /** Allowed IP addresses. */\n allowList?: IPRestrictionRule[];\n /** Denied IP addresses. */\n denyList?: IPRestrictionRule[];\n /**\n * Whether show more information.\n * By default, it's `false`.\n */\n verbose?: boolean;\n};\n\n/** Options for `ipLimit` middleware. */\ntype IpLimitOptions = Format<\n {\n /** Function to get IP address. */\n getConnInfo: GetIPAddr;\n } & IpLimitBaseOptions\n>;\n\n/**\n * IP limit middleware.\n *\n * Following error will be returned if the IP address is not allowed:\n *\n * ```jsonc\n * // Status: 403\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"forbidden\"\n * }\n * ]\n * }\n * ```\n *\n * When `verbose` is `true`, the error will be like:\n *\n * ```jsonc\n * // Status: 403\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"forbidden\",\n * \"path\": [\n * \"request\",\n * \"ip\"\n * ],\n * \"message\": \"Forbidden IP address: x.x.x.x\"\n * }\n * ]\n * }\n * ```\n *\n * For more information, please refer to\n * [IP Restriction](https://hono.dev/docs/middleware/builtin/ip-restriction).\n *\n * For `getConnInfo`, please refer to\n * [ConnInfo helper](https://hono.dev/docs/helpers/conninfo).\n *\n * ### Example\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { ipLimit } from \"@jderstd/hono/ip-limit\";\n *\n * // getConnInfo helper for Node.js\n * import { getConnInfo } from \"@hono/node-server/conninfo\";\n *\n * const app: Hono = new Hono();\n *\n * app.use(\n * ipLimit({\n * getConnInfo,\n * allowList: [],\n * denyList: [],\n * })\n * );\n * ```\n */\nfunction ipLimit(options: IpLimitOptions): MiddlewareHandler;\n\n/**\n * IP limit middleware for compatibility with `hono/ip-restriction`.\n *\n * This is functionally equivalent to:\n *\n * ```ts\n * ipLimit({ getConnInfo, ...options });\n * ```\n *\n * And it behaves the same as the main `ipLimit` function.\n */\nfunction ipLimit(\n getConnInfo: GetIPAddr,\n options?: IpLimitBaseOptions,\n): MiddlewareHandler;\n\nfunction ipLimit(\n getConnInfoOrOptions: GetIPAddr | IpLimitOptions,\n options?: IpLimitBaseOptions,\n): MiddlewareHandler {\n const code: ResponseErrorCode = ResponseErrorCode.Forbidden;\n\n const getConnInfo: GetIPAddr =\n typeof getConnInfoOrOptions === \"function\"\n ? getConnInfoOrOptions\n : getConnInfoOrOptions.getConnInfo;\n\n const { denyList, allowList, verbose }: IpLimitBaseOptions =\n (typeof getConnInfoOrOptions === \"function\"\n ? options\n : getConnInfoOrOptions) ?? {};\n\n return ipRestriction(\n getConnInfo,\n {\n denyList,\n allowList,\n },\n ({ addr }, c: Context): Response => {\n return createJsonResponse(c, {\n status: 403,\n errors: [\n {\n code,\n ...(verbose\n ? {\n path: [\n \"request\",\n \"ip\",\n ],\n message: `${getResponseErrorMessage(code)}: ${addr}`,\n }\n : {}),\n },\n ],\n });\n },\n );\n}\n\nexport type {\n AddressType,\n NetAddrInfo,\n ConnInfo,\n GetConnInfo,\n GetIPAddr,\n IPRestrictionRule,\n};\nexport type { IpLimitBaseOptions, IpLimitOptions };\nexport { ipLimit };\n"],"mappings":";;;;;;;AA2HA,SAAS,QACL,sBACA,SACiB;CACjB,MAAMA,OAA0BC,yCAAkB;CAElD,MAAMC,cACF,OAAO,yBAAyB,aAC1B,uBACA,qBAAqB;CAE/B,MAAM,EAAE,UAAU,WAAW,aACxB,OAAO,yBAAyB,aAC3B,UACA,yBAAyB,EAAE;AAErC,+CACI,aACA;EACI;EACA;EACH,GACA,EAAE,QAAQ,MAAyB;AAChC,SAAOC,iCAAmB,GAAG;GACzB,QAAQ;GACR,QAAQ,CACJ;IACI;IACA,GAAI,UACE;KACI,MAAM,CACF,WACA,KACH;KACD,SAAS,GAAGC,+CAAwB,KAAK,CAAC,IAAI;KACjD,GACD,EAAE;IACX,CACJ;GACJ,CAAC;GAET"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createJsonResponse } from "./response/json/index.mjs";
|
|
2
|
+
import "./response.mjs";
|
|
3
|
+
import { ResponseErrorCode, getResponseErrorMessage } from "./response/error.mjs";
|
|
4
|
+
import { ipRestriction } from "hono/ip-restriction";
|
|
5
|
+
|
|
6
|
+
function ipLimit(getConnInfoOrOptions, options) {
|
|
7
|
+
const code = ResponseErrorCode.Forbidden;
|
|
8
|
+
const getConnInfo = typeof getConnInfoOrOptions === "function" ? getConnInfoOrOptions : getConnInfoOrOptions.getConnInfo;
|
|
9
|
+
const { denyList, allowList, verbose } = (typeof getConnInfoOrOptions === "function" ? options : getConnInfoOrOptions) ?? {};
|
|
10
|
+
return ipRestriction(getConnInfo, {
|
|
11
|
+
denyList,
|
|
12
|
+
allowList
|
|
13
|
+
}, ({ addr }, c) => {
|
|
14
|
+
return createJsonResponse(c, {
|
|
15
|
+
status: 403,
|
|
16
|
+
errors: [{
|
|
17
|
+
code,
|
|
18
|
+
...verbose ? {
|
|
19
|
+
path: ["request", "ip"],
|
|
20
|
+
message: `${getResponseErrorMessage(code)}: ${addr}`
|
|
21
|
+
} : {}
|
|
22
|
+
}]
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { ipLimit };
|
|
28
|
+
//# sourceMappingURL=ip-limit.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ip-limit.mjs","names":["code: ResponseErrorCode","getConnInfo: GetIPAddr"],"sources":["../src/middlewares/ip-limit.ts"],"sourcesContent":["/**\n * IP limit module\n * @module middlewares/ip-limit\n */\n\nimport type { Context, MiddlewareHandler } from \"hono\";\nimport type {\n AddressType,\n ConnInfo,\n GetConnInfo,\n NetAddrInfo,\n} from \"hono/conninfo\";\nimport type { IPRestrictionRule } from \"hono/ip-restriction\";\nimport type { Format } from \"ts-vista\";\n\nimport { ipRestriction } from \"hono/ip-restriction\";\n\nimport { createJsonResponse } from \"#/response\";\nimport { getResponseErrorMessage, ResponseErrorCode } from \"#/response/error\";\n\ntype GetIPAddr = GetConnInfo | ((c: Context) => string);\n\n/** Base options for `ipLimit` middleware. */\ntype IpLimitBaseOptions = {\n /** Allowed IP addresses. */\n allowList?: IPRestrictionRule[];\n /** Denied IP addresses. */\n denyList?: IPRestrictionRule[];\n /**\n * Whether show more information.\n * By default, it's `false`.\n */\n verbose?: boolean;\n};\n\n/** Options for `ipLimit` middleware. */\ntype IpLimitOptions = Format<\n {\n /** Function to get IP address. */\n getConnInfo: GetIPAddr;\n } & IpLimitBaseOptions\n>;\n\n/**\n * IP limit middleware.\n *\n * Following error will be returned if the IP address is not allowed:\n *\n * ```jsonc\n * // Status: 403\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"forbidden\"\n * }\n * ]\n * }\n * ```\n *\n * When `verbose` is `true`, the error will be like:\n *\n * ```jsonc\n * // Status: 403\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"forbidden\",\n * \"path\": [\n * \"request\",\n * \"ip\"\n * ],\n * \"message\": \"Forbidden IP address: x.x.x.x\"\n * }\n * ]\n * }\n * ```\n *\n * For more information, please refer to\n * [IP Restriction](https://hono.dev/docs/middleware/builtin/ip-restriction).\n *\n * For `getConnInfo`, please refer to\n * [ConnInfo helper](https://hono.dev/docs/helpers/conninfo).\n *\n * ### Example\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { ipLimit } from \"@jderstd/hono/ip-limit\";\n *\n * // getConnInfo helper for Node.js\n * import { getConnInfo } from \"@hono/node-server/conninfo\";\n *\n * const app: Hono = new Hono();\n *\n * app.use(\n * ipLimit({\n * getConnInfo,\n * allowList: [],\n * denyList: [],\n * })\n * );\n * ```\n */\nfunction ipLimit(options: IpLimitOptions): MiddlewareHandler;\n\n/**\n * IP limit middleware for compatibility with `hono/ip-restriction`.\n *\n * This is functionally equivalent to:\n *\n * ```ts\n * ipLimit({ getConnInfo, ...options });\n * ```\n *\n * And it behaves the same as the main `ipLimit` function.\n */\nfunction ipLimit(\n getConnInfo: GetIPAddr,\n options?: IpLimitBaseOptions,\n): MiddlewareHandler;\n\nfunction ipLimit(\n getConnInfoOrOptions: GetIPAddr | IpLimitOptions,\n options?: IpLimitBaseOptions,\n): MiddlewareHandler {\n const code: ResponseErrorCode = ResponseErrorCode.Forbidden;\n\n const getConnInfo: GetIPAddr =\n typeof getConnInfoOrOptions === \"function\"\n ? getConnInfoOrOptions\n : getConnInfoOrOptions.getConnInfo;\n\n const { denyList, allowList, verbose }: IpLimitBaseOptions =\n (typeof getConnInfoOrOptions === \"function\"\n ? options\n : getConnInfoOrOptions) ?? {};\n\n return ipRestriction(\n getConnInfo,\n {\n denyList,\n allowList,\n },\n ({ addr }, c: Context): Response => {\n return createJsonResponse(c, {\n status: 403,\n errors: [\n {\n code,\n ...(verbose\n ? {\n path: [\n \"request\",\n \"ip\",\n ],\n message: `${getResponseErrorMessage(code)}: ${addr}`,\n }\n : {}),\n },\n ],\n });\n },\n );\n}\n\nexport type {\n AddressType,\n NetAddrInfo,\n ConnInfo,\n GetConnInfo,\n GetIPAddr,\n IPRestrictionRule,\n};\nexport type { IpLimitBaseOptions, IpLimitOptions };\nexport { ipLimit };\n"],"mappings":";;;;;AA2HA,SAAS,QACL,sBACA,SACiB;CACjB,MAAMA,OAA0B,kBAAkB;CAElD,MAAMC,cACF,OAAO,yBAAyB,aAC1B,uBACA,qBAAqB;CAE/B,MAAM,EAAE,UAAU,WAAW,aACxB,OAAO,yBAAyB,aAC3B,UACA,yBAAyB,EAAE;AAErC,QAAO,cACH,aACA;EACI;EACA;EACH,GACA,EAAE,QAAQ,MAAyB;AAChC,SAAO,mBAAmB,GAAG;GACzB,QAAQ;GACR,QAAQ,CACJ;IACI;IACA,GAAI,UACE;KACI,MAAM,CACF,WACA,KACH;KACD,SAAS,GAAG,wBAAwB,KAAK,CAAC,IAAI;KACjD,GACD,EAAE;IACX,CACJ;GACJ,CAAC;GAET"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Context } from "hono";
|
|
2
|
+
/**
|
|
3
|
+
* Not found handler.
|
|
4
|
+
*
|
|
5
|
+
* Following response will be reurned 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
|
+
declare const notFoundHandler: () => ((c: Context) => Response);
|
|
32
|
+
export { notFoundHandler };
|
|
33
|
+
//# sourceMappingURL=not-found.d.ts.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const require_index = require('./response/json/index.js');
|
|
2
|
+
require('./response.js');
|
|
3
|
+
const require_response_error = require('./response/error.js');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Not found handler.
|
|
7
|
+
*
|
|
8
|
+
* Following response will be reurned on route not found:
|
|
9
|
+
*
|
|
10
|
+
* ```jsonc
|
|
11
|
+
* // Status: 404
|
|
12
|
+
* {
|
|
13
|
+
* "success": false,
|
|
14
|
+
* "errors": [
|
|
15
|
+
* {
|
|
16
|
+
* "code": "not_found",
|
|
17
|
+
* "message": "Content not found"
|
|
18
|
+
* }
|
|
19
|
+
* ]
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* ### Example
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { Hono } from "hono";
|
|
27
|
+
* import { notFoundHandler } from "@jderstd/hono/not-found";
|
|
28
|
+
*
|
|
29
|
+
* const app: Hono = new Hono();
|
|
30
|
+
*
|
|
31
|
+
* app.notFound(notFoundHandler());
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
const notFoundHandler = () => {
|
|
35
|
+
const code = require_response_error.ResponseErrorCode.NotFound;
|
|
36
|
+
return (c) => {
|
|
37
|
+
return require_index.createJsonResponse(c, {
|
|
38
|
+
status: 404,
|
|
39
|
+
errors: [{
|
|
40
|
+
code,
|
|
41
|
+
message: require_response_error.getResponseErrorMessage(code)
|
|
42
|
+
}]
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
exports.notFoundHandler = notFoundHandler;
|
|
48
|
+
//# sourceMappingURL=not-found.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"not-found.js","names":["code: ResponseErrorCode","ResponseErrorCode","createJsonResponse","getResponseErrorMessage"],"sources":["../src/handlers/not-found.ts"],"sourcesContent":["import 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 reurned 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,MAAM,wBAAoD;CACtD,MAAMA,OAA0BC,yCAAkB;AAElD,SAAQ,MAAyB;AAC7B,SAAOC,iCAAmB,GAAG;GACzB,QAAQ;GACR,QAAQ,CACJ;IACI;IACA,SAASC,+CAAwB,KAAK;IACzC,CACJ;GACJ,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createJsonResponse } from "./response/json/index.mjs";
|
|
2
|
+
import "./response.mjs";
|
|
3
|
+
import { ResponseErrorCode, getResponseErrorMessage } from "./response/error.mjs";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Not found handler.
|
|
7
|
+
*
|
|
8
|
+
* Following response will be reurned on route not found:
|
|
9
|
+
*
|
|
10
|
+
* ```jsonc
|
|
11
|
+
* // Status: 404
|
|
12
|
+
* {
|
|
13
|
+
* "success": false,
|
|
14
|
+
* "errors": [
|
|
15
|
+
* {
|
|
16
|
+
* "code": "not_found",
|
|
17
|
+
* "message": "Content not found"
|
|
18
|
+
* }
|
|
19
|
+
* ]
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* ### Example
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { Hono } from "hono";
|
|
27
|
+
* import { notFoundHandler } from "@jderstd/hono/not-found";
|
|
28
|
+
*
|
|
29
|
+
* const app: Hono = new Hono();
|
|
30
|
+
*
|
|
31
|
+
* app.notFound(notFoundHandler());
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
const notFoundHandler = () => {
|
|
35
|
+
const code = ResponseErrorCode.NotFound;
|
|
36
|
+
return (c) => {
|
|
37
|
+
return createJsonResponse(c, {
|
|
38
|
+
status: 404,
|
|
39
|
+
errors: [{
|
|
40
|
+
code,
|
|
41
|
+
message: getResponseErrorMessage(code)
|
|
42
|
+
}]
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export { notFoundHandler };
|
|
48
|
+
//# sourceMappingURL=not-found.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"not-found.mjs","names":["code: ResponseErrorCode"],"sources":["../src/handlers/not-found.ts"],"sourcesContent":["import 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 reurned 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,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"}
|