@web-ts-toolkit/express-response-handler 0.0.2
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 +202 -0
- package/README.md +322 -0
- package/chunk-PQJP2ZCI.mjs +8 -0
- package/create-express-response-handler.d.mts +10 -0
- package/create-express-response-handler.d.ts +10 -0
- package/create-express-response-handler.js +292 -0
- package/create-express-response-handler.mjs +264 -0
- package/error-format.d.mts +13 -0
- package/error-format.d.ts +13 -0
- package/error-format.js +108 -0
- package/error-format.mjs +86 -0
- package/http-response.d.mts +54 -0
- package/http-response.d.ts +54 -0
- package/http-response.js +71 -0
- package/http-response.mjs +87 -0
- package/index.d.mts +10 -0
- package/index.d.ts +10 -0
- package/index.js +4 -0
- package/index.mjs +11 -0
- package/package.json +63 -0
- package/public-types.d.mts +62 -0
- package/public-types.d.ts +62 -0
- package/public-types.js +16 -0
- package/public-types.mjs +0 -0
- package/responses/csv.d.mts +21 -0
- package/responses/csv.d.ts +21 -0
- package/responses/csv.js +58 -0
- package/responses/csv.mjs +35 -0
- package/responses/index.d.mts +7 -0
- package/responses/index.d.ts +7 -0
- package/responses/index.js +33 -0
- package/responses/index.mjs +10 -0
- package/responses/success.d.mts +34 -0
- package/responses/success.d.ts +34 -0
- package/responses/success.js +96 -0
- package/responses/success.mjs +64 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var create_express_response_handler_exports = {};
|
|
30
|
+
__export(create_express_response_handler_exports, {
|
|
31
|
+
createExpressResponseHandler: () => createExpressResponseHandler
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(create_express_response_handler_exports);
|
|
34
|
+
var import_assert = __toESM(require("assert"));
|
|
35
|
+
var import_csv = require("./responses/csv");
|
|
36
|
+
var import_responses = require("./responses");
|
|
37
|
+
var import_http_response = require("./http-response");
|
|
38
|
+
var import_error_format = require("./error-format");
|
|
39
|
+
const isFunction = (value) => typeof value === "function";
|
|
40
|
+
const isPromise = (value) => Boolean(value) && isFunction(value.then);
|
|
41
|
+
const promisify = (fn) => (value) => Promise.resolve().then(() => fn(value));
|
|
42
|
+
const { isArray } = Array;
|
|
43
|
+
const invokePostHook = (hook, value) => {
|
|
44
|
+
void hook(value).catch(() => void 0);
|
|
45
|
+
};
|
|
46
|
+
const assertMiddleware = (fn) => {
|
|
47
|
+
import_assert.default.ok(isFunction(fn), "middleware handler must be a function");
|
|
48
|
+
};
|
|
49
|
+
const normalizeMiddlewareList = (fns) => {
|
|
50
|
+
import_assert.default.ok(fns.length > 0, "at least one middleware handler is required");
|
|
51
|
+
if (fns.length > 1) {
|
|
52
|
+
fns.forEach(assertMiddleware);
|
|
53
|
+
return fns;
|
|
54
|
+
}
|
|
55
|
+
if (isArray(fns[0])) {
|
|
56
|
+
import_assert.default.ok(fns[0].length > 0, "at least one middleware handler is required");
|
|
57
|
+
fns[0].forEach(assertMiddleware);
|
|
58
|
+
return fns[0];
|
|
59
|
+
}
|
|
60
|
+
assertMiddleware(fns[0]);
|
|
61
|
+
return [fns[0]];
|
|
62
|
+
};
|
|
63
|
+
function createExpressResponseHandler(options = {}) {
|
|
64
|
+
const errorFormat = options.errorFormat || "simple";
|
|
65
|
+
const errorDomain = options.errorDomain || "express-response-handler";
|
|
66
|
+
let errorMessageProvider = import_error_format.defaultErrorMessageProvider;
|
|
67
|
+
let preJson = null;
|
|
68
|
+
let postJson = null;
|
|
69
|
+
let preError = null;
|
|
70
|
+
let postError = null;
|
|
71
|
+
let preJsonHook = null;
|
|
72
|
+
let postJsonHook = null;
|
|
73
|
+
let preErrorHook = null;
|
|
74
|
+
let postErrorHook = null;
|
|
75
|
+
const sendBaseJson = function(res, data, event) {
|
|
76
|
+
if (res.headersSent || event.canceled) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (data instanceof import_responses.Response) {
|
|
80
|
+
res.status(data.statusCode).json(data.data);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (data instanceof import_csv.CSVResponse) {
|
|
84
|
+
data.streamCsv(res);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
res.json(data);
|
|
88
|
+
};
|
|
89
|
+
const sendBaseError = function(res, err, event) {
|
|
90
|
+
if (res.headersSent || event.canceled) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const error = err;
|
|
94
|
+
if (error.statusCode) {
|
|
95
|
+
if (errorFormat === "aip193") {
|
|
96
|
+
res.status(error.statusCode).send((0, import_error_format.toStructuredHttpErrorPayload)(error, errorDomain));
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const payload = { message: error.message || "" };
|
|
100
|
+
if (error.errors !== void 0) {
|
|
101
|
+
payload.errors = error.errors;
|
|
102
|
+
}
|
|
103
|
+
res.status(error.statusCode).send(payload);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const result = errorMessageProvider(err);
|
|
107
|
+
if (errorFormat === "aip193") {
|
|
108
|
+
const payload = (0, import_error_format.toStructuredGenericErrorPayload)(result, errorDomain);
|
|
109
|
+
res.status(payload.error.code).send(payload);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
res.status(422).send((0, import_error_format.toSimpleErrorPayload)(result));
|
|
113
|
+
};
|
|
114
|
+
let sendJson = sendBaseJson;
|
|
115
|
+
let sendError = sendBaseError;
|
|
116
|
+
const rebuildSendJson = () => {
|
|
117
|
+
if (!preJsonHook && !postJsonHook) {
|
|
118
|
+
sendJson = sendBaseJson;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
sendJson = function(res, data, event) {
|
|
122
|
+
const finalize = () => {
|
|
123
|
+
try {
|
|
124
|
+
sendBaseJson(res, data, event);
|
|
125
|
+
} catch (err) {
|
|
126
|
+
sendError(res, err, event);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (postJsonHook) {
|
|
130
|
+
invokePostHook(postJsonHook, data);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
if (preJsonHook) {
|
|
134
|
+
preJsonHook(data).then(finalize, (err) => sendError(res, err, event));
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
finalize();
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
const rebuildSendError = () => {
|
|
141
|
+
if (!preErrorHook && !postErrorHook) {
|
|
142
|
+
sendError = sendBaseError;
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
sendError = function(res, err, event) {
|
|
146
|
+
const finalize = () => {
|
|
147
|
+
sendBaseError(res, err, event);
|
|
148
|
+
if (postErrorHook) {
|
|
149
|
+
invokePostHook(postErrorHook, err);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
if (preErrorHook) {
|
|
153
|
+
preErrorHook(err).then(finalize, (hookErr) => sendBaseError(res, hookErr, event));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
finalize();
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
const handlePromise = function(res, promise, event) {
|
|
160
|
+
promise.then((data) => {
|
|
161
|
+
if (event.nextError) {
|
|
162
|
+
sendError(res, event.nextError, event);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
sendJson(res, data, event);
|
|
166
|
+
}).catch((err) => sendError(res, err, event));
|
|
167
|
+
};
|
|
168
|
+
const handleResult = function(res, result, event) {
|
|
169
|
+
if (res.headersSent || event.canceled) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (event.nextError) {
|
|
173
|
+
sendError(res, event.nextError, event);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (isPromise(result)) {
|
|
177
|
+
handlePromise(res, result, event);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
sendJson(res, result, event);
|
|
181
|
+
};
|
|
182
|
+
const nextFn = function(event, next) {
|
|
183
|
+
return function(...args) {
|
|
184
|
+
if (args.length === 0) {
|
|
185
|
+
event.canceled = true;
|
|
186
|
+
next();
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (args[0] instanceof Error) {
|
|
190
|
+
event.nextError = args[0];
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
event.nextError = new TypeError("next(value) is not supported; return a value instead");
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
const routerFn = function(fn) {
|
|
197
|
+
return function(req, res, next) {
|
|
198
|
+
const event = { canceled: false, nextError: null };
|
|
199
|
+
try {
|
|
200
|
+
const result = fn(req, res, nextFn(event, next));
|
|
201
|
+
handleResult(res, result, event);
|
|
202
|
+
} catch (err) {
|
|
203
|
+
if (res.headersSent) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
sendError(res, err, event);
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
const handleResponse = function(...fns) {
|
|
211
|
+
const middlewares = normalizeMiddlewareList(fns);
|
|
212
|
+
return middlewares.length === 1 ? routerFn(middlewares[0]) : middlewares.map(routerFn);
|
|
213
|
+
};
|
|
214
|
+
return {
|
|
215
|
+
handleResponse,
|
|
216
|
+
handleResult,
|
|
217
|
+
handlePromise,
|
|
218
|
+
HttpResponse: import_http_response.HttpResponse,
|
|
219
|
+
createExpressResponseHandler,
|
|
220
|
+
get errorMessageProvider() {
|
|
221
|
+
return errorMessageProvider;
|
|
222
|
+
},
|
|
223
|
+
set errorMessageProvider(fn) {
|
|
224
|
+
import_assert.default.ok(isFunction(fn), "error message provider must be a function");
|
|
225
|
+
errorMessageProvider = fn;
|
|
226
|
+
},
|
|
227
|
+
get preJson() {
|
|
228
|
+
return preJson;
|
|
229
|
+
},
|
|
230
|
+
set preJson(fn) {
|
|
231
|
+
if (fn === null) {
|
|
232
|
+
preJson = null;
|
|
233
|
+
preJsonHook = null;
|
|
234
|
+
rebuildSendJson();
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
import_assert.default.ok(isFunction(fn), "pre-json hook must be a function");
|
|
238
|
+
preJson = fn;
|
|
239
|
+
preJsonHook = promisify(fn);
|
|
240
|
+
rebuildSendJson();
|
|
241
|
+
},
|
|
242
|
+
get postJson() {
|
|
243
|
+
return postJson;
|
|
244
|
+
},
|
|
245
|
+
set postJson(fn) {
|
|
246
|
+
if (fn === null) {
|
|
247
|
+
postJson = null;
|
|
248
|
+
postJsonHook = null;
|
|
249
|
+
rebuildSendJson();
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
import_assert.default.ok(isFunction(fn), "post-json hook must be a function");
|
|
253
|
+
postJson = fn;
|
|
254
|
+
postJsonHook = promisify(fn);
|
|
255
|
+
rebuildSendJson();
|
|
256
|
+
},
|
|
257
|
+
get preError() {
|
|
258
|
+
return preError;
|
|
259
|
+
},
|
|
260
|
+
set preError(fn) {
|
|
261
|
+
if (fn === null) {
|
|
262
|
+
preError = null;
|
|
263
|
+
preErrorHook = null;
|
|
264
|
+
rebuildSendError();
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
import_assert.default.ok(isFunction(fn), "pre-error hook must be a function");
|
|
268
|
+
preError = fn;
|
|
269
|
+
preErrorHook = promisify(fn);
|
|
270
|
+
rebuildSendError();
|
|
271
|
+
},
|
|
272
|
+
get postError() {
|
|
273
|
+
return postError;
|
|
274
|
+
},
|
|
275
|
+
set postError(fn) {
|
|
276
|
+
if (fn === null) {
|
|
277
|
+
postError = null;
|
|
278
|
+
postErrorHook = null;
|
|
279
|
+
rebuildSendError();
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
import_assert.default.ok(isFunction(fn), "post-error hook must be a function");
|
|
283
|
+
postError = fn;
|
|
284
|
+
postErrorHook = promisify(fn);
|
|
285
|
+
rebuildSendError();
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
290
|
+
0 && (module.exports = {
|
|
291
|
+
createExpressResponseHandler
|
|
292
|
+
});
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import "./chunk-PQJP2ZCI.mjs";
|
|
2
|
+
import assert from "assert";
|
|
3
|
+
import { CSVResponse } from "./responses/csv";
|
|
4
|
+
import { Response } from "./responses";
|
|
5
|
+
import { HttpResponse } from "./http-response";
|
|
6
|
+
import {
|
|
7
|
+
defaultErrorMessageProvider,
|
|
8
|
+
toSimpleErrorPayload,
|
|
9
|
+
toStructuredGenericErrorPayload,
|
|
10
|
+
toStructuredHttpErrorPayload
|
|
11
|
+
} from "./error-format";
|
|
12
|
+
const isFunction = (value) => typeof value === "function";
|
|
13
|
+
const isPromise = (value) => Boolean(value) && isFunction(value.then);
|
|
14
|
+
const promisify = (fn) => (value) => Promise.resolve().then(() => fn(value));
|
|
15
|
+
const { isArray } = Array;
|
|
16
|
+
const invokePostHook = (hook, value) => {
|
|
17
|
+
void hook(value).catch(() => void 0);
|
|
18
|
+
};
|
|
19
|
+
const assertMiddleware = (fn) => {
|
|
20
|
+
assert.ok(isFunction(fn), "middleware handler must be a function");
|
|
21
|
+
};
|
|
22
|
+
const normalizeMiddlewareList = (fns) => {
|
|
23
|
+
assert.ok(fns.length > 0, "at least one middleware handler is required");
|
|
24
|
+
if (fns.length > 1) {
|
|
25
|
+
fns.forEach(assertMiddleware);
|
|
26
|
+
return fns;
|
|
27
|
+
}
|
|
28
|
+
if (isArray(fns[0])) {
|
|
29
|
+
assert.ok(fns[0].length > 0, "at least one middleware handler is required");
|
|
30
|
+
fns[0].forEach(assertMiddleware);
|
|
31
|
+
return fns[0];
|
|
32
|
+
}
|
|
33
|
+
assertMiddleware(fns[0]);
|
|
34
|
+
return [fns[0]];
|
|
35
|
+
};
|
|
36
|
+
function createExpressResponseHandler(options = {}) {
|
|
37
|
+
const errorFormat = options.errorFormat || "simple";
|
|
38
|
+
const errorDomain = options.errorDomain || "express-response-handler";
|
|
39
|
+
let errorMessageProvider = defaultErrorMessageProvider;
|
|
40
|
+
let preJson = null;
|
|
41
|
+
let postJson = null;
|
|
42
|
+
let preError = null;
|
|
43
|
+
let postError = null;
|
|
44
|
+
let preJsonHook = null;
|
|
45
|
+
let postJsonHook = null;
|
|
46
|
+
let preErrorHook = null;
|
|
47
|
+
let postErrorHook = null;
|
|
48
|
+
const sendBaseJson = function(res, data, event) {
|
|
49
|
+
if (res.headersSent || event.canceled) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (data instanceof Response) {
|
|
53
|
+
res.status(data.statusCode).json(data.data);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (data instanceof CSVResponse) {
|
|
57
|
+
data.streamCsv(res);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
res.json(data);
|
|
61
|
+
};
|
|
62
|
+
const sendBaseError = function(res, err, event) {
|
|
63
|
+
if (res.headersSent || event.canceled) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const error = err;
|
|
67
|
+
if (error.statusCode) {
|
|
68
|
+
if (errorFormat === "aip193") {
|
|
69
|
+
res.status(error.statusCode).send(toStructuredHttpErrorPayload(error, errorDomain));
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const payload = { message: error.message || "" };
|
|
73
|
+
if (error.errors !== void 0) {
|
|
74
|
+
payload.errors = error.errors;
|
|
75
|
+
}
|
|
76
|
+
res.status(error.statusCode).send(payload);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const result = errorMessageProvider(err);
|
|
80
|
+
if (errorFormat === "aip193") {
|
|
81
|
+
const payload = toStructuredGenericErrorPayload(result, errorDomain);
|
|
82
|
+
res.status(payload.error.code).send(payload);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
res.status(422).send(toSimpleErrorPayload(result));
|
|
86
|
+
};
|
|
87
|
+
let sendJson = sendBaseJson;
|
|
88
|
+
let sendError = sendBaseError;
|
|
89
|
+
const rebuildSendJson = () => {
|
|
90
|
+
if (!preJsonHook && !postJsonHook) {
|
|
91
|
+
sendJson = sendBaseJson;
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
sendJson = function(res, data, event) {
|
|
95
|
+
const finalize = () => {
|
|
96
|
+
try {
|
|
97
|
+
sendBaseJson(res, data, event);
|
|
98
|
+
} catch (err) {
|
|
99
|
+
sendError(res, err, event);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (postJsonHook) {
|
|
103
|
+
invokePostHook(postJsonHook, data);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
if (preJsonHook) {
|
|
107
|
+
preJsonHook(data).then(finalize, (err) => sendError(res, err, event));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
finalize();
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
const rebuildSendError = () => {
|
|
114
|
+
if (!preErrorHook && !postErrorHook) {
|
|
115
|
+
sendError = sendBaseError;
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
sendError = function(res, err, event) {
|
|
119
|
+
const finalize = () => {
|
|
120
|
+
sendBaseError(res, err, event);
|
|
121
|
+
if (postErrorHook) {
|
|
122
|
+
invokePostHook(postErrorHook, err);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
if (preErrorHook) {
|
|
126
|
+
preErrorHook(err).then(finalize, (hookErr) => sendBaseError(res, hookErr, event));
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
finalize();
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
const handlePromise = function(res, promise, event) {
|
|
133
|
+
promise.then((data) => {
|
|
134
|
+
if (event.nextError) {
|
|
135
|
+
sendError(res, event.nextError, event);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
sendJson(res, data, event);
|
|
139
|
+
}).catch((err) => sendError(res, err, event));
|
|
140
|
+
};
|
|
141
|
+
const handleResult = function(res, result, event) {
|
|
142
|
+
if (res.headersSent || event.canceled) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (event.nextError) {
|
|
146
|
+
sendError(res, event.nextError, event);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (isPromise(result)) {
|
|
150
|
+
handlePromise(res, result, event);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
sendJson(res, result, event);
|
|
154
|
+
};
|
|
155
|
+
const nextFn = function(event, next) {
|
|
156
|
+
return function(...args) {
|
|
157
|
+
if (args.length === 0) {
|
|
158
|
+
event.canceled = true;
|
|
159
|
+
next();
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (args[0] instanceof Error) {
|
|
163
|
+
event.nextError = args[0];
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
event.nextError = new TypeError("next(value) is not supported; return a value instead");
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
const routerFn = function(fn) {
|
|
170
|
+
return function(req, res, next) {
|
|
171
|
+
const event = { canceled: false, nextError: null };
|
|
172
|
+
try {
|
|
173
|
+
const result = fn(req, res, nextFn(event, next));
|
|
174
|
+
handleResult(res, result, event);
|
|
175
|
+
} catch (err) {
|
|
176
|
+
if (res.headersSent) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
sendError(res, err, event);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
const handleResponse = function(...fns) {
|
|
184
|
+
const middlewares = normalizeMiddlewareList(fns);
|
|
185
|
+
return middlewares.length === 1 ? routerFn(middlewares[0]) : middlewares.map(routerFn);
|
|
186
|
+
};
|
|
187
|
+
return {
|
|
188
|
+
handleResponse,
|
|
189
|
+
handleResult,
|
|
190
|
+
handlePromise,
|
|
191
|
+
HttpResponse,
|
|
192
|
+
createExpressResponseHandler,
|
|
193
|
+
get errorMessageProvider() {
|
|
194
|
+
return errorMessageProvider;
|
|
195
|
+
},
|
|
196
|
+
set errorMessageProvider(fn) {
|
|
197
|
+
assert.ok(isFunction(fn), "error message provider must be a function");
|
|
198
|
+
errorMessageProvider = fn;
|
|
199
|
+
},
|
|
200
|
+
get preJson() {
|
|
201
|
+
return preJson;
|
|
202
|
+
},
|
|
203
|
+
set preJson(fn) {
|
|
204
|
+
if (fn === null) {
|
|
205
|
+
preJson = null;
|
|
206
|
+
preJsonHook = null;
|
|
207
|
+
rebuildSendJson();
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
assert.ok(isFunction(fn), "pre-json hook must be a function");
|
|
211
|
+
preJson = fn;
|
|
212
|
+
preJsonHook = promisify(fn);
|
|
213
|
+
rebuildSendJson();
|
|
214
|
+
},
|
|
215
|
+
get postJson() {
|
|
216
|
+
return postJson;
|
|
217
|
+
},
|
|
218
|
+
set postJson(fn) {
|
|
219
|
+
if (fn === null) {
|
|
220
|
+
postJson = null;
|
|
221
|
+
postJsonHook = null;
|
|
222
|
+
rebuildSendJson();
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
assert.ok(isFunction(fn), "post-json hook must be a function");
|
|
226
|
+
postJson = fn;
|
|
227
|
+
postJsonHook = promisify(fn);
|
|
228
|
+
rebuildSendJson();
|
|
229
|
+
},
|
|
230
|
+
get preError() {
|
|
231
|
+
return preError;
|
|
232
|
+
},
|
|
233
|
+
set preError(fn) {
|
|
234
|
+
if (fn === null) {
|
|
235
|
+
preError = null;
|
|
236
|
+
preErrorHook = null;
|
|
237
|
+
rebuildSendError();
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
assert.ok(isFunction(fn), "pre-error hook must be a function");
|
|
241
|
+
preError = fn;
|
|
242
|
+
preErrorHook = promisify(fn);
|
|
243
|
+
rebuildSendError();
|
|
244
|
+
},
|
|
245
|
+
get postError() {
|
|
246
|
+
return postError;
|
|
247
|
+
},
|
|
248
|
+
set postError(fn) {
|
|
249
|
+
if (fn === null) {
|
|
250
|
+
postError = null;
|
|
251
|
+
postErrorHook = null;
|
|
252
|
+
rebuildSendError();
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
assert.ok(isFunction(fn), "post-error hook must be a function");
|
|
256
|
+
postError = fn;
|
|
257
|
+
postErrorHook = promisify(fn);
|
|
258
|
+
rebuildSendError();
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
export {
|
|
263
|
+
createExpressResponseHandler
|
|
264
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Aip193ErrorPayload } from '@web-ts-toolkit/http-errors';
|
|
2
|
+
import { ErrorMessageProvider, ErrorMessageResult, ErrorWithPayload } from './public-types.mjs';
|
|
3
|
+
import './http-response.mjs';
|
|
4
|
+
import './responses/success.mjs';
|
|
5
|
+
import './responses/index.mjs';
|
|
6
|
+
import './responses/csv.mjs';
|
|
7
|
+
|
|
8
|
+
declare const defaultErrorMessageProvider: ErrorMessageProvider;
|
|
9
|
+
declare const toSimpleErrorPayload: (result: ErrorMessageResult) => Record<string, unknown>;
|
|
10
|
+
declare const toStructuredHttpErrorPayload: (error: ErrorWithPayload, errorDomain: string) => Aip193ErrorPayload;
|
|
11
|
+
declare const toStructuredGenericErrorPayload: (result: ErrorMessageResult, errorDomain: string) => Aip193ErrorPayload;
|
|
12
|
+
|
|
13
|
+
export { defaultErrorMessageProvider, toSimpleErrorPayload, toStructuredGenericErrorPayload, toStructuredHttpErrorPayload };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Aip193ErrorPayload } from '@web-ts-toolkit/http-errors';
|
|
2
|
+
import { ErrorMessageProvider, ErrorMessageResult, ErrorWithPayload } from './public-types.js';
|
|
3
|
+
import './http-response.js';
|
|
4
|
+
import './responses/success.js';
|
|
5
|
+
import './responses/index.js';
|
|
6
|
+
import './responses/csv.js';
|
|
7
|
+
|
|
8
|
+
declare const defaultErrorMessageProvider: ErrorMessageProvider;
|
|
9
|
+
declare const toSimpleErrorPayload: (result: ErrorMessageResult) => Record<string, unknown>;
|
|
10
|
+
declare const toStructuredHttpErrorPayload: (error: ErrorWithPayload, errorDomain: string) => Aip193ErrorPayload;
|
|
11
|
+
declare const toStructuredGenericErrorPayload: (result: ErrorMessageResult, errorDomain: string) => Aip193ErrorPayload;
|
|
12
|
+
|
|
13
|
+
export { defaultErrorMessageProvider, toSimpleErrorPayload, toStructuredGenericErrorPayload, toStructuredHttpErrorPayload };
|
package/error-format.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
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_format_exports = {};
|
|
20
|
+
__export(error_format_exports, {
|
|
21
|
+
defaultErrorMessageProvider: () => defaultErrorMessageProvider,
|
|
22
|
+
toSimpleErrorPayload: () => toSimpleErrorPayload,
|
|
23
|
+
toStructuredGenericErrorPayload: () => toStructuredGenericErrorPayload,
|
|
24
|
+
toStructuredHttpErrorPayload: () => toStructuredHttpErrorPayload
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(error_format_exports);
|
|
27
|
+
var import_http_errors = require("@web-ts-toolkit/http-errors");
|
|
28
|
+
const isString = (value) => typeof value === "string";
|
|
29
|
+
const isPlainObject = (value) => value !== null && typeof value === "object";
|
|
30
|
+
const { isArray } = Array;
|
|
31
|
+
const toMetadata = (value) => {
|
|
32
|
+
if (!isPlainObject(value)) {
|
|
33
|
+
return void 0;
|
|
34
|
+
}
|
|
35
|
+
const metadata = Object.entries(value).reduce((result, [key, entry]) => {
|
|
36
|
+
result[key] = String(entry);
|
|
37
|
+
return result;
|
|
38
|
+
}, {});
|
|
39
|
+
return Object.keys(metadata).length > 0 ? metadata : void 0;
|
|
40
|
+
};
|
|
41
|
+
const toArray = (value) => {
|
|
42
|
+
if (value === void 0 || value === null) {
|
|
43
|
+
return void 0;
|
|
44
|
+
}
|
|
45
|
+
return isArray(value) ? value : [value];
|
|
46
|
+
};
|
|
47
|
+
const toHttpErrorShape = (error, fallbackDomain) => ({
|
|
48
|
+
statusCode: error.statusCode || 500,
|
|
49
|
+
status: error.status,
|
|
50
|
+
message: error.message || "",
|
|
51
|
+
reason: error.reason,
|
|
52
|
+
domain: error.domain || fallbackDomain,
|
|
53
|
+
metadata: toMetadata(error.metadata),
|
|
54
|
+
details: toArray(error.details),
|
|
55
|
+
errors: error.errors
|
|
56
|
+
});
|
|
57
|
+
const defaultErrorMessageProvider = function(error) {
|
|
58
|
+
const errorLike = error;
|
|
59
|
+
return errorLike.message || errorLike._message || String(error);
|
|
60
|
+
};
|
|
61
|
+
const toSimpleErrorPayload = (result) => isString(result) ? { message: result } : { ...result };
|
|
62
|
+
const toStructuredHttpErrorPayload = (error, errorDomain) => (0, import_http_errors.toAip193ErrorPayload)(toHttpErrorShape(error, errorDomain), errorDomain);
|
|
63
|
+
const toStructuredGenericErrorPayload = (result, errorDomain) => {
|
|
64
|
+
if (isPlainObject(result) && isPlainObject(result.error)) {
|
|
65
|
+
const error = result.error;
|
|
66
|
+
const statusCode = typeof error.code === "number" ? error.code : 422;
|
|
67
|
+
const status = typeof error.status === "string" ? error.status : (0, import_http_errors.getCanonicalStatus)(statusCode);
|
|
68
|
+
const message = typeof error.message === "string" ? error.message : "";
|
|
69
|
+
const details = toArray(error.details);
|
|
70
|
+
return {
|
|
71
|
+
error: {
|
|
72
|
+
code: statusCode,
|
|
73
|
+
status,
|
|
74
|
+
message,
|
|
75
|
+
...details ? { details } : {}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
if (isPlainObject(result)) {
|
|
80
|
+
const statusCode = typeof result.code === "number" ? result.code : 422;
|
|
81
|
+
const status = typeof result.status === "string" ? result.status : (0, import_http_errors.getCanonicalStatus)(statusCode);
|
|
82
|
+
const message = typeof result.message === "string" ? result.message : "";
|
|
83
|
+
const details = toArray(result.details) || [(0, import_http_errors.createAip193ErrorInfoDetail)(status, errorDomain)];
|
|
84
|
+
return {
|
|
85
|
+
error: {
|
|
86
|
+
code: statusCode,
|
|
87
|
+
status,
|
|
88
|
+
message,
|
|
89
|
+
...details ? { details } : {}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
error: {
|
|
95
|
+
code: 422,
|
|
96
|
+
status: (0, import_http_errors.getCanonicalStatus)(422),
|
|
97
|
+
message: String(result),
|
|
98
|
+
details: [(0, import_http_errors.createAip193ErrorInfoDetail)((0, import_http_errors.getCanonicalStatus)(422), errorDomain)]
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
103
|
+
0 && (module.exports = {
|
|
104
|
+
defaultErrorMessageProvider,
|
|
105
|
+
toSimpleErrorPayload,
|
|
106
|
+
toStructuredGenericErrorPayload,
|
|
107
|
+
toStructuredHttpErrorPayload
|
|
108
|
+
});
|