@web-ts-toolkit/express-response-handler 0.31.0 → 0.31.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/http-response.js CHANGED
@@ -16,25 +16,127 @@ 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/http-response.ts
19
21
  var http_response_exports = {};
20
22
  __export(http_response_exports, {
21
23
  HttpResponse: () => HttpResponse
22
24
  });
23
25
  module.exports = __toCommonJS(http_response_exports);
24
- var import_success = require("./responses/success");
25
- var import_csv = require("./responses/csv");
26
+
27
+ // src/responses/index.ts
28
+ var responseBrand = /* @__PURE__ */ Symbol.for("@web-ts-toolkit/express-response-handler.response");
29
+ var _a;
30
+ _a = responseBrand;
31
+ var Response = class {
32
+ constructor(statusCode = 200, data) {
33
+ this[_a] = true;
34
+ this.statusCode = statusCode;
35
+ this.data = data;
36
+ }
37
+ };
38
+
39
+ // src/responses/success.ts
40
+ var OK = class extends Response {
41
+ constructor(data) {
42
+ super(200, data);
43
+ }
44
+ };
45
+ var Created = class extends Response {
46
+ constructor(data) {
47
+ super(201, data);
48
+ }
49
+ };
50
+ var Accepted = class extends Response {
51
+ constructor(data) {
52
+ super(202, data);
53
+ }
54
+ };
55
+ var NonAuthoritativeInfo = class extends Response {
56
+ constructor(data) {
57
+ super(203, data);
58
+ }
59
+ };
60
+ var NoContent = class extends Response {
61
+ constructor() {
62
+ super(204, void 0);
63
+ }
64
+ };
65
+ var ResetContent = class extends Response {
66
+ constructor(data) {
67
+ super(205, data);
68
+ }
69
+ };
70
+ var PartialContent = class extends Response {
71
+ constructor(data) {
72
+ super(206, data);
73
+ }
74
+ };
75
+ var MultiStatus = class extends Response {
76
+ constructor(data) {
77
+ super(207, data);
78
+ }
79
+ };
80
+ var AlreadyReported = class extends Response {
81
+ constructor(data) {
82
+ super(208, data);
83
+ }
84
+ };
85
+ var IMUsed = class extends Response {
86
+ constructor(data) {
87
+ super(226, data);
88
+ }
89
+ };
90
+
91
+ // src/responses/csv.ts
92
+ var import_format = require("@fast-csv/format");
93
+ var import_utils = require("@web-ts-toolkit/utils");
94
+ var csvResponseBrand = /* @__PURE__ */ Symbol.for("@web-ts-toolkit/express-response-handler.csv-response");
95
+ var _a2;
96
+ _a2 = csvResponseBrand;
97
+ var CSVResponse = class {
98
+ constructor(dataset = [], options = {}) {
99
+ this[_a2] = true;
100
+ this.dataset = (0, import_utils.castArray)(dataset);
101
+ this.filename = options.filename || "download.csv";
102
+ this.processor = options.processor || ((value) => value);
103
+ if ((0, import_utils.isBoolean)(options.headers)) {
104
+ this.headers = options.headers;
105
+ } else if (this.dataset.length > 0) {
106
+ this.headers = (0, import_utils.isPlainObject)(this.dataset[0]);
107
+ }
108
+ }
109
+ streamCsv(res) {
110
+ const stream = (0, import_format.format)({ headers: this.headers });
111
+ res.set("Content-Type", "text/csv");
112
+ res.set("Content-Disposition", `attachment;filename=${this.filename}`);
113
+ stream.pipe(res);
114
+ stream.on("error", (error) => {
115
+ if (res.destroy) {
116
+ res.destroy(error);
117
+ return;
118
+ }
119
+ res.end();
120
+ });
121
+ stream.on("end", () => res.end());
122
+ this.dataset.forEach((value) => stream.write(this.processor(value)));
123
+ stream.end();
124
+ }
125
+ };
126
+
127
+ // src/http-response.ts
26
128
  var import_http_errors = require("@web-ts-toolkit/http-errors");
