ismx-nexo-node-app 0.3.24
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/dist/js/api/Service.js +45 -0
- package/dist/js/api/ServiceRest.js +33 -0
- package/dist/js/api/ServiceRestFormal.js +97 -0
- package/dist/js/api/ServiceRestFormalTemplate.js +127 -0
- package/dist/js/business/Business.js +5 -0
- package/dist/js/business/BusinessErrors.js +101 -0
- package/dist/js/business/BusinessLogger.js +9 -0
- package/dist/js/business/BusinessProxy.js +90 -0
- package/dist/js/business/BusinessServer.js +97 -0
- package/dist/js/business/BusinessState.js +5 -0
- package/dist/js/business/BusinessThread.js +23 -0
- package/dist/js/business/utils/CryptoUtils.js +30 -0
- package/dist/js/business/utils/NumberUtils.js +8 -0
- package/dist/js/business/utils/StringUtils.js +20 -0
- package/dist/js/index.js +56 -0
- package/dist/js/repository/Repository.js +5 -0
- package/dist/js/repository/RepositoryPostgres.js +215 -0
- package/dist/js/repository/RepositoryRest.js +55 -0
- package/dist/js/repository/RepositoryRestFormal.js +46 -0
- package/dist/js/repository/utils/PostgresUtils.js +51 -0
- package/dist/js/repository/utils/QueryUtils.js +13 -0
- package/dist/types/api/Service.d.ts +45 -0
- package/dist/types/api/ServiceRest.d.ts +6 -0
- package/dist/types/api/ServiceRestFormal.d.ts +23 -0
- package/dist/types/api/ServiceRestFormalTemplate.d.ts +40 -0
- package/dist/types/business/Business.d.ts +2 -0
- package/dist/types/business/BusinessErrors.d.ts +39 -0
- package/dist/types/business/BusinessLogger.d.ts +3 -0
- package/dist/types/business/BusinessProxy.d.ts +15 -0
- package/dist/types/business/BusinessServer.d.ts +15 -0
- package/dist/types/business/BusinessState.d.ts +2 -0
- package/dist/types/business/BusinessThread.d.ts +8 -0
- package/dist/types/business/utils/CryptoUtils.d.ts +5 -0
- package/dist/types/business/utils/NumberUtils.d.ts +3 -0
- package/dist/types/business/utils/StringUtils.d.ts +5 -0
- package/dist/types/index.d.ts +35 -0
- package/dist/types/repository/Repository.d.ts +2 -0
- package/dist/types/repository/RepositoryPostgres.d.ts +75 -0
- package/dist/types/repository/RepositoryRest.d.ts +17 -0
- package/dist/types/repository/RepositoryRestFormal.d.ts +7 -0
- package/dist/types/repository/utils/PostgresUtils.d.ts +17 -0
- package/dist/types/repository/utils/QueryUtils.d.ts +5 -0
- package/package.json +35 -0
- package/src/main/node/api/Service.ts +55 -0
- package/src/main/node/api/ServiceRest.ts +19 -0
- package/src/main/node/api/ServiceRestFormal.ts +58 -0
- package/src/main/node/api/ServiceRestFormalTemplate.ts +133 -0
- package/src/main/node/business/Business.ts +3 -0
- package/src/main/node/business/BusinessErrors.ts +94 -0
- package/src/main/node/business/BusinessLogger.ts +6 -0
- package/src/main/node/business/BusinessProxy.ts +57 -0
- package/src/main/node/business/BusinessServer.ts +76 -0
- package/src/main/node/business/BusinessState.ts +4 -0
- package/src/main/node/business/BusinessThread.ts +19 -0
- package/src/main/node/business/utils/CryptoUtils.ts +32 -0
- package/src/main/node/business/utils/NumberUtils.ts +6 -0
- package/src/main/node/business/utils/StringUtils.ts +20 -0
- package/src/main/node/index.ts +45 -0
- package/src/main/node/repository/Repository.ts +3 -0
- package/src/main/node/repository/RepositoryPostgres.ts +246 -0
- package/src/main/node/repository/RepositoryRest.ts +70 -0
- package/src/main/node/repository/RepositoryRestFormal.ts +37 -0
- package/src/main/node/repository/utils/PostgresUtils.ts +59 -0
- package/src/main/node/repository/utils/QueryUtils.ts +10 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class StringUtils {
|
|
4
|
+
static base64Decode(base64) {
|
|
5
|
+
return Buffer.from(base64, 'base64');
|
|
6
|
+
}
|
|
7
|
+
static base64Encode(buffer) {
|
|
8
|
+
return buffer.toString('base64');
|
|
9
|
+
}
|
|
10
|
+
static base64UrlDecode(base64) {
|
|
11
|
+
let base64String = base64.replace(/-/g, '+').replace(/_/g, '/');
|
|
12
|
+
const padding = base64String.length % 4;
|
|
13
|
+
if (padding === 2)
|
|
14
|
+
base64String += '==';
|
|
15
|
+
else if (padding === 3)
|
|
16
|
+
base64String += '=';
|
|
17
|
+
return StringUtils.base64Decode(base64String);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.default = StringUtils;
|
package/dist/js/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.RepositoryPostgres = exports.Repository = exports.BusinessLogger = exports.BusinessErrors = exports.BusinessThread = exports.BusinessServer = exports.BusinessProxy = exports.BusinessState = exports.Business = exports.ServiceRestFormalTemplate = exports.ServiceRestFormal = exports.ServiceRest = exports.HttpResponse = exports.Service = void 0;
|
|
30
|
+
const Service_1 = __importStar(require("./api/Service"));
|
|
31
|
+
exports.Service = Service_1.default;
|
|
32
|
+
exports.HttpResponse = Service_1.HttpResponse;
|
|
33
|
+
const ServiceRest_1 = __importDefault(require("./api/ServiceRest"));
|
|
34
|
+
exports.ServiceRest = ServiceRest_1.default;
|
|
35
|
+
const ServiceRestFormal_1 = __importDefault(require("./api/ServiceRestFormal"));
|
|
36
|
+
exports.ServiceRestFormal = ServiceRestFormal_1.default;
|
|
37
|
+
const ServiceRestFormalTemplate_1 = __importDefault(require("./api/ServiceRestFormalTemplate"));
|
|
38
|
+
exports.ServiceRestFormalTemplate = ServiceRestFormalTemplate_1.default;
|
|
39
|
+
const Business_1 = __importDefault(require("./business/Business"));
|
|
40
|
+
exports.Business = Business_1.default;
|
|
41
|
+
const BusinessState_1 = __importDefault(require("./business/BusinessState"));
|
|
42
|
+
exports.BusinessState = BusinessState_1.default;
|
|
43
|
+
const BusinessProxy_1 = __importDefault(require("./business/BusinessProxy"));
|
|
44
|
+
exports.BusinessProxy = BusinessProxy_1.default;
|
|
45
|
+
const BusinessServer_1 = __importDefault(require("./business/BusinessServer"));
|
|
46
|
+
exports.BusinessServer = BusinessServer_1.default;
|
|
47
|
+
const BusinessThread_1 = __importDefault(require("./business/BusinessThread"));
|
|
48
|
+
exports.BusinessThread = BusinessThread_1.default;
|
|
49
|
+
const BusinessErrors_1 = __importDefault(require("./business/BusinessErrors"));
|
|
50
|
+
exports.BusinessErrors = BusinessErrors_1.default;
|
|
51
|
+
const BusinessLogger_1 = __importDefault(require("./business/BusinessLogger"));
|
|
52
|
+
exports.BusinessLogger = BusinessLogger_1.default;
|
|
53
|
+
const Repository_1 = __importDefault(require("./repository/Repository"));
|
|
54
|
+
exports.Repository = Repository_1.default;
|
|
55
|
+
const RepositoryPostgres_1 = __importDefault(require("./repository/RepositoryPostgres"));
|
|
56
|
+
exports.RepositoryPostgres = RepositoryPostgres_1.default;
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const pg_1 = __importDefault(require("pg"));
|
|
16
|
+
const PostgresUtils_1 = __importDefault(require("./utils/PostgresUtils"));
|
|
17
|
+
class RepositoryPostgres {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.connected = false;
|
|
20
|
+
this.tables = {};
|
|
21
|
+
this.onConnectListeners = [];
|
|
22
|
+
this.onQueryWillExecuteListeners = [];
|
|
23
|
+
this.onQueryDidExecuteListeners = [];
|
|
24
|
+
this.onIntrospectedListener = [];
|
|
25
|
+
this.introspectIntervalTime = 5 * 60 * 1000;
|
|
26
|
+
this.schema = "public";
|
|
27
|
+
this.query = this.query.bind(this);
|
|
28
|
+
this.one = this.one.bind(this);
|
|
29
|
+
this.add = this.add.bind(this);
|
|
30
|
+
this.update = this.update.bind(this);
|
|
31
|
+
this.del = this.del.bind(this);
|
|
32
|
+
this.introspect = this.introspect.bind(this);
|
|
33
|
+
}
|
|
34
|
+
init(chain) {
|
|
35
|
+
this.chain = chain;
|
|
36
|
+
this.schema = chain.schema || "public";
|
|
37
|
+
this.connect(chain);
|
|
38
|
+
}
|
|
39
|
+
isConnected() {
|
|
40
|
+
return this.connected;
|
|
41
|
+
}
|
|
42
|
+
addOnConnect(listener) { this.onConnectListeners.push(listener); }
|
|
43
|
+
addOnQueryWillExecuteListener(listener) { this.onQueryWillExecuteListeners.push(listener); }
|
|
44
|
+
setIntrospectInterval(time) { this.introspectIntervalTime = time; }
|
|
45
|
+
addOnIntrospected(listener) { this.onIntrospectedListener.push(listener); }
|
|
46
|
+
connect(connection) {
|
|
47
|
+
this.client = new pg_1.default.Client(connection);
|
|
48
|
+
this.client.connect().then(() => __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
yield this.introspect();
|
|
50
|
+
setInterval(this.introspect, this.introspectIntervalTime);
|
|
51
|
+
this.connected = true;
|
|
52
|
+
this.onConnectListeners.map(l => l());
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
native(query_1) {
|
|
56
|
+
return __awaiter(this, arguments, void 0, function* (query, values = [], options = {}) {
|
|
57
|
+
this.onQueryWillExecuteListeners.forEach((l) => l(query));
|
|
58
|
+
let cursor = this.client.query(query, values);
|
|
59
|
+
this.onQueryDidExecuteListeners.forEach((l) => l(query));
|
|
60
|
+
return cursor;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
query(query_1) {
|
|
64
|
+
return __awaiter(this, arguments, void 0, function* (query, values = [], options = {}) {
|
|
65
|
+
return yield this.native(query, values, options).then((result) => {
|
|
66
|
+
return PostgresUtils_1.default.snakeToCamel(result.rows);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
one(tableName, id) {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
return this.any(tableName, { id });
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
any(tableName, filters) {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
return yield this.find(tableName, filters).then((rows) => rows[0]);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
find(tableName, filters) {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
if (!this.tables[tableName])
|
|
83
|
+
throw new Error(`table ${tableName} does not exist`);
|
|
84
|
+
let { where, values } = this.toQuery(tableName, filters);
|
|
85
|
+
let query = `SELECT * FROM ${this.schema}.${tableName} WHERE ${where}`;
|
|
86
|
+
return this.query(query, values);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
all_depr(tableName_1) {
|
|
90
|
+
return __awaiter(this, arguments, void 0, function* (tableName, options = {}) {
|
|
91
|
+
var _a, _b;
|
|
92
|
+
if (!this.tables[tableName])
|
|
93
|
+
throw new Error(`table ${tableName} does not exist`);
|
|
94
|
+
let table = this.tables[tableName];
|
|
95
|
+
let schema = (_a = options.schema) !== null && _a !== void 0 ? _a : this.schema;
|
|
96
|
+
return this.query(`
|
|
97
|
+
SELECT * FROM ${schema}.${tableName}
|
|
98
|
+
WHERE ${(_b = options.filters) !== null && _b !== void 0 ? _b : "TRUE"}
|
|
99
|
+
`);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
add(tableName, object, id) {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
return this.addAll(tableName, [Object.assign({ id: id }, object)]).then((rows) => rows[0]);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
addAll(tableName, objects) {
|
|
108
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
if (!this.tables[tableName])
|
|
110
|
+
throw new Error(`table ${tableName} does not exist`);
|
|
111
|
+
let table = this.tables[tableName];
|
|
112
|
+
let columns = PostgresUtils_1.default.columns(table);
|
|
113
|
+
let query = `INSERT INTO ${this.schema}.${tableName} (${columns.join(",")}) VALUES `;
|
|
114
|
+
let values = [];
|
|
115
|
+
let index = 1;
|
|
116
|
+
for (let object of objects) {
|
|
117
|
+
let { bindings, values: valueList } = PostgresUtils_1.default.bindOrDefault(table, object, index);
|
|
118
|
+
index += valueList.length;
|
|
119
|
+
values.push(...valueList);
|
|
120
|
+
query += `(${bindings.join(",")}), `;
|
|
121
|
+
}
|
|
122
|
+
query = query.slice(0, -2);
|
|
123
|
+
query += ` RETURNING *`;
|
|
124
|
+
return this.query(query, values);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
update(tableName, id, object) {
|
|
128
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
if (!id)
|
|
130
|
+
throw new Error(`field 'id' is mandatory when updating ${tableName}`);
|
|
131
|
+
if (!this.tables[tableName])
|
|
132
|
+
throw new Error(`table ${tableName} does not exist`);
|
|
133
|
+
let table = this.tables[tableName];
|
|
134
|
+
let columns = PostgresUtils_1.default.columns(table);
|
|
135
|
+
let params = columns.map((column, index) => `\$${index + 2}`).join(",");
|
|
136
|
+
let values = [id, ...columns.map((column) => object[PostgresUtils_1.default.stringToCamel(column)])];
|
|
137
|
+
let query = `UPDATE ${this.schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 `;
|
|
138
|
+
query += `RETURNING *`;
|
|
139
|
+
return this.query(query, values);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
page(tableName_1, sortKey_1) {
|
|
143
|
+
return __awaiter(this, arguments, void 0, function* (tableName, sortKey, maxResults = 50, pageNumber = 0, filters) {
|
|
144
|
+
if (!this.tables[tableName])
|
|
145
|
+
throw new Error(`table ${tableName} does not exist`);
|
|
146
|
+
let table = this.tables[tableName];
|
|
147
|
+
let columns = PostgresUtils_1.default.columns(table);
|
|
148
|
+
let offset = maxResults * pageNumber;
|
|
149
|
+
let { where, values } = this.toQuery(tableName, filters, "", 4);
|
|
150
|
+
let query = `SELECT * FROM ${this.schema}.${tableName} WHERE ${where} ORDER BY $1 LIMIT $2 OFFSET $3`;
|
|
151
|
+
let elements = this.query(query, [sortKey, maxResults, offset, ...values]);
|
|
152
|
+
let total = this.count(tableName, filters);
|
|
153
|
+
return Promise.all([total, elements]).then(([total, elements]) => ({ total, elements }));
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
count(tableName, filters) {
|
|
157
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
+
if (!this.tables[tableName])
|
|
159
|
+
throw new Error(`table ${tableName} does not exist`);
|
|
160
|
+
let { where, values } = this.toQuery(tableName, filters);
|
|
161
|
+
let query = `SELECT count(*) as count FROM ${this.schema}.${tableName} WHERE ${where}`;
|
|
162
|
+
return this.query(query, values).then((result) => result[0].count);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
select(tableName, select, filters) {
|
|
166
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
167
|
+
if (!this.tables[tableName])
|
|
168
|
+
throw new Error(`table ${tableName} does not exist`);
|
|
169
|
+
let { where, values } = this.toQuery(tableName, filters);
|
|
170
|
+
let query = `SELECT json_agg(${select}) as list FROM ${this.schema}.${tableName} WHERE ${where}`;
|
|
171
|
+
return this.query(query, values).then((result) => result[0].list);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
del(tableName, id) {
|
|
175
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
throw new Error(`not implemented yet`);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
toQuery(tableName, filters, alias = "", index = 1) {
|
|
180
|
+
let table = this.tables[tableName];
|
|
181
|
+
let columns = PostgresUtils_1.default.columns(table);
|
|
182
|
+
let elements = Object.entries(filters !== null && filters !== void 0 ? filters : []);
|
|
183
|
+
let where = "TRUE";
|
|
184
|
+
let iter = index;
|
|
185
|
+
let values = [];
|
|
186
|
+
for (let [key, value] of elements) {
|
|
187
|
+
let colum = PostgresUtils_1.default.camelToSnake(key);
|
|
188
|
+
if (key !== "id" && !columns.includes(colum))
|
|
189
|
+
continue;
|
|
190
|
+
if (alias !== "")
|
|
191
|
+
alias += ".";
|
|
192
|
+
if (value instanceof Array)
|
|
193
|
+
where += ` AND ${alias}${colum} = ANY(\$${iter++})`;
|
|
194
|
+
else
|
|
195
|
+
where += ` AND ${alias}${colum}=\$${iter++}`;
|
|
196
|
+
values.push(value);
|
|
197
|
+
}
|
|
198
|
+
return { where, values };
|
|
199
|
+
}
|
|
200
|
+
introspect() {
|
|
201
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
202
|
+
let result = yield this.native(`
|
|
203
|
+
SELECT table_name as name,
|
|
204
|
+
json_agg(json_build_object('name', column_name, 'default', column_default)) as columns
|
|
205
|
+
FROM information_schema.columns
|
|
206
|
+
WHERE (table_schema = $1 or table_schema = 'public')
|
|
207
|
+
GROUP BY table_name
|
|
208
|
+
`, [this.schema]);
|
|
209
|
+
for (let table of result.rows)
|
|
210
|
+
this.tables[table["name"]] = table["columns"];
|
|
211
|
+
this.onIntrospectedListener.forEach((l) => l(this.tables));
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
exports.default = RepositoryPostgres;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const Repository_1 = __importDefault(require("./Repository"));
|
|
16
|
+
const QueryUtils_1 = __importDefault(require("./utils/QueryUtils"));
|
|
17
|
+
class RepositoryRest extends Repository_1.default {
|
|
18
|
+
constructor(baseUrl, options) {
|
|
19
|
+
super();
|
|
20
|
+
this.baseUrl = baseUrl;
|
|
21
|
+
this.interceptor = options === null || options === void 0 ? void 0 : options.interceptor;
|
|
22
|
+
}
|
|
23
|
+
setOnRequestListener(listener) {
|
|
24
|
+
this.onRequestListener = listener;
|
|
25
|
+
}
|
|
26
|
+
call() {
|
|
27
|
+
return __awaiter(this, arguments, void 0, function* (method = 'GET', endpoint = '/', request = {}) {
|
|
28
|
+
var _a;
|
|
29
|
+
// Especifica el método de la llamada HTTP al recurso.
|
|
30
|
+
request.method = method;
|
|
31
|
+
// Completa la información de las cabeceras con cabeceras comunes por llamada a API.
|
|
32
|
+
let headers = new Headers(request.headers);
|
|
33
|
+
headers.set('Content-Type', 'application/json');
|
|
34
|
+
headers.set('Cache-Control', 'no-cache');
|
|
35
|
+
headers.set('Pragma', 'no-cache');
|
|
36
|
+
for (let header in request.headers)
|
|
37
|
+
headers.set(header, request.headers[header]);
|
|
38
|
+
// Notifica que una petición REST va a ser realizada
|
|
39
|
+
(_a = this.onRequestListener) === null || _a === void 0 ? void 0 : _a.call(this, method, endpoint, request);
|
|
40
|
+
// Realiza la llamada a la API con las cabeceras y la URL base de la llamada.
|
|
41
|
+
let timestamp = new Date();
|
|
42
|
+
let call = fetch(this.baseUrl + endpoint + QueryUtils_1.default.map(request.query), Object.assign(Object.assign({}, request), { headers }));
|
|
43
|
+
// Añade información adicional a la respuesta y devuelve el resultado (o el error en su caso).
|
|
44
|
+
return call.then((res) => {
|
|
45
|
+
var _a;
|
|
46
|
+
let response = res;
|
|
47
|
+
response.duration = new Date().valueOf() - timestamp.valueOf();
|
|
48
|
+
response.timestamp = timestamp;
|
|
49
|
+
(_a = this.interceptor) === null || _a === void 0 ? void 0 : _a.call(this, response);
|
|
50
|
+
return response;
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.default = RepositoryRest;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const RepositoryRest_1 = __importDefault(require("./RepositoryRest"));
|
|
16
|
+
class RepositoryRestFormal extends RepositoryRest_1.default {
|
|
17
|
+
constructor(baseUrl, ...methods) {
|
|
18
|
+
super(baseUrl);
|
|
19
|
+
}
|
|
20
|
+
call() {
|
|
21
|
+
const _super = Object.create(null, {
|
|
22
|
+
call: { get: () => super.call }
|
|
23
|
+
});
|
|
24
|
+
return __awaiter(this, arguments, void 0, function* (method = 'GET', endpoint = '/', request = {}) {
|
|
25
|
+
request.body = JSON.stringify(request.body);
|
|
26
|
+
return _super.call.call(this, method, endpoint, request)
|
|
27
|
+
.then((call) => __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
let json = yield call.json();
|
|
29
|
+
if (json.code === "0")
|
|
30
|
+
return json.data;
|
|
31
|
+
else { // @ts-ignore
|
|
32
|
+
throw new BusinessErrors_1.FormalError(json.code, json.description, call);
|
|
33
|
+
}
|
|
34
|
+
}))
|
|
35
|
+
.catch((error) => {
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
if (error instanceof BusinessErrors_1.FormalError)
|
|
38
|
+
throw error;
|
|
39
|
+
else { // @ts-ignore
|
|
40
|
+
throw new BusinessErrors_1.FormalError("-1", "ConnectionError", undefined, error);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.default = RepositoryRestFormal;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class PostgresUtils {
|
|
4
|
+
static stringToCamel(column) {
|
|
5
|
+
return column.toLowerCase().replace(/([-_][a-z])/g, group => group.toUpperCase().replace('-', '').replace('_', ''));
|
|
6
|
+
}
|
|
7
|
+
static camelToSnake(column) {
|
|
8
|
+
return column.replace(/([A-Z])/g, '_$1').toLowerCase();
|
|
9
|
+
}
|
|
10
|
+
static snakeToCamel(obj) {
|
|
11
|
+
if (Array.isArray(obj))
|
|
12
|
+
return obj.map(el => PostgresUtils.snakeToCamel(el));
|
|
13
|
+
else if (typeof obj === 'function' || obj !== Object(obj) || obj instanceof Date)
|
|
14
|
+
return obj;
|
|
15
|
+
else
|
|
16
|
+
return PostgresUtils.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
17
|
+
key.replace(/([-_][a-z])/gi, c => c.toUpperCase().replace(/[-_]/g, '')),
|
|
18
|
+
PostgresUtils.snakeToCamel(value),
|
|
19
|
+
]));
|
|
20
|
+
}
|
|
21
|
+
static columns(table) {
|
|
22
|
+
return table.map((t) => t.name);
|
|
23
|
+
}
|
|
24
|
+
static bindOrDefault(table, object, startIndex) {
|
|
25
|
+
let index = startIndex;
|
|
26
|
+
let bindings = [];
|
|
27
|
+
let values = [];
|
|
28
|
+
table.forEach((colum) => {
|
|
29
|
+
let value = object[PostgresUtils.stringToCamel(colum.name)];
|
|
30
|
+
if (value !== undefined || colum.default === undefined) {
|
|
31
|
+
bindings.push(`\$${index++}`);
|
|
32
|
+
values.push(value);
|
|
33
|
+
}
|
|
34
|
+
else
|
|
35
|
+
bindings.push("DEFAULT");
|
|
36
|
+
});
|
|
37
|
+
return { bindings, values };
|
|
38
|
+
}
|
|
39
|
+
static valueOrDefault(columns, object) {
|
|
40
|
+
return columns.map((column) => {
|
|
41
|
+
return object[PostgresUtils.stringToCamel(column)];
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
static fromEntries(iterable) {
|
|
45
|
+
const result = {};
|
|
46
|
+
for (const [key, value] of iterable)
|
|
47
|
+
result[key] = value;
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.default = PostgresUtils;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class QueryUtils {
|
|
4
|
+
static map(query = {}) {
|
|
5
|
+
if (!query || Object.entries(query).length === 0)
|
|
6
|
+
return "";
|
|
7
|
+
let queryString = "?";
|
|
8
|
+
for (const param in query)
|
|
9
|
+
queryString += `${param}=${query[param]}&`;
|
|
10
|
+
return queryString;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.default = QueryUtils;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface HttpRequest<T = any> {
|
|
2
|
+
id?: string;
|
|
3
|
+
url?: string;
|
|
4
|
+
method?: string;
|
|
5
|
+
endpoint?: string;
|
|
6
|
+
timestamp?: Date;
|
|
7
|
+
params?: {
|
|
8
|
+
[key: string]: string;
|
|
9
|
+
};
|
|
10
|
+
query?: {
|
|
11
|
+
[key: string]: string;
|
|
12
|
+
};
|
|
13
|
+
body?: T;
|
|
14
|
+
headers?: {
|
|
15
|
+
[key: string]: string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export declare class HttpResponse<T = any> {
|
|
19
|
+
content?: T;
|
|
20
|
+
httpCode?: number;
|
|
21
|
+
headers?: {
|
|
22
|
+
[key: string]: string;
|
|
23
|
+
};
|
|
24
|
+
static ok<T = any>(content: T): HttpResponse<any>;
|
|
25
|
+
static ko<T = any>(httpCode: number, content: T | undefined): HttpResponse<any>;
|
|
26
|
+
}
|
|
27
|
+
export default abstract class Service<Req, Res> {
|
|
28
|
+
static readonly services: Service<any, any>[];
|
|
29
|
+
readonly method: string;
|
|
30
|
+
readonly endpoint: string;
|
|
31
|
+
protected constructor(method: string, endpoint?: string);
|
|
32
|
+
serve(request: HttpRequest<Req>): Promise<HttpResponse<Res>>;
|
|
33
|
+
static clone<R, S>(request: HttpRequest<R>, clone: {
|
|
34
|
+
params?: {
|
|
35
|
+
[key: string]: string;
|
|
36
|
+
};
|
|
37
|
+
query?: {
|
|
38
|
+
[key: string]: string;
|
|
39
|
+
};
|
|
40
|
+
body?: S;
|
|
41
|
+
headers?: {
|
|
42
|
+
[key: string]: string;
|
|
43
|
+
};
|
|
44
|
+
}): HttpRequest<S>;
|
|
45
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import Service, { HttpRequest, HttpResponse } from "./Service";
|
|
2
|
+
export default class ServiceRest<Req, Res> extends Service<Req, Res> {
|
|
3
|
+
constructor(method: string, endpoint?: string);
|
|
4
|
+
serve(request: HttpRequest<Req>): Promise<HttpResponse<Res>>;
|
|
5
|
+
serveRest(request: HttpRequest<Req>): Promise<HttpResponse<Res>>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { HttpRequest, HttpResponse } from "./Service";
|
|
2
|
+
import ServiceRest from "./ServiceRest";
|
|
3
|
+
export interface Wrapper<T> {
|
|
4
|
+
code: string;
|
|
5
|
+
description: string;
|
|
6
|
+
data?: T;
|
|
7
|
+
debug?: Error;
|
|
8
|
+
}
|
|
9
|
+
export default class ServiceRestFormal<Req = any, Res = any> extends ServiceRest<Req, Wrapper<Res>> {
|
|
10
|
+
constructor(method: string, endpoint?: string);
|
|
11
|
+
serveRest(request: HttpRequest<Req>): Promise<HttpResponse<Wrapper<Res>>>;
|
|
12
|
+
serveRestFormal(request: HttpRequest<Req>): Promise<HttpResponse<Res>>;
|
|
13
|
+
unwrap<Res>(request: Promise<HttpResponse<Wrapper<Res>>>): Promise<Res | undefined>;
|
|
14
|
+
fullSelect<Req, Res>(request: HttpRequest<Req>, api: {
|
|
15
|
+
select: ServiceRestFormal<Req, string[]>;
|
|
16
|
+
full: ServiceRestFormal<Req, Res>;
|
|
17
|
+
}): Promise<(Awaited<Res> | undefined)[]>;
|
|
18
|
+
static unwrap<Res>(request: Promise<HttpResponse<Wrapper<Res>>>): Promise<Res | undefined>;
|
|
19
|
+
static fullSelect<Req, Res>(request: HttpRequest<Req>, api: {
|
|
20
|
+
select: ServiceRestFormal<Req, string[]>;
|
|
21
|
+
full: ServiceRestFormal<Req, Res>;
|
|
22
|
+
}): Promise<(Awaited<Res> | undefined)[]>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { HttpRequest, HttpResponse } from "./Service";
|
|
2
|
+
import ServiceRestFormal from "./ServiceRestFormal";
|
|
3
|
+
import RepositoryPostgres, { Pagination } from "../repository/RepositoryPostgres";
|
|
4
|
+
import BusinessErrors from "../business/BusinessErrors";
|
|
5
|
+
export default class ServiceRestFormalTemplate<Req = any, Res = any> {
|
|
6
|
+
readonly resource: string;
|
|
7
|
+
readonly tableName: string;
|
|
8
|
+
readonly database: RepositoryPostgres;
|
|
9
|
+
readonly errors: BusinessErrors;
|
|
10
|
+
readonly get: ServiceRestFormal<Req, Res>;
|
|
11
|
+
readonly post: ServiceRestFormal<Req, Res>;
|
|
12
|
+
readonly put: ServiceRestFormal<Req, Res>;
|
|
13
|
+
readonly del: ServiceRestFormal<Req, Res>;
|
|
14
|
+
readonly select: ServiceRestFormal<Req, string[]>;
|
|
15
|
+
readonly exist: ServiceRestFormal<Req, Boolean>;
|
|
16
|
+
readonly count: ServiceRestFormal<Req, Number>;
|
|
17
|
+
readonly getList: ServiceRestFormal<Req, Res[]>;
|
|
18
|
+
readonly postList: ServiceRestFormal<Req, Res[]>;
|
|
19
|
+
readonly page: ServiceRestFormal<Req, Pagination<Res>>;
|
|
20
|
+
readonly map: ServiceRestFormal<Req, {
|
|
21
|
+
[key: string]: Res;
|
|
22
|
+
}>;
|
|
23
|
+
constructor(resource: string, tableName: string);
|
|
24
|
+
protected serveGet(request: HttpRequest<Req>): Promise<HttpResponse<Res>>;
|
|
25
|
+
protected serveGetList(request: HttpRequest<Req>): Promise<HttpResponse<Res[]>>;
|
|
26
|
+
protected servePostList(request: HttpRequest<Req>): Promise<HttpResponse<Res[]>>;
|
|
27
|
+
protected servePage(request: HttpRequest<Req>): Promise<HttpResponse<Pagination<Res>>>;
|
|
28
|
+
protected serveMap(request: HttpRequest<Req>): Promise<HttpResponse<{
|
|
29
|
+
[key: string]: Res;
|
|
30
|
+
}>>;
|
|
31
|
+
protected indexer: string;
|
|
32
|
+
protected serveSelect(request: HttpRequest<Req>): Promise<HttpResponse<string[]>>;
|
|
33
|
+
protected serveExist(request: HttpRequest<Req>): Promise<HttpResponse<Boolean>>;
|
|
34
|
+
protected serveCount(request: HttpRequest<Req>): Promise<HttpResponse<Number>>;
|
|
35
|
+
protected servePost(request: HttpRequest<Req>): Promise<HttpResponse<Res>>;
|
|
36
|
+
protected servePut(request: HttpRequest<Req>): Promise<HttpResponse<Res>>;
|
|
37
|
+
protected serveDel(request: HttpRequest<Req>): Promise<HttpResponse<Res>>;
|
|
38
|
+
protected readonly unwrap: typeof ServiceRestFormal.unwrap;
|
|
39
|
+
protected readonly fullSelect: typeof ServiceRestFormal.fullSelect;
|
|
40
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export interface FormalError {
|
|
2
|
+
id: string;
|
|
3
|
+
code: string;
|
|
4
|
+
description: string;
|
|
5
|
+
httpCode?: number;
|
|
6
|
+
group?: string;
|
|
7
|
+
}
|
|
8
|
+
export default class BusinessErrors {
|
|
9
|
+
private errors;
|
|
10
|
+
init(): Promise<void>;
|
|
11
|
+
setErrors(errors: {
|
|
12
|
+
[key: string]: FormalError;
|
|
13
|
+
}): void;
|
|
14
|
+
get(code: string, ...params: string[]): FormalError;
|
|
15
|
+
except(code: string, ...params: string[]): void;
|
|
16
|
+
notFound(endpoint: string): void;
|
|
17
|
+
getQuery(key: string, query: {
|
|
18
|
+
[key: string]: string;
|
|
19
|
+
} | undefined): string;
|
|
20
|
+
eitherQuery(keys: string[], query: {
|
|
21
|
+
[p: string]: string;
|
|
22
|
+
}): {
|
|
23
|
+
key: string;
|
|
24
|
+
value: string;
|
|
25
|
+
index: number;
|
|
26
|
+
};
|
|
27
|
+
getPath(key: string, path: {
|
|
28
|
+
[key: string]: string;
|
|
29
|
+
}): string;
|
|
30
|
+
getBody<T>(body?: T): T;
|
|
31
|
+
getHeaders(key: string, headers: {
|
|
32
|
+
[key: string]: string;
|
|
33
|
+
}): string;
|
|
34
|
+
isType<T>(key: string, value: string, type: string, assertion: (value: string) => T): T;
|
|
35
|
+
exists<T>(type: string, key: string, value: string, object?: T): T;
|
|
36
|
+
isRegex(key: string, value: string, regex: string): string;
|
|
37
|
+
isDate(key: string, value: string, format: string): Date;
|
|
38
|
+
getFallback(): FormalError;
|
|
39
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { HttpRequest } from "../api/Service";
|
|
2
|
+
export interface Module {
|
|
3
|
+
id: string;
|
|
4
|
+
tag: string;
|
|
5
|
+
host: string;
|
|
6
|
+
}
|
|
7
|
+
export default class BusinessProxy {
|
|
8
|
+
private modules;
|
|
9
|
+
setModules(modules: {
|
|
10
|
+
[key: string]: Module;
|
|
11
|
+
}): void;
|
|
12
|
+
callFormal<Req, Res>(tag: string, method: string, endpoint: string, request: HttpRequest<Req>): Promise<Res | undefined>;
|
|
13
|
+
call<Req, Res>(tag: string, method: string, endpoint: string, request: HttpRequest<Req>): Promise<Res>;
|
|
14
|
+
private map;
|
|
15
|
+
}
|