@web-ts-toolkit/express-response-handler 0.30.0 → 0.31.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/create-handler.js +312 -39
- package/create-handler.mjs +322 -24
- package/error-format.js +14 -12
- package/error-format.mjs +13 -12
- package/error-formats.js +3 -1
- package/error-formats.mjs +2 -1
- package/http-response.js +117 -15
- package/http-response.mjs +102 -14
- package/index.js +603 -23
- package/index.mjs +609 -20
- package/package.json +3 -3
- package/public-types.js +10 -2
- package/public-types.mjs +6 -1
- package/responses/csv.d.mts +4 -1
- package/responses/csv.d.ts +4 -1
- package/responses/csv.js +24 -4
- package/responses/csv.mjs +20 -3
- package/responses/index.d.mts +4 -1
- package/responses/index.d.ts +4 -1
- package/responses/index.js +24 -4
- package/responses/index.mjs +20 -3
- package/responses/success.js +36 -21
- package/responses/success.mjs +33 -21
package/create-handler.mjs
CHANGED
|
@@ -1,29 +1,327 @@
|
|
|
1
|
+
// src/create-handler.ts
|
|
1
2
|
import assert from "assert";
|
|
2
|
-
import { isArray, isFunction, isPromise } from "@web-ts-toolkit/utils";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import {
|
|
3
|
+
import { isArray as isArray2, isFunction, isPromise } from "@web-ts-toolkit/utils";
|
|
4
|
+
|
|
5
|
+
// src/responses/csv.ts
|
|
6
|
+
import { format } from "@fast-csv/format";
|
|
7
|
+
import { castArray, isBoolean, isPlainObject } from "@web-ts-toolkit/utils";
|
|
8
|
+
var csvResponseBrand = /* @__PURE__ */ Symbol.for("@web-ts-toolkit/express-response-handler.csv-response");
|
|
9
|
+
var isCSVResponse = (value) => {
|
|
10
|
+
if (value instanceof CSVResponse) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
if (typeof value !== "object" || value === null) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
const candidate = value;
|
|
17
|
+
return candidate[csvResponseBrand] === true && typeof candidate.streamCsv === "function";
|
|
18
|
+
};
|
|
19
|
+
var _a;
|
|
20
|
+
_a = csvResponseBrand;
|
|
21
|
+
var CSVResponse = class {
|
|
22
|
+
constructor(dataset = [], options = {}) {
|
|
23
|
+
this[_a] = true;
|
|
24
|
+
this.dataset = castArray(dataset);
|
|
25
|
+
this.filename = options.filename || "download.csv";
|
|
26
|
+
this.processor = options.processor || ((value) => value);
|
|
27
|
+
if (isBoolean(options.headers)) {
|
|
28
|
+
this.headers = options.headers;
|
|
29
|
+
} else if (this.dataset.length > 0) {
|
|
30
|
+
this.headers = isPlainObject(this.dataset[0]);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
streamCsv(res) {
|
|
34
|
+
const stream = format({ headers: this.headers });
|
|
35
|
+
res.set("Content-Type", "text/csv");
|
|
36
|
+
res.set("Content-Disposition", `attachment;filename=${this.filename}`);
|
|
37
|
+
stream.pipe(res);
|
|
38
|
+
stream.on("error", (error) => {
|
|
39
|
+
if (res.destroy) {
|
|
40
|
+
res.destroy(error);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
res.end();
|
|
44
|
+
});
|
|
45
|
+
stream.on("end", () => res.end());
|
|
46
|
+
this.dataset.forEach((value) => stream.write(this.processor(value)));
|
|
47
|
+
stream.end();
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// src/responses/index.ts
|
|
52
|
+
var responseBrand = /* @__PURE__ */ Symbol.for("@web-ts-toolkit/express-response-handler.response");
|
|
53
|
+
var isResponse = (value) => {
|
|
54
|
+
if (value instanceof Response) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
if (typeof value !== "object" || value === null) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const candidate = value;
|
|
61
|
+
return candidate[responseBrand] === true && typeof candidate.statusCode === "number" && "data" in candidate;
|
|
62
|
+
};
|
|
63
|
+
var _a2;
|
|
64
|
+
_a2 = responseBrand;
|
|
65
|
+
var Response = class {
|
|
66
|
+
constructor(statusCode = 200, data) {
|
|
67
|
+
this[_a2] = true;
|
|
68
|
+
this.statusCode = statusCode;
|
|
69
|
+
this.data = data;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// src/responses/success.ts
|
|
74
|
+
var OK = class extends Response {
|
|
75
|
+
constructor(data) {
|
|
76
|
+
super(200, data);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
var Created = class extends Response {
|
|
80
|
+
constructor(data) {
|
|
81
|
+
super(201, data);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
var Accepted = class extends Response {
|
|
85
|
+
constructor(data) {
|
|
86
|
+
super(202, data);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
var NonAuthoritativeInfo = class extends Response {
|
|
90
|
+
constructor(data) {
|
|
91
|
+
super(203, data);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
var NoContent = class extends Response {
|
|
95
|
+
constructor() {
|
|
96
|
+
super(204, void 0);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
var ResetContent = class extends Response {
|
|
100
|
+
constructor(data) {
|
|
101
|
+
super(205, data);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
var PartialContent = class extends Response {
|
|
105
|
+
constructor(data) {
|
|
106
|
+
super(206, data);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
var MultiStatus = class extends Response {
|
|
110
|
+
constructor(data) {
|
|
111
|
+
super(207, data);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
var AlreadyReported = class extends Response {
|
|
115
|
+
constructor(data) {
|
|
116
|
+
super(208, data);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
var IMUsed = class extends Response {
|
|
120
|
+
constructor(data) {
|
|
121
|
+
super(226, data);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// src/http-response.ts
|
|
126
|
+
import {
|
|
127
|
+
BadRequestError,
|
|
128
|
+
ConflictError,
|
|
129
|
+
ExpectationFailedError,
|
|
130
|
+
FailedDependencyError,
|
|
131
|
+
ForbiddenError,
|
|
132
|
+
GoneError,
|
|
133
|
+
LengthRequiredError,
|
|
134
|
+
LockedError,
|
|
135
|
+
MethodNotAllowedError,
|
|
136
|
+
MisdirectedRequestError,
|
|
137
|
+
NotAcceptableError,
|
|
138
|
+
NotFoundError,
|
|
139
|
+
PayloadTooLargeError,
|
|
140
|
+
PreconditionFailedError,
|
|
141
|
+
PreconditionRequiredError,
|
|
142
|
+
ProxyAuthRequiredError,
|
|
143
|
+
RequestHeaderFieldsTooLargeError,
|
|
144
|
+
RequestedRangeNotSatisfiableError,
|
|
145
|
+
RequestTimeoutError,
|
|
146
|
+
TeapotError,
|
|
147
|
+
TooManyRequestsError,
|
|
148
|
+
UnauthorizedError,
|
|
149
|
+
UnavailableForLegalReasonsError,
|
|
150
|
+
UnprocessableEntityError,
|
|
151
|
+
UnsupportedMediaTypeError,
|
|
152
|
+
UpgradeRequiredError,
|
|
153
|
+
UriTooLongError
|
|
154
|
+
} from "@web-ts-toolkit/http-errors";
|
|
155
|
+
var HttpResponse = {
|
|
156
|
+
ok: (...args) => new OK(...args),
|
|
157
|
+
created: (...args) => new Created(...args),
|
|
158
|
+
accepted: (...args) => new Accepted(...args),
|
|
159
|
+
nonAuthoritativeInfo: (...args) => new NonAuthoritativeInfo(...args),
|
|
160
|
+
noContent: (...args) => new NoContent(...args),
|
|
161
|
+
resetContent: (...args) => new ResetContent(...args),
|
|
162
|
+
partialContent: (...args) => new PartialContent(...args),
|
|
163
|
+
multiStatus: (...args) => new MultiStatus(...args),
|
|
164
|
+
alreadyReported: (...args) => new AlreadyReported(...args),
|
|
165
|
+
imUsed: (...args) => new IMUsed(...args),
|
|
166
|
+
badRequest: (...args) => new BadRequestError(...args),
|
|
167
|
+
unauthorized: (...args) => new UnauthorizedError(...args),
|
|
168
|
+
forbidden: (...args) => new ForbiddenError(...args),
|
|
169
|
+
notFound: (...args) => new NotFoundError(...args),
|
|
170
|
+
methodNotAllowed: (...args) => new MethodNotAllowedError(...args),
|
|
171
|
+
notAcceptable: (...args) => new NotAcceptableError(...args),
|
|
172
|
+
proxyAuthRequired: (...args) => new ProxyAuthRequiredError(...args),
|
|
173
|
+
requestTimeout: (...args) => new RequestTimeoutError(...args),
|
|
174
|
+
conflict: (...args) => new ConflictError(...args),
|
|
175
|
+
gone: (...args) => new GoneError(...args),
|
|
176
|
+
lengthRequired: (...args) => new LengthRequiredError(...args),
|
|
177
|
+
preconditionFailed: (...args) => new PreconditionFailedError(...args),
|
|
178
|
+
payloadTooLarge: (...args) => new PayloadTooLargeError(...args),
|
|
179
|
+
uriTooLong: (...args) => new UriTooLongError(...args),
|
|
180
|
+
unsupportedMediaType: (...args) => new UnsupportedMediaTypeError(...args),
|
|
181
|
+
requestedRangeNotSatisfiable: (...args) => new RequestedRangeNotSatisfiableError(...args),
|
|
182
|
+
expectationFailed: (...args) => new ExpectationFailedError(...args),
|
|
183
|
+
teapot: (...args) => new TeapotError(...args),
|
|
184
|
+
misdirectedRequest: (...args) => new MisdirectedRequestError(...args),
|
|
185
|
+
unprocessableEntity: (...args) => new UnprocessableEntityError(...args),
|
|
186
|
+
locked: (...args) => new LockedError(...args),
|
|
187
|
+
failedDependency: (...args) => new FailedDependencyError(...args),
|
|
188
|
+
upgradeRequired: (...args) => new UpgradeRequiredError(...args),
|
|
189
|
+
preconditionRequired: (...args) => new PreconditionRequiredError(...args),
|
|
190
|
+
tooManyRequests: (...args) => new TooManyRequestsError(...args),
|
|
191
|
+
requestHeaderFieldsTooLarge: (...args) => new RequestHeaderFieldsTooLargeError(...args),
|
|
192
|
+
unavailableForLegalReasons: (...args) => new UnavailableForLegalReasonsError(...args),
|
|
193
|
+
json: (...args) => new OK(...args),
|
|
194
|
+
csv: (...args) => new CSVResponse(...args)
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// src/error-format.ts
|
|
6
198
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
199
|
+
createAip193ErrorInfoDetail,
|
|
200
|
+
getCanonicalStatus,
|
|
201
|
+
toAip193ErrorPayload,
|
|
202
|
+
toRfc9457ErrorPayload
|
|
203
|
+
} from "@web-ts-toolkit/http-errors";
|
|
204
|
+
import { isArray, isPlainObject as isPlainObject2, isString, toStringRecord } from "@web-ts-toolkit/utils";
|
|
205
|
+
var toStatusCode = (status, code, fallbackStatusCode) => {
|
|
206
|
+
if (typeof status === "number") {
|
|
207
|
+
return status;
|
|
208
|
+
}
|
|
209
|
+
return typeof code === "number" ? code : fallbackStatusCode;
|
|
210
|
+
};
|
|
211
|
+
var toOptionalString = (value) => typeof value === "string" ? value : void 0;
|
|
212
|
+
var toMetadata = (value) => {
|
|
213
|
+
return toStringRecord(value);
|
|
214
|
+
};
|
|
215
|
+
var toArray = (value) => {
|
|
216
|
+
if (value === void 0 || value === null) {
|
|
217
|
+
return void 0;
|
|
218
|
+
}
|
|
219
|
+
return isArray(value) ? value : [value];
|
|
220
|
+
};
|
|
221
|
+
var toHttpErrorShape = (error, fallbackDomain) => ({
|
|
222
|
+
statusCode: error.statusCode ?? 500,
|
|
223
|
+
status: error.status,
|
|
224
|
+
message: error.message ?? "",
|
|
225
|
+
reason: error.reason,
|
|
226
|
+
domain: error.domain ?? fallbackDomain,
|
|
227
|
+
metadata: toMetadata(error.metadata),
|
|
228
|
+
details: toArray(error.details),
|
|
229
|
+
errors: error.errors,
|
|
230
|
+
type: error.type,
|
|
231
|
+
title: error.title,
|
|
232
|
+
instance: error.instance
|
|
233
|
+
});
|
|
234
|
+
var toProblemDetailsSource = (result) => {
|
|
235
|
+
if (isPlainObject2(result) && isPlainObject2(result.error)) {
|
|
236
|
+
return result.error;
|
|
237
|
+
}
|
|
238
|
+
return isPlainObject2(result) ? result : void 0;
|
|
239
|
+
};
|
|
240
|
+
var defaultErrorMessageProvider = function(error) {
|
|
241
|
+
const errorLike = error;
|
|
242
|
+
return errorLike.message ?? errorLike._message ?? String(error);
|
|
243
|
+
};
|
|
244
|
+
var toSimpleErrorPayload = (result) => isString(result) ? { message: result } : { ...result };
|
|
245
|
+
var toStructuredHttpErrorPayload = (error, errorDomain) => toAip193ErrorPayload(toHttpErrorShape(error, errorDomain), errorDomain);
|
|
246
|
+
var toRfc9457HttpErrorPayload = (error, errorDomain) => toRfc9457ErrorPayload(toHttpErrorShape(error, errorDomain));
|
|
247
|
+
var toStructuredGenericErrorPayload = (result, errorDomain) => {
|
|
248
|
+
if (isPlainObject2(result) && isPlainObject2(result.error)) {
|
|
249
|
+
const error = result.error;
|
|
250
|
+
const statusCode = typeof error.code === "number" ? error.code : 422;
|
|
251
|
+
const status = typeof error.status === "string" ? error.status : getCanonicalStatus(statusCode);
|
|
252
|
+
const message = typeof error.message === "string" ? error.message : "";
|
|
253
|
+
const details = toArray(error.details);
|
|
254
|
+
return {
|
|
255
|
+
error: {
|
|
256
|
+
code: statusCode,
|
|
257
|
+
status,
|
|
258
|
+
message,
|
|
259
|
+
...details ? { details } : {}
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
if (isPlainObject2(result)) {
|
|
264
|
+
const statusCode = typeof result.code === "number" ? result.code : 422;
|
|
265
|
+
const status = typeof result.status === "string" ? result.status : getCanonicalStatus(statusCode);
|
|
266
|
+
const message = typeof result.message === "string" ? result.message : "";
|
|
267
|
+
const details = toArray(result.details) || [createAip193ErrorInfoDetail(status, errorDomain)];
|
|
268
|
+
return {
|
|
269
|
+
error: {
|
|
270
|
+
code: statusCode,
|
|
271
|
+
status,
|
|
272
|
+
message,
|
|
273
|
+
...details ? { details } : {}
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
return {
|
|
278
|
+
error: {
|
|
279
|
+
code: 422,
|
|
280
|
+
status: getCanonicalStatus(422),
|
|
281
|
+
message: String(result),
|
|
282
|
+
details: [createAip193ErrorInfoDetail(getCanonicalStatus(422), errorDomain)]
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
};
|
|
286
|
+
var toRfc9457GenericErrorPayload = (result) => {
|
|
287
|
+
const problem = toProblemDetailsSource(result);
|
|
288
|
+
if (problem) {
|
|
289
|
+
const statusCode = toStatusCode(problem.status, problem.code, 422);
|
|
290
|
+
return toRfc9457ErrorPayload({
|
|
291
|
+
statusCode,
|
|
292
|
+
message: toOptionalString(problem.detail) ?? toOptionalString(problem.message) ?? "",
|
|
293
|
+
errors: problem.errors,
|
|
294
|
+
type: toOptionalString(problem.type),
|
|
295
|
+
title: toOptionalString(problem.title),
|
|
296
|
+
instance: toOptionalString(problem.instance)
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
return toRfc9457ErrorPayload({
|
|
300
|
+
statusCode: 422,
|
|
301
|
+
message: String(result)
|
|
302
|
+
});
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
// src/error-formats.ts
|
|
306
|
+
var ErrorFormats = {
|
|
307
|
+
simple: "simple",
|
|
308
|
+
aip193: "aip193",
|
|
309
|
+
rfc9457: "rfc9457"
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
// src/create-handler.ts
|
|
313
|
+
var promisify = (fn) => (value) => Promise.resolve().then(() => fn(value));
|
|
314
|
+
var invokePostHook = (hook, value) => {
|
|
17
315
|
void hook(value).catch(() => void 0);
|
|
18
316
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
317
|
+
var RFC_9457_CONTENT_TYPE = "application/problem+json";
|
|
318
|
+
var shouldSkipResponse = (res, event) => res.headersSent || event.canceled;
|
|
319
|
+
var sendProblemJson = (res, statusCode, payload, contentType) => {
|
|
22
320
|
res.status(statusCode);
|
|
23
321
|
res.set("Content-Type", contentType);
|
|
24
322
|
res.send(payload);
|
|
25
323
|
};
|
|
26
|
-
|
|
324
|
+
var sendHttpErrorByFormat = {
|
|
27
325
|
[ErrorFormats.simple]: (res, error) => {
|
|
28
326
|
const payload = { message: error.message ?? "" };
|
|
29
327
|
if (error.errors !== void 0) {
|
|
@@ -38,7 +336,7 @@ const sendHttpErrorByFormat = {
|
|
|
38
336
|
sendProblemJson(res, error.statusCode ?? 500, toRfc9457HttpErrorPayload(error, domain), RFC_9457_CONTENT_TYPE);
|
|
39
337
|
}
|
|
40
338
|
};
|
|
41
|
-
|
|
339
|
+
var sendGenericErrorByFormat = {
|
|
42
340
|
[ErrorFormats.simple]: (res, result) => {
|
|
43
341
|
res.status(422).send(toSimpleErrorPayload(result));
|
|
44
342
|
},
|
|
@@ -51,16 +349,16 @@ const sendGenericErrorByFormat = {
|
|
|
51
349
|
sendProblemJson(res, payload.status ?? 422, payload, RFC_9457_CONTENT_TYPE);
|
|
52
350
|
}
|
|
53
351
|
};
|
|
54
|
-
|
|
352
|
+
var assertMiddleware = (fn) => {
|
|
55
353
|
assert.ok(isFunction(fn), "middleware handler must be a function");
|
|
56
354
|
};
|
|
57
|
-
|
|
355
|
+
var normalizeMiddlewareList = (fns) => {
|
|
58
356
|
assert.ok(fns.length > 0, "at least one middleware handler is required");
|
|
59
357
|
if (fns.length > 1) {
|
|
60
358
|
fns.forEach(assertMiddleware);
|
|
61
359
|
return fns;
|
|
62
360
|
}
|
|
63
|
-
if (
|
|
361
|
+
if (isArray2(fns[0])) {
|
|
64
362
|
assert.ok(fns[0].length > 0, "at least one middleware handler is required");
|
|
65
363
|
fns[0].forEach(assertMiddleware);
|
|
66
364
|
return fns[0];
|
|
@@ -98,11 +396,11 @@ function createHandler(options = {}) {
|
|
|
98
396
|
if (data === void 0) {
|
|
99
397
|
return;
|
|
100
398
|
}
|
|
101
|
-
if (data
|
|
399
|
+
if (isResponse(data)) {
|
|
102
400
|
res.status(data.statusCode).json(data.data);
|
|
103
401
|
return;
|
|
104
402
|
}
|
|
105
|
-
if (data
|
|
403
|
+
if (isCSVResponse(data)) {
|
|
106
404
|
data.streamCsv(res);
|
|
107
405
|
return;
|
|
108
406
|
}
|
package/error-format.js
CHANGED
|
@@ -16,6 +16,8 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/error-format.ts
|
|
19
21
|
var error_format_exports = {};
|
|
20
22
|
__export(error_format_exports, {
|
|
21
23
|
defaultErrorMessageProvider: () => defaultErrorMessageProvider,
|
|
@@ -28,23 +30,23 @@ __export(error_format_exports, {
|
|
|
28
30
|
module.exports = __toCommonJS(error_format_exports);
|
|
29
31
|
var import_http_errors = require("@web-ts-toolkit/http-errors");
|
|
30
32
|
var import_utils = require("@web-ts-toolkit/utils");
|
|
31
|
-
|
|
33
|
+
var toStatusCode = (status, code, fallbackStatusCode) => {
|
|
32
34
|
if (typeof status === "number") {
|
|
33
35
|
return status;
|
|
34
36
|
}
|
|
35
37
|
return typeof code === "number" ? code : fallbackStatusCode;
|
|
36
38
|
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
var toOptionalString = (value) => typeof value === "string" ? value : void 0;
|
|
40
|
+
var toMetadata = (value) => {
|
|
39
41
|
return (0, import_utils.toStringRecord)(value);
|
|
40
42
|
};
|
|
41
|
-
|
|
43
|
+
var toArray = (value) => {
|
|
42
44
|
if (value === void 0 || value === null) {
|
|
43
45
|
return void 0;
|
|
44
46
|
}
|
|
45
47
|
return (0, import_utils.isArray)(value) ? value : [value];
|
|
46
48
|
};
|
|
47
|
-
|
|
49
|
+
var toHttpErrorShape = (error, fallbackDomain) => ({
|
|
48
50
|
statusCode: error.statusCode ?? 500,
|
|
49
51
|
status: error.status,
|
|
50
52
|
message: error.message ?? "",
|
|
@@ -57,20 +59,20 @@ const toHttpErrorShape = (error, fallbackDomain) => ({
|
|
|
57
59
|
title: error.title,
|
|
58
60
|
instance: error.instance
|
|
59
61
|
});
|
|
60
|
-
|
|
62
|
+
var toProblemDetailsSource = (result) => {
|
|
61
63
|
if ((0, import_utils.isPlainObject)(result) && (0, import_utils.isPlainObject)(result.error)) {
|
|
62
64
|
return result.error;
|
|
63
65
|
}
|
|
64
66
|
return (0, import_utils.isPlainObject)(result) ? result : void 0;
|
|
65
67
|
};
|
|
66
|
-
|
|
68
|
+
var defaultErrorMessageProvider = function(error) {
|
|
67
69
|
const errorLike = error;
|
|
68
70
|
return errorLike.message ?? errorLike._message ?? String(error);
|
|
69
71
|
};
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
var toSimpleErrorPayload = (result) => (0, import_utils.isString)(result) ? { message: result } : { ...result };
|
|
73
|
+
var toStructuredHttpErrorPayload = (error, errorDomain) => (0, import_http_errors.toAip193ErrorPayload)(toHttpErrorShape(error, errorDomain), errorDomain);
|
|
74
|
+
var toRfc9457HttpErrorPayload = (error, errorDomain) => (0, import_http_errors.toRfc9457ErrorPayload)(toHttpErrorShape(error, errorDomain));
|
|
75
|
+
var toStructuredGenericErrorPayload = (result, errorDomain) => {
|
|
74
76
|
if ((0, import_utils.isPlainObject)(result) && (0, import_utils.isPlainObject)(result.error)) {
|
|
75
77
|
const error = result.error;
|
|
76
78
|
const statusCode = typeof error.code === "number" ? error.code : 422;
|
|
@@ -109,7 +111,7 @@ const toStructuredGenericErrorPayload = (result, errorDomain) => {
|
|
|
109
111
|
}
|
|
110
112
|
};
|
|
111
113
|
};
|
|
112
|
-
|
|
114
|
+
var toRfc9457GenericErrorPayload = (result) => {
|
|
113
115
|
const problem = toProblemDetailsSource(result);
|
|
114
116
|
if (problem) {
|
|
115
117
|
const statusCode = toStatusCode(problem.status, problem.code, 422);
|
package/error-format.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// src/error-format.ts
|
|
1
2
|
import {
|
|
2
3
|
createAip193ErrorInfoDetail,
|
|
3
4
|
getCanonicalStatus,
|
|
@@ -5,23 +6,23 @@ import {
|
|
|
5
6
|
toRfc9457ErrorPayload
|
|
6
7
|
} from "@web-ts-toolkit/http-errors";
|
|
7
8
|
import { isArray, isPlainObject, isString, toStringRecord } from "@web-ts-toolkit/utils";
|
|
8
|
-
|
|
9
|
+
var toStatusCode = (status, code, fallbackStatusCode) => {
|
|
9
10
|
if (typeof status === "number") {
|
|
10
11
|
return status;
|
|
11
12
|
}
|
|
12
13
|
return typeof code === "number" ? code : fallbackStatusCode;
|
|
13
14
|
};
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
var toOptionalString = (value) => typeof value === "string" ? value : void 0;
|
|
16
|
+
var toMetadata = (value) => {
|
|
16
17
|
return toStringRecord(value);
|
|
17
18
|
};
|
|
18
|
-
|
|
19
|
+
var toArray = (value) => {
|
|
19
20
|
if (value === void 0 || value === null) {
|
|
20
21
|
return void 0;
|
|
21
22
|
}
|
|
22
23
|
return isArray(value) ? value : [value];
|
|
23
24
|
};
|
|
24
|
-
|
|
25
|
+
var toHttpErrorShape = (error, fallbackDomain) => ({
|
|
25
26
|
statusCode: error.statusCode ?? 500,
|
|
26
27
|
status: error.status,
|
|
27
28
|
message: error.message ?? "",
|
|
@@ -34,20 +35,20 @@ const toHttpErrorShape = (error, fallbackDomain) => ({
|
|
|
34
35
|
title: error.title,
|
|
35
36
|
instance: error.instance
|
|
36
37
|
});
|
|
37
|
-
|
|
38
|
+
var toProblemDetailsSource = (result) => {
|
|
38
39
|
if (isPlainObject(result) && isPlainObject(result.error)) {
|
|
39
40
|
return result.error;
|
|
40
41
|
}
|
|
41
42
|
return isPlainObject(result) ? result : void 0;
|
|
42
43
|
};
|
|
43
|
-
|
|
44
|
+
var defaultErrorMessageProvider = function(error) {
|
|
44
45
|
const errorLike = error;
|
|
45
46
|
return errorLike.message ?? errorLike._message ?? String(error);
|
|
46
47
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
var toSimpleErrorPayload = (result) => isString(result) ? { message: result } : { ...result };
|
|
49
|
+
var toStructuredHttpErrorPayload = (error, errorDomain) => toAip193ErrorPayload(toHttpErrorShape(error, errorDomain), errorDomain);
|
|
50
|
+
var toRfc9457HttpErrorPayload = (error, errorDomain) => toRfc9457ErrorPayload(toHttpErrorShape(error, errorDomain));
|
|
51
|
+
var toStructuredGenericErrorPayload = (result, errorDomain) => {
|
|
51
52
|
if (isPlainObject(result) && isPlainObject(result.error)) {
|
|
52
53
|
const error = result.error;
|
|
53
54
|
const statusCode = typeof error.code === "number" ? error.code : 422;
|
|
@@ -86,7 +87,7 @@ const toStructuredGenericErrorPayload = (result, errorDomain) => {
|
|
|
86
87
|
}
|
|
87
88
|
};
|
|
88
89
|
};
|
|
89
|
-
|
|
90
|
+
var toRfc9457GenericErrorPayload = (result) => {
|
|
90
91
|
const problem = toProblemDetailsSource(result);
|
|
91
92
|
if (problem) {
|
|
92
93
|
const statusCode = toStatusCode(problem.status, problem.code, 422);
|
package/error-formats.js
CHANGED
|
@@ -16,12 +16,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/error-formats.ts
|
|
19
21
|
var error_formats_exports = {};
|
|
20
22
|
__export(error_formats_exports, {
|
|
21
23
|
ErrorFormats: () => ErrorFormats
|
|
22
24
|
});
|
|
23
25
|
module.exports = __toCommonJS(error_formats_exports);
|
|
24
|
-
|
|
26
|
+
var ErrorFormats = {
|
|
25
27
|
simple: "simple",
|
|
26
28
|
aip193: "aip193",
|
|
27
29
|
rfc9457: "rfc9457"
|
package/error-formats.mjs
CHANGED