@jderstd/express 0.1.0 → 0.2.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/README.md CHANGED
@@ -22,7 +22,9 @@ And the response will be shown as below:
22
22
 
23
23
  ```json
24
24
  {
25
- "success": true
25
+ "success": true,
26
+ "data": null,
27
+ "errors": []
26
28
  }
27
29
  ```
28
30
 
package/dist/index.d.ts CHANGED
@@ -1,267 +1,267 @@
1
- import { JsonResponse, JsonResponseError } from "@jderstd/core";
1
+ import { JsonResponse, JsonResponse as JsonResponse$1, JsonResponseError, JsonResponseErrorInput } from "@jderstd/core";
2
2
  import { ErrorRequestHandler, Response } from "express";
3
3
  import { CreateResponseStructOptions } from "@jderstd/core/response/common/struct";
4
4
  import { CreateJsonResponseStructOptions } from "@jderstd/core/response/json/struct";
5
5
  /** Options for `errorRequestHandler` function. */
6
6
  type ErrorRequestHandlerOptions = {
7
7
  /**
8
- * Whether show more information.
9
- * By default, it's `false`.
10
- */
8
+ * Whether show more information.
9
+ * By default, it's `false`.
10
+ */
11
11
  verbose?: boolean;
12
12
  };
13
13
  /**
14
- * Error request handler.
15
- *
16
- * Following response could be returned on error:
17
- *
18
- * ```jsonc
19
- * // Status: 500
20
- * {
21
- * "success": false,
22
- * "errors": [
23
- * {
24
- * "code": "server"
25
- * }
26
- * ]
27
- * }
28
- * ```
29
- *
30
- * ### Examples
31
- *
32
- * Basic example of using `errorRequestHandler` handler:
33
- *
34
- * ```js
35
- * // JavaScript
36
- *
37
- * import express from "express";
38
- * import { errorRequestHandler } from "@jderstd/express";
39
- *
40
- * const app = express();
41
- *
42
- * app.use(errorRequestHandler());
43
- * ```
44
- *
45
- * ```ts
46
- * // TypeScript
47
- *
48
- * import type { Express } from "express";
49
- *
50
- * import express from "express";
51
- * import { errorRequestHandler } from "@jderstd/express";
52
- *
53
- * const app: Express = express();
54
- *
55
- * app.use(errorRequestHandler());
56
- * ```
57
- *
58
- * Show more information with `verbose` option:
59
- *
60
- * ```js
61
- * // JavaScript
62
- *
63
- * import express from "express";
64
- * import { errorRequestHandler } from "@jderstd/express";
65
- *
66
- * const app = express();
67
- *
68
- * app.use(errorRequestHandler({ verbose: true }));
69
- * ```
70
- *
71
- * ```ts
72
- * // TypeScript
73
- *
74
- * import type { Express } from "express";
75
- *
76
- * import express from "express";
77
- * import { errorRequestHandler } from "@jderstd/express";
78
- *
79
- * const app: Express = express();
80
- *
81
- * app.use(errorRequestHandler({ verbose: true }));
82
- * ```
83
- */
14
+ * Error request handler.
15
+ *
16
+ * Following response could be returned on error:
17
+ *
18
+ * ```jsonc
19
+ * // Status: 500
20
+ * {
21
+ * "success": false,
22
+ * "errors": [
23
+ * {
24
+ * "code": "server"
25
+ * }
26
+ * ]
27
+ * }
28
+ * ```
29
+ *
30
+ * ### Examples
31
+ *
32
+ * Basic example of using `errorRequestHandler` handler:
33
+ *
34
+ * ```js
35
+ * // JavaScript
36
+ *
37
+ * import express from "express";
38
+ * import { errorRequestHandler } from "@jderstd/express";
39
+ *
40
+ * const app = express();
41
+ *
42
+ * app.use(errorRequestHandler());
43
+ * ```
44
+ *
45
+ * ```ts
46
+ * // TypeScript
47
+ *
48
+ * import type { Express } from "express";
49
+ *
50
+ * import express from "express";
51
+ * import { errorRequestHandler } from "@jderstd/express";
52
+ *
53
+ * const app: Express = express();
54
+ *
55
+ * app.use(errorRequestHandler());
56
+ * ```
57
+ *
58
+ * Show more information with `verbose` option:
59
+ *
60
+ * ```js
61
+ * // JavaScript
62
+ *
63
+ * import express from "express";
64
+ * import { errorRequestHandler } from "@jderstd/express";
65
+ *
66
+ * const app = express();
67
+ *
68
+ * app.use(errorRequestHandler({ verbose: true }));
69
+ * ```
70
+ *
71
+ * ```ts
72
+ * // TypeScript
73
+ *
74
+ * import type { Express } from "express";
75
+ *
76
+ * import express from "express";
77
+ * import { errorRequestHandler } from "@jderstd/express";
78
+ *
79
+ * const app: Express = express();
80
+ *
81
+ * app.use(errorRequestHandler({ verbose: true }));
82
+ * ```
83
+ */
84
84
  declare const errorRequestHandler: (options?: ErrorRequestHandlerOptions) => ErrorRequestHandler;
85
85
  /** Options of `createResponse` function. */
