@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.
@@ -0,0 +1,58 @@
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 csv_exports = {};
20
+ __export(csv_exports, {
21
+ CSVResponse: () => CSVResponse
22
+ });
23
+ module.exports = __toCommonJS(csv_exports);
24
+ var import_format = require("@fast-csv/format");
25
+ const isBoolean = (value) => typeof value === "boolean";
26
+ const isPlainObject = (value) => value !== null && typeof value === "object" && value.constructor === Object;
27
+ class CSVResponse {
28
+ constructor(dataset = [], options = {}) {
29
+ this.dataset = Array.isArray(dataset) ? dataset : [dataset];
30
+ this.filename = options.filename || "download.csv";
31
+ this.processor = options.processor || ((value) => value);
32
+ if (isBoolean(options.headers)) {
33
+ this.headers = options.headers;
34
+ } else if (this.dataset.length > 0) {
35
+ this.headers = isPlainObject(this.dataset[0]);
36
+ }
37
+ }
38
+ streamCsv(res) {
39
+ const stream = (0, import_format.format)({ headers: this.headers });
40
+ res.set("Content-Type", "text/csv");
41
+ res.set("Content-Disposition", `attachment;filename=${this.filename}`);
42
+ stream.pipe(res);
43
+ stream.on("error", (error) => {
44
+ if (res.destroy) {
45
+ res.destroy(error);
46
+ return;
47
+ }
48
+ res.end();
49
+ });
50
+ stream.on("end", () => res.end());
51
+ this.dataset.forEach((value) => stream.write(this.processor(value)));
52
+ stream.end();
53
+ }
54
+ }
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ CSVResponse
58
+ });
@@ -0,0 +1,35 @@
1
+ import "../chunk-PQJP2ZCI.mjs";
2
+ import { format } from "@fast-csv/format";
3
+ const isBoolean = (value) => typeof value === "boolean";
4
+ const isPlainObject = (value) => value !== null && typeof value === "object" && value.constructor === Object;
5
+ class CSVResponse {
6
+ constructor(dataset = [], options = {}) {
7
+ this.dataset = Array.isArray(dataset) ? dataset : [dataset];
8
+ this.filename = options.filename || "download.csv";
9
+ this.processor = options.processor || ((value) => value);
10
+ if (isBoolean(options.headers)) {
11
+ this.headers = options.headers;
12
+ } else if (this.dataset.length > 0) {
13
+ this.headers = isPlainObject(this.dataset[0]);
14
+ }
15
+ }
16
+ streamCsv(res) {
17
+ const stream = format({ headers: this.headers });
18
+ res.set("Content-Type", "text/csv");
19
+ res.set("Content-Disposition", `attachment;filename=${this.filename}`);
20
+ stream.pipe(res);
21
+ stream.on("error", (error) => {
22
+ if (res.destroy) {
23
+ res.destroy(error);
24
+ return;
25
+ }
26
+ res.end();
27
+ });
28
+ stream.on("end", () => res.end());
29
+ this.dataset.forEach((value) => stream.write(this.processor(value)));
30
+ stream.end();
31
+ }
32
+ }
33
+ export {
34
+ CSVResponse
35
+ };
@@ -0,0 +1,7 @@
1
+ declare class Response<T = unknown> {
2
+ readonly statusCode: number;
3
+ readonly data: T;
4
+ constructor(statusCode: number | undefined, data: T);
5
+ }
6
+
7
+ export { Response };
@@ -0,0 +1,7 @@
1
+ declare class Response<T = unknown> {
2
+ readonly statusCode: number;
3
+ readonly data: T;
4
+ constructor(statusCode: number | undefined, data: T);
5
+ }
6
+
7
+ export { Response };
@@ -0,0 +1,33 @@
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 responses_exports = {};
20
+ __export(responses_exports, {
21
+ Response: () => Response
22
+ });
23
+ module.exports = __toCommonJS(responses_exports);
24
+ class Response {
25
+ constructor(statusCode = 200, data) {
26
+ this.statusCode = statusCode;
27
+ this.data = data;
28
+ }
29
+ }
30
+ // Annotate the CommonJS export names for ESM import in node:
31
+ 0 && (module.exports = {
32
+ Response
33
+ });
@@ -0,0 +1,10 @@
1
+ import "../chunk-PQJP2ZCI.mjs";
2
+ class Response {
3
+ constructor(statusCode = 200, data) {
4
+ this.statusCode = statusCode;
5
+ this.data = data;
6
+ }
7
+ }
8
+ export {
9
+ Response
10
+ };
@@ -0,0 +1,34 @@
1
+ import { Response } from './index.mjs';
2
+
3
+ declare class OK<T = unknown> extends Response<T> {
4
+ constructor(data: T);
5
+ }
6
+ declare class Created<T = unknown> extends Response<T> {
7
+ constructor(data: T);
8
+ }
9
+ declare class Accepted<T = unknown> extends Response<T> {
10
+ constructor(data: T);
11
+ }
12
+ declare class NonAuthoritativeInfo<T = unknown> extends Response<T> {
13
+ constructor(data: T);
14
+ }
15
+ declare class NoContent<T = unknown> extends Response<T> {
16
+ constructor(data: T);
17
+ }
18
+ declare class ResetContent<T = unknown> extends Response<T> {
19
+ constructor(data: T);
20
+ }
21
+ declare class PartialContent<T = unknown> extends Response<T> {
22
+ constructor(data: T);
23
+ }
24
+ declare class MultiStatus<T = unknown> extends Response<T> {
25
+ constructor(data: T);
26
+ }
27
+ declare class AlreadyReported<T = unknown> extends Response<T> {
28
+ constructor(data: T);
29
+ }
30
+ declare class IMUsed<T = unknown> extends Response<T> {
31
+ constructor(data: T);
32
+ }
33
+
34
+ export { Accepted, AlreadyReported, Created, IMUsed, MultiStatus, NoContent, NonAuthoritativeInfo, OK, PartialContent, ResetContent };
@@ -0,0 +1,34 @@
1
+ import { Response } from './index.js';
2
+
3
+ declare class OK<T = unknown> extends Response<T> {
4
+ constructor(data: T);
5
+ }
6
+ declare class Created<T = unknown> extends Response<T> {
7
+ constructor(data: T);
8
+ }
9
+ declare class Accepted<T = unknown> extends Response<T> {
10
+ constructor(data: T);
11
+ }
12
+ declare class NonAuthoritativeInfo<T = unknown> extends Response<T> {
13
+ constructor(data: T);
14
+ }
15
+ declare class NoContent<T = unknown> extends Response<T> {
16
+ constructor(data: T);
17
+ }
18
+ declare class ResetContent<T = unknown> extends Response<T> {
19
+ constructor(data: T);
20
+ }
21
+ declare class PartialContent<T = unknown> extends Response<T> {
22
+ constructor(data: T);
23
+ }
24
+ declare class MultiStatus<T = unknown> extends Response<T> {
25
+ constructor(data: T);
26
+ }
27
+ declare class AlreadyReported<T = unknown> extends Response<T> {
28
+ constructor(data: T);
29
+ }
30
+ declare class IMUsed<T = unknown> extends Response<T> {
31
+ constructor(data: T);
32
+ }
33
+
34
+ export { Accepted, AlreadyReported, Created, IMUsed, MultiStatus, NoContent, NonAuthoritativeInfo, OK, PartialContent, ResetContent };
@@ -0,0 +1,96 @@
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 success_exports = {};
20
+ __export(success_exports, {
21
+ Accepted: () => Accepted,
22
+ AlreadyReported: () => AlreadyReported,
23
+ Created: () => Created,
24
+ IMUsed: () => IMUsed,
25
+ MultiStatus: () => MultiStatus,
26
+ NoContent: () => NoContent,
27
+ NonAuthoritativeInfo: () => NonAuthoritativeInfo,
28
+ OK: () => OK,
29
+ PartialContent: () => PartialContent,
30
+ ResetContent: () => ResetContent
31
+ });
32
+ module.exports = __toCommonJS(success_exports);
33
+ var import_index = require("./index");
34
+ class OK extends import_index.Response {
35
+ constructor(data) {
36
+ super(200, data);
37
+ }
38
+ }
39
+ class Created extends import_index.Response {
40
+ constructor(data) {
41
+ super(201, data);
42
+ }
43
+ }
44
+ class Accepted extends import_index.Response {
45
+ constructor(data) {
46
+ super(202, data);
47
+ }
48
+ }
49
+ class NonAuthoritativeInfo extends import_index.Response {
50
+ constructor(data) {
51
+ super(203, data);
52
+ }
53
+ }
54
+ class NoContent extends import_index.Response {
55
+ constructor(data) {
56
+ super(204, data);
57
+ }
58
+ }
59
+ class ResetContent extends import_index.Response {
60
+ constructor(data) {
61
+ super(205, data);
62
+ }
63
+ }
64
+ class PartialContent extends import_index.Response {
65
+ constructor(data) {
66
+ super(206, data);
67
+ }
68
+ }
69
+ class MultiStatus extends import_index.Response {
70
+ constructor(data) {
71
+ super(207, data);
72
+ }
73
+ }
74
+ class AlreadyReported extends import_index.Response {
75
+ constructor(data) {
76
+ super(208, data);
77
+ }
78
+ }
79
+ class IMUsed extends import_index.Response {
80
+ constructor(data) {
81
+ super(226, data);
82
+ }
83
+ }
84
+ // Annotate the CommonJS export names for ESM import in node:
85
+ 0 && (module.exports = {
86
+ Accepted,
87
+ AlreadyReported,
88
+ Created,
89
+ IMUsed,
90
+ MultiStatus,
91
+ NoContent,
92
+ NonAuthoritativeInfo,
93
+ OK,
94
+ PartialContent,
95
+ ResetContent
96
+ });
@@ -0,0 +1,64 @@
1
+ import "../chunk-PQJP2ZCI.mjs";
2
+ import { Response } from "./index";
3
+ class OK extends Response {
4
+ constructor(data) {
5
+ super(200, data);
6
+ }
7
+ }
8
+ class Created extends Response {
9
+ constructor(data) {
10
+ super(201, data);
11
+ }
12
+ }
13
+ class Accepted extends Response {
14
+ constructor(data) {
15
+ super(202, data);
16
+ }
17
+ }
18
+ class NonAuthoritativeInfo extends Response {
19
+ constructor(data) {
20
+ super(203, data);
21
+ }
22
+ }
23
+ class NoContent extends Response {
24
+ constructor(data) {
25
+ super(204, data);
26
+ }
27
+ }
28
+ class ResetContent extends Response {
29
+ constructor(data) {
30
+ super(205, data);
31
+ }
32
+ }
33
+ class PartialContent extends Response {
34
+ constructor(data) {
35
+ super(206, data);
36
+ }
37
+ }
38
+ class MultiStatus extends Response {
39
+ constructor(data) {
40
+ super(207, data);
41
+ }
42
+ }
43
+ class AlreadyReported extends Response {
44
+ constructor(data) {
45
+ super(208, data);
46
+ }
47
+ }
48
+ class IMUsed extends Response {
49
+ constructor(data) {
50
+ super(226, data);
51
+ }
52
+ }
53
+ export {
54
+ Accepted,
55
+ AlreadyReported,
56
+ Created,
57
+ IMUsed,
58
+ MultiStatus,
59
+ NoContent,
60
+ NonAuthoritativeInfo,
61
+ OK,
62
+ PartialContent,
63
+ ResetContent
64
+ };