27
- const HttpResponse = {
28
- ok: (...args) => new import_success.OK(...args),
29
- created: (...args) => new import_success.Created(...args),
30
- accepted: (...args) => new import_success.Accepted(...args),
31
- nonAuthoritativeInfo: (...args) => new import_success.NonAuthoritativeInfo(...args),
32
- noContent: (...args) => new import_success.NoContent(...args),
33
- resetContent: (...args) => new import_success.ResetContent(...args),
34
- partialContent: (...args) => new import_success.PartialContent(...args),
35
- multiStatus: (...args) => new import_success.MultiStatus(...args),
36
- alreadyReported: (...args) => new import_success.AlreadyReported(...args),
37
- imUsed: (...args) => new import_success.IMUsed(...args),
129
+ var HttpResponse = {
130
+ ok: (...args) => new OK(...args),
131
+ created: (...args) => new Created(...args),
132
+ accepted: (...args) => new Accepted(...args),
133
+ nonAuthoritativeInfo: (...args) => new NonAuthoritativeInfo(...args),
134
+ noContent: (...args) => new NoContent(...args),
135
+ resetContent: (...args) => new ResetContent(...args),
136
+ partialContent: (...args) => new PartialContent(...args),
137
+ multiStatus: (...args) => new MultiStatus(...args),
138
+ alreadyReported: (...args) => new AlreadyReported(...args),
139
+ imUsed: (...args) => new IMUsed(...args),
38
140
  badRequest: (...args) => new import_http_errors.BadRequestError(...args),
39
141
  unauthorized: (...args) => new import_http_errors.UnauthorizedError(...args),
40
142
  forbidden: (...args) => new import_http_errors.ForbiddenError(...args),
@@ -62,8 +164,8 @@ const HttpResponse = {
62
164
  tooManyRequests: (...args) => new import_http_errors.TooManyRequestsError(...args),
63
165
  requestHeaderFieldsTooLarge: (...args) => new import_http_errors.RequestHeaderFieldsTooLargeError(...args),
64
166
  unavailableForLegalReasons: (...args) => new import_http_errors.UnavailableForLegalReasonsError(...args),
65
- json: (...args) => new import_success.OK(...args),
66
- csv: (...args) => new import_csv.CSVResponse(...args)
167
+ json: (...args) => new OK(...args),
168
+ csv: (...args) => new CSVResponse(...args)
67
169
  };
68
170
  // Annotate the CommonJS export names for ESM import in node:
69
171
  0 && (module.exports = {
package/http-response.mjs CHANGED
@@ -1,16 +1,104 @@
1
- import {
2
- Accepted,
3
- AlreadyReported,
4
- Created,
5
- IMUsed,
6
- MultiStatus,
7
- NoContent,
8
- NonAuthoritativeInfo,
9
- OK,
10
- PartialContent,
11
- ResetContent
12
- } from "./responses/success";
13
- import { CSVResponse } from "./responses/csv";
1
+ // src/responses/index.ts
2
+ var responseBrand = /* @__PURE__ */ Symbol.for("@web-ts-toolkit/express-response-handler.response");
3
+ var _a;
4
+ _a = responseBrand;
5
+ var Response = class {
6
+ constructor(statusCode = 200, data) {
7
+ this[_a] = true;
8
+ this.statusCode = statusCode;
9
+ this.data = data;
10
+ }
11
+ };
12
+
13
+ // src/responses/success.ts
14
+ var OK = class extends Response {
15
+ constructor(data) {
16
+ super(200, data);
17
+ }
18
+ };
19
+ var Created = class extends Response {
20
+ constructor(data) {
21
+ super(201, data);
22
+ }
23
+ };
24
+ var Accepted = class extends Response {
25
+ constructor(data) {
26
+ super(202, data);
27
+ }
28
+ };
29
+ var NonAuthoritativeInfo = class extends Response {
30
+ constructor(data) {
31
+ super(203, data);
32
+ }
33
+ };
34
+ var NoContent = class extends Response {
35
+ constructor() {
36
+ super(204, void 0);
37
+ }
38
+ };
39
+ var ResetContent = class extends Response {
40
+ constructor(data) {
41
+ super(205, data);
42
+ }
43
+ };
44
+ var PartialContent = class extends Response {
45
+ constructor(data) {
46
+ super(206, data);
47
+ }
48
+ };
49
+ var MultiStatus = class extends Response {
50
+ constructor(data) {
51
+ super(207, data);
52
+ }
53
+ };
54
+ var AlreadyReported = class extends Response {
55
+ constructor(data) {
56
+ super(208, data);
57
+ }
58
+ };
59
+ var IMUsed = class extends Response {
60
+ constructor(data) {
61
+ super(226, data);
62
+ }
63
+ };
64
+
65
+ // src/responses/csv.ts
66
+ import { format } from "@fast-csv/format";
67
+ import { castArray, isBoolean, isPlainObject } from "@web-ts-toolkit/utils";
68
+ var csvResponseBrand = /* @__PURE__ */ Symbol.for("@web-ts-toolkit/express-response-handler.csv-response");
69
+ var _a2;
70
+ _a2 = csvResponseBrand;
71
+ var CSVResponse = class {
72
+ constructor(dataset = [], options = {}) {
73
+ this[_a2] = true;
74
+ this.dataset = castArray(dataset);
75
+ this.filename = options.filename || "download.csv";
76
+ this.processor = options.processor || ((value) => value);
77
+ if (isBoolean(options.headers)) {
78
+ this.headers = options.headers;
79
+ } else if (this.dataset.length > 0) {
80
+ this.headers = isPlainObject(this.dataset[0]);
81
+ }
82
+ }
83
+ streamCsv(res) {
84
+ const stream = format({ headers: this.headers });
85
+ res.set("Content-Type", "text/csv");
86
+ res.set("Content-Disposition", `attachment;filename=${this.filename}`);
87
+ stream.pipe(res);
88
+ stream.on("error", (error) => {
89
+ if (res.destroy) {
90
+ res.destroy(error);
91
+ return;
92
+ }
93
+ res.end();
94
+ });
95
+ stream.on("end", () => res.end());
96
+ this.dataset.forEach((value) => stream.write(this.processor(value)));
97
+ stream.end();
98
+ }
99
+ };
100
+
101
+ // src/http-response.ts
14
102
  import {
15
103
  BadRequestError,
16
104
  ConflictError,
@@ -40,7 +128,7 @@ import {
40
128
  UpgradeRequiredError,
41
129
  UriTooLongError
42
130
  } from "@web-ts-toolkit/http-errors";
43
- const HttpResponse = {
131
+ var HttpResponse = {
44
132
  ok: (...args) => new OK(...args),
45
133
  created: (...args) => new Created(...args),
46
134
  accepted: (...args) => new Accepted(...args),