86
- type CreateResponseOptions<B$1 extends BodyInit = BodyInit> = CreateResponseStructOptions<B$1>;
86
+ type CreateResponseOptions<B extends BodyInit = BodyInit> = CreateResponseStructOptions<B>;
87
87
  /**
88
- * Create a response.
89
- *
90
- * ### Examples
91
- *
92
- * Example for creating a basic response:
93
- *
94
- * ```js
95
- * // JavaScript
96
- *
97
- * import { createResponse } from "@jderstd/express";
98
- *
99
- * const route = (req, res) => {
100
- * createResponse(res);
101
- * };
102
- * ```
103
- *
104
- * ```ts
105
- * // TypeScript
106
- *
107
- * import type { Request, Response } from "express";
108
- *
109
- * import { createResponse } from "@jderstd/express";
110
- *
111
- * const route = (req: Request, res: Response): void => {
112
- * createResponse(res);
113
- * };
114
- * ```
115
- *
116
- * Example for creating a response with status, headers, and body:
117
- *
118
- * ```js
119
- * // JavaScript
120
- *
121
- * import { createResponse } from "@jderstd/express";
122
- *
123
- * const route = (req, res) => {
124
- * createResponse(res, {
125
- * status: 404,
126
- * headers: [
127
- * ["Content-Type", "text/plain"],
128
- * ],
129
- * body: "Not Found",
130
- * });
131
- * };
132
- * ```
133
- *
134
- * ```ts
135
- * // TypeScript
136
- *
137
- * import type { Request, Response } from "express";
138
- *
139
- * import { createResponse } from "@jderstd/express";
140
- *
141
- * const route = (req: Request, res: Response): void => {
142
- * createResponse(res, {
143
- * status: 404,
144
- * headers: [
145
- * ["Content-Type", "text/plain"],
146
- * ],
147
- * body: "Not Found",
148
- * });
149
- * };
150
- * ```
151
- */
152
- declare const createResponse: <B extends BodyInit>(res: Response, options?: CreateResponseOptions<B>) => Response<any, Record<string, any>>;
88
+ * Create a response.
89
+ *
90
+ * ### Examples
91
+ *
92
+ * Example for creating a basic response:
93
+ *
94
+ * ```js
95
+ * // JavaScript
96
+ *
97
+ * import { createResponse } from "@jderstd/express";
98
+ *
99
+ * const route = (req, res) => {
100
+ * createResponse(res);
101
+ * };
102
+ * ```
103
+ *
104
+ * ```ts
105
+ * // TypeScript
106
+ *
107
+ * import type { Request, Response } from "express";
108
+ *
109
+ * import { createResponse } from "@jderstd/express";
110
+ *
111
+ * const route = (req: Request, res: Response): void => {
112
+ * createResponse(res);
113
+ * };
114
+ * ```
115
+ *
116
+ * Example for creating a response with status, headers, and body:
117
+ *
118
+ * ```js
119
+ * // JavaScript
120
+ *
121
+ * import { createResponse } from "@jderstd/express";
122
+ *
123
+ * const route = (req, res) => {
124
+ * createResponse(res, {
125
+ * status: 404,
126
+ * headers: [
127
+ * ["Content-Type", "text/plain"],
128
+ * ],
129
+ * body: "Not Found",
130
+ * });
131
+ * };
132
+ * ```
133
+ *
134
+ * ```ts
135
+ * // TypeScript
136
+ *
137
+ * import type { Request, Response } from "express";
138
+ *
139
+ * import { createResponse } from "@jderstd/express";
140
+ *
141
+ * const route = (req: Request, res: Response): void => {
142
+ * createResponse(res, {
143
+ * status: 404,
144
+ * headers: [
145
+ * ["Content-Type", "text/plain"],
146
+ * ],
147
+ * body: "Not Found",
148
+ * });
149
+ * };
150
+ * ```
151
+ */
152
+ declare const createResponse: <B extends BodyInit>(res: Response, options?: CreateResponseOptions<B>) => Response<B, Record<string, any>>;
153
153
  /** Options of `createJsonResponse` function. */
