@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
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.3.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.3.0",
|
|
49
|
+
"@web-ts-toolkit/utils": "0.3.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) {
|
package/responses/csv.mjs
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import "../chunk-PQJP2ZCI.mjs";
|
|
2
1
|
import { format } from "@fast-csv/format";
|
|
3
|
-
|
|
4
|
-
const isPlainObject = (value) => value !== null && typeof value === "object" && value.constructor === Object;
|
|
2
|
+
import { castArray, isBoolean, isPlainObject } from "@web-ts-toolkit/utils";
|
|
5
3
|
class CSVResponse {
|
|
6
4
|
constructor(dataset = [], options = {}) {
|
|
7
|
-
this.dataset =
|
|
5
|
+
this.dataset = castArray(dataset);
|
|
8
6
|
this.filename = options.filename || "download.csv";
|
|
9
7
|
this.processor = options.processor || ((value) => value);
|
|
10
8
|
if (isBoolean(options.headers)) {
|
package/responses/index.mjs
CHANGED
package/responses/success.mjs
CHANGED
package/chunk-PQJP2ZCI.mjs
DELETED