@jderstd/hono 0.6.0 → 0.7.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/dist/body-limit.js +3 -5
- package/dist/body-limit.js.map +1 -1
- package/dist/body-limit.mjs +1 -1
- package/dist/ip-limit.js +3 -5
- package/dist/ip-limit.js.map +1 -1
- package/dist/ip-limit.mjs +1 -1
- package/dist/not-found.d.ts +1 -1
- package/dist/not-found.js +4 -4
- package/dist/not-found.js.map +1 -1
- package/dist/not-found.mjs +2 -2
- package/dist/not-found.mjs.map +1 -1
- package/dist/on-error.js +5 -5
- package/dist/on-error.js.map +1 -1
- package/dist/on-error.mjs +3 -1
- package/dist/on-error.mjs.map +1 -1
- package/dist/response/common/index.js +0 -2
- package/dist/response/common/index.js.map +1 -1
- package/dist/response/error/http.d.ts +4 -0
- package/dist/response/error/http.js +6 -0
- package/dist/response/error/http.js.map +1 -0
- package/dist/response/error/http.mjs +6 -0
- package/dist/response/error/http.mjs.map +1 -0
- package/dist/response/{error.d.ts → error/index.d.ts} +1 -1
- package/dist/response/{error.js → error/index.js} +1 -1
- package/dist/response/error/index.js.map +1 -0
- package/dist/response/{error.mjs → error/index.mjs} +1 -1
- package/dist/response/error/index.mjs.map +1 -0
- package/dist/response/json/index.js +0 -2
- package/dist/response/json/index.js.map +1 -1
- package/dist/time-limit.js +3 -6
- package/dist/time-limit.js.map +1 -1
- package/dist/time-limit.mjs +1 -1
- package/package.json +9 -4
- package/dist/_virtual/rolldown_runtime.js +0 -23
- package/dist/response/error.js.map +0 -1
- package/dist/response/error.mjs.map +0 -1
package/dist/body-limit.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
|
|
2
1
|
const require_index = require('./response/json/index.js');
|
|
3
|
-
const
|
|
2
|
+
const require_response_error_index = require('./response/error/index.js');
|
|
4
3
|
let hono_body_limit = require("hono/body-limit");
|
|
5
|
-
hono_body_limit = require_rolldown_runtime.__toESM(hono_body_limit);
|
|
6
4
|
|
|
7
5
|
/** Default maximum body size in bytes. */
|
|
8
6
|
const BODY_LIMIT_MAX_DEFAULT = 10 * 1024 * 1024;
|
|
@@ -58,7 +56,7 @@ const BODY_LIMIT_MAX_DEFAULT = 10 * 1024 * 1024;
|
|
|
58
56
|
* ```
|
|
59
57
|
*/
|
|
60
58
|
const bodyLimit = (options) => {
|
|
61
|
-
const code =
|
|
59
|
+
const code = require_response_error_index.ResponseErrorCode.TooLarge;
|
|
62
60
|
return (0, hono_body_limit.bodyLimit)({
|
|
63
61
|
maxSize: options?.max ?? BODY_LIMIT_MAX_DEFAULT,
|
|
64
62
|
onError: (c) => {
|
|
@@ -67,7 +65,7 @@ const bodyLimit = (options) => {
|
|
|
67
65
|
errors: [{
|
|
68
66
|
code,
|
|
69
67
|
path: ["request", "body"],
|
|
70
|
-
message:
|
|
68
|
+
message: require_response_error_index.getResponseErrorMessage(code)
|
|
71
69
|
}]
|
|
72
70
|
});
|
|
73
71
|
}
|
package/dist/body-limit.js.map
CHANGED
|
@@ -1 +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":"
|
|
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,+CAAkB;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,qDAAwB,KAAK;KACzC,CACJ;IACJ,CAAC;;EAET,CAAC"}
|
package/dist/body-limit.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createJsonResponse } from "./response/json/index.mjs";
|
|
2
|
-
import { ResponseErrorCode, getResponseErrorMessage } from "./response/error.mjs";
|
|
2
|
+
import { ResponseErrorCode, getResponseErrorMessage } from "./response/error/index.mjs";
|
|
3
3
|
import { bodyLimit as bodyLimit$1 } from "hono/body-limit";
|
|
4
4
|
|
|
5
5
|
/** Default maximum body size in bytes. */
|
package/dist/ip-limit.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
|
|
2
1
|
const require_index = require('./response/json/index.js');
|
|
3
2
|
require('./response.js');
|
|
4
|
-
const
|
|
3
|
+
const require_response_error_index = require('./response/error/index.js');
|
|
5
4
|
let hono_ip_restriction = require("hono/ip-restriction");
|
|
6
|
-
hono_ip_restriction = require_rolldown_runtime.__toESM(hono_ip_restriction);
|
|
7
5
|
|
|
8
6
|
function ipLimit(getConnInfoOrOptions, options) {
|
|
9
|
-
const code =
|
|
7
|
+
const code = require_response_error_index.ResponseErrorCode.Forbidden;
|
|
10
8
|
const getConnInfo = typeof getConnInfoOrOptions === "function" ? getConnInfoOrOptions : getConnInfoOrOptions.getConnInfo;
|
|
11
9
|
const { denyList, allowList, verbose } = (typeof getConnInfoOrOptions === "function" ? options : getConnInfoOrOptions) ?? {};
|
|
12
10
|
return (0, hono_ip_restriction.ipRestriction)(getConnInfo, {
|
|
@@ -19,7 +17,7 @@ function ipLimit(getConnInfoOrOptions, options) {
|
|
|
19
17
|
code,
|
|
20
18
|
...verbose ? {
|
|
21
19
|
path: ["request", "ip"],
|
|
22
|
-
message: `${
|
|
20
|
+
message: `${require_response_error_index.getResponseErrorMessage(code)}: ${addr}`
|
|
23
21
|
} : {}
|
|
24
22
|
}]
|
|
25
23
|
});
|
package/dist/ip-limit.js.map
CHANGED
|
@@ -1 +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":"
|
|
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,+CAAkB;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,qDAAwB,KAAK,CAAC,IAAI;KACjD,GACD,EAAE;IACX,CACJ;GACJ,CAAC;GAET"}
|
package/dist/ip-limit.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createJsonResponse } from "./response/json/index.mjs";
|
|
2
2
|
import "./response.mjs";
|
|
3
|
-
import { ResponseErrorCode, getResponseErrorMessage } from "./response/error.mjs";
|
|
3
|
+
import { ResponseErrorCode, getResponseErrorMessage } from "./response/error/index.mjs";
|
|
4
4
|
import { ipRestriction } from "hono/ip-restriction";
|
|
5
5
|
|
|
6
6
|
function ipLimit(getConnInfoOrOptions, options) {
|
package/dist/not-found.d.ts
CHANGED
package/dist/not-found.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
const require_index = require('./response/json/index.js');
|
|
2
2
|
require('./response.js');
|
|
3
|
-
const
|
|
3
|
+
const require_response_error_index = require('./response/error/index.js');
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Not found handler.
|
|
7
7
|
*
|
|
8
|
-
* Following response will be
|
|
8
|
+
* Following response will be returned on route not found:
|
|
9
9
|
*
|
|
10
10
|
* ```jsonc
|
|
11
11
|
* // Status: 404
|
|
@@ -32,13 +32,13 @@ const require_response_error = require('./response/error.js');
|
|
|
32
32
|
* ```
|
|
33
33
|
*/
|
|
34
34
|
const notFoundHandler = () => {
|
|
35
|
-
const code =
|
|
35
|
+
const code = require_response_error_index.ResponseErrorCode.NotFound;
|
|
36
36
|
return (c) => {
|
|
37
37
|
return require_index.createJsonResponse(c, {
|
|
38
38
|
status: 404,
|
|
39
39
|
errors: [{
|
|
40
40
|
code,
|
|
41
|
-
message:
|
|
41
|
+
message: require_response_error_index.getResponseErrorMessage(code)
|
|
42
42
|
}]
|
|
43
43
|
});
|
|
44
44
|
};
|
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":["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
|
|
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 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,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
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { createJsonResponse } from "./response/json/index.mjs";
|
|
2
2
|
import "./response.mjs";
|
|
3
|
-
import { ResponseErrorCode, getResponseErrorMessage } from "./response/error.mjs";
|
|
3
|
+
import { ResponseErrorCode, getResponseErrorMessage } from "./response/error/index.mjs";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Not found handler.
|
|
7
7
|
*
|
|
8
|
-
* Following response will be
|
|
8
|
+
* Following response will be returned on route not found:
|
|
9
9
|
*
|
|
10
10
|
* ```jsonc
|
|
11
11
|
* // Status: 404
|
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":["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
|
|
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 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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"}
|
package/dist/on-error.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
|
|
2
1
|
const require_index = require('./response/json/index.js');
|
|
3
2
|
require('./response.js');
|
|
4
|
-
const
|
|
3
|
+
const require_response_error_index = require('./response/error/index.js');
|
|
4
|
+
const require_response_error_http = require('./response/error/http.js');
|
|
5
5
|
let hono_http_exception = require("hono/http-exception");
|
|
6
|
-
hono_http_exception = require_rolldown_runtime.__toESM(hono_http_exception);
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* On error handler.
|
|
@@ -60,9 +59,10 @@ hono_http_exception = require_rolldown_runtime.__toESM(hono_http_exception);
|
|
|
60
59
|
*/
|
|
61
60
|
const onErrorHandler = (options) => {
|
|
62
61
|
return (err, c) => {
|
|
62
|
+
if (err instanceof require_response_error_http.JderHttpException) return err.getResponse();
|
|
63
63
|
if (err instanceof hono_http_exception.HTTPException) {
|
|
64
64
|
const res = err.getResponse();
|
|
65
|
-
const code = res.status >= 500 ?
|
|
65
|
+
const code = res.status >= 500 ? require_response_error_index.ResponseErrorCode.Server : require_response_error_index.ResponseErrorCode.BadRequest;
|
|
66
66
|
return require_index.createJsonResponse(c, {
|
|
67
67
|
status: res.status,
|
|
68
68
|
errors: [{
|
|
@@ -74,7 +74,7 @@ const onErrorHandler = (options) => {
|
|
|
74
74
|
return require_index.createJsonResponse(c, {
|
|
75
75
|
status: 500,
|
|
76
76
|
errors: [{
|
|
77
|
-
code:
|
|
77
|
+
code: require_response_error_index.ResponseErrorCode.Server,
|
|
78
78
|
...options?.verbose && { message: err.message }
|
|
79
79
|
}]
|
|
80
80
|
});
|
package/dist/on-error.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"on-error.js","names":["HTTPException","res: Response","code: ResponseErrorCode","ResponseErrorCode","createJsonResponse"],"sources":["../src/handlers/on-error.ts"],"sourcesContent":["import 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\";\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 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 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 { onErrorHandler };\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"on-error.js","names":["JderHttpException","HTTPException","res: Response","code: ResponseErrorCode","ResponseErrorCode","createJsonResponse"],"sources":["../src/handlers/on-error.ts"],"sourcesContent":["import 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 if (err instanceof JderHttpException) return err.getResponse();\n\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 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 { onErrorHandler };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,MAAM,kBACF,YAC6D;AAC7D,SAAQ,KAAgC,MAAyB;AAC7D,MAAI,eAAeA,8CAAmB,QAAO,IAAI,aAAa;AAE9D,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;;AAGN,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
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createJsonResponse } from "./response/json/index.mjs";
|
|
2
2
|
import "./response.mjs";
|
|
3
|
-
import { ResponseErrorCode } from "./response/error.mjs";
|
|
3
|
+
import { ResponseErrorCode } from "./response/error/index.mjs";
|
|
4
|
+
import { JderHttpException } from "./response/error/http.mjs";
|
|
4
5
|
import { HTTPException } from "hono/http-exception";
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -58,6 +59,7 @@ import { HTTPException } from "hono/http-exception";
|
|
|
58
59
|
*/
|
|
59
60
|
const onErrorHandler = (options) => {
|
|
60
61
|
return (err, c) => {
|
|
62
|
+
if (err instanceof JderHttpException) return err.getResponse();
|
|
61
63
|
if (err instanceof HTTPException) {
|
|
62
64
|
const res = err.getResponse();
|
|
63
65
|
const code = res.status >= 500 ? ResponseErrorCode.Server : ResponseErrorCode.BadRequest;
|
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":["import 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\";\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 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 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 { onErrorHandler };\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"on-error.mjs","names":["res: Response","code: ResponseErrorCode"],"sources":["../src/handlers/on-error.ts"],"sourcesContent":["import 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 if (err instanceof JderHttpException) return err.getResponse();\n\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 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 { onErrorHandler };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,MAAM,kBACF,YAC6D;AAC7D,SAAQ,KAAgC,MAAyB;AAC7D,MAAI,eAAe,kBAAmB,QAAO,IAAI,aAAa;AAE9D,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;;AAGN,SAAO,mBAAmB,GAAG;GACzB,QAAQ;GACR,QAAQ,CACJ;IACI,MAAM,kBAAkB;IACxB,GAAI,SAAS,WAAW,EACpB,SAAS,IAAI,SAChB;IACJ,CACJ;GACJ,CAAC"}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.js');
|
|
2
1
|
const require_index = require('../json/index.js');
|
|
3
2
|
let __jderstd_core_response_common_struct = require("@jderstd/core/response/common/struct");
|
|
4
|
-
__jderstd_core_response_common_struct = require_rolldown_runtime.__toESM(__jderstd_core_response_common_struct);
|
|
5
3
|
|
|
6
4
|
function createResponse(contextOrOptions, options) {
|
|
7
5
|
const { status, headers, body } = require_index.isContext(contextOrOptions) ? (0, __jderstd_core_response_common_struct.createResponseStruct)(options) : (0, __jderstd_core_response_common_struct.createResponseStruct)(contextOrOptions);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["isContext","c: Context"],"sources":["../../../src/response/common/index.ts"],"sourcesContent":["import type { CreateResponseStructOptions } from \"@jderstd/core/response/common/struct\";\nimport type { Context } from \"hono\";\nimport type { StatusCode } from \"hono/utils/http-status\";\nimport type { Format, Omit } from \"ts-vista\";\n\nimport { createResponseStruct } from \"@jderstd/core/response/common/struct\";\n\nimport { isContext } from \"#/response/json\";\n\n/** Options of `createResponse` function. */\ntype CreateResponseOptions<B extends BodyInit = BodyInit> = Format<\n {\n /**\n * Status code of the response.\n * By default, it is `200` for success and `400` for failure.\n */\n status?: StatusCode;\n } & Omit<CreateResponseStructOptions<B>, \"status\">\n>;\n\n/**\n * Create a response.\n *\n * ### Examples\n *\n * Example for creating a basic response:\n *\n * ```ts\n * import { createResponse } from \"@jderstd/hono/response\";\n *\n * const route = (): Response => {\n * return createResponse();\n * };\n * ```\n *\n * Example for creating a response with status, headers, and body:\n *\n * ```ts\n * import { createResponse } from \"@jderstd/hono/response\";\n *\n * const route = (): Response => {\n * return createResponse({\n * status: 404,\n * headers: [\n * [\"Content-Type\", \"text/plain\"],\n * ],\n * body: \"Not Found\",\n * });\n * };\n * ```\n */\nfunction createResponse<B extends BodyInit = BodyInit>(\n options?: CreateResponseOptions<B>,\n): Response;\n\n/**\n * Create a response with context.\n *\n * ### Examples\n *\n * Example for creating a basic response:\n *\n * ```ts\n * import type { Context } from \"hono\";\n *\n * import { createResponse } from \"@jderstd/hono/response\";\n *\n * const route = (c: Context): Response => {\n * return createResponse(c);\n * };\n * ```\n *\n * Example for creating a response with status, headers, and body:\n *\n * ```ts\n * import type { Context } from \"hono\";\n *\n * import { createResponse } from \"@jderstd/hono/response\";\n *\n * const route = (c: Context): Response => {\n * return createResponse(c, {\n * status: 404,\n * headers: [\n * [\"Content-Type\", \"text/plain\"],\n * ],\n * body: \"Not Found\",\n * });\n * };\n * ```\n */\nfunction createResponse<B extends BodyInit = BodyInit>(\n context?: Context,\n options?: CreateResponseOptions<B>,\n): Response;\n\nfunction createResponse<B extends BodyInit = BodyInit>(\n contextOrOptions?: Context | CreateResponseOptions<B>,\n options?: CreateResponseOptions<B>,\n): Response {\n const { status, headers, body } = isContext(contextOrOptions)\n ? createResponseStruct(options)\n : createResponseStruct(contextOrOptions);\n\n if (isContext(contextOrOptions)) {\n const c: Context = contextOrOptions;\n\n c.status(status as StatusCode);\n\n for (const [key, value] of headers) c.header(key, value);\n\n return body\n ? c.body(body as string | ArrayBuffer | ReadableStream)\n : c.body(null);\n }\n\n return new Response(body, {\n status,\n headers,\n });\n}\n\nexport type { CreateResponseOptions };\nexport { createResponse };\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":["isContext","c: Context"],"sources":["../../../src/response/common/index.ts"],"sourcesContent":["import type { CreateResponseStructOptions } from \"@jderstd/core/response/common/struct\";\nimport type { Context } from \"hono\";\nimport type { StatusCode } from \"hono/utils/http-status\";\nimport type { Format, Omit } from \"ts-vista\";\n\nimport { createResponseStruct } from \"@jderstd/core/response/common/struct\";\n\nimport { isContext } from \"#/response/json\";\n\n/** Options of `createResponse` function. */\ntype CreateResponseOptions<B extends BodyInit = BodyInit> = Format<\n {\n /**\n * Status code of the response.\n * By default, it is `200` for success and `400` for failure.\n */\n status?: StatusCode;\n } & Omit<CreateResponseStructOptions<B>, \"status\">\n>;\n\n/**\n * Create a response.\n *\n * ### Examples\n *\n * Example for creating a basic response:\n *\n * ```ts\n * import { createResponse } from \"@jderstd/hono/response\";\n *\n * const route = (): Response => {\n * return createResponse();\n * };\n * ```\n *\n * Example for creating a response with status, headers, and body:\n *\n * ```ts\n * import { createResponse } from \"@jderstd/hono/response\";\n *\n * const route = (): Response => {\n * return createResponse({\n * status: 404,\n * headers: [\n * [\"Content-Type\", \"text/plain\"],\n * ],\n * body: \"Not Found\",\n * });\n * };\n * ```\n */\nfunction createResponse<B extends BodyInit = BodyInit>(\n options?: CreateResponseOptions<B>,\n): Response;\n\n/**\n * Create a response with context.\n *\n * ### Examples\n *\n * Example for creating a basic response:\n *\n * ```ts\n * import type { Context } from \"hono\";\n *\n * import { createResponse } from \"@jderstd/hono/response\";\n *\n * const route = (c: Context): Response => {\n * return createResponse(c);\n * };\n * ```\n *\n * Example for creating a response with status, headers, and body:\n *\n * ```ts\n * import type { Context } from \"hono\";\n *\n * import { createResponse } from \"@jderstd/hono/response\";\n *\n * const route = (c: Context): Response => {\n * return createResponse(c, {\n * status: 404,\n * headers: [\n * [\"Content-Type\", \"text/plain\"],\n * ],\n * body: \"Not Found\",\n * });\n * };\n * ```\n */\nfunction createResponse<B extends BodyInit = BodyInit>(\n context?: Context,\n options?: CreateResponseOptions<B>,\n): Response;\n\nfunction createResponse<B extends BodyInit = BodyInit>(\n contextOrOptions?: Context | CreateResponseOptions<B>,\n options?: CreateResponseOptions<B>,\n): Response {\n const { status, headers, body } = isContext(contextOrOptions)\n ? createResponseStruct(options)\n : createResponseStruct(contextOrOptions);\n\n if (isContext(contextOrOptions)) {\n const c: Context = contextOrOptions;\n\n c.status(status as StatusCode);\n\n for (const [key, value] of headers) c.header(key, value);\n\n return body\n ? c.body(body as string | ArrayBuffer | ReadableStream)\n : c.body(null);\n }\n\n return new Response(body, {\n status,\n headers,\n });\n}\n\nexport type { CreateResponseOptions };\nexport { createResponse };\n"],"mappings":";;;AA+FA,SAAS,eACL,kBACA,SACQ;CACR,MAAM,EAAE,QAAQ,SAAS,SAASA,wBAAU,iBAAiB,mEAClC,QAAQ,mEACR,iBAAiB;AAE5C,KAAIA,wBAAU,iBAAiB,EAAE;EAC7B,MAAMC,IAAa;AAEnB,IAAE,OAAO,OAAqB;AAE9B,OAAK,MAAM,CAAC,KAAK,UAAU,QAAS,GAAE,OAAO,KAAK,MAAM;AAExD,SAAO,OACD,EAAE,KAAK,KAA8C,GACrD,EAAE,KAAK,KAAK;;AAGtB,QAAO,IAAI,SAAS,MAAM;EACtB;EACA;EACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","names":["HTTPException"],"sources":["../../../src/response/error/http.ts"],"sourcesContent":["import { HTTPException } from \"hono/http-exception\";\n\nclass JderHttpException extends HTTPException {}\n\nexport { JderHttpException };\n"],"mappings":";;AAEA,IAAM,oBAAN,cAAgCA,kCAAc"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.mjs","names":[],"sources":["../../../src/response/error/http.ts"],"sourcesContent":["import { HTTPException } from \"hono/http-exception\";\n\nclass JderHttpException extends HTTPException {}\n\nexport { JderHttpException };\n"],"mappings":";;AAEA,IAAM,oBAAN,cAAgC,cAAc"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/response/error/index.ts"],"sourcesContent":["/**\n * Response error code.\n */\nenum ResponseErrorCode {\n /**\n * Request body is too large.\n *\n * For `bodyLimit` middleware.\n */\n TooLarge = \"too_large\",\n /**\n * Forbidden access.\n *\n * For `ipLimit` middleware.\n */\n Forbidden = \"forbidden\",\n /**\n * Request timeout.\n *\n * For `timeLimit` middleware.\n */\n Timeout = \"timeout\",\n /**\n * Content not found.\n *\n * For `notFoundHandler` function.\n */\n NotFound = \"not_found\",\n /**\n * Bad request.\n *\n * For `onErrorHandler` function.\n */\n BadRequest = \"bad_request\",\n /**\n * Internal server error.\n *\n * For `onErrorHandler` function.\n */\n Server = \"server\",\n /**\n * Validation error.\n *\n * For validator package.\n */\n Parse = \"parse\",\n}\n\n/**\n * Get response error message by code.\n */\nconst getResponseErrorMessage = (code: ResponseErrorCode): string => {\n switch (code) {\n case ResponseErrorCode.TooLarge: {\n return \"Request body is too large\";\n }\n case ResponseErrorCode.Forbidden: {\n return \"Forbidden IP address\";\n }\n case ResponseErrorCode.Timeout: {\n return \"Gateway timeout\";\n }\n case ResponseErrorCode.NotFound: {\n return \"Content not found\";\n }\n case ResponseErrorCode.BadRequest: {\n return \"Bad request\";\n }\n case ResponseErrorCode.Server: {\n return \"Internal server error\";\n }\n case ResponseErrorCode.Parse: {\n return \"Failed to parse the request\";\n }\n }\n};\n\nexport { ResponseErrorCode, getResponseErrorMessage };\n"],"mappings":";;;;AAGA,IAAK,kEAAL;;;;;;AAMI;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;EA1CC;;;;AAgDL,MAAM,2BAA2B,SAAoC;AACjE,SAAQ,MAAR;EACI,KAAK,kBAAkB,SACnB,QAAO;EAEX,KAAK,kBAAkB,UACnB,QAAO;EAEX,KAAK,kBAAkB,QACnB,QAAO;EAEX,KAAK,kBAAkB,SACnB,QAAO;EAEX,KAAK,kBAAkB,WACnB,QAAO;EAEX,KAAK,kBAAkB,OACnB,QAAO;EAEX,KAAK,kBAAkB,MACnB,QAAO"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/response/error/index.ts"],"sourcesContent":["/**\n * Response error code.\n */\nenum ResponseErrorCode {\n /**\n * Request body is too large.\n *\n * For `bodyLimit` middleware.\n */\n TooLarge = \"too_large\",\n /**\n * Forbidden access.\n *\n * For `ipLimit` middleware.\n */\n Forbidden = \"forbidden\",\n /**\n * Request timeout.\n *\n * For `timeLimit` middleware.\n */\n Timeout = \"timeout\",\n /**\n * Content not found.\n *\n * For `notFoundHandler` function.\n */\n NotFound = \"not_found\",\n /**\n * Bad request.\n *\n * For `onErrorHandler` function.\n */\n BadRequest = \"bad_request\",\n /**\n * Internal server error.\n *\n * For `onErrorHandler` function.\n */\n Server = \"server\",\n /**\n * Validation error.\n *\n * For validator package.\n */\n Parse = \"parse\",\n}\n\n/**\n * Get response error message by code.\n */\nconst getResponseErrorMessage = (code: ResponseErrorCode): string => {\n switch (code) {\n case ResponseErrorCode.TooLarge: {\n return \"Request body is too large\";\n }\n case ResponseErrorCode.Forbidden: {\n return \"Forbidden IP address\";\n }\n case ResponseErrorCode.Timeout: {\n return \"Gateway timeout\";\n }\n case ResponseErrorCode.NotFound: {\n return \"Content not found\";\n }\n case ResponseErrorCode.BadRequest: {\n return \"Bad request\";\n }\n case ResponseErrorCode.Server: {\n return \"Internal server error\";\n }\n case ResponseErrorCode.Parse: {\n return \"Failed to parse the request\";\n }\n }\n};\n\nexport { ResponseErrorCode, getResponseErrorMessage };\n"],"mappings":";;;AAGA,IAAK,kEAAL;;;;;;AAMI;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;EA1CC;;;;AAgDL,MAAM,2BAA2B,SAAoC;AACjE,SAAQ,MAAR;EACI,KAAK,kBAAkB,SACnB,QAAO;EAEX,KAAK,kBAAkB,UACnB,QAAO;EAEX,KAAK,kBAAkB,QACnB,QAAO;EAEX,KAAK,kBAAkB,SACnB,QAAO;EAEX,KAAK,kBAAkB,WACnB,QAAO;EAEX,KAAK,kBAAkB,OACnB,QAAO;EAEX,KAAK,kBAAkB,MACnB,QAAO"}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.js');
|
|
2
1
|
let __jderstd_core_response_json_struct = require("@jderstd/core/response/json/struct");
|
|
3
|
-
__jderstd_core_response_json_struct = require_rolldown_runtime.__toESM(__jderstd_core_response_json_struct);
|
|
4
2
|
|
|
5
3
|
/** Check if the argument is a Hono context. */
|
|
6
4
|
const isContext = (arg) => arg?.req !== void 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["c: Context"],"sources":["../../../src/response/json/index.ts"],"sourcesContent":["import type { CreateJsonResponseStructOptions } from \"@jderstd/core/response/json/struct\";\nimport type { Context } from \"hono\";\nimport type { StatusCode } from \"hono/utils/http-status\";\nimport type { Format, Omit } from \"ts-vista\";\n\nimport { createJsonResponseStruct } from \"@jderstd/core/response/json/struct\";\n\n/** Check if the argument is a Hono context. */\nconst isContext = (arg: any): arg is Context => arg?.req !== undefined;\n\n/** Options of `createJsonResponse` function. */\ntype CreateJsonResponseOptions<D = unknown> = Format<\n {\n /**\n * Status code of the response.\n * By default, it is `200` for success and `400` for failure.\n */\n status?: StatusCode;\n } & Omit<CreateJsonResponseStructOptions<D>, \"status\">\n>;\n\n/**\n * Create a JSON response.\n *\n * ### Examples\n *\n * Example for creating a successful JSON response without data:\n *\n * ```ts\n * import { createJsonResponse } from \"@jderstd/hono/response\";\n *\n * const route = (): Response => {\n * return createJsonResponse();\n * };\n * ```\n *\n * Example for creating a successful JSON response with data:\n *\n * ```ts\n * import { createJsonResponse } from \"@jderstd/hono/response\";\n *\n * const route = (): Response => {\n * return createJsonResponse({\n * data: \"Hello, World!\",\n * });\n * }\n * ```\n *\n * Example for creating a failed JSON response:\n *\n * ```ts\n * import { createJsonResponse } from \"@jderstd/hono/response\";\n *\n * const route = (): Response => {\n * return createJsonResponse({\n * errors: [\n * {\n * code: \"server\",\n * message: \"Internal server error\",\n * },\n * ],\n * });\n * };\n * ```\n */\nfunction createJsonResponse<D = unknown>(\n options?: CreateJsonResponseOptions<D>,\n): Response;\n\n/**\n * Create a JSON response with context.\n *\n * ### Examples\n *\n * Example for creating a successful JSON response without data:\n *\n * ```ts\n * import type { Context } from \"hono\";\n *\n * import { createJsonResponse } from \"@jderstd/hono/response\";\n *\n * const route = (c: Context): Response => {\n * return createJsonResponse(c);\n * };\n * ```\n *\n * Example for creating a successful JSON response with data:\n *\n * ```ts\n * import type { Context } from \"hono\";\n *\n * import { createJsonResponse } from \"@jderstd/hono/response\";\n *\n * const route = (c: Context): Response => {\n * return createJsonResponse(c, {\n * data: \"Hello, World!\",\n * });\n * }\n * ```\n *\n * Example for creating a failed JSON response:\n *\n * ```ts\n * import type { Context } from \"hono\";\n *\n * import { createJsonResponse } from \"@jderstd/hono/response\";\n *\n * const route = (c: Context): Response => {\n * return createJsonResponse(c, {\n * errors: [\n * {\n * code: \"server\",\n * message: \"Internal server error\",\n * },\n * ],\n * });\n * };\n * ```\n */\nfunction createJsonResponse<D = unknown>(\n context?: Context,\n options?: CreateJsonResponseOptions<D>,\n): Response;\n\nfunction createJsonResponse<D = unknown>(\n contextOrOptions?: Context | CreateJsonResponseOptions<D>,\n options?: CreateJsonResponseOptions<D>,\n): Response {\n const { status, headers, json } = isContext(contextOrOptions)\n ? createJsonResponseStruct(options)\n : createJsonResponseStruct(contextOrOptions);\n\n if (isContext(contextOrOptions)) {\n const c: Context = contextOrOptions;\n\n c.status(status as StatusCode);\n\n for (const [key, value] of headers) c.header(key, value);\n\n return c.json(json);\n }\n\n return new Response(JSON.stringify(json), {\n status,\n headers,\n });\n}\n\nexport type { CreateJsonResponseOptions };\nexport { isContext, createJsonResponse };\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":["c: Context"],"sources":["../../../src/response/json/index.ts"],"sourcesContent":["import type { CreateJsonResponseStructOptions } from \"@jderstd/core/response/json/struct\";\nimport type { Context } from \"hono\";\nimport type { StatusCode } from \"hono/utils/http-status\";\nimport type { Format, Omit } from \"ts-vista\";\n\nimport { createJsonResponseStruct } from \"@jderstd/core/response/json/struct\";\n\n/** Check if the argument is a Hono context. */\nconst isContext = (arg: any): arg is Context => arg?.req !== undefined;\n\n/** Options of `createJsonResponse` function. */\ntype CreateJsonResponseOptions<D = unknown> = Format<\n {\n /**\n * Status code of the response.\n * By default, it is `200` for success and `400` for failure.\n */\n status?: StatusCode;\n } & Omit<CreateJsonResponseStructOptions<D>, \"status\">\n>;\n\n/**\n * Create a JSON response.\n *\n * ### Examples\n *\n * Example for creating a successful JSON response without data:\n *\n * ```ts\n * import { createJsonResponse } from \"@jderstd/hono/response\";\n *\n * const route = (): Response => {\n * return createJsonResponse();\n * };\n * ```\n *\n * Example for creating a successful JSON response with data:\n *\n * ```ts\n * import { createJsonResponse } from \"@jderstd/hono/response\";\n *\n * const route = (): Response => {\n * return createJsonResponse({\n * data: \"Hello, World!\",\n * });\n * }\n * ```\n *\n * Example for creating a failed JSON response:\n *\n * ```ts\n * import { createJsonResponse } from \"@jderstd/hono/response\";\n *\n * const route = (): Response => {\n * return createJsonResponse({\n * errors: [\n * {\n * code: \"server\",\n * message: \"Internal server error\",\n * },\n * ],\n * });\n * };\n * ```\n */\nfunction createJsonResponse<D = unknown>(\n options?: CreateJsonResponseOptions<D>,\n): Response;\n\n/**\n * Create a JSON response with context.\n *\n * ### Examples\n *\n * Example for creating a successful JSON response without data:\n *\n * ```ts\n * import type { Context } from \"hono\";\n *\n * import { createJsonResponse } from \"@jderstd/hono/response\";\n *\n * const route = (c: Context): Response => {\n * return createJsonResponse(c);\n * };\n * ```\n *\n * Example for creating a successful JSON response with data:\n *\n * ```ts\n * import type { Context } from \"hono\";\n *\n * import { createJsonResponse } from \"@jderstd/hono/response\";\n *\n * const route = (c: Context): Response => {\n * return createJsonResponse(c, {\n * data: \"Hello, World!\",\n * });\n * }\n * ```\n *\n * Example for creating a failed JSON response:\n *\n * ```ts\n * import type { Context } from \"hono\";\n *\n * import { createJsonResponse } from \"@jderstd/hono/response\";\n *\n * const route = (c: Context): Response => {\n * return createJsonResponse(c, {\n * errors: [\n * {\n * code: \"server\",\n * message: \"Internal server error\",\n * },\n * ],\n * });\n * };\n * ```\n */\nfunction createJsonResponse<D = unknown>(\n context?: Context,\n options?: CreateJsonResponseOptions<D>,\n): Response;\n\nfunction createJsonResponse<D = unknown>(\n contextOrOptions?: Context | CreateJsonResponseOptions<D>,\n options?: CreateJsonResponseOptions<D>,\n): Response {\n const { status, headers, json } = isContext(contextOrOptions)\n ? createJsonResponseStruct(options)\n : createJsonResponseStruct(contextOrOptions);\n\n if (isContext(contextOrOptions)) {\n const c: Context = contextOrOptions;\n\n c.status(status as StatusCode);\n\n for (const [key, value] of headers) c.header(key, value);\n\n return c.json(json);\n }\n\n return new Response(JSON.stringify(json), {\n status,\n headers,\n });\n}\n\nexport type { CreateJsonResponseOptions };\nexport { isContext, createJsonResponse };\n"],"mappings":";;;AAQA,MAAM,aAAa,QAA6B,KAAK,QAAQ;AAoH7D,SAAS,mBACL,kBACA,SACQ;CACR,MAAM,EAAE,QAAQ,SAAS,SAAS,UAAU,iBAAiB,qEAC9B,QAAQ,qEACR,iBAAiB;AAEhD,KAAI,UAAU,iBAAiB,EAAE;EAC7B,MAAMA,IAAa;AAEnB,IAAE,OAAO,OAAqB;AAE9B,OAAK,MAAM,CAAC,KAAK,UAAU,QAAS,GAAE,OAAO,KAAK,MAAM;AAExD,SAAO,EAAE,KAAK,KAAK;;AAGvB,QAAO,IAAI,SAAS,KAAK,UAAU,KAAK,EAAE;EACtC;EACA;EACH,CAAC"}
|
package/dist/time-limit.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
|
|
2
1
|
const require_index = require('./response/json/index.js');
|
|
3
|
-
const
|
|
2
|
+
const require_response_error_index = require('./response/error/index.js');
|
|
4
3
|
let hono_http_exception = require("hono/http-exception");
|
|
5
|
-
hono_http_exception = require_rolldown_runtime.__toESM(hono_http_exception);
|
|
6
4
|
let hono_timeout = require("hono/timeout");
|
|
7
|
-
hono_timeout = require_rolldown_runtime.__toESM(hono_timeout);
|
|
8
5
|
|
|
9
6
|
/** Default maximum time in milliseconds. */
|
|
10
7
|
const TIME_LIMIT_MAX_DEFAULT = 5 * 1e3;
|
|
@@ -57,13 +54,13 @@ const TIME_LIMIT_MAX_DEFAULT = 5 * 1e3;
|
|
|
57
54
|
*/
|
|
58
55
|
const timeLimit = (options) => {
|
|
59
56
|
const status = 504;
|
|
60
|
-
const code =
|
|
57
|
+
const code = require_response_error_index.ResponseErrorCode.Timeout;
|
|
61
58
|
return (0, hono_timeout.timeout)(options?.max ?? 5 * 1e3, (c) => {
|
|
62
59
|
return new hono_http_exception.HTTPException(status, { res: require_index.createJsonResponse(c, {
|
|
63
60
|
status,
|
|
64
61
|
errors: [{
|
|
65
62
|
code,
|
|
66
|
-
message:
|
|
63
|
+
message: require_response_error_index.getResponseErrorMessage(code)
|
|
67
64
|
}]
|
|
68
65
|
}) });
|
|
69
66
|
});
|
package/dist/time-limit.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time-limit.js","names":["status: StatusCode","code: ResponseErrorCode","ResponseErrorCode","HTTPException","createJsonResponse","getResponseErrorMessage"],"sources":["../src/middlewares/time-limit.ts"],"sourcesContent":["/**\n * Time limit module\n * @module middlewares/time-limit\n */\n\nimport type { Context, MiddlewareHandler } from \"hono\";\nimport type { StatusCode } from \"hono/utils/http-status\";\n\nimport { HTTPException } from \"hono/http-exception\";\nimport { timeout } from \"hono/timeout\";\n\nimport { getResponseErrorMessage, ResponseErrorCode } from \"#/response/error\";\nimport { createJsonResponse } from \"#/response/json\";\n\n/** Default maximum time in milliseconds. */\nconst TIME_LIMIT_MAX_DEFAULT = (5 * 1000) as 5000;\n\n/** Options for `timeLimit` middleware. */\ntype TimeLimitOptions = {\n /**\n * Maximum time in milliseconds.\n *\n * By default, it is `TIME_LIMIT_MAX_DEFAULT`.\n */\n max?: number;\n};\n\n/**\n * Time limit middleware.\n *\n * Following error will be returned if the request takes longer than the limit:\n *\n * ```jsonc\n * // Status: 504\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"timeout\",\n * \"message\": \"Request timeout\"\n * }\n * ]\n * }\n * ```\n *\n * For more information, please refer to\n * [Timeout](https://hono.dev/docs/middleware/builtin/timeout).\n *\n * ### Examples\n *\n * A example of using `timeLimit` middleware:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { timeLimit } from \"@jderstd/hono/time-limit\";\n *\n * const app: Hono = new Hono();\n *\n * app.use(timeLimit());\n * ```\n *\n * A example of using `timeLimit` middleware with options:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { timeLimit } from \"@jderstd/hono/time-limit\";\n *\n * const app: Hono = new Hono();\n *\n * app.use(timeLimit({\n * max: 10 * 1000, // 10s\n * }));\n * ```\n */\nconst timeLimit = (options?: TimeLimitOptions): MiddlewareHandler => {\n const status: StatusCode = 504;\n const code: ResponseErrorCode = ResponseErrorCode.Timeout;\n\n return timeout(options?.max ?? 5 * 1000, (c: Context): HTTPException => {\n return new HTTPException(status, {\n res: createJsonResponse(c, {\n status,\n errors: [\n {\n code,\n message: getResponseErrorMessage(code),\n },\n ],\n }),\n });\n });\n};\n\nexport type { TimeLimitOptions };\nexport { timeLimit, TIME_LIMIT_MAX_DEFAULT };\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"time-limit.js","names":["status: StatusCode","code: ResponseErrorCode","ResponseErrorCode","HTTPException","createJsonResponse","getResponseErrorMessage"],"sources":["../src/middlewares/time-limit.ts"],"sourcesContent":["/**\n * Time limit module\n * @module middlewares/time-limit\n */\n\nimport type { Context, MiddlewareHandler } from \"hono\";\nimport type { StatusCode } from \"hono/utils/http-status\";\n\nimport { HTTPException } from \"hono/http-exception\";\nimport { timeout } from \"hono/timeout\";\n\nimport { getResponseErrorMessage, ResponseErrorCode } from \"#/response/error\";\nimport { createJsonResponse } from \"#/response/json\";\n\n/** Default maximum time in milliseconds. */\nconst TIME_LIMIT_MAX_DEFAULT = (5 * 1000) as 5000;\n\n/** Options for `timeLimit` middleware. */\ntype TimeLimitOptions = {\n /**\n * Maximum time in milliseconds.\n *\n * By default, it is `TIME_LIMIT_MAX_DEFAULT`.\n */\n max?: number;\n};\n\n/**\n * Time limit middleware.\n *\n * Following error will be returned if the request takes longer than the limit:\n *\n * ```jsonc\n * // Status: 504\n * {\n * \"success\": false,\n * \"errors\": [\n * {\n * \"code\": \"timeout\",\n * \"message\": \"Request timeout\"\n * }\n * ]\n * }\n * ```\n *\n * For more information, please refer to\n * [Timeout](https://hono.dev/docs/middleware/builtin/timeout).\n *\n * ### Examples\n *\n * A example of using `timeLimit` middleware:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { timeLimit } from \"@jderstd/hono/time-limit\";\n *\n * const app: Hono = new Hono();\n *\n * app.use(timeLimit());\n * ```\n *\n * A example of using `timeLimit` middleware with options:\n *\n * ```ts\n * import { Hono } from \"hono\";\n * import { timeLimit } from \"@jderstd/hono/time-limit\";\n *\n * const app: Hono = new Hono();\n *\n * app.use(timeLimit({\n * max: 10 * 1000, // 10s\n * }));\n * ```\n */\nconst timeLimit = (options?: TimeLimitOptions): MiddlewareHandler => {\n const status: StatusCode = 504;\n const code: ResponseErrorCode = ResponseErrorCode.Timeout;\n\n return timeout(options?.max ?? 5 * 1000, (c: Context): HTTPException => {\n return new HTTPException(status, {\n res: createJsonResponse(c, {\n status,\n errors: [\n {\n code,\n message: getResponseErrorMessage(code),\n },\n ],\n }),\n });\n });\n};\n\nexport type { TimeLimitOptions };\nexport { timeLimit, TIME_LIMIT_MAX_DEFAULT };\n"],"mappings":";;;;;;AAeA,MAAM,yBAA0B,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2DpC,MAAM,aAAa,YAAkD;CACjE,MAAMA,SAAqB;CAC3B,MAAMC,OAA0BC,+CAAkB;AAElD,kCAAe,SAAS,OAAO,IAAI,MAAO,MAA8B;AACpE,SAAO,IAAIC,kCAAc,QAAQ,EAC7B,KAAKC,iCAAmB,GAAG;GACvB;GACA,QAAQ,CACJ;IACI;IACA,SAASC,qDAAwB,KAAK;IACzC,CACJ;GACJ,CAAC,EACL,CAAC;GACJ"}
|
package/dist/time-limit.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createJsonResponse } from "./response/json/index.mjs";
|
|
2
|
-
import { ResponseErrorCode, getResponseErrorMessage } from "./response/error.mjs";
|
|
2
|
+
import { ResponseErrorCode, getResponseErrorMessage } from "./response/error/index.mjs";
|
|
3
3
|
import { HTTPException } from "hono/http-exception";
|
|
4
4
|
import { timeout } from "hono/timeout";
|
|
5
5
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jderstd/hono",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "A response builder for Hono",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"jder",
|
|
@@ -30,9 +30,14 @@
|
|
|
30
30
|
"require": "./dist/response.js"
|
|
31
31
|
},
|
|
32
32
|
"./response/error": {
|
|
33
|
-
"types": "./dist/response/error.d.ts",
|
|
34
|
-
"import": "./dist/response/error.mjs",
|
|
35
|
-
"require": "./dist/response/error.js"
|
|
33
|
+
"types": "./dist/response/error/index.d.ts",
|
|
34
|
+
"import": "./dist/response/error/index.mjs",
|
|
35
|
+
"require": "./dist/response/error/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./response/error/http": {
|
|
38
|
+
"types": "./dist/response/error/http.d.ts",
|
|
39
|
+
"import": "./dist/response/error/http.mjs",
|
|
40
|
+
"require": "./dist/response/error/http.js"
|
|
36
41
|
},
|
|
37
42
|
"./body-limit": {
|
|
38
43
|
"types": "./dist/body-limit.d.ts",
|
|
@@ -1,23 +0,0 @@
|
|
|
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;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"error.js","names":[],"sources":["../../src/response/error/index.ts"],"sourcesContent":["/**\n * Response error code.\n */\nenum ResponseErrorCode {\n /**\n * Request body is too large.\n *\n * For `bodyLimit` middleware.\n */\n TooLarge = \"too_large\",\n /**\n * Forbidden access.\n *\n * For `ipLimit` middleware.\n */\n Forbidden = \"forbidden\",\n /**\n * Request timeout.\n *\n * For `timeLimit` middleware.\n */\n Timeout = \"timeout\",\n /**\n * Content not found.\n *\n * For `notFoundHandler` function.\n */\n NotFound = \"not_found\",\n /**\n * Bad request.\n *\n * For `onErrorHandler` function.\n */\n BadRequest = \"bad_request\",\n /**\n * Internal server error.\n *\n * For `onErrorHandler` function.\n */\n Server = \"server\",\n /**\n * Validation error.\n *\n * For validator package.\n */\n Parse = \"parse\",\n}\n\n/**\n * Get response error message by code.\n */\nconst getResponseErrorMessage = (code: ResponseErrorCode): string => {\n switch (code) {\n case ResponseErrorCode.TooLarge: {\n return \"Request body is too large\";\n }\n case ResponseErrorCode.Forbidden: {\n return \"Forbidden IP address\";\n }\n case ResponseErrorCode.Timeout: {\n return \"Gateway timeout\";\n }\n case ResponseErrorCode.NotFound: {\n return \"Content not found\";\n }\n case ResponseErrorCode.BadRequest: {\n return \"Bad request\";\n }\n case ResponseErrorCode.Server: {\n return \"Internal server error\";\n }\n case ResponseErrorCode.Parse: {\n return \"Failed to parse the request\";\n }\n }\n};\n\nexport { ResponseErrorCode, getResponseErrorMessage };\n"],"mappings":";;;;AAGA,IAAK,kEAAL;;;;;;AAMI;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;EA1CC;;;;AAgDL,MAAM,2BAA2B,SAAoC;AACjE,SAAQ,MAAR;EACI,KAAK,kBAAkB,SACnB,QAAO;EAEX,KAAK,kBAAkB,UACnB,QAAO;EAEX,KAAK,kBAAkB,QACnB,QAAO;EAEX,KAAK,kBAAkB,SACnB,QAAO;EAEX,KAAK,kBAAkB,WACnB,QAAO;EAEX,KAAK,kBAAkB,OACnB,QAAO;EAEX,KAAK,kBAAkB,MACnB,QAAO"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"error.mjs","names":[],"sources":["../../src/response/error/index.ts"],"sourcesContent":["/**\n * Response error code.\n */\nenum ResponseErrorCode {\n /**\n * Request body is too large.\n *\n * For `bodyLimit` middleware.\n */\n TooLarge = \"too_large\",\n /**\n * Forbidden access.\n *\n * For `ipLimit` middleware.\n */\n Forbidden = \"forbidden\",\n /**\n * Request timeout.\n *\n * For `timeLimit` middleware.\n */\n Timeout = \"timeout\",\n /**\n * Content not found.\n *\n * For `notFoundHandler` function.\n */\n NotFound = \"not_found\",\n /**\n * Bad request.\n *\n * For `onErrorHandler` function.\n */\n BadRequest = \"bad_request\",\n /**\n * Internal server error.\n *\n * For `onErrorHandler` function.\n */\n Server = \"server\",\n /**\n * Validation error.\n *\n * For validator package.\n */\n Parse = \"parse\",\n}\n\n/**\n * Get response error message by code.\n */\nconst getResponseErrorMessage = (code: ResponseErrorCode): string => {\n switch (code) {\n case ResponseErrorCode.TooLarge: {\n return \"Request body is too large\";\n }\n case ResponseErrorCode.Forbidden: {\n return \"Forbidden IP address\";\n }\n case ResponseErrorCode.Timeout: {\n return \"Gateway timeout\";\n }\n case ResponseErrorCode.NotFound: {\n return \"Content not found\";\n }\n case ResponseErrorCode.BadRequest: {\n return \"Bad request\";\n }\n case ResponseErrorCode.Server: {\n return \"Internal server error\";\n }\n case ResponseErrorCode.Parse: {\n return \"Failed to parse the request\";\n }\n }\n};\n\nexport { ResponseErrorCode, getResponseErrorMessage };\n"],"mappings":";;;AAGA,IAAK,kEAAL;;;;;;AAMI;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;;;;;AAMA;;EA1CC;;;;AAgDL,MAAM,2BAA2B,SAAoC;AACjE,SAAQ,MAAR;EACI,KAAK,kBAAkB,SACnB,QAAO;EAEX,KAAK,kBAAkB,UACnB,QAAO;EAEX,KAAK,kBAAkB,QACnB,QAAO;EAEX,KAAK,kBAAkB,SACnB,QAAO;EAEX,KAAK,kBAAkB,WACnB,QAAO;EAEX,KAAK,kBAAkB,OACnB,QAAO;EAEX,KAAK,kBAAkB,MACnB,QAAO"}
|