154
- type CreateJsonResponseOptions<D$1 = unknown> = CreateJsonResponseStructOptions<D$1>;
154
+ type CreateJsonResponseOptions<D = unknown> = CreateJsonResponseStructOptions<D>;
155
155
  /**
156
- * Create a JSON response with context.
157
- *
158
- * ### Examples
159
- *
160
- * Example for creating a successful JSON response without data:
161
- *
162
- * ```js
163
- * // JavaScript
164
- *
165
- * import { createJsonResponse } from "@jderstd/express";
166
- *
167
- * const route = (req, res) => {
168
- * createJsonResponse(res);
169
- * };
170
- * ```
171
- *
172
- * ```ts
173
- * // TypeScript
174
- *
175
- * import type { Request, Response } from "express";
176
- *
177
- * import { createJsonResponse } from "@jderstd/express";
178
- *
179
- * const route = (req: Request, res: Response): void => {
180
- * createJsonResponse(res);
181
- * };
182
- * ```
183
- *
184
- * Example for creating a successful JSON response with data:
185
- *
186
- * ```js
187
- * // JavaScript
188
- *
189
- * import { createJsonResponse } from "@jderstd/express";
190
- *
191
- * const route = (req, res) => {
192
- * return createJsonResponse(res, {
193
- * data: "Hello, World!",
194
- * });
195
- * };
196
- * ```
197
- *
198
- * ```ts
199
- * // TypeScript
200
- *
201
- * import type { Request, Response } from "express";
202
- *
203
- * import { createJsonResponse } from "@jderstd/express";
204
- *
205
- * const route = (req: Request, res: Response): void => {
206
- * return createJsonResponse(res, {
207
- * data: "Hello, World!",
208
- * });
209
- * };
210
- * ```
211
- *
212
- * Example for creating a failed JSON response:
213
- *
214
- * ```js
215
- * // JavaScript
216
- *
217
- * import { createJsonResponse } from "@jderstd/express";
218
- *
219
- * const route = (req, res) => {
220
- * return createJsonResponse(res, {
221
- * errors: [
222
- * {
223
- * code: "server",
224
- * message: "Internal server error",
225
- * },
226
- * ],
227
- * });
228
- * };
229
- * ```
230
- *
231
- * ```ts
232
- * // TypeScript
233
- *
234
- * import type { Request, Response } from "express";
235
- *
236
- * import { createJsonResponse } from "@jderstd/express";
237
- *
238
- * const route = (req: Request, res: Response): void => {
239
- * return createJsonResponse(res, {
240
- * errors: [
241
- * {
242
- * code: "server",
243
- * message: "Internal server error",
244
- * },
245
- * ],
246
- * });
247
- * };
248
- * ```
249
- */
250
- declare const createJsonResponse: <D = unknown>(res: Response, options?: CreateJsonResponseOptions<D>) => Response<any, Record<string, any>>;
156
+ * Create a JSON response with context.
157
+ *
158
+ * ### Examples
159
+ *
160
+ * Example for creating a successful JSON response without data:
161
+ *
162
+ * ```js
163
+ * // JavaScript
164
+ *
165
+ * import { createJsonResponse } from "@jderstd/express";
166
+ *
167
+ * const route = (req, res) => {
168
+ * createJsonResponse(res);
169
+ * };
170
+ * ```
171
+ *
172
+ * ```ts
173
+ * // TypeScript
174
+ *
175
+ * import type { Request, Response } from "express";
176
+ *
177
+ * import { createJsonResponse } from "@jderstd/express";
178
+ *
179
+ * const route = (req: Request, res: Response): void => {
180
+ * createJsonResponse(res);
181
+ * };
182
+ * ```
183
+ *
184
+ * Example for creating a successful JSON response with data:
185
+ *
186
+ * ```js
187
+ * // JavaScript
188
+ *
189
+ * import { createJsonResponse } from "@jderstd/express";
190
+ *
191
+ * const route = (req, res) => {
192
+ * return createJsonResponse(res, {
193
+ * data: "Hello, World!",
194
+ * });
195
+ * };
196
+ * ```
197
+ *
198
+ * ```ts
199
+ * // TypeScript
200
+ *
201
+ * import type { Request, Response } from "express";
202
+ *
203
+ * import { createJsonResponse } from "@jderstd/express";
204
+ *
205
+ * const route = (req: Request, res: Response): void => {
206
+ * return createJsonResponse(res, {
207
+ * data: "Hello, World!",
208
+ * });
209
+ * };
210
+ * ```
211
+ *
212
+ * Example for creating a failed JSON response:
213
+ *
214
+ * ```js
215
+ * // JavaScript
216
+ *
217
+ * import { createJsonResponse } from "@jderstd/express";
218
+ *
219
+ * const route = (req, res) => {
220
+ * return createJsonResponse(res, {
221
+ * errors: [
222
+ * {
223
+ * code: "server",
224
+ * message: "Internal server error",
225
+ * },
226
+ * ],
227
+ * });
228
+ * };
229
+ * ```
230
+ *
231
+ * ```ts
232
+ * // TypeScript
233
+ *
234
+ * import type { Request, Response } from "express";
235
+ *
236
+ * import { createJsonResponse } from "@jderstd/express";
237
+ *
238
+ * const route = (req: Request, res: Response): void => {
239
+ * return createJsonResponse(res, {
240
+ * errors: [
241
+ * {
242
+ * code: "server",
243
+ * message: "Internal server error",
244
+ * },
245
+ * ],
246
+ * });
247
+ * };
248
+ * ```
249
+ */
250
+ declare const createJsonResponse: <D = unknown>(res: Response, options?: CreateJsonResponseOptions<D>) => Response<JsonResponse$1<D>, Record<string, any>>;
251
251
  /**
252
- * Response error code.
253
- */
252
+ * Response error code.
253
+ */
254
254
  declare enum ResponseErrorCode {
255
255
  /**
256
- * Internal server error.
257
- *
258
- * For `errorRequestHandler` function.
259
- */
256
+ * Internal server error.
257
+ *
258
+ * For `errorRequestHandler` function.
259
+ */
260
260
  Server = "server",
261
261
  }
262
262
  /**
263
- * Get response error message by code.
264
- */
263
+ * Get response error message by code.
264
+ */
265
265
  declare const getResponseErrorMessage: (code: ResponseErrorCode) => string;
266
- export { type CreateJsonResponseOptions, type CreateResponseOptions, type ErrorRequestHandlerOptions, type JsonResponse, type JsonResponseError, ResponseErrorCode, createJsonResponse, createResponse, errorRequestHandler, getResponseErrorMessage };
266
+ export { type CreateJsonResponseOptions, type CreateResponseOptions, type ErrorRequestHandlerOptions, type JsonResponse, type JsonResponseError, type JsonResponseErrorInput, ResponseErrorCode, createJsonResponse, createResponse, errorRequestHandler, getResponseErrorMessage };
267
267
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,28 +1,5 @@
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
- let __jderstd_core_response_json_struct = require("@jderstd/core/response/json/struct");
23
- __jderstd_core_response_json_struct = __toESM(__jderstd_core_response_json_struct);
24
- let __jderstd_core_response_common_struct = require("@jderstd/core/response/common/struct");
25
- __jderstd_core_response_common_struct = __toESM(__jderstd_core_response_common_struct);
1
+ let _jderstd_core_response_json_struct = require("@jderstd/core/response/json/struct");
2
+ let _jderstd_core_response_common_struct = require("@jderstd/core/response/common/struct");
26
3
 
27
4
  /**
28
5
  * Response error code.
@@ -141,7 +118,7 @@ const getResponseErrorMessage = (code) => {
141
118
  * ```
142
119
  */
