@web-ts-toolkit/express-response-handler 0.1.0 → 0.3.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 +1 -2
- package/README.md +4 -269
- package/{create-express-response-handler.d.mts → create-handler.d.mts} +3 -2
- package/{create-express-response-handler.d.ts → create-handler.d.ts} +3 -2
- package/{create-express-response-handler.js → create-handler.js} +113 -71
- package/{create-express-response-handler.mjs → create-handler.mjs} +107 -64
- package/error-format.d.mts +5 -2
- package/error-format.d.ts +5 -2
- package/error-format.js +50 -20
- package/error-format.mjs +46 -18
- package/error-formats.d.mts +7 -0
- package/error-formats.d.ts +7 -0
- package/error-formats.js +32 -0
- package/error-formats.mjs +8 -0
- package/http-response.mjs +0 -1
- package/index.d.mts +8 -5
- package/index.d.ts +8 -5
- package/index.js +64 -3
- package/index.mjs +37 -10
- package/package.json +4 -3
- package/public-types.d.mts +9 -4
- package/public-types.d.ts +9 -4
- package/public-types.js +12 -0
- package/public-types.mjs +4 -0
- package/responses/csv.js +4 -5
- package/responses/csv.mjs +2 -4
- package/responses/index.mjs +0 -1
- package/responses/success.mjs +0 -1
- package/chunk-PQJP2ZCI.mjs +0 -8
|
@@ -1,21 +1,56 @@
|
|
|
1
|
-
import "./chunk-PQJP2ZCI.mjs";
|
|
2
1
|
import assert from "assert";
|
|
2
|
+
import { isArray, isFunction, isPromise } from "@web-ts-toolkit/utils";
|
|
3
3
|
import { CSVResponse } from "./responses/csv";
|
|
4
4
|
import { Response } from "./responses";
|
|
5
5
|
import { HttpResponse } from "./http-response";
|
|
6
6
|
import {
|
|
7
7
|
defaultErrorMessageProvider,
|
|
8
|
+
toRfc9457GenericErrorPayload,
|
|
9
|
+
toRfc9457HttpErrorPayload,
|
|
8
10
|
toSimpleErrorPayload,
|
|
9
11
|
toStructuredGenericErrorPayload,
|
|
10
12
|
toStructuredHttpErrorPayload
|
|
11
13
|
} from "./error-format";
|
|
12
|
-
|
|
13
|
-
const isPromise = (value) => Boolean(value) && isFunction(value.then);
|
|
14
|
+
import { ErrorFormats } from "./error-formats";
|
|
14
15
|
const promisify = (fn) => (value) => Promise.resolve().then(() => fn(value));
|
|
15
|
-
const { isArray } = Array;
|
|
16
16
|
const invokePostHook = (hook, value) => {
|
|
17
17
|
void hook(value).catch(() => void 0);
|
|
18
18
|
};
|
|
19
|
+
const RFC_9457_CONTENT_TYPE = "application/problem+json";
|
|
20
|
+
const shouldSkipResponse = (res, event) => res.headersSent || event.canceled;
|
|
21
|
+
const sendProblemJson = (res, statusCode, payload, contentType) => {
|
|
22
|
+
res.status(statusCode);
|
|
23
|
+
res.set("Content-Type", contentType);
|
|
24
|
+
res.send(payload);
|
|
25
|
+
};
|
|
26
|
+
const sendHttpErrorByFormat = {
|
|
27
|
+
[ErrorFormats.simple]: (res, error) => {
|
|
28
|
+
const payload = { message: error.message ?? "" };
|
|
29
|
+
if (error.errors !== void 0) {
|
|
30
|
+
payload.errors = error.errors;
|
|
31
|
+
}
|
|
32
|
+
res.status(error.statusCode ?? 500).send(payload);
|
|
33
|
+
},
|
|
34
|
+
[ErrorFormats.aip193]: (res, error, domain) => {
|
|
35
|
+
res.status(error.statusCode ?? 500).send(toStructuredHttpErrorPayload(error, domain));
|
|
36
|
+
},
|
|
37
|
+
[ErrorFormats.rfc9457]: (res, error, domain) => {
|
|
38
|
+
sendProblemJson(res, error.statusCode ?? 500, toRfc9457HttpErrorPayload(error, domain), RFC_9457_CONTENT_TYPE);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const sendGenericErrorByFormat = {
|
|
42
|
+
[ErrorFormats.simple]: (res, result) => {
|
|
43
|
+
res.status(422).send(toSimpleErrorPayload(result));
|
|
44
|
+
},
|
|
45
|
+
[ErrorFormats.aip193]: (res, result, domain) => {
|
|
46
|
+
const payload = toStructuredGenericErrorPayload(result, domain);
|
|
47
|
+
res.status(payload.error.code).send(payload);
|
|
48
|
+
},
|
|
49
|
+
[ErrorFormats.rfc9457]: (res, result) => {
|
|
50
|
+
const payload = toRfc9457GenericErrorPayload(result);
|
|
51
|
+
sendProblemJson(res, payload.status ?? 422, payload, RFC_9457_CONTENT_TYPE);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
19
54
|
const assertMiddleware = (fn) => {
|
|
20
55
|
assert.ok(isFunction(fn), "middleware handler must be a function");
|
|
21
56
|
};
|
|
@@ -33,9 +68,10 @@ const normalizeMiddlewareList = (fns) => {
|
|
|
33
68
|
assertMiddleware(fns[0]);
|
|
34
69
|
return [fns[0]];
|
|
35
70
|
};
|
|
36
|
-
function
|
|
37
|
-
const errorFormat = options.errorFormat
|
|
38
|
-
const errorDomain = options.errorDomain
|
|
71
|
+
function createHandler(options = {}) {
|
|
72
|
+
const errorFormat = options.errorFormat ?? ErrorFormats.simple;
|
|
73
|
+
const errorDomain = options.errorDomain ?? "express-response-handler";
|
|
74
|
+
const rfc9457ContentType = options.rfc9457ContentType ?? RFC_9457_CONTENT_TYPE;
|
|
39
75
|
let errorMessageProvider = defaultErrorMessageProvider;
|
|
40
76
|
let preJson = null;
|
|
41
77
|
let postJson = null;
|
|
@@ -45,8 +81,18 @@ function createExpressResponseHandler(options = {}) {
|
|
|
45
81
|
let postJsonHook = null;
|
|
46
82
|
let preErrorHook = null;
|
|
47
83
|
let postErrorHook = null;
|
|
84
|
+
const updateHook = (fn, name, setState, rebuild) => {
|
|
85
|
+
if (fn === null) {
|
|
86
|
+
setState(null, null);
|
|
87
|
+
rebuild();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
assert.ok(isFunction(fn), `${name} hook must be a function`);
|
|
91
|
+
setState(fn, promisify(fn));
|
|
92
|
+
rebuild();
|
|
93
|
+
};
|
|
48
94
|
const sendBaseJson = function(res, data, event) {
|
|
49
|
-
if (res
|
|
95
|
+
if (shouldSkipResponse(res, event)) {
|
|
50
96
|
return;
|
|
51
97
|
}
|
|
52
98
|
if (data instanceof Response) {
|
|
@@ -60,29 +106,30 @@ function createExpressResponseHandler(options = {}) {
|
|
|
60
106
|
res.json(data);
|
|
61
107
|
};
|
|
62
108
|
const sendBaseError = function(res, err, event) {
|
|
63
|
-
if (res
|
|
109
|
+
if (shouldSkipResponse(res, event)) {
|
|
64
110
|
return;
|
|
65
111
|
}
|
|
66
112
|
const error = err;
|
|
67
113
|
if (error.statusCode) {
|
|
68
|
-
if (errorFormat ===
|
|
69
|
-
|
|
114
|
+
if (errorFormat === ErrorFormats.rfc9457) {
|
|
115
|
+
sendProblemJson(
|
|
116
|
+
res,
|
|
117
|
+
error.statusCode ?? 500,
|
|
118
|
+
toRfc9457HttpErrorPayload(error, errorDomain),
|
|
119
|
+
rfc9457ContentType
|
|
120
|
+
);
|
|
70
121
|
return;
|
|
71
122
|
}
|
|
72
|
-
|
|
73
|
-
if (error.errors !== void 0) {
|
|
74
|
-
payload.errors = error.errors;
|
|
75
|
-
}
|
|
76
|
-
res.status(error.statusCode).send(payload);
|
|
123
|
+
sendHttpErrorByFormat[errorFormat](res, error, errorDomain);
|
|
77
124
|
return;
|
|
78
125
|
}
|
|
79
126
|
const result = errorMessageProvider(err);
|
|
80
|
-
if (errorFormat ===
|
|
81
|
-
const payload =
|
|
82
|
-
res.status
|
|
127
|
+
if (errorFormat === ErrorFormats.rfc9457) {
|
|
128
|
+
const payload = toRfc9457GenericErrorPayload(result);
|
|
129
|
+
sendProblemJson(res, payload.status ?? 422, payload, rfc9457ContentType);
|
|
83
130
|
return;
|
|
84
131
|
}
|
|
85
|
-
res
|
|
132
|
+
sendGenericErrorByFormat[errorFormat](res, result, errorDomain);
|
|
86
133
|
};
|
|
87
134
|
let sendJson = sendBaseJson;
|
|
88
135
|
let sendError = sendBaseError;
|
|
@@ -130,7 +177,7 @@ function createExpressResponseHandler(options = {}) {
|
|
|
130
177
|
};
|
|
131
178
|
};
|
|
132
179
|
const handlePromise = function(res, promise, event) {
|
|
133
|
-
promise.then((data) => {
|
|
180
|
+
Promise.resolve(promise).then((data) => {
|
|
134
181
|
if (event.nextError) {
|
|
135
182
|
sendError(res, event.nextError, event);
|
|
136
183
|
return;
|
|
@@ -139,7 +186,7 @@ function createExpressResponseHandler(options = {}) {
|
|
|
139
186
|
}).catch((err) => sendError(res, err, event));
|
|
140
187
|
};
|
|
141
188
|
const handleResult = function(res, result, event) {
|
|
142
|
-
if (res
|
|
189
|
+
if (shouldSkipResponse(res, event)) {
|
|
143
190
|
return;
|
|
144
191
|
}
|
|
145
192
|
if (event.nextError) {
|
|
@@ -189,7 +236,7 @@ function createExpressResponseHandler(options = {}) {
|
|
|
189
236
|
handleResult,
|
|
190
237
|
handlePromise,
|
|
191
238
|
HttpResponse,
|
|
192
|
-
|
|
239
|
+
createHandler,
|
|
193
240
|
get errorMessageProvider() {
|
|
194
241
|
return errorMessageProvider;
|
|
195
242
|
},
|
|
@@ -201,64 +248,60 @@ function createExpressResponseHandler(options = {}) {
|
|
|
201
248
|
return preJson;
|
|
202
249
|
},
|
|
203
250
|
set preJson(fn) {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
rebuildSendJson();
|
|
251
|
+
updateHook(
|
|
252
|
+
fn,
|
|
253
|
+
"pre-json",
|
|
254
|
+
(syncHook, asyncHook) => {
|
|
255
|
+
preJson = syncHook;
|
|
256
|
+
preJsonHook = asyncHook;
|
|
257
|
+
},
|
|
258
|
+
rebuildSendJson
|
|
259
|
+
);
|
|
214
260
|
},
|
|
215
261
|
get postJson() {
|
|
216
262
|
return postJson;
|
|
217
263
|
},
|
|
218
264
|
set postJson(fn) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
rebuildSendJson();
|
|
265
|
+
updateHook(
|
|
266
|
+
fn,
|
|
267
|
+
"post-json",
|
|
268
|
+
(syncHook, asyncHook) => {
|
|
269
|
+
postJson = syncHook;
|
|
270
|
+
postJsonHook = asyncHook;
|
|
271
|
+
},
|
|
272
|
+
rebuildSendJson
|
|
273
|
+
);
|
|
229
274
|
},
|
|
230
275
|
get preError() {
|
|
231
276
|
return preError;
|
|
232
277
|
},
|
|
233
278
|
set preError(fn) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
rebuildSendError();
|
|
279
|
+
updateHook(
|
|
280
|
+
fn,
|
|
281
|
+
"pre-error",
|
|
282
|
+
(syncHook, asyncHook) => {
|
|
283
|
+
preError = syncHook;
|
|
284
|
+
preErrorHook = asyncHook;
|
|
285
|
+
},
|
|
286
|
+
rebuildSendError
|
|
287
|
+
);
|
|
244
288
|
},
|
|
245
289
|
get postError() {
|
|
246
290
|
return postError;
|
|
247
291
|
},
|
|
248
292
|
set postError(fn) {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
rebuildSendError();
|
|
293
|
+
updateHook(
|
|
294
|
+
fn,
|
|
295
|
+
"post-error",
|
|
296
|
+
(syncHook, asyncHook) => {
|
|
297
|
+
postError = syncHook;
|
|
298
|
+
postErrorHook = asyncHook;
|
|
299
|
+
},
|
|
300
|
+
rebuildSendError
|
|
301
|
+
);
|
|
259
302
|
}
|
|
260
303
|
};
|
|
261
304
|
}
|
|
262
305
|
export {
|
|
263
|
-
|
|
306
|
+
createHandler
|
|
264
307
|
};
|
package/error-format.d.mts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import { Aip193ErrorPayload } from '@web-ts-toolkit/http-errors';
|
|
1
|
+
import { Rfc9457ErrorPayload, Aip193ErrorPayload } from '@web-ts-toolkit/http-errors';
|
|
2
2
|
import { ErrorMessageProvider, ErrorMessageResult, ErrorWithPayload } from './public-types.mjs';
|
|
3
3
|
import './http-response.mjs';
|
|
4
4
|
import './responses/success.mjs';
|
|
5
5
|
import './responses/index.mjs';
|
|
6
6
|
import './responses/csv.mjs';
|
|
7
|
+
import './error-formats.mjs';
|
|
7
8
|
|
|
8
9
|
declare const defaultErrorMessageProvider: ErrorMessageProvider;
|
|
9
10
|
declare const toSimpleErrorPayload: (result: ErrorMessageResult) => Record<string, unknown>;
|
|
10
11
|
declare const toStructuredHttpErrorPayload: (error: ErrorWithPayload, errorDomain: string) => Aip193ErrorPayload;
|
|
12
|
+
declare const toRfc9457HttpErrorPayload: (error: ErrorWithPayload, errorDomain: string) => Rfc9457ErrorPayload;
|
|
11
13
|
declare const toStructuredGenericErrorPayload: (result: ErrorMessageResult, errorDomain: string) => Aip193ErrorPayload;
|
|
14
|
+
declare const toRfc9457GenericErrorPayload: (result: ErrorMessageResult) => Rfc9457ErrorPayload;
|
|
12
15
|
|
|
13
|
-
export { defaultErrorMessageProvider, toSimpleErrorPayload, toStructuredGenericErrorPayload, toStructuredHttpErrorPayload };
|
|
16
|
+
export { defaultErrorMessageProvider, toRfc9457GenericErrorPayload, toRfc9457HttpErrorPayload, toSimpleErrorPayload, toStructuredGenericErrorPayload, toStructuredHttpErrorPayload };
|
package/error-format.d.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import { Aip193ErrorPayload } from '@web-ts-toolkit/http-errors';
|
|
1
|
+
import { Rfc9457ErrorPayload, Aip193ErrorPayload } from '@web-ts-toolkit/http-errors';
|
|
2
2
|
import { ErrorMessageProvider, ErrorMessageResult, ErrorWithPayload } from './public-types.js';
|
|
3
3
|
import './http-response.js';
|
|
4
4
|
import './responses/success.js';
|
|
5
5
|
import './responses/index.js';
|
|
6
6
|
import './responses/csv.js';
|
|
7
|
+
import './error-formats.js';
|
|
7
8
|
|
|
8
9
|
declare const defaultErrorMessageProvider: ErrorMessageProvider;
|
|
9
10
|
declare const toSimpleErrorPayload: (result: ErrorMessageResult) => Record<string, unknown>;
|
|
10
11
|
declare const toStructuredHttpErrorPayload: (error: ErrorWithPayload, errorDomain: string) => Aip193ErrorPayload;
|
|
12
|
+
declare const toRfc9457HttpErrorPayload: (error: ErrorWithPayload, errorDomain: string) => Rfc9457ErrorPayload;
|
|
11
13
|
declare const toStructuredGenericErrorPayload: (result: ErrorMessageResult, errorDomain: string) => Aip193ErrorPayload;
|
|
14
|
+
declare const toRfc9457GenericErrorPayload: (result: ErrorMessageResult) => Rfc9457ErrorPayload;
|
|
12
15
|
|
|
13
|
-
export { defaultErrorMessageProvider, toSimpleErrorPayload, toStructuredGenericErrorPayload, toStructuredHttpErrorPayload };
|
|
16
|
+
export { defaultErrorMessageProvider, toRfc9457GenericErrorPayload, toRfc9457HttpErrorPayload, toSimpleErrorPayload, toStructuredGenericErrorPayload, toStructuredHttpErrorPayload };
|
package/error-format.js
CHANGED
|
@@ -19,49 +19,59 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var error_format_exports = {};
|
|
20
20
|
__export(error_format_exports, {
|
|
21
21
|
defaultErrorMessageProvider: () => defaultErrorMessageProvider,
|
|
22
|
+
toRfc9457GenericErrorPayload: () => toRfc9457GenericErrorPayload,
|
|
23
|
+
toRfc9457HttpErrorPayload: () => toRfc9457HttpErrorPayload,
|
|
22
24
|
toSimpleErrorPayload: () => toSimpleErrorPayload,
|
|
23
25
|
toStructuredGenericErrorPayload: () => toStructuredGenericErrorPayload,
|
|
24
26
|
toStructuredHttpErrorPayload: () => toStructuredHttpErrorPayload
|
|
25
27
|
});
|
|
26
28
|
module.exports = __toCommonJS(error_format_exports);
|
|
27
29
|
var import_http_errors = require("@web-ts-toolkit/http-errors");
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (!isPlainObject(value)) {
|
|
33
|
-
return void 0;
|
|
30
|
+
var import_utils = require("@web-ts-toolkit/utils");
|
|
31
|
+
const toStatusCode = (status, code, fallbackStatusCode) => {
|
|
32
|
+
if (typeof status === "number") {
|
|
33
|
+
return status;
|
|
34
34
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return
|
|
35
|
+
return typeof code === "number" ? code : fallbackStatusCode;
|
|
36
|
+
};
|
|
37
|
+
const toOptionalString = (value) => typeof value === "string" ? value : void 0;
|
|
38
|
+
const toMetadata = (value) => {
|
|
39
|
+
return (0, import_utils.toStringRecord)(value);
|
|
40
40
|
};
|
|
41
41
|
const toArray = (value) => {
|
|
42
42
|
if (value === void 0 || value === null) {
|
|
43
43
|
return void 0;
|
|
44
44
|
}
|
|
45
|
-
return isArray(value) ? value : [value];
|
|
45
|
+
return (0, import_utils.isArray)(value) ? value : [value];
|
|
46
46
|
};
|
|
47
47
|
const toHttpErrorShape = (error, fallbackDomain) => ({
|
|
48
|
-
statusCode: error.statusCode
|
|
48
|
+
statusCode: error.statusCode ?? 500,
|
|
49
49
|
status: error.status,
|
|
50
|
-
message: error.message
|
|
50
|
+
message: error.message ?? "",
|
|
51
51
|
reason: error.reason,
|
|
52
|
-
domain: error.domain
|
|
52
|
+
domain: error.domain ?? fallbackDomain,
|
|
53
53
|
metadata: toMetadata(error.metadata),
|
|
54
54
|
details: toArray(error.details),
|
|
55
|
-
errors: error.errors
|
|
55
|
+
errors: error.errors,
|
|
56
|
+
type: error.type,
|
|
57
|
+
title: error.title,
|
|
58
|
+
instance: error.instance
|
|
56
59
|
});
|
|
60
|
+
const toProblemDetailsSource = (result) => {
|
|
61
|
+
if ((0, import_utils.isPlainObject)(result) && (0, import_utils.isPlainObject)(result.error)) {
|
|
62
|
+
return result.error;
|
|
63
|
+
}
|
|
64
|
+
return (0, import_utils.isPlainObject)(result) ? result : void 0;
|
|
65
|
+
};
|
|
57
66
|
const defaultErrorMessageProvider = function(error) {
|
|
58
67
|
const errorLike = error;
|
|
59
|
-
return errorLike.message
|
|
68
|
+
return errorLike.message ?? errorLike._message ?? String(error);
|
|
60
69
|
};
|
|
61
|
-
const toSimpleErrorPayload = (result) => isString(result) ? { message: result } : { ...result };
|
|
70
|
+
const toSimpleErrorPayload = (result) => (0, import_utils.isString)(result) ? { message: result } : { ...result };
|
|
62
71
|
const toStructuredHttpErrorPayload = (error, errorDomain) => (0, import_http_errors.toAip193ErrorPayload)(toHttpErrorShape(error, errorDomain), errorDomain);
|
|
72
|
+
const toRfc9457HttpErrorPayload = (error, errorDomain) => (0, import_http_errors.toRfc9457ErrorPayload)(toHttpErrorShape(error, errorDomain));
|
|
63
73
|
const toStructuredGenericErrorPayload = (result, errorDomain) => {
|
|
64
|
-
if (isPlainObject(result) && isPlainObject(result.error)) {
|
|
74
|
+
if ((0, import_utils.isPlainObject)(result) && (0, import_utils.isPlainObject)(result.error)) {
|
|
65
75
|
const error = result.error;
|
|
66
76
|
const statusCode = typeof error.code === "number" ? error.code : 422;
|
|
67
77
|
const status = typeof error.status === "string" ? error.status : (0, import_http_errors.getCanonicalStatus)(statusCode);
|
|
@@ -76,7 +86,7 @@ const toStructuredGenericErrorPayload = (result, errorDomain) => {
|
|
|
76
86
|
}
|
|
77
87
|
};
|
|
78
88
|
}
|
|
79
|
-
if (isPlainObject(result)) {
|
|
89
|
+
if ((0, import_utils.isPlainObject)(result)) {
|
|
80
90
|
const statusCode = typeof result.code === "number" ? result.code : 422;
|
|
81
91
|
const status = typeof result.status === "string" ? result.status : (0, import_http_errors.getCanonicalStatus)(statusCode);
|
|
82
92
|
const message = typeof result.message === "string" ? result.message : "";
|
|
@@ -99,9 +109,29 @@ const toStructuredGenericErrorPayload = (result, errorDomain) => {
|
|
|
99
109
|
}
|
|
100
110
|
};
|
|
101
111
|
};
|
|
112
|
+
const toRfc9457GenericErrorPayload = (result) => {
|
|
113
|
+
const problem = toProblemDetailsSource(result);
|
|
114
|
+
if (problem) {
|
|
115
|
+
const statusCode = toStatusCode(problem.status, problem.code, 422);
|
|
116
|
+
return (0, import_http_errors.toRfc9457ErrorPayload)({
|
|
117
|
+
statusCode,
|
|
118
|
+
message: toOptionalString(problem.detail) ?? toOptionalString(problem.message) ?? "",
|
|
119
|
+
errors: problem.errors,
|
|
120
|
+
type: toOptionalString(problem.type),
|
|
121
|
+
title: toOptionalString(problem.title),
|
|
122
|
+
instance: toOptionalString(problem.instance)
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return (0, import_http_errors.toRfc9457ErrorPayload)({
|
|
126
|
+
statusCode: 422,
|
|
127
|
+
message: String(result)
|
|
128
|
+
});
|
|
129
|
+
};
|
|
102
130
|
// Annotate the CommonJS export names for ESM import in node:
|
|
103
131
|
0 && (module.exports = {
|
|
104
132
|
defaultErrorMessageProvider,
|
|
133
|
+
toRfc9457GenericErrorPayload,
|
|
134
|
+
toRfc9457HttpErrorPayload,
|
|
105
135
|
toSimpleErrorPayload,
|
|
106
136
|
toStructuredGenericErrorPayload,
|
|
107
137
|
toStructuredHttpErrorPayload
|
package/error-format.mjs
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
|
-
import "./chunk-PQJP2ZCI.mjs";
|
|
2
1
|
import {
|
|
3
2
|
createAip193ErrorInfoDetail,
|
|
4
3
|
getCanonicalStatus,
|
|
5
|
-
toAip193ErrorPayload
|
|
4
|
+
toAip193ErrorPayload,
|
|
5
|
+
toRfc9457ErrorPayload
|
|
6
6
|
} from "@web-ts-toolkit/http-errors";
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if (!isPlainObject(value)) {
|
|
12
|
-
return void 0;
|
|
7
|
+
import { isArray, isPlainObject, isString, toStringRecord } from "@web-ts-toolkit/utils";
|
|
8
|
+
const toStatusCode = (status, code, fallbackStatusCode) => {
|
|
9
|
+
if (typeof status === "number") {
|
|
10
|
+
return status;
|
|
13
11
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return
|
|
12
|
+
return typeof code === "number" ? code : fallbackStatusCode;
|
|
13
|
+
};
|
|
14
|
+
const toOptionalString = (value) => typeof value === "string" ? value : void 0;
|
|
15
|
+
const toMetadata = (value) => {
|
|
16
|
+
return toStringRecord(value);
|
|
19
17
|
};
|
|
20
18
|
const toArray = (value) => {
|
|
21
19
|
if (value === void 0 || value === null) {
|
|
@@ -24,21 +22,31 @@ const toArray = (value) => {
|
|
|
24
22
|
return isArray(value) ? value : [value];
|
|
25
23
|
};
|
|
26
24
|
const toHttpErrorShape = (error, fallbackDomain) => ({
|
|
27
|
-
statusCode: error.statusCode
|
|
25
|
+
statusCode: error.statusCode ?? 500,
|
|
28
26
|
status: error.status,
|
|
29
|
-
message: error.message
|
|
27
|
+
message: error.message ?? "",
|
|
30
28
|
reason: error.reason,
|
|
31
|
-
domain: error.domain
|
|
29
|
+
domain: error.domain ?? fallbackDomain,
|
|
32
30
|
metadata: toMetadata(error.metadata),
|
|
33
31
|
details: toArray(error.details),
|
|
34
|
-
errors: error.errors
|
|
32
|
+
errors: error.errors,
|
|
33
|
+
type: error.type,
|
|
34
|
+
title: error.title,
|
|
35
|
+
instance: error.instance
|
|
35
36
|
});
|
|
37
|
+
const toProblemDetailsSource = (result) => {
|
|
38
|
+
if (isPlainObject(result) && isPlainObject(result.error)) {
|
|
39
|
+
return result.error;
|
|
40
|
+
}
|
|
41
|
+
return isPlainObject(result) ? result : void 0;
|
|
42
|
+
};
|
|
36
43
|
const defaultErrorMessageProvider = function(error) {
|
|
37
44
|
const errorLike = error;
|
|
38
|
-
return errorLike.message
|
|
45
|
+
return errorLike.message ?? errorLike._message ?? String(error);
|
|
39
46
|
};
|
|
40
47
|
const toSimpleErrorPayload = (result) => isString(result) ? { message: result } : { ...result };
|
|
41
48
|
const toStructuredHttpErrorPayload = (error, errorDomain) => toAip193ErrorPayload(toHttpErrorShape(error, errorDomain), errorDomain);
|
|
49
|
+
const toRfc9457HttpErrorPayload = (error, errorDomain) => toRfc9457ErrorPayload(toHttpErrorShape(error, errorDomain));
|
|
42
50
|
const toStructuredGenericErrorPayload = (result, errorDomain) => {
|
|
43
51
|
if (isPlainObject(result) && isPlainObject(result.error)) {
|
|
44
52
|
const error = result.error;
|
|
@@ -78,8 +86,28 @@ const toStructuredGenericErrorPayload = (result, errorDomain) => {
|
|
|
78
86
|
}
|
|
79
87
|
};
|
|
80
88
|
};
|
|
89
|
+
const toRfc9457GenericErrorPayload = (result) => {
|
|
90
|
+
const problem = toProblemDetailsSource(result);
|
|
91
|
+
if (problem) {
|
|
92
|
+
const statusCode = toStatusCode(problem.status, problem.code, 422);
|
|
93
|
+
return toRfc9457ErrorPayload({
|
|
94
|
+
statusCode,
|
|
95
|
+
message: toOptionalString(problem.detail) ?? toOptionalString(problem.message) ?? "",
|
|
96
|
+
errors: problem.errors,
|
|
97
|
+
type: toOptionalString(problem.type),
|
|
98
|
+
title: toOptionalString(problem.title),
|
|
99
|
+
instance: toOptionalString(problem.instance)
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return toRfc9457ErrorPayload({
|
|
103
|
+
statusCode: 422,
|
|
104
|
+
message: String(result)
|
|
105
|
+
});
|
|
106
|
+
};
|
|
81
107
|
export {
|
|
82
108
|
defaultErrorMessageProvider,
|
|
109
|
+
toRfc9457GenericErrorPayload,
|
|
110
|
+
toRfc9457HttpErrorPayload,
|
|
83
111
|
toSimpleErrorPayload,
|
|
84
112
|
toStructuredGenericErrorPayload,
|
|
85
113
|
toStructuredHttpErrorPayload
|
package/error-formats.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var error_formats_exports = {};
|
|
20
|
+
__export(error_formats_exports, {
|
|
21
|
+
ErrorFormats: () => ErrorFormats
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(error_formats_exports);
|
|
24
|
+
const ErrorFormats = {
|
|
25
|
+
simple: "simple",
|
|
26
|
+
aip193: "aip193",
|
|
27
|
+
rfc9457: "rfc9457"
|
|
28
|
+
};
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
ErrorFormats
|
|
32
|
+
});
|
package/http-response.mjs
CHANGED
package/index.d.mts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { ExpressResponseHandler } from './public-types.mjs';
|
|
2
|
-
|
|
2
|
+
export { AsyncHook, CreateHandler, ErrorFormat, ErrorMessageProvider, ErrorMessageResult, ErrorWithPayload, EventState, ExpressResponseHandlerOptions, HandleResponse, Hook, MaybePromise, MiddlewareFunction, NextFunction, ResponseLike, RouterFunction } from './public-types.mjs';
|
|
3
|
+
export { createHandler } from './create-handler.mjs';
|
|
4
|
+
export { ErrorFormats } from './error-formats.mjs';
|
|
5
|
+
export { HttpResponse, HttpResponseHelpers } from './http-response.mjs';
|
|
6
|
+
export { CSVResponse } from './responses/csv.mjs';
|
|
7
|
+
export { Response } from './responses/index.mjs';
|
|
8
|
+
export { Accepted, AlreadyReported, Created, IMUsed, MultiStatus, NoContent, NonAuthoritativeInfo, OK, PartialContent, ResetContent } from './responses/success.mjs';
|
|
3
9
|
import '@web-ts-toolkit/http-errors';
|
|
4
|
-
import './responses/success.mjs';
|
|
5
|
-
import './responses/index.mjs';
|
|
6
|
-
import './responses/csv.mjs';
|
|
7
10
|
|
|
8
11
|
declare const apiHandler: ExpressResponseHandler;
|
|
9
12
|
|
|
10
|
-
export { apiHandler as default };
|
|
13
|
+
export { ExpressResponseHandler, apiHandler as default };
|
package/index.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { ExpressResponseHandler } from './public-types.js';
|
|
2
|
-
|
|
2
|
+
export { AsyncHook, CreateHandler, ErrorFormat, ErrorMessageProvider, ErrorMessageResult, ErrorWithPayload, EventState, ExpressResponseHandlerOptions, HandleResponse, Hook, MaybePromise, MiddlewareFunction, NextFunction, ResponseLike, RouterFunction } from './public-types.js';
|
|
3
|
+
export { createHandler } from './create-handler.js';
|
|
4
|
+
export { ErrorFormats } from './error-formats.js';
|
|
5
|
+
export { HttpResponse, HttpResponseHelpers } from './http-response.js';
|
|
6
|
+
export { CSVResponse } from './responses/csv.js';
|
|
7
|
+
export { Response } from './responses/index.js';
|
|
8
|
+
export { Accepted, AlreadyReported, Created, IMUsed, MultiStatus, NoContent, NonAuthoritativeInfo, OK, PartialContent, ResetContent } from './responses/success.js';
|
|
3
9
|
import '@web-ts-toolkit/http-errors';
|
|
4
|
-
import './responses/success.js';
|
|
5
|
-
import './responses/index.js';
|
|
6
|
-
import './responses/csv.js';
|
|
7
10
|
|
|
8
11
|
declare const apiHandler: ExpressResponseHandler;
|
|
9
12
|
|
|
10
|
-
export { apiHandler as default };
|
|
13
|
+
export { ExpressResponseHandler, apiHandler as default };
|