mp-holistic
Version:
si prefigge l'umile compito di aiutare nella creazione di : - un server API REST mediante express - un semplice ORM per Postgres Questo tramite decoratori o classi particolari.
818 lines (815 loc) • 43.3 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ListaExpressMetodo = exports.ExpressMetodo = void 0;
const express_slow_down_1 = __importDefault(require("express-slow-down"));
const express_rate_limit_1 = __importDefault(require("express-rate-limit"));
const helmet_1 = __importDefault(require("helmet"));
const cors_1 = __importDefault(require("cors"));
const fs_1 = __importDefault(require("fs"));
const metodo_metadata_1 = require("../metadata/metodo.metadata");
const parametro_express_1 = require("./parametro.express");
const MetodoParametri_1 = require("./metodo/MetodoParametri");
const MetodoVettori_1 = require("./metodo/MetodoVettori");
const MetodoEventi_1 = require("./metodo/MetodoEventi");
const MetodoLimitazioni_1 = require("./metodo/MetodoLimitazioni");
const utility_1 = require("./utility/utility");
const ErroreMio_1 = require("./utility/ErroreMio");
const main_express_1 = require("./main.express");
const utility_2 = require("../utility");
const MetodoSpawProcess_1 = require("./metodo/MetodoSpawProcess");
class ExpressMetodo extends metodo_metadata_1.MetadataMetodo {
constructor(item) {
super(item);
this.metodoSpawProcess = new MetodoSpawProcess_1.MetodoSpawProcess();
this.metodoEventi = new MetodoEventi_1.MetodoEventi();
this.metodoParametri = new MetodoParametri_1.MetodoParametri();
this.metodoLimitazioni = new MetodoLimitazioni_1.MetodoLimitazioni();
this.metodoVettori = new MetodoVettori_1.MetodoVettori();
this.listaParametri = new parametro_express_1.ListaExpressParametro();
if (item.metodoEventi)
this.metodoEventi.Init(item.metodoEventi);
if (item.metodoParametri)
this.metodoParametri.Init(item.metodoParametri, this.nomeVariante);
else
this.metodoParametri.Init({ path: this.nomeVariante });
if (item.metodoLimitazioni)
this.metodoLimitazioni.Init(item.metodoLimitazioni);
if (item.metodoVettori)
this.metodoVettori.Init(item.metodoVettori);
if (item.metodoSpawProcess)
this.metodoSpawProcess.Init(item.metodoSpawProcess);
}
Init(item) {
/* this.metodoParametri.path = item.nomeVariante; */
if (item.listaParametri != undefined)
this.listaParametri = item.listaParametri;
if (item.metodoAvviabile)
this.metodoAvviabile = item.metodoAvviabile;
this.metodoEventi.Init(item.metodoEventi);
this.metodoParametri.Init(item.metodoParametri, this.nomeVariante);
this.metodoLimitazioni.Init(item.metodoLimitazioni);
this.metodoVettori.Init(item.metodoVettori);
this.metodoSpawProcess.Init(item.metodoSpawProcess);
}
Mergia(item) {
super.Mergia(item);
if (item.listaParametri != undefined && this.listaParametri != undefined)
this.listaParametri.Mergia(item.listaParametri);
if (item.metodoAvviabile)
this.metodoAvviabile = item.metodoAvviabile;
this.metodoEventi.Init(item.metodoEventi);
this.metodoParametri.Init(item.metodoParametri, this.nomeVariante);
this.metodoLimitazioni.Init(item.metodoLimitazioni);
this.metodoVettori.Init(item.metodoVettori);
this.metodoSpawProcess.Init(item.metodoSpawProcess);
}
GetThis() { return this; }
ConfiguraRottaApplicazione(app, percorsi) {
this.metodoParametri.percorsi.patheader = percorsi.patheader;
this.metodoParametri.percorsi.porta = percorsi.porta;
const pathGlobalTmp = percorsi.pathGlobal;
const pathGlobal = percorsi.pathGlobal + '/' + this.metodoParametri.path;
this.metodoParametri.percorsi.pathGlobal = pathGlobal;
/* */
const middlew = [];
let percorsoTmp = '';
if (this.metodoParametri.percorsoIndipendente) {
percorsoTmp = '/' + this.metodoParametri.path;
this.metodoParametri.percorsi.pathGlobal = percorsoTmp;
}
else {
percorsoTmp = this.metodoParametri.percorsi.pathGlobal;
}
if (this.metodoAvviabile != undefined) {
this.ConfiguraRotteSwitch(app, percorsoTmp, middlew);
}
if (this.metodoVettori.listaHtml) {
percorsoTmp = '';
for (let index = 0; index < this.metodoVettori.listaHtml.length; index++) {
const element = this.metodoVettori.listaHtml[index];
if (element.percorsoIndipendente)
percorsoTmp = '/' + element.path;
else
percorsoTmp = pathGlobalTmp + '/' + element.path;
if (this.metodoAvviabile != undefined) {
this.ConfiguraRotteHtml(app, percorsoTmp, element.contenuto);
}
}
}
}
ConfiguraRotteHtml(app, percorsoTmp, contenuto) {
this.metodoAvviabile.body;
let corsOptions = {};
corsOptions = {
methods: 'GET',
};
if (this.metodoLimitazioni.cors == undefined) {
this.metodoLimitazioni.cors = (0, cors_1.default)(corsOptions);
}
if (this.metodoLimitazioni.helmet == undefined) {
this.metodoLimitazioni.helmet = (0, helmet_1.default)();
}
app.get(percorsoTmp,
/* this.cors,
this.helmet, */
(req, res) => __awaiter(this, void 0, void 0, function* () {
if (this.metodoVettori.listaHtml)
res.send(contenuto);
else
res.sendStatus(404);
}));
}
PrintStruttura() {
let parametri = "";
for (let index = 0; index < this.listaParametri.length; index++) {
const element = this.listaParametri[index];
parametri = parametri + element.PrintStruttura() + '\n';
}
if (this.metodoEventi)
parametri = parametri + this.metodoEventi.PrintStruttura();
if (this.metodoParametri)
parametri = parametri + this.metodoParametri.PrintStruttura();
if (this.metodoLimitazioni)
parametri = parametri + this.metodoLimitazioni.PrintStruttura();
if (this.metodoVettori)
parametri = parametri + this.metodoVettori.PrintStruttura();
const tmp = this.nomeVariante + ' | ' + this.metodoParametri.percorsi.pathGlobal + '\n' + parametri + '\n' + 'metodo : \n' + this.metodoAvviabile;
////console.log(tmp);
return tmp;
}
ConfiguraRotteSwitch(app, percorsoTmp, middlew) {
var _a, _b, _c, _d, _e, _f, _g, _h;
let corsOptions = {};
const apiRateLimiter = this.metodoLimitazioni.rate_limit ?
(0, express_rate_limit_1.default)(this.metodoLimitazioni.rate_limit)
: undefined;
const apiSpeedLimiter = this.metodoLimitazioni.slow_down ?
(0, express_slow_down_1.default)(this.metodoLimitazioni.slow_down)
: undefined;
if (this.metodoSpawProcess.isSpawTrigger && main_express_1.MainExpress.isSottoProcesso == true) {
//
this.metodoAvviabile.body;
corsOptions = {
methods: 'GET',
};
if (this.metodoLimitazioni.cors == undefined) {
this.metodoLimitazioni.cors = (0, cors_1.default)((_a = this.metodoLimitazioni.corsOption) !== null && _a !== void 0 ? _a : corsOptions);
}
if (this.metodoLimitazioni.helmet == undefined) {
this.metodoLimitazioni.helmet = undefined; //helmet();
}
app.get(percorsoTmp, this.metodoLimitazioni.cors, this.metodoLimitazioni.helmet, middlew,
//cacheMiddleware.route(this.cacheOptionRedis ?? <OptionsCache>{ expire: 1 , client: redisClient }),
apiRateLimiter, apiSpeedLimiter,
/*csrfProtection,*/
(req, res) => __awaiter(this, void 0, void 0, function* () {
//console.log("GET");
res.statusCode = 999;
res.send({
dalleFondamentaConFurore: 'Hei? sei molto in basso ma non puoi scavare piu di cosi! Sei sicuro di stare ad andare nella direzione giusta?'
});
}));
}
else {
//
//const csrfProtection = csrf({ cookie: true })
switch (this.metodoParametri.tipo) {
case 'get':
this.metodoAvviabile.body;
corsOptions = {
methods: 'GET',
};
this.CostruisciCors_e_Helmet((_b = this.metodoLimitazioni.corsOption) !== null && _b !== void 0 ? _b : corsOptions);
app.get(percorsoTmp, this.metodoLimitazioni.cors, this.metodoLimitazioni.helmet, middlew,
//cacheMiddleware.route(this.metodoLimitazioni.cacheOptionRedis ?? <OptionsCache>{ expire: 1 , client: redisClient }),
apiRateLimiter, apiSpeedLimiter, /*csrfProtection,*/ (req, res) => __awaiter(this, void 0, void 0, function* () {
//console.log("GET");
const id = {
id: (0, utility_2.GenerateID)(),
inizio: new Date().getTime(),
differenza: undefined,
fine: undefined,
req: undefined,
res: undefined
};
yield this.ChiamataGenerica(req, res, id);
}));
break;
case 'post':
corsOptions = {
methods: 'POST'
};
this.CostruisciCors_e_Helmet((_c = this.metodoLimitazioni.corsOption) !== null && _c !== void 0 ? _c : corsOptions);
app.post(percorsoTmp, this.metodoLimitazioni.cors, this.metodoLimitazioni.helmet, middlew,
//cacheMiddleware.route(this.metodoLimitazioni.cacheOptionRedis ?? <OptionsCache>{ expire: 1 /* secondi */, client: redisClient }),
apiRateLimiter, apiSpeedLimiter, /*csrfProtection,*/ (req, res) => __awaiter(this, void 0, void 0, function* () {
//console.log("POST");
const id = {
id: (0, utility_2.GenerateID)(),
inizio: new Date().getTime(),
differenza: undefined,
fine: undefined,
req: undefined,
res: undefined
};
yield this.ChiamataGenerica(req, res, id);
}));
break;
case 'delete':
corsOptions = {
methods: "DELETE"
};
this.CostruisciCors_e_Helmet((_d = this.metodoLimitazioni.corsOption) !== null && _d !== void 0 ? _d : corsOptions);
app.delete(percorsoTmp, this.metodoLimitazioni.cors, this.metodoLimitazioni.helmet, middlew,
//cacheMiddleware.route(this.metodoLimitazioni.cacheOptionRedis ?? <OptionsCache>{ expire: 1 /* secondi */, client: redisClient }),
apiRateLimiter, apiSpeedLimiter, /*csrfProtection,*/ (req, res) => __awaiter(this, void 0, void 0, function* () {
//console.log("DELETE");
const id = {
id: (0, utility_2.GenerateID)(),
inizio: new Date().getTime(),
differenza: undefined,
fine: undefined,
req: undefined,
res: undefined
};
yield this.ChiamataGenerica(req, res, id);
}));
break;
case 'patch':
corsOptions = {
methods: "PATCH"
};
this.CostruisciCors_e_Helmet((_e = this.metodoLimitazioni.corsOption) !== null && _e !== void 0 ? _e : corsOptions);
app.patch(percorsoTmp, this.metodoLimitazioni.cors, this.metodoLimitazioni.helmet, middlew,
//cacheMiddleware.route(this.metodoLimitazioni.cacheOptionRedis ?? <OptionsCache>{ expire: 1 /* secondi */, client: redisClient }),
apiRateLimiter, apiSpeedLimiter, /*csrfProtection,*/ (req, res) => __awaiter(this, void 0, void 0, function* () {
//console.log("PATCH");
const id = {
id: (0, utility_2.GenerateID)(),
inizio: new Date().getTime(),
differenza: undefined,
fine: undefined,
req: undefined,
res: undefined
};
yield this.ChiamataGenerica(req, res, id);
}));
break;
case 'purge':
corsOptions = {
methods: "PURGE"
};
this.CostruisciCors_e_Helmet((_f = this.metodoLimitazioni.corsOption) !== null && _f !== void 0 ? _f : corsOptions);
app.purge(percorsoTmp, this.metodoLimitazioni.cors, this.metodoLimitazioni.helmet, middlew,
//cacheMiddleware.route(this.metodoLimitazioni.cacheOptionRedis ?? <OptionsCache>{ expire: 1 /* secondi */, client: redisClient }),
apiRateLimiter, apiSpeedLimiter, /*csrfProtection,*/ (req, res) => __awaiter(this, void 0, void 0, function* () {
//console.log("PURGE");
const id = {
id: (0, utility_2.GenerateID)(),
inizio: new Date().getTime(),
differenza: undefined,
fine: undefined,
req: undefined,
res: undefined
};
yield this.ChiamataGenerica(req, res, id);
}));
break;
case 'put':
corsOptions = {
methods: "PUT"
};
this.CostruisciCors_e_Helmet((_g = this.metodoLimitazioni.corsOption) !== null && _g !== void 0 ? _g : corsOptions);
app.put(percorsoTmp, this.metodoLimitazioni.cors, this.metodoLimitazioni.helmet, middlew,
//cacheMiddleware.route(this.cacheOptionRedis ?? {}),
apiRateLimiter, apiSpeedLimiter, /*csrfProtection,*/ (req, res) => __awaiter(this, void 0, void 0, function* () {
//console.log("PUT");
const id = {
id: (0, utility_2.GenerateID)(),
inizio: new Date().getTime(),
differenza: undefined,
fine: undefined,
req: undefined,
res: undefined
};
yield this.ChiamataGenerica(req, res, id);
}));
break;
case 'file':
corsOptions = {
methods: "GET"
};
this.CostruisciCors_e_Helmet((_h = this.metodoLimitazioni.corsOption) !== null && _h !== void 0 ? _h : corsOptions);
app.get(percorsoTmp, this.metodoLimitazioni.cors, this.metodoLimitazioni.helmet, middlew,
//cacheMiddleware.route(this.cacheOptionRedis ?? {}),
apiRateLimiter, apiSpeedLimiter, /*csrfProtection,*/ (req, res) => __awaiter(this, void 0, void 0, function* () {
//console.log("PUT");
const id = {
id: (0, utility_2.GenerateID)(),
inizio: new Date().getTime(),
differenza: undefined,
fine: undefined,
req: undefined,
res: undefined
};
yield this.ChiamataGenerica(req, res, id, this.metodoParametri.isFile);
}));
break;
}
}
}
ChiamataGenerica(req, res, id, isFile) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
let passato = false;
let logIn;
let logOut;
let tmp;
if (id && 'req' in id && req) {
id.req = req;
}
const key = this.metodoLimitazioni.cacheOptionMemory != undefined ? (0, utility_1.CalcolaChiaveMemoryCache)(req) : undefined;
const durationSecondi = this.metodoLimitazioni.cacheOptionMemory != undefined ? this.metodoLimitazioni.cacheOptionMemory.durationSecondi : undefined;
try {
//console.log('Inizio Chiamata generica per : ' + this.percorsi.pathGlobal);
logIn = (0, utility_1.InizializzaLogbaseIn)(req, this.nomeOriginale.toString());
if (this.metodoEventi.onPrimaDiEseguire)
req = yield this.metodoEventi.onPrimaDiEseguire(req);
const cachedBody = key == undefined ? undefined : main_express_1.MainExpress.cache.get(key);
if (cachedBody == undefined || cachedBody == null) {
tmp = yield this.Esegui(req);
if (tmp != undefined) {
if (this.metodoEventi.onRispostaControllatePradefinita &&
this.metodoVettori.VerificaPresenzaRispostaControllata(tmp) == false) {
const rispostaPilotata = yield this.metodoEventi.onRispostaControllatePradefinita(tmp);
/* if (this.isSpawTrigger && this.VerificaPresenzaSpawnTrigger(this.isSpawTrigger, rispostaPilotata)) {
console.log('è un evento scatenante dovrei avviare un nuovo processo.');
} */
(0, utility_1.Rispondi)(res, rispostaPilotata, id, isFile, key, durationSecondi);
//throw new Error("Attenzione, cosa stai facendo?");
}
else {
try {
if (!this.metodoVettori.VerificaTrigger(req)) {
if (this.metodoVettori.VerificaPresenzaRispostaControllata(tmp)) {
tmp = yield this.metodoVettori.EseguiRispostaControllata(tmp);
}
try {
if (this.metodoSpawProcess.isSpawTrigger && this.metodoSpawProcess.VerificaPresenzaSpawnTrigger(tmp)
/* && (tmp instanceof IReturn) */ ) {
if (tmp.body instanceof Object) {
const tt = tmp.body[this.metodoSpawProcess.isSpawTrigger];
let t1 = false;
for (let index = 0; index < main_express_1.MainExpress.vettoreProcessi.length && t1 == false; index++) {
const x = main_express_1.MainExpress.vettoreProcessi[index];
if (String(x.nomeVariabile) == String(this.metodoSpawProcess.isSpawTrigger)
&& String(x.valoreValiabile) == String(tt)) {
t1 = true;
}
}
if (tt != undefined && tt != '' && t1 == false) {
let porta = this.metodoParametri.percorsi.porta + 2;
this.EseguiProcessoParallelo(this.metodoSpawProcess, tt, porta);
}
}
}
}
catch (error) {
console.log(error);
}
(0, utility_1.Rispondi)(res, tmp !== null && tmp !== void 0 ? tmp : (0, utility_1.ConstruisciErrore)('Attenzione! Rimpiazzato.'), id, isFile, key, durationSecondi);
}
else {
const risposta = this.metodoVettori.CercaRispostaConTrigger(req);
if (risposta) {
let source = "";
if (risposta.stato >= 1 && risposta.stato < 600) {
if (risposta.htmlPath != undefined)
source = fs_1.default.readFileSync(risposta.htmlPath).toString();
else if (risposta.html != undefined)
source = risposta.html;
else
throw new Error("Errorissimo");
(0, utility_1.Rispondi)(res, { stato: risposta.stato, body: source }, id, isFile, key, durationSecondi);
passato = true;
}
else {
(0, utility_1.Rispondi)(res, tmp, id, isFile, key, durationSecondi);
passato = true;
}
}
else {
throw new Error("Errore nel trigger");
}
}
}
catch (errore) {
const err = (0, utility_1.ConstruisciErrore)(errore);
err.stato = 598;
(0, utility_1.Rispondi)(res, err, id, isFile, key, durationSecondi);
}
}
//if (this.onPrimaDiTerminareLaChiamata) tmp = this.onPrimaDiTerminareLaChiamata(tmp);
}
else {
throw new Error("Attenzione qualcosa è andato storto nell'Esegui(req), guarda @mpMet");
}
}
else {
const parametri = this.listaParametri.EstraiParametriDaRequest(req);
let valido = undefined;
res.setHeader("specchio", "riflesso");
if (this.metodoEventi.Validatore) {
valido = (_a = this.metodoEventi.Validatore(parametri, this.listaParametri)) !== null && _a !== void 0 ? _a : undefined;
if (valido && (valido === null || valido === void 0 ? void 0 : valido.approvato) == true && this.metodoEventi.Istanziatore) {
yield this.metodoEventi.Istanziatore(parametri, this.listaParametri);
res.setHeader('Content-Type', 'application/json');
res.status(cachedBody.stato).send(cachedBody.body);
}
else {
res.status(555).send({ errore: 'Errore cache.' });
}
}
else if (parametri.errori.length > 0) {
valido = { approvato: false, stato: 400, messaggio: 'Parametri in errore.' /* parametri.errori.toString() */ };
res.status(555).send({ errore: 'Errore cache.' });
}
else {
valido = { approvato: true, stato: 200, messaggio: '' };
res.setHeader('Content-Type', 'application/json');
res.status(cachedBody.stato).send(cachedBody.body);
}
}
logOut = (0, utility_1.InizializzaLogbaseOut)(res, this.nomeOriginale.toString());
if (this.metodoEventi.onChiamataCompletata) {
this.metodoEventi.onChiamataCompletata(logIn, tmp, logOut, undefined);
}
if (this.metodoEventi.onLog) {
this.metodoEventi.onLog(logIn, tmp, logOut, undefined);
}
}
catch (error) {
if (this.metodoEventi.onChiamataInErrore) {
tmp = yield this.metodoEventi.onChiamataInErrore(logIn, tmp, logOut, error);
/* let num = 0;
num = tmp.stato;
res.statusCode = Number.parseInt('' + num);
res.send(tmp.body); */
(0, utility_1.Rispondi)(res, tmp, id, isFile, key, durationSecondi);
}
else if (passato == false) {
if (error instanceof ErroreMio_1.ErroreMio) {
//console.log("ciao");
/* return <IReturn>{
stato: (<ErroreMio>error).codiceErrore,
body: {
errore: (<ErroreMio>error).message
}
}; */
(0, utility_1.Rispondi)(res, {
stato: error.codiceErrore,
body: { errore: error.message }
}, id, isFile, key, durationSecondi);
}
else {
(0, utility_1.Rispondi)(res, {
stato: 500,
body: {
error: error,
passato: passato,
info: ''
}
}, id, isFile, key, durationSecondi);
}
}
else {
(0, utility_1.Rispondi)(res, {
stato: 500,
body: {
error: error,
passato: passato,
info: ''
}
}, id, isFile, key, durationSecondi);
}
if (this.metodoEventi.onLog) {
this.metodoEventi.onLog(logIn, tmp, logOut, error);
}
}
});
}
EseguiProcessoParallelo(metodoSpawProcess, valoreValiabile, porta) {
main_express_1.MainExpress.AggiungiProcessoParallelo(metodoSpawProcess, valoreValiabile, porta);
/* try {
if (MainExpress.vettoreProcessi.length > 0) {
if ('porta' in MainExpress.vettoreProcessi[MainExpress.vettoreProcessi.length - 1])
porta = MainExpress.vettoreProcessi[MainExpress.vettoreProcessi.length - 1].porta + 1;
}
if (MainExpress.vettoreProcessi.length == 0) {
porta = porta + (Math.random() * 100 + 30) +
(
(Math.random() * 10 * Math.random() * 20 * Math.random() * 30) +
(Math.random() * 10 * Math.random() * 20 * Math.random() * 30) -
(Math.random() * 10 * Math.random() * 20 * Math.random() * 10) +
(Math.random() * 10 * Math.random() * 20 * Math.random() * 30) -
(Math.random() * 10 * Math.random() * 20 * Math.random() * 5)
);
}
} catch (error) {
porta = porta + (Math.random() * 100 + 30) +
(
(Math.random() * 10 * Math.random() * 20 * Math.random() * 30) +
(Math.random() * 10 * Math.random() * 20 * Math.random() * 30) -
(Math.random() * 10 * Math.random() * 20 * Math.random() * 10) +
(Math.random() * 10 * Math.random() * 20 * Math.random() * 30) -
(Math.random() * 10 * Math.random() * 20 * Math.random() * 5)
);
}
try {
porta = Number(porta.toFixed(0));
const temporaneamente = `node ./${MainExpress.pathExe}`;
console.log(temporaneamente);
const proc = exec(`node ./${MainExpress.pathExe}${MainExpress.pathExeIIparte}${porta}`); //exec(`npm run start-esempio`);
MainExpress.vettoreProcessi.push({
porta: porta,
nomeVariabile: metodoSpawProcess.isSpawTrigger ?? String(randomUUID()),
valoreValiabile: valoreValiabile,
vettorePossibiliPosizioni: metodoSpawProcess.checkSpawTrigger ?? [],
processo: proc
});
} catch (error) {
console.log(error);
} */
}
Esegui(req) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
const parametri = this.listaParametri.EstraiParametriDaRequest(req);
/*!!!
Qui bisogna che ci metto qualcosa per convertire i parametri in una chimata, quindi
connettermi al db ed estrarli con una query.
i parametri devono essere segnalati come tali.
come fare a gestire il ritorno, perche io cosi facendo sto eseguendo una select
come eseguire una delete o una set?
*/
let valido = undefined;
if (this.metodoEventi.Validatore) {
valido = (_a = this.metodoEventi.Validatore(parametri, this.listaParametri)) !== null && _a !== void 0 ? _a : undefined;
}
else if (parametri.errori.length > 0) {
valido = { approvato: false, stato: 400, messaggio: 'Parametri in errore.' /* parametri.errori.toString() */ };
}
else {
valido = { approvato: true, stato: 200, messaggio: '' };
}
/* verifico che il metodo possa essere eseguito come volevasi ovvero approvato = true o undefiend */
if ((valido && (valido.approvato == undefined || valido.approvato == true))
|| (!valido && parametri.errori.length == 0)) {
let tmp = {
body: {}, nonTrovati: parametri.nontrovato,
inErrore: parametri.errori, stato: 200
};
try {
const tmpRitorno = yield this.EseguiMetodo(parametri);
const tmpReturn = tmpRitorno.result;
if ((0, utility_1.IsJsonString)(tmpReturn)) {
if (tmpReturn.name === "ErroreMio" || tmpReturn.name === "ErroreGenerico") {
//console.log("ciao");
}
if ('body' in tmpReturn) {
tmp.body = tmpReturn.body;
}
else {
if (typeof tmpReturn === 'object' && tmpReturn !== null)
tmp.body = tmpReturn;
else {
tmp.body = { tmpReturn };
}
}
if ('stato' in tmpReturn) {
tmp.stato = tmpReturn.stato;
}
else {
tmp.stato = 298;
}
}
else {
/* if ('body' in tmpReturn && 'stato' in tmpReturn && typeof tmpReturn.body === 'string') {
tmp.stato = tmpReturn.stato;
tmp.body = tmpReturn.body;
}
else */ if (typeof tmpReturn === 'object' && tmpReturn !== null &&
'stato' in tmpReturn && 'body' in tmpReturn) {
// eslint-disable-next-line prefer-const
for (let attribut in tmpReturn.body) {
tmp.body[attribut] = tmpReturn.body[attribut];
}
tmp.body = Object.assign({}, tmpReturn.body);
tmp.stato = tmpReturn.stato;
}
else if (typeof tmpReturn === 'object') {
tmp.body = tmpReturn;
tmp.stato = 200; //299;
}
else if (tmpReturn) {
tmp.body = tmpReturn; //{ 'messaggio': tmpReturn };
tmp.stato = 200; //299;
}
else {
tmp = {
body: { "Errore Interno filtrato ": 'internal error!!!!' },
stato: 599,
nonTrovati: parametri.nontrovato
};
}
}
tmp.attore = tmpRitorno.attore;
return tmp;
}
catch (error) {
if (error instanceof ErroreMio_1.ErroreMio) {
throw error;
}
else {
throw new ErroreMio_1.ErroreMio({
codiceErrore: 598,
messaggio: error.message,
percorsoErrore: error.stack,
});
}
return tmp;
}
} /* altrimenti lo vado a costruire */
else {
let tmp = {
body: parametri.errori,
nonTrovati: parametri.nontrovato,
inErrore: parametri.errori,
stato: 597
};
if (valido) {
if (valido.body != undefined) {
tmp = {
body: valido.body,
stato: (_b = valido.stato) !== null && _b !== void 0 ? _b : 596,
};
}
else {
tmp = {
body: tmp.body,
stato: (_c = valido.stato) !== null && _c !== void 0 ? _c : 595,
};
}
}
return tmp;
}
throw new Error("Attenzione qualcosa è andato storto.");
}
catch (error) {
console.log('');
throw error;
}
});
}
EseguiMetodo(parametri) {
return __awaiter(this, void 0, void 0, function* () {
let tmpReturn = '';
let attore = undefined;
if (this.metodoEventi.onPrimaDiEseguireMetodo)
parametri = yield this.metodoEventi.onPrimaDiEseguireMetodo(parametri);
/* let count = 0; */
if (this.metodoEventi.Istanziatore) {
const classeInstanziata = yield this.metodoEventi.Istanziatore(parametri, this.listaParametri);
attore = classeInstanziata;
// eslint-disable-next-line prefer-spread
tmpReturn = yield classeInstanziata[this.nomeOriginale.toString()].apply(classeInstanziata, parametri.valoriParametri);
}
else {
tmpReturn = yield this.metodoAvviabile /* .value */.apply(this.metodoAvviabile /* .value */, parametri.valoriParametri);
}
if (this.metodoVettori.ListaSanificatori && 'length' in this.metodoVettori.ListaSanificatori && this.metodoVettori.ListaSanificatori.length > 0)
tmpReturn = (0, utility_1.SostituisciRicorsivo)(this.metodoVettori.ListaSanificatori, tmpReturn);
if (this.metodoEventi.onDopoAverTerminatoLaFunzione)
tmpReturn = this.metodoEventi.onDopoAverTerminatoLaFunzione(tmpReturn);
return {
attore: attore,
result: tmpReturn
};
});
}
CostruisciCors_e_Helmet(corsOptions) {
if (this.metodoLimitazioni.cors == undefined && this.metodoLimitazioni.cors != false) {
this.metodoLimitazioni.cors = (0, cors_1.default)(corsOptions);
}
else if (this.metodoLimitazioni.cors == false) {
this.metodoLimitazioni.cors = [];
}
if (this.metodoLimitazioni.helmet == undefined && this.metodoLimitazioni.helmet != false) {
this.metodoLimitazioni.helmet = (0, helmet_1.default)();
}
else if (this.metodoLimitazioni.helmet == false) {
this.metodoLimitazioni.helmet = [];
}
}
}
exports.ExpressMetodo = ExpressMetodo;
class ListaExpressMetodo extends metodo_metadata_1.ListaMetadataMetodo {
constructor(item) {
super();
if (item)
for (let index = 0; index < item.length; index++) {
const element = new ExpressMetodo(item[index]);
const tmp = this.Cerca(element);
if (tmp)
tmp.Mergia(element);
}
}
Mergia(item) {
const t = super.Mergia(item);
return t;
/* for (let index = 0; index < item.length; index++) {
const element = item[index];
const elementoCercato = this.Cerca(element);
if (elementoCercato == undefined) {
const tmp = element;
this.push(tmp);
}
else {
elementoCercato.Mergia(elementoCercato);
}
} */
}
/* Init(item: IListaMetadataMetodo) {
for (let index = 0; index < item.length; index++) {
const element = item[index];
const tmp = new MetadataMetodo(element);
this.push(tmp);
}
} */
CercaSeNoAggiungi(item) {
const t = super.CercaSeNoAggiungi(item);
return t;
/* let metodo = undefined;
for (let index = 0; index < this.length && metodo == undefined; index++) {
const element = this[index];
if (Meta.Compara(element, item) == 0) metodo = element.GetThis();
}
if (metodo == undefined) {
if (item.GetThis) {
metodo = <ExpressMetodo>item.GetThis(item);
}
else {
metodo = new MetadataMetodo(item);
}
this.AggiungiElemento(metodo);
}
return metodo; */
}
Cerca(item) {
const t = super.Cerca(item);
return t;
/* for (let index = 0; index < this.length; index++) {
const element = this[index];
if (Meta.Compara(element, item) == 0) return element;
}
return undefined; */
}
AggiungiElemento(item) {
const t = super.AggiungiElemento(item);
return t;
/* for (let index = 0; index < this.length; index++) {
const element = this[index];
if (Meta.Compara(element, item) == 0) {
this[index].Mergia(item);
return this[index];
}
}
this.push(item);
return item; */
}
ConfiguraListaRotteApplicazione(app, percorsi) {
for (let index = 0; index < this.length; index++) {
const element = this[index];
if (element.metodoParametri.interazione == 'rotta' || element.metodoParametri.interazione == 'ambo') {
//element.ConfiguraRotta(this.rotte, this.percorsi);
element.ConfiguraRottaApplicazione(app, percorsi);
}
//element.listaRotteGeneraChiavi=this.listaMetodiGeneraKey;
}
}
}
exports.ListaExpressMetodo = ListaExpressMetodo;
//# sourceMappingURL=metodo.express.js.map