143
120
  const createJsonResponse = (res, options) => {
144
- const { status, headers, json } = (0, __jderstd_core_response_json_struct.createJsonResponseStruct)(options);
121
+ const { status, headers, json } = (0, _jderstd_core_response_json_struct.createJsonResponseStruct)(options);
145
122
  return res.status(status).setHeaders(new Map(headers)).json(json);
146
123
  };
147
124
 
@@ -294,7 +271,7 @@ const errorRequestHandler = (options) => {
294
271
  * ```
295
272
  */
296
273
  const createResponse = (res, options) => {
297
- const { status, headers, body } = (0, __jderstd_core_response_common_struct.createResponseStruct)(options);
274
+ const { status, headers, body } = (0, _jderstd_core_response_common_struct.createResponseStruct)(options);
298
275
  return res.status(status).setHeaders(new Map(headers)).send(body);
299
276
  };
300
277
 
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/response/error/index.ts","../src/response/json/index.ts","../src/middlewares/error.ts","../src/response/common/index.ts"],"sourcesContent":["/**\n * Response error code.\n */\nenum ResponseErrorCode {\n /**\n * Internal server error.\n *\n * For `errorRequestHandler` function.\n */\n Server = \"server\",\n}\n\n/**\n * Get response error message by code.\n */\nconst getResponseErrorMessage = (code: ResponseErrorCode): string => {\n switch (code) {\n case ResponseErrorCode.Server: {\n return \"Internal server error\";\n }\n }\n};\n\nexport { ResponseErrorCode, getResponseErrorMessage };\n","import type { CreateJsonResponseStructOptions } from \"@jderstd/core/response/json/struct\";\nimport type { Response } from \"express\";\n\nimport { createJsonResponseStruct } from \"@jderstd/core/response/json/struct\";\n\n/** Options of `createJsonResponse` function. */\ntype CreateJsonResponseOptions<D = unknown> =\n CreateJsonResponseStructOptions<D>;\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 * ```js\n * // JavaScript\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * createJsonResponse(res);\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * createJsonResponse(res);\n * };\n * ```\n *\n * Example for creating a successful JSON response with data:\n *\n * ```js\n * // JavaScript\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * return createJsonResponse(res, {\n * data: \"Hello, World!\",\n * });\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * return createJsonResponse(res, {\n * data: \"Hello, World!\",\n * });\n * };\n * ```\n *\n * Example for creating a failed JSON response:\n *\n * ```js\n * // JavaScript\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * return createJsonResponse(res, {\n * errors: [\n * {\n * code: \"server\",\n * message: \"Internal server error\",\n * },\n * ],\n * });\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * return createJsonResponse(res, {\n * errors: [\n * {\n * code: \"server\",\n * message: \"Internal server error\",\n * },\n * ],\n * });\n * };\n * ```\n */\nconst createJsonResponse = <D = unknown>(\n res: Response,\n options?: CreateJsonResponseOptions<D>,\n) => {\n const { status, headers, json } = createJsonResponseStruct<D>(options);\n return res.status(status).setHeaders(new Map(headers)).json(json);\n};\n\nexport type { CreateJsonResponseOptions };\nexport { createJsonResponse };\n","import type { ErrorRequestHandler } from \"express\";\n\nimport { ResponseErrorCode } from \"#/response/error\";\nimport { createJsonResponse } from \"#/response/json\";\n\n/** Options for `errorRequestHandler` function. */\ntype ErrorRequestHandlerOptions = {\n /**\n * Whether show more information.\n * By default, it's `false`.\n */\n verbose?: boolean;\n};\n\n/**\n * Error request handler.\n *\n * Following response could be returned on error:\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 `errorRequestHandler` handler:\n *\n * ```js\n * // JavaScript\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app = express();\n *\n * app.use(errorRequestHandler());\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Express } from \"express\";\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app: Express = express();\n *\n * app.use(errorRequestHandler());\n * ```\n *\n * Show more information with `verbose` option:\n *\n * ```js\n * // JavaScript\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app = express();\n *\n * app.use(errorRequestHandler({ verbose: true }));\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Express } from \"express\";\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app: Express = express();\n *\n * app.use(errorRequestHandler({ verbose: true }));\n * ```\n */\nconst errorRequestHandler = (\n options?: ErrorRequestHandlerOptions,\n): ErrorRequestHandler => {\n return (err, _req, res, _next) => {\n return createJsonResponse(res, {\n status: 500,\n errors: [\n {\n code: ResponseErrorCode.Server,\n ...(options?.verbose && {\n message:\n err instanceof Error ? err.message : String(err),\n }),\n },\n ],\n });\n };\n};\n\nexport type { ErrorRequestHandlerOptions };\nexport { errorRequestHandler };\n","import type { CreateResponseStructOptions } from \"@jderstd/core/response/common/struct\";\nimport type { Response } from \"express\";\n\nimport { createResponseStruct } from \"@jderstd/core/response/common/struct\";\n\n/** Options of `createResponse` function. */\ntype CreateResponseOptions<B extends BodyInit = BodyInit> =\n CreateResponseStructOptions<B>;\n\n/**\n * Create a response.\n *\n * ### Examples\n *\n * Example for creating a basic response:\n *\n * ```js\n * // JavaScript\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * createResponse(res);\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * createResponse(res);\n * };\n * ```\n *\n * Example for creating a response with status, headers, and body:\n *\n * ```js\n * // JavaScript\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * createResponse(res, {\n * status: 404,\n * headers: [\n * [\"Content-Type\", \"text/plain\"],\n * ],\n * body: \"Not Found\",\n * });\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * createResponse(res, {\n * status: 404,\n * headers: [\n * [\"Content-Type\", \"text/plain\"],\n * ],\n * body: \"Not Found\",\n * });\n * };\n * ```\n */\nconst createResponse = <B extends BodyInit>(\n res: Response,\n options?: CreateResponseOptions<B>,\n) => {\n const { status, headers, body } = createResponseStruct(options);\n return res.status(status).setHeaders(new Map(headers)).send(body);\n};\n\nexport type { CreateResponseOptions };\nexport { createResponse };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,IAAK,kEAAL;;;;;;AAMI;;EANC;;;;AAYL,MAAM,2BAA2B,SAAoC;AACjE,SAAQ,MAAR;EACI,KAAK,kBAAkB,OACnB,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACsFnB,MAAM,sBACF,KACA,YACC;CACD,MAAM,EAAE,QAAQ,SAAS,2EAAqC,QAAQ;AACtE,QAAO,IAAI,OAAO,OAAO,CAAC,WAAW,IAAI,IAAI,QAAQ,CAAC,CAAC,KAAK,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACxBrE,MAAM,uBACF,YACsB;AACtB,SAAQ,KAAK,MAAM,KAAK,UAAU;AAC9B,SAAO,mBAAmB,KAAK;GAC3B,QAAQ;GACR,QAAQ,CACJ;IACI,MAAM,kBAAkB;IACxB,GAAI,SAAS,WAAW,EACpB,SACI,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,EACvD;IACJ,CACJ;GACJ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC1BV,MAAM,kBACF,KACA,YACC;CACD,MAAM,EAAE,QAAQ,SAAS,yEAA8B,QAAQ;AAC/D,QAAO,IAAI,OAAO,OAAO,CAAC,WAAW,IAAI,IAAI,QAAQ,CAAC,CAAC,KAAK,KAAK"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/response/error/index.ts","../src/response/json/index.ts","../src/middlewares/error.ts","../src/response/common/index.ts"],"sourcesContent":["/**\n * Response error code.\n */\nenum ResponseErrorCode {\n /**\n * Internal server error.\n *\n * For `errorRequestHandler` function.\n */\n Server = \"server\",\n}\n\n/**\n * Get response error message by code.\n */\nconst getResponseErrorMessage = (code: ResponseErrorCode): string => {\n switch (code) {\n case ResponseErrorCode.Server: {\n return \"Internal server error\";\n }\n }\n};\n\nexport { ResponseErrorCode, getResponseErrorMessage };\n","import type { JsonResponse } from \"@jderstd/core\";\nimport type { CreateJsonResponseStructOptions } from \"@jderstd/core/response/json/struct\";\nimport type { Response } from \"express\";\n\nimport { createJsonResponseStruct } from \"@jderstd/core/response/json/struct\";\n\n/** Options of `createJsonResponse` function. */\ntype CreateJsonResponseOptions<D = unknown> =\n CreateJsonResponseStructOptions<D>;\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 * ```js\n * // JavaScript\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * createJsonResponse(res);\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * createJsonResponse(res);\n * };\n * ```\n *\n * Example for creating a successful JSON response with data:\n *\n * ```js\n * // JavaScript\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * return createJsonResponse(res, {\n * data: \"Hello, World!\",\n * });\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * return createJsonResponse(res, {\n * data: \"Hello, World!\",\n * });\n * };\n * ```\n *\n * Example for creating a failed JSON response:\n *\n * ```js\n * // JavaScript\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * return createJsonResponse(res, {\n * errors: [\n * {\n * code: \"server\",\n * message: \"Internal server error\",\n * },\n * ],\n * });\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * return createJsonResponse(res, {\n * errors: [\n * {\n * code: \"server\",\n * message: \"Internal server error\",\n * },\n * ],\n * });\n * };\n * ```\n */\nconst createJsonResponse = <D = unknown>(\n res: Response,\n options?: CreateJsonResponseOptions<D>,\n): Response<JsonResponse<D>, Record<string, any>> => {\n const { status, headers, json } = createJsonResponseStruct<D>(options);\n return res.status(status).setHeaders(new Map(headers)).json(json);\n};\n\nexport type { CreateJsonResponseOptions };\nexport { createJsonResponse };\n","import type { ErrorRequestHandler } from \"express\";\n\nimport { ResponseErrorCode } from \"#/response/error\";\nimport { createJsonResponse } from \"#/response/json\";\n\n/** Options for `errorRequestHandler` function. */\ntype ErrorRequestHandlerOptions = {\n /**\n * Whether show more information.\n * By default, it's `false`.\n */\n verbose?: boolean;\n};\n\n/**\n * Error request handler.\n *\n * Following response could be returned on error:\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 `errorRequestHandler` handler:\n *\n * ```js\n * // JavaScript\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app = express();\n *\n * app.use(errorRequestHandler());\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Express } from \"express\";\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app: Express = express();\n *\n * app.use(errorRequestHandler());\n * ```\n *\n * Show more information with `verbose` option:\n *\n * ```js\n * // JavaScript\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app = express();\n *\n * app.use(errorRequestHandler({ verbose: true }));\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Express } from \"express\";\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app: Express = express();\n *\n * app.use(errorRequestHandler({ verbose: true }));\n * ```\n */\nconst errorRequestHandler = (\n options?: ErrorRequestHandlerOptions,\n): ErrorRequestHandler => {\n return (err, _req, res, _next) => {\n return createJsonResponse(res, {\n status: 500,\n errors: [\n {\n code: ResponseErrorCode.Server,\n ...(options?.verbose && {\n message:\n err instanceof Error ? err.message : String(err),\n }),\n },\n ],\n });\n };\n};\n\nexport type { ErrorRequestHandlerOptions };\nexport { errorRequestHandler };\n","import type { CreateResponseStructOptions } from \"@jderstd/core/response/common/struct\";\nimport type { Response } from \"express\";\n\nimport { createResponseStruct } from \"@jderstd/core/response/common/struct\";\n\n/** Options of `createResponse` function. */\ntype CreateResponseOptions<B extends BodyInit = BodyInit> =\n CreateResponseStructOptions<B>;\n\n/**\n * Create a response.\n *\n * ### Examples\n *\n * Example for creating a basic response:\n *\n * ```js\n * // JavaScript\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * createResponse(res);\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * createResponse(res);\n * };\n * ```\n *\n * Example for creating a response with status, headers, and body:\n *\n * ```js\n * // JavaScript\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * createResponse(res, {\n * status: 404,\n * headers: [\n * [\"Content-Type\", \"text/plain\"],\n * ],\n * body: \"Not Found\",\n * });\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * createResponse(res, {\n * status: 404,\n * headers: [\n * [\"Content-Type\", \"text/plain\"],\n * ],\n * body: \"Not Found\",\n * });\n * };\n * ```\n */\nconst createResponse = <B extends BodyInit>(\n res: Response,\n options?: CreateResponseOptions<B>,\n): Response<B, Record<string, any>> => {\n const { status, headers, body } = createResponseStruct(options);\n return res.status(status).setHeaders(new Map(headers)).send(body);\n};\n\nexport type { CreateResponseOptions };\nexport { createResponse };\n"],"mappings":";;;;;;AAGA,IAAK,kEAAL;;;;;;AAMI;;EANC;;;;AAYL,MAAM,2BAA2B,SAAoC;AACjE,SAAQ,MAAR;EACI,KAAK,kBAAkB,OACnB,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACuFnB,MAAM,sBACF,KACA,YACiD;CACjD,MAAM,EAAE,QAAQ,SAAS,0EAAqC,QAAQ;AACtE,QAAO,IAAI,OAAO,OAAO,CAAC,WAAW,IAAI,IAAI,QAAQ,CAAC,CAAC,KAAK,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACzBrE,MAAM,uBACF,YACsB;AACtB,SAAQ,KAAK,MAAM,KAAK,UAAU;AAC9B,SAAO,mBAAmB,KAAK;GAC3B,QAAQ;GACR,QAAQ,CACJ;IACI,MAAM,kBAAkB;IACxB,GAAI,SAAS,WAAW,EACpB,SACI,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,EACvD;IACJ,CACJ;GACJ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC1BV,MAAM,kBACF,KACA,YACmC;CACnC,MAAM,EAAE,QAAQ,SAAS,wEAA8B,QAAQ;AAC/D,QAAO,IAAI,OAAO,OAAO,CAAC,WAAW,IAAI,IAAI,QAAQ,CAAC,CAAC,KAAK,KAAK"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/response/error/index.ts","../src/response/json/index.ts","../src/middlewares/error.ts","../src/response/common/index.ts"],"sourcesContent":["/**\n * Response error code.\n */\nenum ResponseErrorCode {\n /**\n * Internal server error.\n *\n * For `errorRequestHandler` function.\n */\n Server = \"server\",\n}\n\n/**\n * Get response error message by code.\n */\nconst getResponseErrorMessage = (code: ResponseErrorCode): string => {\n switch (code) {\n case ResponseErrorCode.Server: {\n return \"Internal server error\";\n }\n }\n};\n\nexport { ResponseErrorCode, getResponseErrorMessage };\n","import type { CreateJsonResponseStructOptions } from \"@jderstd/core/response/json/struct\";\nimport type { Response } from \"express\";\n\nimport { createJsonResponseStruct } from \"@jderstd/core/response/json/struct\";\n\n/** Options of `createJsonResponse` function. */\ntype CreateJsonResponseOptions<D = unknown> =\n CreateJsonResponseStructOptions<D>;\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 * ```js\n * // JavaScript\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * createJsonResponse(res);\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * createJsonResponse(res);\n * };\n * ```\n *\n * Example for creating a successful JSON response with data:\n *\n * ```js\n * // JavaScript\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * return createJsonResponse(res, {\n * data: \"Hello, World!\",\n * });\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * return createJsonResponse(res, {\n * data: \"Hello, World!\",\n * });\n * };\n * ```\n *\n * Example for creating a failed JSON response:\n *\n * ```js\n * // JavaScript\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * return createJsonResponse(res, {\n * errors: [\n * {\n * code: \"server\",\n * message: \"Internal server error\",\n * },\n * ],\n * });\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * return createJsonResponse(res, {\n * errors: [\n * {\n * code: \"server\",\n * message: \"Internal server error\",\n * },\n * ],\n * });\n * };\n * ```\n */\nconst createJsonResponse = <D = unknown>(\n res: Response,\n options?: CreateJsonResponseOptions<D>,\n) => {\n const { status, headers, json } = createJsonResponseStruct<D>(options);\n return res.status(status).setHeaders(new Map(headers)).json(json);\n};\n\nexport type { CreateJsonResponseOptions };\nexport { createJsonResponse };\n","import type { ErrorRequestHandler } from \"express\";\n\nimport { ResponseErrorCode } from \"#/response/error\";\nimport { createJsonResponse } from \"#/response/json\";\n\n/** Options for `errorRequestHandler` function. */\ntype ErrorRequestHandlerOptions = {\n /**\n * Whether show more information.\n * By default, it's `false`.\n */\n verbose?: boolean;\n};\n\n/**\n * Error request handler.\n *\n * Following response could be returned on error:\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 `errorRequestHandler` handler:\n *\n * ```js\n * // JavaScript\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app = express();\n *\n * app.use(errorRequestHandler());\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Express } from \"express\";\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app: Express = express();\n *\n * app.use(errorRequestHandler());\n * ```\n *\n * Show more information with `verbose` option:\n *\n * ```js\n * // JavaScript\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app = express();\n *\n * app.use(errorRequestHandler({ verbose: true }));\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Express } from \"express\";\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app: Express = express();\n *\n * app.use(errorRequestHandler({ verbose: true }));\n * ```\n */\nconst errorRequestHandler = (\n options?: ErrorRequestHandlerOptions,\n): ErrorRequestHandler => {\n return (err, _req, res, _next) => {\n return createJsonResponse(res, {\n status: 500,\n errors: [\n {\n code: ResponseErrorCode.Server,\n ...(options?.verbose && {\n message:\n err instanceof Error ? err.message : String(err),\n }),\n },\n ],\n });\n };\n};\n\nexport type { ErrorRequestHandlerOptions };\nexport { errorRequestHandler };\n","import type { CreateResponseStructOptions } from \"@jderstd/core/response/common/struct\";\nimport type { Response } from \"express\";\n\nimport { createResponseStruct } from \"@jderstd/core/response/common/struct\";\n\n/** Options of `createResponse` function. */\ntype CreateResponseOptions<B extends BodyInit = BodyInit> =\n CreateResponseStructOptions<B>;\n\n/**\n * Create a response.\n *\n * ### Examples\n *\n * Example for creating a basic response:\n *\n * ```js\n * // JavaScript\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * createResponse(res);\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * createResponse(res);\n * };\n * ```\n *\n * Example for creating a response with status, headers, and body:\n *\n * ```js\n * // JavaScript\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * createResponse(res, {\n * status: 404,\n * headers: [\n * [\"Content-Type\", \"text/plain\"],\n * ],\n * body: \"Not Found\",\n * });\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * createResponse(res, {\n * status: 404,\n * headers: [\n * [\"Content-Type\", \"text/plain\"],\n * ],\n * body: \"Not Found\",\n * });\n * };\n * ```\n */\nconst createResponse = <B extends BodyInit>(\n res: Response,\n options?: CreateResponseOptions<B>,\n) => {\n const { status, headers, body } = createResponseStruct(options);\n return res.status(status).setHeaders(new Map(headers)).send(body);\n};\n\nexport type { CreateResponseOptions };\nexport { createResponse };\n"],"mappings":";;;;;;AAGA,IAAK,kEAAL;;;;;;AAMI;;EANC;;;;AAYL,MAAM,2BAA2B,SAAoC;AACjE,SAAQ,MAAR;EACI,KAAK,kBAAkB,OACnB,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACsFnB,MAAM,sBACF,KACA,YACC;CACD,MAAM,EAAE,QAAQ,SAAS,SAAS,yBAA4B,QAAQ;AACtE,QAAO,IAAI,OAAO,OAAO,CAAC,WAAW,IAAI,IAAI,QAAQ,CAAC,CAAC,KAAK,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACxBrE,MAAM,uBACF,YACsB;AACtB,SAAQ,KAAK,MAAM,KAAK,UAAU;AAC9B,SAAO,mBAAmB,KAAK;GAC3B,QAAQ;GACR,QAAQ,CACJ;IACI,MAAM,kBAAkB;IACxB,GAAI,SAAS,WAAW,EACpB,SACI,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,EACvD;IACJ,CACJ;GACJ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC1BV,MAAM,kBACF,KACA,YACC;CACD,MAAM,EAAE,QAAQ,SAAS,SAAS,qBAAqB,QAAQ;AAC/D,QAAO,IAAI,OAAO,OAAO,CAAC,WAAW,IAAI,IAAI,QAAQ,CAAC,CAAC,KAAK,KAAK"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/response/error/index.ts","../src/response/json/index.ts","../src/middlewares/error.ts","../src/response/common/index.ts"],"sourcesContent":["/**\n * Response error code.\n */\nenum ResponseErrorCode {\n /**\n * Internal server error.\n *\n * For `errorRequestHandler` function.\n */\n Server = \"server\",\n}\n\n/**\n * Get response error message by code.\n */\nconst getResponseErrorMessage = (code: ResponseErrorCode): string => {\n switch (code) {\n case ResponseErrorCode.Server: {\n return \"Internal server error\";\n }\n }\n};\n\nexport { ResponseErrorCode, getResponseErrorMessage };\n","import type { JsonResponse } from \"@jderstd/core\";\nimport type { CreateJsonResponseStructOptions } from \"@jderstd/core/response/json/struct\";\nimport type { Response } from \"express\";\n\nimport { createJsonResponseStruct } from \"@jderstd/core/response/json/struct\";\n\n/** Options of `createJsonResponse` function. */\ntype CreateJsonResponseOptions<D = unknown> =\n CreateJsonResponseStructOptions<D>;\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 * ```js\n * // JavaScript\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * createJsonResponse(res);\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * createJsonResponse(res);\n * };\n * ```\n *\n * Example for creating a successful JSON response with data:\n *\n * ```js\n * // JavaScript\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * return createJsonResponse(res, {\n * data: \"Hello, World!\",\n * });\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * return createJsonResponse(res, {\n * data: \"Hello, World!\",\n * });\n * };\n * ```\n *\n * Example for creating a failed JSON response:\n *\n * ```js\n * // JavaScript\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * return createJsonResponse(res, {\n * errors: [\n * {\n * code: \"server\",\n * message: \"Internal server error\",\n * },\n * ],\n * });\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createJsonResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * return createJsonResponse(res, {\n * errors: [\n * {\n * code: \"server\",\n * message: \"Internal server error\",\n * },\n * ],\n * });\n * };\n * ```\n */\nconst createJsonResponse = <D = unknown>(\n res: Response,\n options?: CreateJsonResponseOptions<D>,\n): Response<JsonResponse<D>, Record<string, any>> => {\n const { status, headers, json } = createJsonResponseStruct<D>(options);\n return res.status(status).setHeaders(new Map(headers)).json(json);\n};\n\nexport type { CreateJsonResponseOptions };\nexport { createJsonResponse };\n","import type { ErrorRequestHandler } from \"express\";\n\nimport { ResponseErrorCode } from \"#/response/error\";\nimport { createJsonResponse } from \"#/response/json\";\n\n/** Options for `errorRequestHandler` function. */\ntype ErrorRequestHandlerOptions = {\n /**\n * Whether show more information.\n * By default, it's `false`.\n */\n verbose?: boolean;\n};\n\n/**\n * Error request handler.\n *\n * Following response could be returned on error:\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 `errorRequestHandler` handler:\n *\n * ```js\n * // JavaScript\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app = express();\n *\n * app.use(errorRequestHandler());\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Express } from \"express\";\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app: Express = express();\n *\n * app.use(errorRequestHandler());\n * ```\n *\n * Show more information with `verbose` option:\n *\n * ```js\n * // JavaScript\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app = express();\n *\n * app.use(errorRequestHandler({ verbose: true }));\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Express } from \"express\";\n *\n * import express from \"express\";\n * import { errorRequestHandler } from \"@jderstd/express\";\n *\n * const app: Express = express();\n *\n * app.use(errorRequestHandler({ verbose: true }));\n * ```\n */\nconst errorRequestHandler = (\n options?: ErrorRequestHandlerOptions,\n): ErrorRequestHandler => {\n return (err, _req, res, _next) => {\n return createJsonResponse(res, {\n status: 500,\n errors: [\n {\n code: ResponseErrorCode.Server,\n ...(options?.verbose && {\n message:\n err instanceof Error ? err.message : String(err),\n }),\n },\n ],\n });\n };\n};\n\nexport type { ErrorRequestHandlerOptions };\nexport { errorRequestHandler };\n","import type { CreateResponseStructOptions } from \"@jderstd/core/response/common/struct\";\nimport type { Response } from \"express\";\n\nimport { createResponseStruct } from \"@jderstd/core/response/common/struct\";\n\n/** Options of `createResponse` function. */\ntype CreateResponseOptions<B extends BodyInit = BodyInit> =\n CreateResponseStructOptions<B>;\n\n/**\n * Create a response.\n *\n * ### Examples\n *\n * Example for creating a basic response:\n *\n * ```js\n * // JavaScript\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * createResponse(res);\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * createResponse(res);\n * };\n * ```\n *\n * Example for creating a response with status, headers, and body:\n *\n * ```js\n * // JavaScript\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req, res) => {\n * createResponse(res, {\n * status: 404,\n * headers: [\n * [\"Content-Type\", \"text/plain\"],\n * ],\n * body: \"Not Found\",\n * });\n * };\n * ```\n *\n * ```ts\n * // TypeScript\n *\n * import type { Request, Response } from \"express\";\n *\n * import { createResponse } from \"@jderstd/express\";\n *\n * const route = (req: Request, res: Response): void => {\n * createResponse(res, {\n * status: 404,\n * headers: [\n * [\"Content-Type\", \"text/plain\"],\n * ],\n * body: \"Not Found\",\n * });\n * };\n * ```\n */\nconst createResponse = <B extends BodyInit>(\n res: Response,\n options?: CreateResponseOptions<B>,\n): Response<B, Record<string, any>> => {\n const { status, headers, body } = createResponseStruct(options);\n return res.status(status).setHeaders(new Map(headers)).send(body);\n};\n\nexport type { CreateResponseOptions };\nexport { createResponse };\n"],"mappings":";;;;;;AAGA,IAAK,kEAAL;;;;;;AAMI;;EANC;;;;AAYL,MAAM,2BAA2B,SAAoC;AACjE,SAAQ,MAAR;EACI,KAAK,kBAAkB,OACnB,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACuFnB,MAAM,sBACF,KACA,YACiD;CACjD,MAAM,EAAE,QAAQ,SAAS,SAAS,yBAA4B,QAAQ;AACtE,QAAO,IAAI,OAAO,OAAO,CAAC,WAAW,IAAI,IAAI,QAAQ,CAAC,CAAC,KAAK,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACzBrE,MAAM,uBACF,YACsB;AACtB,SAAQ,KAAK,MAAM,KAAK,UAAU;AAC9B,SAAO,mBAAmB,KAAK;GAC3B,QAAQ;GACR,QAAQ,CACJ;IACI,MAAM,kBAAkB;IACxB,GAAI,SAAS,WAAW,EACpB,SACI,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,EACvD;IACJ,CACJ;GACJ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC1BV,MAAM,kBACF,KACA,YACmC;CACnC,MAAM,EAAE,QAAQ,SAAS,SAAS,qBAAqB,QAAQ;AAC/D,QAAO,IAAI,OAAO,OAAO,CAAC,WAAW,IAAI,IAAI,QAAQ,CAAC,CAAC,KAAK,KAAK"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jderstd/express",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "A response builder for Express",
5
5
  "keywords": [
6
6
  "jder",
@@ -38,7 +38,7 @@
38
38
  "dist"
39
39
  ],
40
40
  "dependencies": {
41
- "@jderstd/core": "~0.4.0"
41
+ "@jderstd/core": "~0.5.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@types/express": "~5.0.0",