emilsoftware-utilities 1.1.1 → 1.1.3
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/package.json +2 -1
- package/src/utilities.ts +41 -8
- package/utilities.d.ts +6 -3
- package/utilities.js +30 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "emilsoftware-utilities",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "Utilities for EmilSoftware",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"winston": "^3.11.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
+
"@types/express": "^4.17.21",
|
|
26
27
|
"@types/node": "^20.10.5",
|
|
27
28
|
"typescript": "^5.3.3"
|
|
28
29
|
}
|
package/src/utilities.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import {Response} from 'express';
|
|
2
|
+
|
|
3
|
+
|
|
1
4
|
enum STATUS_CODE {
|
|
2
5
|
OK = 0, WARNING = 1, ERROR = 2,
|
|
3
6
|
}
|
|
@@ -7,7 +10,7 @@ const parseDate = (date: string) => {
|
|
|
7
10
|
return new Date(Number(parts[2]), Number(parts[1]) - 1, Number(parts[0]));
|
|
8
11
|
}
|
|
9
12
|
|
|
10
|
-
const sendOKMessage = (res:
|
|
13
|
+
const sendOKMessage = (res: Response, message: string): Response => {
|
|
11
14
|
return res.send({
|
|
12
15
|
severity: "success", status: 200, statusCode: STATUS_CODE.OK, message,
|
|
13
16
|
});
|
|
@@ -26,17 +29,17 @@ const getNowDateString = (): string => {
|
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
|
|
29
|
-
const sendErrorMessage = (res:
|
|
32
|
+
const sendErrorMessage = (res: Response, error: any, tag: string = "[BASE ERROR]", status: number = 500): Response => {
|
|
30
33
|
return res.status(status).send({
|
|
31
34
|
severity: "error",
|
|
32
35
|
status: 500,
|
|
33
36
|
statusCode: STATUS_CODE.ERROR,
|
|
34
37
|
message: " Si è verificato un errore",
|
|
35
|
-
error: tag + ": " +
|
|
38
|
+
error: tag + ": " + error,
|
|
36
39
|
});
|
|
37
40
|
}
|
|
38
41
|
|
|
39
|
-
const sendBaseResponse = (res:
|
|
42
|
+
const sendBaseResponse = (res: Response, payload: any): Response => {
|
|
40
43
|
try {
|
|
41
44
|
payload = JSON.parse(JSON.stringify(payload));
|
|
42
45
|
const clearPayload = payload; // this.keysToCamel(payload);
|
|
@@ -67,7 +70,7 @@ const isObject = (o: any): boolean => {
|
|
|
67
70
|
};
|
|
68
71
|
|
|
69
72
|
|
|
70
|
-
const keysToCamel = (o: any) => {
|
|
73
|
+
const keysToCamel = (o: any): any => {
|
|
71
74
|
if (isObject(o)) {
|
|
72
75
|
const n = {};
|
|
73
76
|
|
|
@@ -127,12 +130,40 @@ const dateToSimple = (dData: Date): string => {
|
|
|
127
130
|
return dd + '-' + mm + '-' + yy;
|
|
128
131
|
}
|
|
129
132
|
|
|
133
|
+
const sendExecMessage = (res: Response, executionObject: any, title: string): Response => {
|
|
134
|
+
try {
|
|
135
|
+
let sSql = "";
|
|
136
|
+
let response = {
|
|
137
|
+
Status: {
|
|
138
|
+
errorCode: "0", errorDescription: "",
|
|
139
|
+
}, Sql: sSql, ID: executionObject?.id, Title: title
|
|
140
|
+
};
|
|
141
|
+
return res.send(response);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
return sendErrorMessage(res, "Errore nell'invio della risposta: " + error, title, 500);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const printQueryWithParams = (query: string = "", params: any[]): string => {
|
|
148
|
+
try {
|
|
149
|
+
params.forEach(param => {
|
|
150
|
+
query = query.replace("?", param);
|
|
151
|
+
});
|
|
152
|
+
return query;
|
|
153
|
+
} catch (error) {
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
|
|
130
159
|
interface Utilities {
|
|
131
160
|
parseDate: (date: string) => Date,
|
|
132
|
-
|
|
161
|
+
printQueryWithParams: (query: string, params: any[]) => string,
|
|
162
|
+
sendOKMessage: (res: Response, message: string) => Response,
|
|
163
|
+
sendExecMessage: (res: Response, executionObject: any, title: string) => Response,
|
|
133
164
|
getNowDateString: () => {},
|
|
134
|
-
sendErrorMessage: (res:
|
|
135
|
-
sendBaseResponse: (res:
|
|
165
|
+
sendErrorMessage: (res: Response, error: any, tag?: string, status?: number) => Response,
|
|
166
|
+
sendBaseResponse: (res: Response, payload: any) => Response,
|
|
136
167
|
toCamel: (s: any) => any,
|
|
137
168
|
isArray: (a: any) => boolean,
|
|
138
169
|
isObject: (o: any) => boolean,
|
|
@@ -145,7 +176,9 @@ interface Utilities {
|
|
|
145
176
|
|
|
146
177
|
export const Utilities: Utilities = {
|
|
147
178
|
parseDate,
|
|
179
|
+
printQueryWithParams,
|
|
148
180
|
sendOKMessage,
|
|
181
|
+
sendExecMessage,
|
|
149
182
|
getNowDateString,
|
|
150
183
|
sendErrorMessage,
|
|
151
184
|
sendBaseResponse,
|
package/utilities.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { Response } from 'express';
|
|
1
2
|
interface Utilities {
|
|
2
3
|
parseDate: (date: string) => Date;
|
|
3
|
-
|
|
4
|
+
printQueryWithParams: (query: string, params: any[]) => string;
|
|
5
|
+
sendOKMessage: (res: Response, message: string) => Response;
|
|
6
|
+
sendExecMessage: (res: Response, executionObject: any, title: string) => Response;
|
|
4
7
|
getNowDateString: () => {};
|
|
5
|
-
sendErrorMessage: (res:
|
|
6
|
-
sendBaseResponse: (res:
|
|
8
|
+
sendErrorMessage: (res: Response, error: any, tag?: string, status?: number) => Response;
|
|
9
|
+
sendBaseResponse: (res: Response, payload: any) => Response;
|
|
7
10
|
toCamel: (s: any) => any;
|
|
8
11
|
isArray: (a: any) => boolean;
|
|
9
12
|
isObject: (o: any) => boolean;
|
package/utilities.js
CHANGED
|
@@ -27,7 +27,7 @@ var getNowDateString = function () {
|
|
|
27
27
|
var seconds = now.getSeconds() < 9 ? "0" + now.getSeconds() : now.getSeconds();
|
|
28
28
|
return day + "." + month + "." + year + " " + hours + ":" + minutes + ":" + seconds;
|
|
29
29
|
};
|
|
30
|
-
var sendErrorMessage = function (res,
|
|
30
|
+
var sendErrorMessage = function (res, error, tag, status) {
|
|
31
31
|
if (tag === void 0) { tag = "[BASE ERROR]"; }
|
|
32
32
|
if (status === void 0) { status = 500; }
|
|
33
33
|
return res.status(status).send({
|
|
@@ -35,7 +35,7 @@ var sendErrorMessage = function (res, err, tag, status) {
|
|
|
35
35
|
status: 500,
|
|
36
36
|
statusCode: STATUS_CODE.ERROR,
|
|
37
37
|
message: " Si è verificato un errore",
|
|
38
|
-
error: tag + ": " +
|
|
38
|
+
error: tag + ": " + error,
|
|
39
39
|
});
|
|
40
40
|
};
|
|
41
41
|
var sendBaseResponse = function (res, payload) {
|
|
@@ -121,9 +121,37 @@ var dateToSimple = function (dData) {
|
|
|
121
121
|
var dd = addStartingZeros(dData.getDate(), 2);
|
|
122
122
|
return dd + '-' + mm + '-' + yy;
|
|
123
123
|
};
|
|
124
|
+
var sendExecMessage = function (res, executionObject, title) {
|
|
125
|
+
try {
|
|
126
|
+
var sSql = "";
|
|
127
|
+
var response = {
|
|
128
|
+
Status: {
|
|
129
|
+
errorCode: "0", errorDescription: "",
|
|
130
|
+
}, Sql: sSql, ID: executionObject === null || executionObject === void 0 ? void 0 : executionObject.id, Title: title
|
|
131
|
+
};
|
|
132
|
+
return res.send(response);
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
return sendErrorMessage(res, "Errore nell'invio della risposta: " + error, title, 500);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
var printQueryWithParams = function (query, params) {
|
|
139
|
+
if (query === void 0) { query = ""; }
|
|
140
|
+
try {
|
|
141
|
+
params.forEach(function (param) {
|
|
142
|
+
query = query.replace("?", param);
|
|
143
|
+
});
|
|
144
|
+
return query;
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
};
|
|
124
150
|
exports.Utilities = {
|
|
125
151
|
parseDate: parseDate,
|
|
152
|
+
printQueryWithParams: printQueryWithParams,
|
|
126
153
|
sendOKMessage: sendOKMessage,
|
|
154
|
+
sendExecMessage: sendExecMessage,
|
|
127
155
|
getNowDateString: getNowDateString,
|
|
128
156
|
sendErrorMessage: sendErrorMessage,
|
|
129
157
|
sendBaseResponse: sendBaseResponse,
|