@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.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +29 -0
  3. package/dist/_virtual/rolldown_runtime.js +23 -0
  4. package/dist/body-limit.d.ts +66 -0
  5. package/dist/body-limit.js +79 -0
  6. package/dist/body-limit.js.map +1 -0
  7. package/dist/body-limit.mjs +76 -0
  8. package/dist/body-limit.mjs.map +1 -0
  9. package/dist/ip-limit.d.ts +99 -0
  10. package/dist/ip-limit.js +30 -0
  11. package/dist/ip-limit.js.map +1 -0
  12. package/dist/ip-limit.mjs +28 -0
  13. package/dist/ip-limit.mjs.map +1 -0
  14. package/dist/not-found.d.ts +33 -0
  15. package/dist/not-found.js +48 -0
  16. package/dist/not-found.js.map +1 -0
  17. package/dist/not-found.mjs +48 -0
  18. package/dist/not-found.mjs.map +1 -0
  19. package/dist/on-error.d.ts +66 -0
  20. package/dist/on-error.js +85 -0
  21. package/dist/on-error.js.map +1 -0
  22. package/dist/on-error.mjs +83 -0
  23. package/dist/on-error.mjs.map +1 -0
  24. package/dist/response/common/index.d.ts +82 -0
  25. package/dist/response/common/index.js +21 -0
  26. package/dist/response/common/index.js.map +1 -0
  27. package/dist/response/common/index.mjs +19 -0
  28. package/dist/response/common/index.mjs.map +1 -0
  29. package/dist/response/error.d.ts +53 -0
  30. package/dist/response/error.js +67 -0
  31. package/dist/response/error.js.map +1 -0
  32. package/dist/response/error.mjs +65 -0
  33. package/dist/response/error.mjs.map +1 -0
  34. package/dist/response/json/index.d.ts +110 -0
  35. package/dist/response/json/index.js +23 -0
  36. package/dist/response/json/index.js.map +1 -0
  37. package/dist/response/json/index.mjs +20 -0
  38. package/dist/response/json/index.mjs.map +1 -0
  39. package/dist/response.d.ts +4 -0
  40. package/dist/response.js +5 -0
  41. package/dist/response.mjs +4 -0
  42. package/dist/time-limit.d.ts +62 -0
  43. package/dist/time-limit.js +74 -0
  44. package/dist/time-limit.js.map +1 -0
  45. package/dist/time-limit.mjs +70 -0
  46. package/dist/time-limit.mjs.map +1 -0
  47. package/package.json +80 -0
