@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
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Context } from "hono";
|
|
2
|
+
import { Format, Omit } from "ts-vista";
|
|
3
|
+
import { StatusCode } from "hono/utils/http-status";
|
|
4
|
+
import { CreateJsonResponseStructOptions } from "@jderstd/core/response/json/struct";
|
|
5
|
+
/** Options of `createJsonResponse` function. */
|
|
6
|
+
type CreateJsonResponseOptions<D = unknown> = Format<{
|
|
7
|
+
/**
|
|
8
|
+
* Status code of the response.
|
|
9
|
+
* By default, it is `200` for success and `400` for failure.
|
|
10
|
+
*/
|
|
11
|
+
status?: StatusCode;
|
|
12
|
+
} & Omit<CreateJsonResponseStructOptions<D>, "status">>;
|
|
13
|
+
/**
|
|
14
|
+
* Create a JSON response.
|
|
15
|
+
*
|
|
16
|
+
* ### Examples
|
|
17
|
+
*
|
|
18
|
+
* Example for creating a successful JSON response without data:
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { createJsonResponse } from "@jderstd/hono/response";
|
|
22
|
+
*
|
|
23
|
+
* const route = (): Response => {
|
|
24
|
+
* return createJsonResponse();
|
|
25
|
+
* };
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* Example for creating a successful JSON response with data:
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { createJsonResponse } from "@jderstd/hono/response";
|
|
32
|
+
*
|
|
33
|
+
* const route = (): Response => {
|
|
34
|
+
* return createJsonResponse({
|
|
35
|
+
* data: "Hello, World!",
|
|
36
|
+
* });
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* Example for creating a failed JSON response:
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { createJsonResponse } from "@jderstd/hono/response";
|
|
44
|
+
*
|
|
45
|
+
* const route = (): Response => {
|
|
46
|
+
* return createJsonResponse({
|
|
47
|
+
* errors: [
|
|
48
|
+
* {
|
|
49
|
+
* code: "server",
|
|
50
|
+
* message: "Internal server error",
|
|
51
|
+
* },
|
|
52
|
+
* ],
|
|
53
|
+
* });
|
|
54
|
+
* };
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare function createJsonResponse<D = unknown>(options?: CreateJsonResponseOptions<D>): Response;
|
|
58
|
+
/**
|
|
59
|
+
* Create a JSON response with context.
|
|
60
|
+
*
|
|
61
|
+
* ### Examples
|
|
62
|
+
*
|
|
63
|
+
* Example for creating a successful JSON response without data:
|
|
64
|
+
*
|
|
65
|
+
* ```ts
|
|
66
|
+
* import type { Context } from "hono";
|
|
67
|
+
*
|
|
68
|
+
* import { createJsonResponse } from "@jderstd/hono/response";
|
|
69
|
+
*
|
|
70
|
+
* const route = (c: Context): Response => {
|
|
71
|
+
* return createJsonResponse(c);
|
|
72
|
+
* };
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* Example for creating a successful JSON response with data:
|
|
76
|
+
*
|
|
77
|
+
* ```ts
|
|
78
|
+
* import type { Context } from "hono";
|
|
79
|
+
*
|
|
80
|
+
* import { createJsonResponse } from "@jderstd/hono/response";
|
|
81
|
+
*
|
|
82
|
+
* const route = (c: Context): Response => {
|
|
83
|
+
* return createJsonResponse(c, {
|
|
84
|
+
* data: "Hello, World!",
|
|
85
|
+
* });
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* Example for creating a failed JSON response:
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* import type { Context } from "hono";
|
|
93
|
+
*
|
|
94
|
+
* import { createJsonResponse } from "@jderstd/hono/response";
|
|
95
|
+
*
|
|
96
|
+
* const route = (c: Context): Response => {
|
|
97
|
+
* return createJsonResponse(c, {
|
|
98
|
+
* errors: [
|
|
99
|
+
* {
|
|
100
|
+
* code: "server",
|
|
101
|
+
* message: "Internal server error",
|
|
102
|
+
* },
|
|
103
|
+
* ],
|
|
104
|
+
* });
|
|
105
|
+
* };
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
declare function createJsonResponse<D = unknown>(context?: Context, options?: CreateJsonResponseOptions<D>): Response;
|
|
109
|
+
export { type CreateJsonResponseOptions, createJsonResponse };
|
|
110
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.js');
|
|
2
|
+
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
|
+
|
|
5
|
+
/** Check if the argument is a Hono context. */
|
|
6
|
+
const isContext = (arg) => arg?.req !== void 0;
|
|
7
|
+
function createJsonResponse(contextOrOptions, options) {
|
|
8
|
+
const { status, headers, json } = isContext(contextOrOptions) ? (0, __jderstd_core_response_json_struct.createJsonResponseStruct)(options) : (0, __jderstd_core_response_json_struct.createJsonResponseStruct)(contextOrOptions);
|
|
9
|
+
if (isContext(contextOrOptions)) {
|
|
10
|
+
const c = contextOrOptions;
|
|
11
|
+
c.status(status);
|
|
12
|
+
for (const [key, value] of headers) c.header(key, value);
|
|
13
|
+
return c.json(json);
|
|
14
|
+
}
|
|
15
|
+
return new Response(JSON.stringify(json), {
|
|
16
|
+
status,
|
|
17
|
+
headers
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
exports.createJsonResponse = createJsonResponse;
|
|
22
|
+
exports.isContext = isContext;
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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":";;;;;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"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createJsonResponseStruct } from "@jderstd/core/response/json/struct";
|
|
2
|
+
|
|
3
|
+
/** Check if the argument is a Hono context. */
|
|
4
|
+
const isContext = (arg) => arg?.req !== void 0;
|
|
5
|
+
function createJsonResponse(contextOrOptions, options) {
|
|
6
|
+
const { status, headers, json } = isContext(contextOrOptions) ? createJsonResponseStruct(options) : createJsonResponseStruct(contextOrOptions);
|
|
7
|
+
if (isContext(contextOrOptions)) {
|
|
8
|
+
const c = contextOrOptions;
|
|
9
|
+
c.status(status);
|
|
10
|
+
for (const [key, value] of headers) c.header(key, value);
|
|
11
|
+
return c.json(json);
|
|
12
|
+
}
|
|
13
|
+
return new Response(JSON.stringify(json), {
|
|
14
|
+
status,
|
|
15
|
+
headers
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { createJsonResponse, isContext };
|
|
20
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","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,GACvD,yBAAyB,QAAQ,GACjC,yBAAyB,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"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { CreateResponseOptions, createResponse } from "./response/common/index.js";
|
|
2
|
+
import { CreateJsonResponseOptions, createJsonResponse } from "./response/json/index.js";
|
|
3
|
+
import { JsonResponse, JsonResponseError } from "@jderstd/core";
|
|
4
|
+
export { type CreateJsonResponseOptions, type CreateResponseOptions, type JsonResponse, type JsonResponseError, createJsonResponse, createResponse };
|
package/dist/response.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { MiddlewareHandler } from "hono";
|
|
2
|
+
/** Default maximum time in milliseconds. */
|
|
3
|
+
declare const TIME_LIMIT_MAX_DEFAULT: 5000;
|
|
4
|
+
/** Options for `timeLimit` middleware. */
|
|
5
|
+
type TimeLimitOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Maximum time in milliseconds.
|
|
8
|
+
*
|
|
9
|
+
* By default, it is `TIME_LIMIT_MAX_DEFAULT`.
|
|
10
|
+
*/
|
|
11
|
+
max?: number;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Time limit middleware.
|
|
15
|
+
*
|
|
16
|
+
* Following error will be returned if the request takes longer than the limit:
|
|
17
|
+
*
|
|
18
|
+
* ```jsonc
|
|
19
|
+
* // Status: 504
|
|
20
|
+
* {
|
|
21
|
+
* "success": false,
|
|
22
|
+
* "errors": [
|
|
23
|
+
* {
|
|
24
|
+
* "code": "timeout",
|
|
25
|
+
* "message": "Request timeout"
|
|
26
|
+
* }
|
|
27
|
+
* ]
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* For more information, please refer to
|
|
32
|
+
* [Timeout](https://hono.dev/docs/middleware/builtin/timeout).
|
|
33
|
+
*
|
|
34
|
+
* ### Examples
|
|
35
|
+
*
|
|
36
|
+
* A example of using `timeLimit` middleware:
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { Hono } from "hono";
|
|
40
|
+
* import { timeLimit } from "@jderstd/hono/time-limit";
|
|
41
|
+
*
|
|
42
|
+
* const app: Hono = new Hono();
|
|
43
|
+
*
|
|
44
|
+
* app.use(timeLimit());
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* A example of using `timeLimit` middleware with options:
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { Hono } from "hono";
|
|
51
|
+
* import { timeLimit } from "@jderstd/hono/time-limit";
|
|
52
|
+
*
|
|
53
|
+
* const app: Hono = new Hono();
|
|
54
|
+
*
|
|
55
|
+
* app.use(timeLimit({
|
|
56
|
+
* max: 10 * 1000, // 10s
|
|
57
|
+
* }));
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
declare const timeLimit: (options?: TimeLimitOptions) => MiddlewareHandler;
|
|
61
|
+
export { TIME_LIMIT_MAX_DEFAULT, type TimeLimitOptions, timeLimit };
|
|
62
|
+
//# sourceMappingURL=time-limit.d.ts.map
|
|
@@ -0,0 +1,74 @@
|
|
|
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_http_exception = require("hono/http-exception");
|
|
5
|
+
hono_http_exception = require_rolldown_runtime.__toESM(hono_http_exception);
|
|
6
|
+
let hono_timeout = require("hono/timeout");
|
|
7
|
+
hono_timeout = require_rolldown_runtime.__toESM(hono_timeout);
|
|
8
|
+
|
|
9
|
+
/** Default maximum time in milliseconds. */
|
|
10
|
+
const TIME_LIMIT_MAX_DEFAULT = 5 * 1e3;
|
|
11
|
+
/**
|
|
12
|
+
* Time limit middleware.
|
|
13
|
+
*
|
|
14
|
+
* Following error will be returned if the request takes longer than the limit:
|
|
15
|
+
*
|
|
16
|
+
* ```jsonc
|
|
17
|
+
* // Status: 504
|
|
18
|
+
* {
|
|
19
|
+
* "success": false,
|
|
20
|
+
* "errors": [
|
|
21
|
+
* {
|
|
22
|
+
* "code": "timeout",
|
|
23
|
+
* "message": "Request timeout"
|
|
24
|
+
* }
|
|
25
|
+
* ]
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* For more information, please refer to
|
|
30
|
+
* [Timeout](https://hono.dev/docs/middleware/builtin/timeout).
|
|
31
|
+
*
|
|
32
|
+
* ### Examples
|
|
33
|
+
*
|
|
34
|
+
* A example of using `timeLimit` middleware:
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { Hono } from "hono";
|
|
38
|
+
* import { timeLimit } from "@jderstd/hono/time-limit";
|
|
39
|
+
*
|
|
40
|
+
* const app: Hono = new Hono();
|
|
41
|
+
*
|
|
42
|
+
* app.use(timeLimit());
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* A example of using `timeLimit` middleware with options:
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { Hono } from "hono";
|
|
49
|
+
* import { timeLimit } from "@jderstd/hono/time-limit";
|
|
50
|
+
*
|
|
51
|
+
* const app: Hono = new Hono();
|
|
52
|
+
*
|
|
53
|
+
* app.use(timeLimit({
|
|
54
|
+
* max: 10 * 1000, // 10s
|
|
55
|
+
* }));
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
const timeLimit = (options) => {
|
|
59
|
+
const status = 504;
|
|
60
|
+
const code = require_response_error.ResponseErrorCode.Timeout;
|
|
61
|
+
return (0, hono_timeout.timeout)(options?.max ?? 5 * 1e3, (c) => {
|
|
62
|
+
return new hono_http_exception.HTTPException(status, { res: require_index.createJsonResponse(c, {
|
|
63
|
+
status,
|
|
64
|
+
errors: [{
|
|
65
|
+
code,
|
|
66
|
+
message: require_response_error.getResponseErrorMessage(code)
|
|
67
|
+
}]
|
|
68
|
+
}) });
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
exports.TIME_LIMIT_MAX_DEFAULT = TIME_LIMIT_MAX_DEFAULT;
|
|
73
|
+
exports.timeLimit = timeLimit;
|
|
74
|
+
//# sourceMappingURL=time-limit.js.map
|
|
@@ -0,0 +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":";;;;;;;;;AAeA,MAAM,yBAA0B,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2DpC,MAAM,aAAa,YAAkD;CACjE,MAAMA,SAAqB;CAC3B,MAAMC,OAA0BC,yCAAkB;AAElD,kCAAe,SAAS,OAAO,IAAI,MAAO,MAA8B;AACpE,SAAO,IAAIC,kCAAc,QAAQ,EAC7B,KAAKC,iCAAmB,GAAG;GACvB;GACA,QAAQ,CACJ;IACI;IACA,SAASC,+CAAwB,KAAK;IACzC,CACJ;GACJ,CAAC,EACL,CAAC;GACJ"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { createJsonResponse } from "./response/json/index.mjs";
|
|
2
|
+
import { ResponseErrorCode, getResponseErrorMessage } from "./response/error.mjs";
|
|
3
|
+
import { HTTPException } from "hono/http-exception";
|
|
4
|
+
import { timeout } from "hono/timeout";
|
|
5
|
+
|
|
6
|
+
/** Default maximum time in milliseconds. */
|
|
7
|
+
const TIME_LIMIT_MAX_DEFAULT = 5 * 1e3;
|
|
8
|
+
/**
|
|
9
|
+
* Time limit middleware.
|
|
10
|
+
*
|
|
11
|
+
* Following error will be returned if the request takes longer than the limit:
|
|
12
|
+
*
|
|
13
|
+
* ```jsonc
|
|
14
|
+
* // Status: 504
|
|
15
|
+
* {
|
|
16
|
+
* "success": false,
|
|
17
|
+
* "errors": [
|
|
18
|
+
* {
|
|
19
|
+
* "code": "timeout",
|
|
20
|
+
* "message": "Request timeout"
|
|
21
|
+
* }
|
|
22
|
+
* ]
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* For more information, please refer to
|
|
27
|
+
* [Timeout](https://hono.dev/docs/middleware/builtin/timeout).
|
|
28
|
+
*
|
|
29
|
+
* ### Examples
|
|
30
|
+
*
|
|
31
|
+
* A example of using `timeLimit` middleware:
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { Hono } from "hono";
|
|
35
|
+
* import { timeLimit } from "@jderstd/hono/time-limit";
|
|
36
|
+
*
|
|
37
|
+
* const app: Hono = new Hono();
|
|
38
|
+
*
|
|
39
|
+
* app.use(timeLimit());
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* A example of using `timeLimit` middleware with options:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { Hono } from "hono";
|
|
46
|
+
* import { timeLimit } from "@jderstd/hono/time-limit";
|
|
47
|
+
*
|
|
48
|
+
* const app: Hono = new Hono();
|
|
49
|
+
*
|
|
50
|
+
* app.use(timeLimit({
|
|
51
|
+
* max: 10 * 1000, // 10s
|
|
52
|
+
* }));
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
const timeLimit = (options) => {
|
|
56
|
+
const status = 504;
|
|
57
|
+
const code = ResponseErrorCode.Timeout;
|
|
58
|
+
return timeout(options?.max ?? 5 * 1e3, (c) => {
|
|
59
|
+
return new HTTPException(status, { res: createJsonResponse(c, {
|
|
60
|
+
status,
|
|
61
|
+
errors: [{
|
|
62
|
+
code,
|
|
63
|
+
message: getResponseErrorMessage(code)
|
|
64
|
+
}]
|
|
65
|
+
}) });
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export { TIME_LIMIT_MAX_DEFAULT, timeLimit };
|
|
70
|
+
//# sourceMappingURL=time-limit.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"time-limit.mjs","names":["status: StatusCode","code: ResponseErrorCode"],"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,OAA0B,kBAAkB;AAElD,QAAO,QAAQ,SAAS,OAAO,IAAI,MAAO,MAA8B;AACpE,SAAO,IAAI,cAAc,QAAQ,EAC7B,KAAK,mBAAmB,GAAG;GACvB;GACA,QAAQ,CACJ;IACI;IACA,SAAS,wBAAwB,KAAK;IACzC,CACJ;GACJ,CAAC,EACL,CAAC;GACJ"}
|
package/package.json
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jderstd/hono",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "A response builder for Hono",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"jder",
|
|
7
|
+
"hono",
|
|
8
|
+
"response",
|
|
9
|
+
"ts",
|
|
10
|
+
"typescript",
|
|
11
|
+
"js",
|
|
12
|
+
"javascript"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/jderstd/hono",
|
|
15
|
+
"bugs": "https://github.com/jderstd/hono/issues",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/jderstd/hono.git",
|
|
19
|
+
"directory": "packages/hono"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "Alpheus",
|
|
24
|
+
"email": "contact@alphe.us"
|
|
25
|
+
},
|
|
26
|
+
"exports": {
|
|
27
|
+
"./response": {
|
|
28
|
+
"types": "./dist/response.d.ts",
|
|
29
|
+
"import": "./dist/response.mjs",
|
|
30
|
+
"require": "./dist/response.js"
|
|
31
|
+
},
|
|
32
|
+
"./response/error": {
|
|
33
|
+
"types": "./dist/response/error.d.ts",
|
|
34
|
+
"import": "./dist/response/error.mjs",
|
|
35
|
+
"require": "./dist/response/error.js"
|
|
36
|
+
},
|
|
37
|
+
"./body-limit": {
|
|
38
|
+
"types": "./dist/body-limit.d.ts",
|
|
39
|
+
"import": "./dist/body-limit.mjs",
|
|
40
|
+
"require": "./dist/body-limit.js"
|
|
41
|
+
},
|
|
42
|
+
"./ip-limit": {
|
|
43
|
+
"types": "./dist/ip-limit.d.ts",
|
|
44
|
+
"import": "./dist/ip-limit.mjs",
|
|
45
|
+
"require": "./dist/ip-limit.js"
|
|
46
|
+
},
|
|
47
|
+
"./time-limit": {
|
|
48
|
+
"types": "./dist/time-limit.d.ts",
|
|
49
|
+
"import": "./dist/time-limit.mjs",
|
|
50
|
+
"require": "./dist/time-limit.js"
|
|
51
|
+
},
|
|
52
|
+
"./not-found": {
|
|
53
|
+
"types": "./dist/not-found.d.ts",
|
|
54
|
+
"import": "./dist/not-found.mjs",
|
|
55
|
+
"require": "./dist/not-found.js"
|
|
56
|
+
},
|
|
57
|
+
"./on-error": {
|
|
58
|
+
"types": "./dist/on-error.d.ts",
|
|
59
|
+
"import": "./dist/on-error.mjs",
|
|
60
|
+
"require": "./dist/on-error.js"
|
|
61
|
+
},
|
|
62
|
+
"./package.json": "./package.json"
|
|
63
|
+
},
|
|
64
|
+
"main": "./dist/response.js",
|
|
65
|
+
"module": "./dist/response.mjs",
|
|
66
|
+
"types": "./dist/response.d.ts",
|
|
67
|
+
"files": [
|
|
68
|
+
"dist"
|
|
69
|
+
],
|
|
70
|
+
"dependencies": {
|
|
71
|
+
"@jderstd/core": "~0.4.0",
|
|
72
|
+
"ts-vista": "~0.2.2"
|
|
73
|
+
},
|
|
74
|
+
"devDependencies": {
|
|
75
|
+
"hono": "4.5.0"
|
|
76
|
+
},
|
|
77
|
+
"peerDependencies": {
|
|
78
|
+
"hono": "^4.5.0"
|
|
79
|
+
}
|
|
80
|
+
}
|