@web-ts-toolkit/express-response-handler 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -2
- package/README.md +58 -4
- 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
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 };
|
package/index.js
CHANGED
|
@@ -1,4 +1,65 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
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 index_exports = {};
|
|
20
|
+
__export(index_exports, {
|
|
21
|
+
Accepted: () => import_success.Accepted,
|
|
22
|
+
AlreadyReported: () => import_success.AlreadyReported,
|
|
23
|
+
CSVResponse: () => import_csv.CSVResponse,
|
|
24
|
+
Created: () => import_success.Created,
|
|
25
|
+
ErrorFormats: () => import_error_formats.ErrorFormats,
|
|
26
|
+
HttpResponse: () => import_http_response.HttpResponse,
|
|
27
|
+
IMUsed: () => import_success.IMUsed,
|
|
28
|
+
MultiStatus: () => import_success.MultiStatus,
|
|
29
|
+
NoContent: () => import_success.NoContent,
|
|
30
|
+
NonAuthoritativeInfo: () => import_success.NonAuthoritativeInfo,
|
|
31
|
+
OK: () => import_success.OK,
|
|
32
|
+
PartialContent: () => import_success.PartialContent,
|
|
33
|
+
ResetContent: () => import_success.ResetContent,
|
|
34
|
+
Response: () => import_responses.Response,
|
|
35
|
+
createHandler: () => import_create_handler2.createHandler,
|
|
36
|
+
default: () => index_default
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
var import_create_handler = require("./create-handler");
|
|
40
|
+
var import_create_handler2 = require("./create-handler");
|
|
41
|
+
var import_error_formats = require("./error-formats");
|
|
42
|
+
var import_http_response = require("./http-response");
|
|
43
|
+
var import_csv = require("./responses/csv");
|
|
44
|
+
var import_responses = require("./responses");
|
|
45
|
+
var import_success = require("./responses/success");
|
|
46
|
+
const apiHandler = (0, import_create_handler.createHandler)();
|
|
47
|
+
var index_default = apiHandler;
|
|
48
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
49
|
+
0 && (module.exports = {
|
|
50
|
+
Accepted,
|
|
51
|
+
AlreadyReported,
|
|
52
|
+
CSVResponse,
|
|
53
|
+
Created,
|
|
54
|
+
ErrorFormats,
|
|
55
|
+
HttpResponse,
|
|
56
|
+
IMUsed,
|
|
57
|
+
MultiStatus,
|
|
58
|
+
NoContent,
|
|
59
|
+
NonAuthoritativeInfo,
|
|
60
|
+
OK,
|
|
61
|
+
PartialContent,
|
|
62
|
+
ResetContent,
|
|
63
|
+
Response,
|
|
64
|
+
createHandler
|
|
65
|
+
});
|
package/index.mjs
CHANGED
|
@@ -1,11 +1,38 @@
|
|
|
1
|
+
import { createHandler } from "./create-handler";
|
|
2
|
+
const apiHandler = createHandler();
|
|
3
|
+
var index_default = apiHandler;
|
|
4
|
+
import { createHandler as createHandler2 } from "./create-handler";
|
|
5
|
+
import { ErrorFormats } from "./error-formats";
|
|
6
|
+
import { HttpResponse } from "./http-response";
|
|
7
|
+
import { CSVResponse } from "./responses/csv";
|
|
8
|
+
import { Response } from "./responses";
|
|
1
9
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
Accepted,
|
|
11
|
+
AlreadyReported,
|
|
12
|
+
Created,
|
|
13
|
+
IMUsed,
|
|
14
|
+
MultiStatus,
|
|
15
|
+
NoContent,
|
|
16
|
+
NonAuthoritativeInfo,
|
|
17
|
+
OK,
|
|
18
|
+
PartialContent,
|
|
19
|
+
ResetContent
|
|
20
|
+
} from "./responses/success";
|
|
21
|
+
export {
|
|
22
|
+
Accepted,
|
|
23
|
+
AlreadyReported,
|
|
24
|
+
CSVResponse,
|
|
25
|
+
Created,
|
|
26
|
+
ErrorFormats,
|
|
27
|
+
HttpResponse,
|
|
28
|
+
IMUsed,
|
|
29
|
+
MultiStatus,
|
|
30
|
+
NoContent,
|
|
31
|
+
NonAuthoritativeInfo,
|
|
32
|
+
OK,
|
|
33
|
+
PartialContent,
|
|
34
|
+
ResetContent,
|
|
35
|
+
Response,
|
|
36
|
+
createHandler2 as createHandler,
|
|
37
|
+
index_default as default
|
|
38
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@web-ts-toolkit/express-response-handler",
|
|
3
3
|
"description": "Express response handler",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"express",
|
|
7
7
|
"api",
|
|
@@ -45,9 +45,10 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@fast-csv/format": "^5.0.5",
|
|
48
|
-
"@web-ts-toolkit/http-errors": "0.
|
|
48
|
+
"@web-ts-toolkit/http-errors": "0.2.0",
|
|
49
|
+
"@web-ts-toolkit/utils": "0.2.0"
|
|
49
50
|
},
|
|
50
|
-
"author": "Junmin
|
|
51
|
+
"author": "Junmin Ahn",
|
|
51
52
|
"bugs": {
|
|
52
53
|
"url": "https://github.com/egose/web-ts-toolkit/issues"
|
|
53
54
|
},
|
package/public-types.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HttpResponseHelpers } from './http-response.mjs';
|
|
2
|
+
import { ErrorFormats } from './error-formats.mjs';
|
|
2
3
|
import '@web-ts-toolkit/http-errors';
|
|
3
4
|
import './responses/success.mjs';
|
|
4
5
|
import './responses/index.mjs';
|
|
@@ -6,7 +7,7 @@ import './responses/csv.mjs';
|
|
|
6
7
|
|
|
7
8
|
type ErrorMessageResult = string | Record<string, unknown>;
|
|
8
9
|
type ErrorMessageProvider = (error: unknown) => ErrorMessageResult;
|
|
9
|
-
type ErrorFormat =
|
|
10
|
+
type ErrorFormat = (typeof ErrorFormats)[keyof typeof ErrorFormats];
|
|
10
11
|
type MaybePromise<T> = T | Promise<T>;
|
|
11
12
|
type Hook = (value: unknown) => unknown;
|
|
12
13
|
type AsyncHook = (value: unknown) => Promise<unknown>;
|
|
@@ -14,6 +15,7 @@ type NextFunction = (error?: unknown) => void;
|
|
|
14
15
|
type ExpressResponseHandlerOptions = {
|
|
15
16
|
errorFormat?: ErrorFormat;
|
|
16
17
|
errorDomain?: string;
|
|
18
|
+
rfc9457ContentType?: 'application/problem+json' | 'application/json';
|
|
17
19
|
};
|
|
18
20
|
type ResponseLike = {
|
|
19
21
|
headersSent: boolean;
|
|
@@ -44,14 +46,17 @@ type ErrorWithPayload = {
|
|
|
44
46
|
domain?: string;
|
|
45
47
|
metadata?: unknown;
|
|
46
48
|
details?: unknown;
|
|
49
|
+
type?: string;
|
|
50
|
+
title?: string;
|
|
51
|
+
instance?: string;
|
|
47
52
|
};
|
|
48
|
-
type
|
|
53
|
+
type CreateHandler = (options?: ExpressResponseHandlerOptions) => ExpressResponseHandler;
|
|
49
54
|
type ExpressResponseHandler = {
|
|
50
55
|
handleResponse: HandleResponse;
|
|
51
56
|
handleResult: (res: ResponseLike, result: unknown, event: EventState) => void;
|
|
52
57
|
handlePromise: (res: ResponseLike, promise: Promise<unknown>, event: EventState) => void;
|
|
53
58
|
HttpResponse: HttpResponseHelpers;
|
|
54
|
-
|
|
59
|
+
createHandler: CreateHandler;
|
|
55
60
|
errorMessageProvider: ErrorMessageProvider;
|
|
56
61
|
preJson: Hook | null;
|
|
57
62
|
postJson: Hook | null;
|
|
@@ -59,4 +64,4 @@ type ExpressResponseHandler = {
|
|
|
59
64
|
postError: Hook | null;
|
|
60
65
|
};
|
|
61
66
|
|
|
62
|
-
export { type AsyncHook, type ErrorFormat, type ErrorMessageProvider, type ErrorMessageResult, type ErrorWithPayload, type EventState, type ExpressResponseHandler, type
|
|
67
|
+
export { type AsyncHook, type CreateHandler, type ErrorFormat, ErrorFormats, type ErrorMessageProvider, type ErrorMessageResult, type ErrorWithPayload, type EventState, type ExpressResponseHandler, type ExpressResponseHandlerOptions, type HandleResponse, type Hook, HttpResponseHelpers, type MaybePromise, type MiddlewareFunction, type NextFunction, type ResponseLike, type RouterFunction };
|
package/public-types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HttpResponseHelpers } from './http-response.js';
|
|
2
|
+
import { ErrorFormats } from './error-formats.js';
|
|
2
3
|
import '@web-ts-toolkit/http-errors';
|
|
3
4
|
import './responses/success.js';
|
|
4
5
|
import './responses/index.js';
|
|
@@ -6,7 +7,7 @@ import './responses/csv.js';
|
|
|
6
7
|
|
|
7
8
|
type ErrorMessageResult = string | Record<string, unknown>;
|
|
8
9
|
type ErrorMessageProvider = (error: unknown) => ErrorMessageResult;
|
|
9
|
-
type ErrorFormat =
|
|
10
|
+
type ErrorFormat = (typeof ErrorFormats)[keyof typeof ErrorFormats];
|
|
10
11
|
type MaybePromise<T> = T | Promise<T>;
|
|
11
12
|
type Hook = (value: unknown) => unknown;
|
|
12
13
|
type AsyncHook = (value: unknown) => Promise<unknown>;
|
|
@@ -14,6 +15,7 @@ type NextFunction = (error?: unknown) => void;
|
|
|
14
15
|
type ExpressResponseHandlerOptions = {
|
|
15
16
|
errorFormat?: ErrorFormat;
|
|
16
17
|
errorDomain?: string;
|
|
18
|
+
rfc9457ContentType?: 'application/problem+json' | 'application/json';
|
|
17
19
|
};
|
|
18
20
|
type ResponseLike = {
|
|
19
21
|
headersSent: boolean;
|
|
@@ -44,14 +46,17 @@ type ErrorWithPayload = {
|
|
|
44
46
|
domain?: string;
|
|
45
47
|
metadata?: unknown;
|
|
46
48
|
details?: unknown;
|
|
49
|
+
type?: string;
|
|
50
|
+
title?: string;
|
|
51
|
+
instance?: string;
|
|
47
52
|
};
|
|
48
|
-
type
|
|
53
|
+
type CreateHandler = (options?: ExpressResponseHandlerOptions) => ExpressResponseHandler;
|
|
49
54
|
type ExpressResponseHandler = {
|
|
50
55
|
handleResponse: HandleResponse;
|
|
51
56
|
handleResult: (res: ResponseLike, result: unknown, event: EventState) => void;
|
|
52
57
|
handlePromise: (res: ResponseLike, promise: Promise<unknown>, event: EventState) => void;
|
|
53
58
|
HttpResponse: HttpResponseHelpers;
|
|
54
|
-
|
|
59
|
+
createHandler: CreateHandler;
|
|
55
60
|
errorMessageProvider: ErrorMessageProvider;
|
|
56
61
|
preJson: Hook | null;
|
|
57
62
|
postJson: Hook | null;
|
|
@@ -59,4 +64,4 @@ type ExpressResponseHandler = {
|
|
|
59
64
|
postError: Hook | null;
|
|
60
65
|
};
|
|
61
66
|
|
|
62
|
-
export { type AsyncHook, type ErrorFormat, type ErrorMessageProvider, type ErrorMessageResult, type ErrorWithPayload, type EventState, type ExpressResponseHandler, type
|
|
67
|
+
export { type AsyncHook, type CreateHandler, type ErrorFormat, ErrorFormats, type ErrorMessageProvider, type ErrorMessageResult, type ErrorWithPayload, type EventState, type ExpressResponseHandler, type ExpressResponseHandlerOptions, type HandleResponse, type Hook, HttpResponseHelpers, type MaybePromise, type MiddlewareFunction, type NextFunction, type ResponseLike, type RouterFunction };
|
package/public-types.js
CHANGED
|
@@ -3,6 +3,10 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
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
|
+
};
|
|
6
10
|
var __copyProps = (to, from, except, desc) => {
|
|
7
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
12
|
for (let key of __getOwnPropNames(from))
|
|
@@ -13,4 +17,12 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
13
17
|
};
|
|
14
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
19
|
var public_types_exports = {};
|
|
20
|
+
__export(public_types_exports, {
|
|
21
|
+
ErrorFormats: () => import_error_formats.ErrorFormats
|
|
22
|
+
});
|
|
16
23
|
module.exports = __toCommonJS(public_types_exports);
|
|
24
|
+
var import_error_formats = require("./error-formats");
|
|
25
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
26
|
+
0 && (module.exports = {
|
|
27
|
+
ErrorFormats
|
|
28
|
+
});
|
package/public-types.mjs
CHANGED
package/responses/csv.js
CHANGED
|
@@ -22,17 +22,16 @@ __export(csv_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(csv_exports);
|
|
24
24
|
var import_format = require("@fast-csv/format");
|
|
25
|
-
|
|
26
|
-
const isPlainObject = (value) => value !== null && typeof value === "object" && value.constructor === Object;
|
|
25
|
+
var import_utils = require("@web-ts-toolkit/utils");
|
|
27
26
|
class CSVResponse {
|
|
28
27
|
constructor(dataset = [], options = {}) {
|
|
29
|
-
this.dataset =
|
|
28
|
+
this.dataset = (0, import_utils.castArray)(dataset);
|
|
30
29
|
this.filename = options.filename || "download.csv";
|
|
31
30
|
this.processor = options.processor || ((value) => value);
|
|
32
|
-
if (isBoolean(options.headers)) {
|
|
31
|
+
if ((0, import_utils.isBoolean)(options.headers)) {
|
|
33
32
|
this.headers = options.headers;
|
|
34
33
|
} else if (this.dataset.length > 0) {
|
|
35
|
-
this.headers = isPlainObject(this.dataset[0]);
|
|
34
|
+
this.headers = (0, import_utils.isPlainObject)(this.dataset[0]);
|
|
36
35
|
}
|
|
37
36
|
}
|
|
38
37
|
streamCsv(res) {
|