@@ -0,0 +1,66 @@
1
+ import { Context } from "hono";
2
+ import { HTTPResponseError } from "hono/types";
3
+ /** Options for `onErrorHandler` function. */
4
+ type OnErrorHandlerOptions = {
5
+ /**
6
+ * Whether show more information.
7
+ * By default, it's `false`.
8
+ */
9
+ verbose?: boolean;
10
+ };
11
+ /**
12
+ * On error handler.
13
+ *
14
+ * Following responses could be returned on error:
15
+ *
16
+ * ```jsonc
17
+ * // Status: 400
18
+ * {
19
+ * "success": false,
20
+ * "errors": [
21
+ * {
22
+ * "code": "bad_request"
23
+ * }
24
+ * ]
25
+ * }
26
+ * ```
27
+ *
28
+ * ```jsonc
29
+ * // Status: 500
30
+ * {
31
+ * "success": false,
32
+ * "errors": [
33
+ * {
34
+ * "code": "server"
35
+ * }
36
+ * ]
37
+ * }
38
+ * ```
39
+ *
40
+ * ### Examples
41
+ *
42
+ * Basic example of using `onErrorHandler` handler:
43
+ *
44
+ * ```ts
45
+ * import { Hono } from "hono";
46
+ * import { onErrorHandler } from "@jderstd/hono/on-error";
47
+ *
48
+ * const app: Hono = new Hono();
49
+ *
50
+ * app.onError(onErrorHandler());
51
+ * ```
52
+ *
53
+ * Show more information with `verbose` option:
54
+ *
55
+ * ```ts
56
+ * import { Hono } from "hono";
57
+ * import { onErrorHandler } from "@jderstd/hono/on-error";
58
+ *
59
+ * const app: Hono = new Hono();
60
+ *
61
+ * app.onError(onErrorHandler({ verbose: true }));
62
+ * ```
63
+ */
64
+ declare const onErrorHandler: (options?: OnErrorHandlerOptions) => ((err: Error | HTTPResponseError, c: Context) => Response);
65
+ export { onErrorHandler };
66
+ //# sourceMappingURL=on-error.d.ts.map
@@ -0,0 +1,85 @@
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js');
2
+ const require_index = require('./response/json/index.js');
3
+ require('./response.js');
4
+ const require_response_error = require('./response/error.js');
5
+ let hono_http_exception = require("hono/http-exception");
6
+ hono_http_exception = require_rolldown_runtime.__toESM(hono_http_exception);
7
+
8
+ /**
9
+ * On error handler.
10
+ *
11
+ * Following responses could be returned on error:
12
+ *
13
+ * ```jsonc
14
+ * // Status: 400
15
+ * {
16
+ * "success": false,
17
+ * "errors": [
18
+ * {
19
+ * "code": "bad_request"
20
+ * }
21
+ * ]
22
+ * }
23
+ * ```
24
+ *
25
+ * ```jsonc
26
+ * // Status: 500
27
+ * {
28
+ * "success": false,
29
+ * "errors": [
30
+ * {
31
+ * "code": "server"
32
+ * }
33
+ * ]
34
+ * }
35
+ * ```
36
+ *
37
+ * ### Examples
38
+ *
39
+ * Basic example of using `onErrorHandler` handler:
40
+ *
41
+ * ```ts
42
+ * import { Hono } from "hono";
43
+ * import { onErrorHandler } from "@jderstd/hono/on-error";
44
+ *
45
+ * const app: Hono = new Hono();
46
+ *
47
+ * app.onError(onErrorHandler());
48
+ * ```
49
+ *
50
+ * Show more information with `verbose` option:
51
+ *
52
+ * ```ts
53
+ * import { Hono } from "hono";
54
+ * import { onErrorHandler } from "@jderstd/hono/on-error";
55
+ *
56
+ * const app: Hono = new Hono();
57
+ *
58
+ * app.onError(onErrorHandler({ verbose: true }));
59
+ * ```
60
+ */
61
+ const onErrorHandler = (options) => {
62
+ return (err, c) => {
63
+ if (err instanceof hono_http_exception.HTTPException) {
64
+ const res = err.getResponse();
65
+ const code = res.status >= 500 ? require_response_error.ResponseErrorCode.Server : require_response_error.ResponseErrorCode.BadRequest;
66
+ return require_index.createJsonResponse(c, {
67
+ status: res.status,
68
+ errors: [{
69
+ code,
70
+ ...options?.verbose && { message: err.message }
71
+ }]
72
+ });
73
+ }
74
+ return require_index.createJsonResponse(c, {
75
+ status: 500,
76
+ errors: [{
77
+ code: require_response_error.ResponseErrorCode.Server,
78
+ ...options?.verbose && { message: err.message }
79
+ }]
80
+ });
81
+ };
82
+ };
83
+
84
+ exports.onErrorHandler = onErrorHandler;
85
+ //# sourceMappingURL=on-error.js.map
@@ -0,0 +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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,MAAM,kBACF,YAC6D;AAC7D,SAAQ,KAAgC,MAAyB;AAC7D,MAAI,eAAeA,mCAAe;GAC9B,MAAMC,MAAgB,IAAI,aAAa;GAIvC,MAAMC,OAFqB,IAAI,UAGjB,MACJC,yCAAkB,SAClBA,yCAAkB;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,yCAAkB;IACxB,GAAI,SAAS,WAAW,EACpB,SAAS,IAAI,SAChB;IACJ,CACJ;GACJ,CAAC"}
@@ -0,0 +1,83 @@
1
+ import { createJsonResponse } from "./response/json/index.mjs";
2
+ import "./response.mjs";
3
+ import { ResponseErrorCode } from "./response/error.mjs";
4
+ import { HTTPException } from "hono/http-exception";
5
+
6
+ /**
7
+ * On error handler.
8
+ *
9
+ * Following responses could be returned on error:
10
+ *
11
+ * ```jsonc
12
+ * // Status: 400
13
+ * {
14
+ * "success": false,
15
+ * "errors": [
16
+ * {
17
+ * "code": "bad_request"
18
+ * }
19
+ * ]
20
+ * }
21
+ * ```
22
+ *
23
+ * ```jsonc
24
+ * // Status: 500
25
+ * {
26
+ * "success": false,
27
+ * "errors": [
28
+ * {
29
+ * "code": "server"
30
+ * }
31
+ * ]
32
+ * }
33
+ * ```
34
+ *
35
+ * ### Examples
36
+ *
37
+ * Basic example of using `onErrorHandler` handler:
38
+ *
39
+ * ```ts
40
+ * import { Hono } from "hono";
41
+ * import { onErrorHandler } from "@jderstd/hono/on-error";
42
+ *
43
+ * const app: Hono = new Hono();
44
+ *
45
+ * app.onError(onErrorHandler());
46
+ * ```
47
+ *
48
+ * Show more information with `verbose` option:
49
+ *
50
+ * ```ts
51
+ * import { Hono } from "hono";
52
+ * import { onErrorHandler } from "@jderstd/hono/on-error";
53
+ *
54
+ * const app: Hono = new Hono();
55
+ *
56
+ * app.onError(onErrorHandler({ verbose: true }));
57
+ * ```
58
+ */
59
+ const onErrorHandler = (options) => {
60
+ return (err, c) => {
61
+ if (err instanceof HTTPException) {
62
+ const res = err.getResponse();
63
+ const code = res.status >= 500 ? ResponseErrorCode.Server : ResponseErrorCode.BadRequest;
64
+ return createJsonResponse(c, {
65
+ status: res.status,
66
+ errors: [{
67
+ code,
68
+ ...options?.verbose && { message: err.message }
69
+ }]
70
+ });
71
+ }
72
+ return createJsonResponse(c, {
73
+ status: 500,
74
+ errors: [{
75
+ code: ResponseErrorCode.Server,
76
+ ...options?.verbose && { message: err.message }
77
+ }]
78
+ });
79
+ };
80
+ };
81
+
82
+ export { onErrorHandler };
83
+ //# sourceMappingURL=on-error.mjs.map
@@ -0,0 +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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,MAAM,kBACF,YAC6D;AAC7D,SAAQ,KAAgC,MAAyB;AAC7D,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"}
@@ -0,0 +1,82 @@
1
+ import { Context } from "hono";
2
+ import { Format, Omit } from "ts-vista";
3
+ import { CreateResponseStructOptions } from "@jderstd/core/response/common/struct";
4
+ import { StatusCode } from "hono/utils/http-status";
5
+ /** Options of `createResponse` function. */
6
+ type CreateResponseOptions<B extends BodyInit = BodyInit> = 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<CreateResponseStructOptions<B>, "status">>;
13
+ /**
14
+ * Create a response.
15
+ *
16
+ * ### Examples
17
+ *
18
+ * Example for creating a basic response:
19
+ *
20
+ * ```ts
21
+ * import { createResponse } from "@jderstd/hono/response";
22
+ *
23
+ * const route = (): Response => {
24
+ * return createResponse();
25
+ * };
26
+ * ```
27
+ *
28
+ * Example for creating a response with status, headers, and body:
29
+ *
30
+ * ```ts
31
+ * import { createResponse } from "@jderstd/hono/response";
32
+ *
33
+ * const route = (): Response => {
34
+ * return createResponse({
35
+ * status: 404,
36
+ * headers: [
37
+ * ["Content-Type", "text/plain"],
38
+ * ],
39
+ * body: "Not Found",
40
+ * });
41
+ * };
42
+ * ```
43
+ */
44
+ declare function createResponse<B extends BodyInit = BodyInit>(options?: CreateResponseOptions<B>): Response;
45
+ /**
46
+ * Create a response with context.
47
+ *
48
+ * ### Examples
49
+ *
50
+ * Example for creating a basic response:
51
+ *
52
+ * ```ts
53
+ * import type { Context } from "hono";
54
+ *
55
+ * import { createResponse } from "@jderstd/hono/response";
56
+ *
57
+ * const route = (c: Context): Response => {
58
+ * return createResponse(c);
59
+ * };
60
+ * ```
61
+ *
62
+ * Example for creating a response with status, headers, and body:
63
+ *
64
+ * ```ts
65
+ * import type { Context } from "hono";
66
+ *
67
+ * import { createResponse } from "@jderstd/hono/response";
68
+ *
69
+ * const route = (c: Context): Response => {
70
+ * return createResponse(c, {
71
+ * status: 404,
72
+ * headers: [
73
+ * ["Content-Type", "text/plain"],
74
+ * ],
75
+ * body: "Not Found",
76
+ * });
77
+ * };
78
+ * ```
79
+ */
80
+ declare function createResponse<B extends BodyInit = BodyInit>(context?: Context, options?: CreateResponseOptions<B>): Response;
81
+ export { type CreateResponseOptions, createResponse };
82
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,21 @@
1
+ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.js');
2
+ const require_index = require('../json/index.js');
3
+ 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
+
6
+ function createResponse(contextOrOptions, options) {
7
+ const { status, headers, body } = require_index.isContext(contextOrOptions) ? (0, __jderstd_core_response_common_struct.createResponseStruct)(options) : (0, __jderstd_core_response_common_struct.createResponseStruct)(contextOrOptions);
8
+ if (require_index.isContext(contextOrOptions)) {
9
+ const c = contextOrOptions;
10
+ c.status(status);
11
+ for (const [key, value] of headers) c.header(key, value);
12
+ return body ? c.body(body) : c.body(null);
13
+ }
14
+ return new Response(body, {
15
+ status,
16
+ headers
17
+ });
18
+ }
19
+
20
+ exports.createResponse = createResponse;
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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":";;;;;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,19 @@
1
+ import { isContext } from "../json/index.mjs";
2
+ import { createResponseStruct } from "@jderstd/core/response/common/struct";
3
+
4
+ function createResponse(contextOrOptions, options) {
5
+ const { status, headers, body } = isContext(contextOrOptions) ? createResponseStruct(options) : createResponseStruct(contextOrOptions);
6
+ if (isContext(contextOrOptions)) {
7
+ const c = contextOrOptions;
8
+ c.status(status);
9
+ for (const [key, value] of headers) c.header(key, value);
10
+ return body ? c.body(body) : c.body(null);
11
+ }
12
+ return new Response(body, {
13
+ status,
14
+ headers
15
+ });
16
+ }
17
+
18
+ export { createResponse };
19
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":["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,SAAS,UAAU,iBAAiB,GACvD,qBAAqB,QAAQ,GAC7B,qBAAqB,iBAAiB;AAE5C,KAAI,UAAU,iBAAiB,EAAE;EAC7B,MAAMA,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,53 @@
1
+ /**
2
+ * Response error code.
3
+ */
4
+ declare enum ResponseErrorCode {
5
+ /**
6
+ * Request body is too large.
7
+ *
8
+ * For `bodyLimit` middleware.
9
+ */
10
+ TooLarge = "too_large",
11
+ /**
12
+ * Forbidden access.
13
+ *
14
+ * For `ipLimit` middleware.
15
+ */
16
+ Forbidden = "forbidden",
17
+ /**
18
+ * Request timeout.
19
+ *
20
+ * For `timeLimit` middleware.
21
+ */
22
+ Timeout = "timeout",
23
+ /**
24
+ * Content not found.
25
+ *
26
+ * For `notFoundHandler` function.
27
+ */
28
+ NotFound = "not_found",
29
+ /**
30
+ * Bad request.
31
+ *
32
+ * For `onErrorHandler` function.
33
+ */
34
+ BadRequest = "bad_request",
35
+ /**
36
+ * Internal server error.
37
+ *
38
+ * For `onErrorHandler` function.
39
+ */
40
+ Server = "server",
41
+ /**
42
+ * Validation error.
43
+ *
44
+ * For validator package.
45
+ */
46
+ Parse = "parse",
47
+ }
48
+ /**
49
+ * Get response error message by code.
50
+ */
51
+ declare const getResponseErrorMessage: (code: ResponseErrorCode) => string;
52
+ export { ResponseErrorCode, getResponseErrorMessage };
53
+ //# sourceMappingURL=error.d.ts.map
@@ -0,0 +1,67 @@
1
+
2
+ /**
3
+ * Response error code.
4
+ */
5
+ var ResponseErrorCode = /* @__PURE__ */ function(ResponseErrorCode$1) {
6
+ /**
7
+ * Request body is too large.
8
+ *
9
+ * For `bodyLimit` middleware.
10
+ */
11
+ ResponseErrorCode$1["TooLarge"] = "too_large";
12
+ /**
13
+ * Forbidden access.
14
+ *
15
+ * For `ipLimit` middleware.
16
+ */
17
+ ResponseErrorCode$1["Forbidden"] = "forbidden";
18
+ /**
19
+ * Request timeout.
20
+ *
21
+ * For `timeLimit` middleware.
22
+ */
23
+ ResponseErrorCode$1["Timeout"] = "timeout";
24
+ /**
25
+ * Content not found.
26
+ *
27
+ * For `notFoundHandler` function.
28
+ */
29
+ ResponseErrorCode$1["NotFound"] = "not_found";
30
+ /**
31
+ * Bad request.
32
+ *
33
+ * For `onErrorHandler` function.
34
+ */
35
+ ResponseErrorCode$1["BadRequest"] = "bad_request";
36
+ /**
37
+ * Internal server error.
38
+ *
39
+ * For `onErrorHandler` function.
40
+ */
41
+ ResponseErrorCode$1["Server"] = "server";
42
+ /**
43
+ * Validation error.
44
+ *
45
+ * For validator package.
46
+ */
47
+ ResponseErrorCode$1["Parse"] = "parse";
48
+ return ResponseErrorCode$1;
49
+ }(ResponseErrorCode || {});
50
+ /**
51
+ * Get response error message by code.
52
+ */
53
+ const getResponseErrorMessage = (code) => {
54
+ switch (code) {
55
+ case ResponseErrorCode.TooLarge: return "Request body is too large";
56
+ case ResponseErrorCode.Forbidden: return "Forbidden IP address";
57
+ case ResponseErrorCode.Timeout: return "Gateway timeout";
58
+ case ResponseErrorCode.NotFound: return "Content not found";
59
+ case ResponseErrorCode.BadRequest: return "Bad request";
60
+ case ResponseErrorCode.Server: return "Internal server error";
61
+ case ResponseErrorCode.Parse: return "Failed to parse the request";
62
+ }
63
+ };
64
+
65
+ exports.ResponseErrorCode = ResponseErrorCode;
66
+ exports.getResponseErrorMessage = getResponseErrorMessage;
67
+ //# sourceMappingURL=error.js.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Response error code.
3
+ */
4
+ var ResponseErrorCode = /* @__PURE__ */ function(ResponseErrorCode$1) {
5
+ /**
6
+ * Request body is too large.
7
+ *
8
+ * For `bodyLimit` middleware.
9
+ */
10
+ ResponseErrorCode$1["TooLarge"] = "too_large";
11
+ /**
12
+ * Forbidden access.
13
+ *
14
+ * For `ipLimit` middleware.
15
+ */
16
+ ResponseErrorCode$1["Forbidden"] = "forbidden";
17
+ /**
18
+ * Request timeout.
19
+ *
20
+ * For `timeLimit` middleware.
21
+ */
22
+ ResponseErrorCode$1["Timeout"] = "timeout";
23
+ /**
24
+ * Content not found.
25
+ *
26
+ * For `notFoundHandler` function.
27
+ */
28
+ ResponseErrorCode$1["NotFound"] = "not_found";
29
+ /**
30
+ * Bad request.
31
+ *
32
+ * For `onErrorHandler` function.
33
+ */
34
+ ResponseErrorCode$1["BadRequest"] = "bad_request";
35
+ /**
36
+ * Internal server error.
37
+ *
38
+ * For `onErrorHandler` function.
39
+ */
40
+ ResponseErrorCode$1["Server"] = "server";
41
+ /**
42
+ * Validation error.
43
+ *
44
+ * For validator package.
45
+ */
46
+ ResponseErrorCode$1["Parse"] = "parse";
47
+ return ResponseErrorCode$1;
48
+ }(ResponseErrorCode || {});
49
+ /**
50
+ * Get response error message by code.
51
+ */
52
+ const getResponseErrorMessage = (code) => {
53
+ switch (code) {
54
+ case ResponseErrorCode.TooLarge: return "Request body is too large";
55
+ case ResponseErrorCode.Forbidden: return "Forbidden IP address";
56
+ case ResponseErrorCode.Timeout: return "Gateway timeout";
57
+ case ResponseErrorCode.NotFound: return "Content not found";
58
+ case ResponseErrorCode.BadRequest: return "Bad request";
59
+ case ResponseErrorCode.Server: return "Internal server error";
60
+ case ResponseErrorCode.Parse: return "Failed to parse the request";
61
+ }
62
+ };
63
+
64
+ export { ResponseErrorCode, getResponseErrorMessage };
65
+ //# sourceMappingURL=error.mjs.map
@@ -0,0 +1 @@
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"}