@jderstd/express 0.1.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 +31 -0
- package/dist/index.d.ts +267 -0
- package/dist/index.js +306 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +279 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present, Alpheus
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# JDER Express
|
|
2
|
+
|
|
3
|
+
A response builder for Express.
|
|
4
|
+
|
|
5
|
+
This package includes different response builders based on the JSON response structure specified in [JSON Data Errors Response (JDER)](https://github.com/jder-std/spec). With the builders, various kinds of responses can be created easily instead of sending plain text responses.
|
|
6
|
+
|
|
7
|
+
## Quick Start
|
|
8
|
+
|
|
9
|
+
To create a JSON response, use the following code:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import type { Request, Response } from "express";
|
|
13
|
+
|
|
14
|
+
import { createJsonResponse } from "@jderstd/express";
|
|
15
|
+
|
|
16
|
+
const route = (req: Request, res: Response): Response => {
|
|
17
|
+
return createJsonResponse(res);
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
And the response will be shown as below:
|
|
22
|
+
|
|
23
|
+
```json
|
|
24
|
+
{
|
|
25
|
+
"success": true
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## License
|
|
30
|
+
|
|
31
|
+
This project is licensed under the terms of the MIT license.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { JsonResponse, JsonResponseError } from "@jderstd/core";
|
|
2
|
+
import { ErrorRequestHandler, Response } from "express";
|
|
3
|
+
import { CreateResponseStructOptions } from "@jderstd/core/response/common/struct";
|
|
4
|
+
import { CreateJsonResponseStructOptions } from "@jderstd/core/response/json/struct";
|
|
5
|
+
/** Options for `errorRequestHandler` function. */
|
|
6
|
+
type ErrorRequestHandlerOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* Whether show more information.
|
|
9
|
+
* By default, it's `false`.
|
|
10
|
+
*/
|
|
11
|
+
verbose?: boolean;
|
|
12
|
+
};
|
|
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
|
+
*/
|
|
84
|
+
declare const errorRequestHandler: (options?: ErrorRequestHandlerOptions) => ErrorRequestHandler;
|
|
85
|
+
/** Options of `createResponse` function. */
|
|
86
|
+
type CreateResponseOptions<B$1 extends BodyInit = BodyInit> = CreateResponseStructOptions<B$1>;
|
|
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>>;
|
|
153
|
+
/** Options of `createJsonResponse` function. */
|
|
154
|
+
type CreateJsonResponseOptions<D$1 = unknown> = CreateJsonResponseStructOptions<D$1>;
|
|
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>>;
|
|
251
|
+
/**
|
|
252
|
+
* Response error code.
|
|
253
|
+
*/
|
|
254
|
+
declare enum ResponseErrorCode {
|
|
255
|
+
/**
|
|
256
|
+
* Internal server error.
|
|
257
|
+
*
|
|
258
|
+
* For `errorRequestHandler` function.
|
|
259
|
+
*/
|
|
260
|
+
Server = "server",
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Get response error message by code.
|
|
264
|
+
*/
|
|
265
|
+
declare const getResponseErrorMessage: (code: ResponseErrorCode) => string;
|
|
266
|
+
export { type CreateJsonResponseOptions, type CreateResponseOptions, type ErrorRequestHandlerOptions, type JsonResponse, type JsonResponseError, ResponseErrorCode, createJsonResponse, createResponse, errorRequestHandler, getResponseErrorMessage };
|
|
267
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
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);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Response error code.
|
|
29
|
+
*/
|
|
30
|
+
var ResponseErrorCode = /* @__PURE__ */ function(ResponseErrorCode$1) {
|
|
31
|
+
/**
|
|
32
|
+
* Internal server error.
|
|
33
|
+
*
|
|
34
|
+
* For `errorRequestHandler` function.
|
|
35
|
+
*/
|
|
36
|
+
ResponseErrorCode$1["Server"] = "server";
|
|
37
|
+
return ResponseErrorCode$1;
|
|
38
|
+
}(ResponseErrorCode || {});
|
|
39
|
+
/**
|
|
40
|
+
* Get response error message by code.
|
|
41
|
+
*/
|
|
42
|
+
const getResponseErrorMessage = (code) => {
|
|
43
|
+
switch (code) {
|
|
44
|
+
case ResponseErrorCode.Server: return "Internal server error";
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create a JSON response with context.
|
|
50
|
+
*
|
|
51
|
+
* ### Examples
|
|
52
|
+
*
|
|
53
|
+
* Example for creating a successful JSON response without data:
|
|
54
|
+
*
|
|
55
|
+
* ```js
|
|
56
|
+
* // JavaScript
|
|
57
|
+
*
|
|
58
|
+
* import { createJsonResponse } from "@jderstd/express";
|
|
59
|
+
*
|
|
60
|
+
* const route = (req, res) => {
|
|
61
|
+
* createJsonResponse(res);
|
|
62
|
+
* };
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* ```ts
|
|
66
|
+
* // TypeScript
|
|
67
|
+
*
|
|
68
|
+
* import type { Request, Response } from "express";
|
|
69
|
+
*
|
|
70
|
+
* import { createJsonResponse } from "@jderstd/express";
|
|
71
|
+
*
|
|
72
|
+
* const route = (req: Request, res: Response): void => {
|
|
73
|
+
* createJsonResponse(res);
|
|
74
|
+
* };
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* Example for creating a successful JSON response with data:
|
|
78
|
+
*
|
|
79
|
+
* ```js
|
|
80
|
+
* // JavaScript
|
|
81
|
+
*
|
|
82
|
+
* import { createJsonResponse } from "@jderstd/express";
|
|
83
|
+
*
|
|
84
|
+
* const route = (req, res) => {
|
|
85
|
+
* return createJsonResponse(res, {
|
|
86
|
+
* data: "Hello, World!",
|
|
87
|
+
* });
|
|
88
|
+
* };
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* // TypeScript
|
|
93
|
+
*
|
|
94
|
+
* import type { Request, Response } from "express";
|
|
95
|
+
*
|
|
96
|
+
* import { createJsonResponse } from "@jderstd/express";
|
|
97
|
+
*
|
|
98
|
+
* const route = (req: Request, res: Response): void => {
|
|
99
|
+
* return createJsonResponse(res, {
|
|
100
|
+
* data: "Hello, World!",
|
|
101
|
+
* });
|
|
102
|
+
* };
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* Example for creating a failed JSON response:
|
|
106
|
+
*
|
|
107
|
+
* ```js
|
|
108
|
+
* // JavaScript
|
|
109
|
+
*
|
|
110
|
+
* import { createJsonResponse } from "@jderstd/express";
|
|
111
|
+
*
|
|
112
|
+
* const route = (req, res) => {
|
|
113
|
+
* return createJsonResponse(res, {
|
|
114
|
+
* errors: [
|
|
115
|
+
* {
|
|
116
|
+
* code: "server",
|
|
117
|
+
* message: "Internal server error",
|
|
118
|
+
* },
|
|
119
|
+
* ],
|
|
120
|
+
* });
|
|
121
|
+
* };
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* ```ts
|
|
125
|
+
* // TypeScript
|
|
126
|
+
*
|
|
127
|
+
* import type { Request, Response } from "express";
|
|
128
|
+
*
|
|
129
|
+
* import { createJsonResponse } from "@jderstd/express";
|
|
130
|
+
*
|
|
131
|
+
* const route = (req: Request, res: Response): void => {
|
|
132
|
+
* return createJsonResponse(res, {
|
|
133
|
+
* errors: [
|
|
134
|
+
* {
|
|
135
|
+
* code: "server",
|
|
136
|
+
* message: "Internal server error",
|
|
137
|
+
* },
|
|
138
|
+
* ],
|
|
139
|
+
* });
|
|
140
|
+
* };
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
const createJsonResponse = (res, options) => {
|
|
144
|
+
const { status, headers, json } = (0, __jderstd_core_response_json_struct.createJsonResponseStruct)(options);
|
|
145
|
+
return res.status(status).setHeaders(new Map(headers)).json(json);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Error request handler.
|
|
150
|
+
*
|
|
151
|
+
* Following response could be returned on error:
|
|
152
|
+
*
|
|
153
|
+
* ```jsonc
|
|
154
|
+
* // Status: 500
|
|
155
|
+
* {
|
|
156
|
+
* "success": false,
|
|
157
|
+
* "errors": [
|
|
158
|
+
* {
|
|
159
|
+
* "code": "server"
|
|
160
|
+
* }
|
|
161
|
+
* ]
|
|
162
|
+
* }
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* ### Examples
|
|
166
|
+
*
|
|
167
|
+
* Basic example of using `errorRequestHandler` handler:
|
|
168
|
+
*
|
|
169
|
+
* ```js
|
|
170
|
+
* // JavaScript
|
|
171
|
+
*
|
|
172
|
+
* import express from "express";
|
|
173
|
+
* import { errorRequestHandler } from "@jderstd/express";
|
|
174
|
+
*
|
|
175
|
+
* const app = express();
|
|
176
|
+
*
|
|
177
|
+
* app.use(errorRequestHandler());
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* ```ts
|
|
181
|
+
* // TypeScript
|
|
182
|
+
*
|
|
183
|
+
* import type { Express } from "express";
|
|
184
|
+
*
|
|
185
|
+
* import express from "express";
|
|
186
|
+
* import { errorRequestHandler } from "@jderstd/express";
|
|
187
|
+
*
|
|
188
|
+
* const app: Express = express();
|
|
189
|
+
*
|
|
190
|
+
* app.use(errorRequestHandler());
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* Show more information with `verbose` option:
|
|
194
|
+
*
|
|
195
|
+
* ```js
|
|
196
|
+
* // JavaScript
|
|
197
|
+
*
|
|
198
|
+
* import express from "express";
|
|
199
|
+
* import { errorRequestHandler } from "@jderstd/express";
|
|
200
|
+
*
|
|
201
|
+
* const app = express();
|
|
202
|
+
*
|
|
203
|
+
* app.use(errorRequestHandler({ verbose: true }));
|
|
204
|
+
* ```
|
|
205
|
+
*
|
|
206
|
+
* ```ts
|
|
207
|
+
* // TypeScript
|
|
208
|
+
*
|
|
209
|
+
* import type { Express } from "express";
|
|
210
|
+
*
|
|
211
|
+
* import express from "express";
|
|
212
|
+
* import { errorRequestHandler } from "@jderstd/express";
|
|
213
|
+
*
|
|
214
|
+
* const app: Express = express();
|
|
215
|
+
*
|
|
216
|
+
* app.use(errorRequestHandler({ verbose: true }));
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
const errorRequestHandler = (options) => {
|
|
220
|
+
return (err, _req, res, _next) => {
|
|
221
|
+
return createJsonResponse(res, {
|
|
222
|
+
status: 500,
|
|
223
|
+
errors: [{
|
|
224
|
+
code: ResponseErrorCode.Server,
|
|
225
|
+
...options?.verbose && { message: err instanceof Error ? err.message : String(err) }
|
|
226
|
+
}]
|
|
227
|
+
});
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Create a response.
|
|
233
|
+
*
|
|
234
|
+
* ### Examples
|
|
235
|
+
*
|
|
236
|
+
* Example for creating a basic response:
|
|
237
|
+
*
|
|
238
|
+
* ```js
|
|
239
|
+
* // JavaScript
|
|
240
|
+
*
|
|
241
|
+
* import { createResponse } from "@jderstd/express";
|
|
242
|
+
*
|
|
243
|
+
* const route = (req, res) => {
|
|
244
|
+
* createResponse(res);
|
|
245
|
+
* };
|
|
246
|
+
* ```
|
|
247
|
+
*
|
|
248
|
+
* ```ts
|
|
249
|
+
* // TypeScript
|
|
250
|
+
*
|
|
251
|
+
* import type { Request, Response } from "express";
|
|
252
|
+
*
|
|
253
|
+
* import { createResponse } from "@jderstd/express";
|
|
254
|
+
*
|
|
255
|
+
* const route = (req: Request, res: Response): void => {
|
|
256
|
+
* createResponse(res);
|
|
257
|
+
* };
|
|
258
|
+
* ```
|
|
259
|
+
*
|
|
260
|
+
* Example for creating a response with status, headers, and body:
|
|
261
|
+
*
|
|
262
|
+
* ```js
|
|
263
|
+
* // JavaScript
|
|
264
|
+
*
|
|
265
|
+
* import { createResponse } from "@jderstd/express";
|
|
266
|
+
*
|
|
267
|
+
* const route = (req, res) => {
|
|
268
|
+
* createResponse(res, {
|
|
269
|
+
* status: 404,
|
|
270
|
+
* headers: [
|
|
271
|
+
* ["Content-Type", "text/plain"],
|
|
272
|
+
* ],
|
|
273
|
+
* body: "Not Found",
|
|
274
|
+
* });
|
|
275
|
+
* };
|
|
276
|
+
* ```
|
|
277
|
+
*
|
|
278
|
+
* ```ts
|
|
279
|
+
* // TypeScript
|
|
280
|
+
*
|
|
281
|
+
* import type { Request, Response } from "express";
|
|
282
|
+
*
|
|
283
|
+
* import { createResponse } from "@jderstd/express";
|
|
284
|
+
*
|
|
285
|
+
* const route = (req: Request, res: Response): void => {
|
|
286
|
+
* createResponse(res, {
|
|
287
|
+
* status: 404,
|
|
288
|
+
* headers: [
|
|
289
|
+
* ["Content-Type", "text/plain"],
|
|
290
|
+
* ],
|
|
291
|
+
* body: "Not Found",
|
|
292
|
+
* });
|
|
293
|
+
* };
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
const createResponse = (res, options) => {
|
|
297
|
+
const { status, headers, body } = (0, __jderstd_core_response_common_struct.createResponseStruct)(options);
|
|
298
|
+
return res.status(status).setHeaders(new Map(headers)).send(body);
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
exports.ResponseErrorCode = ResponseErrorCode;
|
|
302
|
+
exports.createJsonResponse = createJsonResponse;
|
|
303
|
+
exports.createResponse = createResponse;
|
|
304
|
+
exports.errorRequestHandler = errorRequestHandler;
|
|
305
|
+
exports.getResponseErrorMessage = getResponseErrorMessage;
|
|
306
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { createJsonResponseStruct } from "@jderstd/core/response/json/struct";
|
|
2
|
+
import { createResponseStruct } from "@jderstd/core/response/common/struct";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Response error code.
|
|
6
|
+
*/
|
|
7
|
+
var ResponseErrorCode = /* @__PURE__ */ function(ResponseErrorCode$1) {
|
|
8
|
+
/**
|
|
9
|
+
* Internal server error.
|
|
10
|
+
*
|
|
11
|
+
* For `errorRequestHandler` function.
|
|
12
|
+
*/
|
|
13
|
+
ResponseErrorCode$1["Server"] = "server";
|
|
14
|
+
return ResponseErrorCode$1;
|
|
15
|
+
}(ResponseErrorCode || {});
|
|
16
|
+
/**
|
|
17
|
+
* Get response error message by code.
|
|
18
|
+
*/
|
|
19
|
+
const getResponseErrorMessage = (code) => {
|
|
20
|
+
switch (code) {
|
|
21
|
+
case ResponseErrorCode.Server: return "Internal server error";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Create a JSON response with context.
|
|
27
|
+
*
|
|
28
|
+
* ### Examples
|
|
29
|
+
*
|
|
30
|
+
* Example for creating a successful JSON response without data:
|
|
31
|
+
*
|
|
32
|
+
* ```js
|
|
33
|
+
* // JavaScript
|
|
34
|
+
*
|
|
35
|
+
* import { createJsonResponse } from "@jderstd/express";
|
|
36
|
+
*
|
|
37
|
+
* const route = (req, res) => {
|
|
38
|
+
* createJsonResponse(res);
|
|
39
|
+
* };
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* // TypeScript
|
|
44
|
+
*
|
|
45
|
+
* import type { Request, Response } from "express";
|
|
46
|
+
*
|
|
47
|
+
* import { createJsonResponse } from "@jderstd/express";
|
|
48
|
+
*
|
|
49
|
+
* const route = (req: Request, res: Response): void => {
|
|
50
|
+
* createJsonResponse(res);
|
|
51
|
+
* };
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* Example for creating a successful JSON response with data:
|
|
55
|
+
*
|
|
56
|
+
* ```js
|
|
57
|
+
* // JavaScript
|
|
58
|
+
*
|
|
59
|
+
* import { createJsonResponse } from "@jderstd/express";
|
|
60
|
+
*
|
|
61
|
+
* const route = (req, res) => {
|
|
62
|
+
* return createJsonResponse(res, {
|
|
63
|
+
* data: "Hello, World!",
|
|
64
|
+
* });
|
|
65
|
+
* };
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* // TypeScript
|
|
70
|
+
*
|
|
71
|
+
* import type { Request, Response } from "express";
|
|
72
|
+
*
|
|
73
|
+
* import { createJsonResponse } from "@jderstd/express";
|
|
74
|
+
*
|
|
75
|
+
* const route = (req: Request, res: Response): void => {
|
|
76
|
+
* return createJsonResponse(res, {
|
|
77
|
+
* data: "Hello, World!",
|
|
78
|
+
* });
|
|
79
|
+
* };
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* Example for creating a failed JSON response:
|
|
83
|
+
*
|
|
84
|
+
* ```js
|
|
85
|
+
* // JavaScript
|
|
86
|
+
*
|
|
87
|
+
* import { createJsonResponse } from "@jderstd/express";
|
|
88
|
+
*
|
|
89
|
+
* const route = (req, res) => {
|
|
90
|
+
* return createJsonResponse(res, {
|
|
91
|
+
* errors: [
|
|
92
|
+
* {
|
|
93
|
+
* code: "server",
|
|
94
|
+
* message: "Internal server error",
|
|
95
|
+
* },
|
|
96
|
+
* ],
|
|
97
|
+
* });
|
|
98
|
+
* };
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* ```ts
|
|
102
|
+
* // TypeScript
|
|
103
|
+
*
|
|
104
|
+
* import type { Request, Response } from "express";
|
|
105
|
+
*
|
|
106
|
+
* import { createJsonResponse } from "@jderstd/express";
|
|
107
|
+
*
|
|
108
|
+
* const route = (req: Request, res: Response): void => {
|
|
109
|
+
* return createJsonResponse(res, {
|
|
110
|
+
* errors: [
|
|
111
|
+
* {
|
|
112
|
+
* code: "server",
|
|
113
|
+
* message: "Internal server error",
|
|
114
|
+
* },
|
|
115
|
+
* ],
|
|
116
|
+
* });
|
|
117
|
+
* };
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
const createJsonResponse = (res, options) => {
|
|
121
|
+
const { status, headers, json } = createJsonResponseStruct(options);
|
|
122
|
+
return res.status(status).setHeaders(new Map(headers)).json(json);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Error request handler.
|
|
127
|
+
*
|
|
128
|
+
* Following response could be returned on error:
|
|
129
|
+
*
|
|
130
|
+
* ```jsonc
|
|
131
|
+
* // Status: 500
|
|
132
|
+
* {
|
|
133
|
+
* "success": false,
|
|
134
|
+
* "errors": [
|
|
135
|
+
* {
|
|
136
|
+
* "code": "server"
|
|
137
|
+
* }
|
|
138
|
+
* ]
|
|
139
|
+
* }
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* ### Examples
|
|
143
|
+
*
|
|
144
|
+
* Basic example of using `errorRequestHandler` handler:
|
|
145
|
+
*
|
|
146
|
+
* ```js
|
|
147
|
+
* // JavaScript
|
|
148
|
+
*
|
|
149
|
+
* import express from "express";
|
|
150
|
+
* import { errorRequestHandler } from "@jderstd/express";
|
|
151
|
+
*
|
|
152
|
+
* const app = express();
|
|
153
|
+
*
|
|
154
|
+
* app.use(errorRequestHandler());
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* ```ts
|
|
158
|
+
* // TypeScript
|
|
159
|
+
*
|
|
160
|
+
* import type { Express } from "express";
|
|
161
|
+
*
|
|
162
|
+
* import express from "express";
|
|
163
|
+
* import { errorRequestHandler } from "@jderstd/express";
|
|
164
|
+
*
|
|
165
|
+
* const app: Express = express();
|
|
166
|
+
*
|
|
167
|
+
* app.use(errorRequestHandler());
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* Show more information with `verbose` option:
|
|
171
|
+
*
|
|
172
|
+
* ```js
|
|
173
|
+
* // JavaScript
|
|
174
|
+
*
|
|
175
|
+
* import express from "express";
|
|
176
|
+
* import { errorRequestHandler } from "@jderstd/express";
|
|
177
|
+
*
|
|
178
|
+
* const app = express();
|
|
179
|
+
*
|
|
180
|
+
* app.use(errorRequestHandler({ verbose: true }));
|
|
181
|
+
* ```
|
|
182
|
+
*
|
|
183
|
+
* ```ts
|
|
184
|
+
* // TypeScript
|
|
185
|
+
*
|
|
186
|
+
* import type { Express } from "express";
|
|
187
|
+
*
|
|
188
|
+
* import express from "express";
|
|
189
|
+
* import { errorRequestHandler } from "@jderstd/express";
|
|
190
|
+
*
|
|
191
|
+
* const app: Express = express();
|
|
192
|
+
*
|
|
193
|
+
* app.use(errorRequestHandler({ verbose: true }));
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
const errorRequestHandler = (options) => {
|
|
197
|
+
return (err, _req, res, _next) => {
|
|
198
|
+
return createJsonResponse(res, {
|
|
199
|
+
status: 500,
|
|
200
|
+
errors: [{
|
|
201
|
+
code: ResponseErrorCode.Server,
|
|
202
|
+
...options?.verbose && { message: err instanceof Error ? err.message : String(err) }
|
|
203
|
+
}]
|
|
204
|
+
});
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Create a response.
|
|
210
|
+
*
|
|
211
|
+
* ### Examples
|
|
212
|
+
*
|
|
213
|
+
* Example for creating a basic response:
|
|
214
|
+
*
|
|
215
|
+
* ```js
|
|
216
|
+
* // JavaScript
|
|
217
|
+
*
|
|
218
|
+
* import { createResponse } from "@jderstd/express";
|
|
219
|
+
*
|
|
220
|
+
* const route = (req, res) => {
|
|
221
|
+
* createResponse(res);
|
|
222
|
+
* };
|
|
223
|
+
* ```
|
|
224
|
+
*
|
|
225
|
+
* ```ts
|
|
226
|
+
* // TypeScript
|
|
227
|
+
*
|
|
228
|
+
* import type { Request, Response } from "express";
|
|
229
|
+
*
|
|
230
|
+
* import { createResponse } from "@jderstd/express";
|
|
231
|
+
*
|
|
232
|
+
* const route = (req: Request, res: Response): void => {
|
|
233
|
+
* createResponse(res);
|
|
234
|
+
* };
|
|
235
|
+
* ```
|
|
236
|
+
*
|
|
237
|
+
* Example for creating a response with status, headers, and body:
|
|
238
|
+
*
|
|
239
|
+
* ```js
|
|
240
|
+
* // JavaScript
|
|
241
|
+
*
|
|
242
|
+
* import { createResponse } from "@jderstd/express";
|
|
243
|
+
*
|
|
244
|
+
* const route = (req, res) => {
|
|
245
|
+
* createResponse(res, {
|
|
246
|
+
* status: 404,
|
|
247
|
+
* headers: [
|
|
248
|
+
* ["Content-Type", "text/plain"],
|
|
249
|
+
* ],
|
|
250
|
+
* body: "Not Found",
|
|
251
|
+
* });
|
|
252
|
+
* };
|
|
253
|
+
* ```
|
|
254
|
+
*
|
|
255
|
+
* ```ts
|
|
256
|
+
* // TypeScript
|
|
257
|
+
*
|
|
258
|
+
* import type { Request, Response } from "express";
|
|
259
|
+
*
|
|
260
|
+
* import { createResponse } from "@jderstd/express";
|
|
261
|
+
*
|
|
262
|
+
* const route = (req: Request, res: Response): void => {
|
|
263
|
+
* createResponse(res, {
|
|
264
|
+
* status: 404,
|
|
265
|
+
* headers: [
|
|
266
|
+
* ["Content-Type", "text/plain"],
|
|
267
|
+
* ],
|
|
268
|
+
* body: "Not Found",
|
|
269
|
+
* });
|
|
270
|
+
* };
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
const createResponse = (res, options) => {
|
|
274
|
+
const { status, headers, body } = createResponseStruct(options);
|
|
275
|
+
return res.status(status).setHeaders(new Map(headers)).send(body);
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
export { ResponseErrorCode, createJsonResponse, createResponse, errorRequestHandler, getResponseErrorMessage };
|
|
279
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +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"}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jderstd/express",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A response builder for Express",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"jder",
|
|
7
|
+
"express",
|
|
8
|
+
"response",
|
|
9
|
+
"ts",
|
|
10
|
+
"typescript",
|
|
11
|
+
"js",
|
|
12
|
+
"javascript"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/jderstd/express",
|
|
15
|
+
"bugs": "https://github.com/jderstd/express/issues",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/jderstd/express.git",
|
|
19
|
+
"directory": "packages/express"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "Alpheus",
|
|
24
|
+
"email": "contact@alphe.us"
|
|
25
|
+
},
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.mjs",
|
|
30
|
+
"require": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"main": "./dist/response.js",
|
|
35
|
+
"module": "./dist/response.mjs",
|
|
36
|
+
"types": "./dist/response.d.ts",
|
|
37
|
+
"files": [
|
|
38
|
+
"dist"
|
|
39
|
+
],
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@jderstd/core": "~0.4.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/express": "~5.0.0",
|
|
45
|
+
"express": "5.0.0"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@types/express": "^5.0.0",
|
|
49
|
+
"express": "^5.0.0"
|
|
50
|
+
}
|
|
51
|
+
}
|