UNPKG

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.

214 lines 8.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ListaExpressParametro = exports.ExpressParametro = void 0; const parametro_metadata_1 = require("../metadata/parametro.metadata"); const utility_1 = require("../utility"); const ErroreMio_1 = require("./utility/ErroreMio"); class ExpressParametro extends parametro_metadata_1.MetadataParametro { constructor(item) { super(item); this.dovePossoTrovarlo = 'rotta'; this.posizione = 'query'; this.autenticatore = false; if (item.posizione) this.posizione = item.posizione; if (item.autenticatore) this.autenticatore = item.autenticatore; } Verifica() { try { if ((0, utility_1.VerificaGenerica)(this.tipo, this.valore)) { return true; } else { return false; } } catch (error) { console.log(''); throw error; } } Init(item) { var _a, _b; if (item.dovePossoTrovarlo != undefined) this.dovePossoTrovarlo = item.dovePossoTrovarlo; else this.dovePossoTrovarlo = 'rotta'; if (item.Validatore != undefined) this.Validatore = item.Validatore; this.autenticatore = (_a = item.autenticatore) !== null && _a !== void 0 ? _a : false; this.posizione = (_b = item.posizione) !== null && _b !== void 0 ? _b : 'query'; } PrintStruttura() { let tmp = ''; tmp = tmp + "- " + this.tipo.toString() + " : " + this.nomeVariante + ' |\n'; //tmp = tmp + '' + this. + ''; tmp = tmp + '' + JSON.stringify(this, null, 4) + ''; return tmp; } } exports.ExpressParametro = ExpressParametro; class ListaExpressParametro extends parametro_metadata_1.ListaMetadataParametro { constructor(item) { super(); if (item) for (let index = 0; index < item.length; index++) { const element = new ExpressParametro(item[index]); const tmp = this.Cerca(element); if (tmp) tmp.Mergia(element); } } GetThis() { return this; } 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); } } */ } CercaSeNoAggiungi(item) { const t = super.CercaSeNoAggiungi(item); return t; /* let parametro = undefined; for (let index = 0; index < this.length && parametro == undefined; index++) { const element = this[index]; if (Meta.Compara(element, item) == 0) parametro = element; } if (parametro == undefined) { if (item.GetThis) { parametro = <ExpressParametro>item.GetThis(item); } else { parametro = new MetadataParametro(item); } this.AggiungiElemento(parametro); } return parametro; */ } 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; */ } /** * Estrae i parametri dalla request, per estrarli legge i valori di se stesso e ne verifica le seguenti cose: * - che sia obbligatorio * - che sia Validato se prevede un validatore * - che sia Verificato * @param richiesta * @returns */ EstraiParametriDaRequest(richiesta) { const ritorno = { errori: [], nontrovato: [], valoriParametri: [] }; for (let index = this.length - 1; index >= 0; index--) { const element = this[index]; let tmp = undefined; /* Verifico che l'emento sia o nel body o nella query o nella header, basandomi sulla sua posizione e lo metto in tmp*/ if (richiesta.body[element.nomeVariante] != undefined && element.posizione == 'body') { tmp = richiesta.body[element.nomeVariante]; } else if (richiesta.query[element.nomeVariante] != undefined && element.posizione == 'query') { tmp = richiesta.query[element.nomeVariante]; } else if (richiesta.headers[element.nomeVariante] != undefined && element.posizione == 'header') { tmp = richiesta.headers[element.nomeVariante]; } else { // se non c'è allora tmp = undefined, pensero poi a costruire l'errore /* if (element.obbligatorio == true) { ritorno.nontrovato.push({ nome: element.nome, posizioneParametro: element.indexParameter }); } else { tmp = undefined; } */ tmp = undefined; } ritorno.valoriParametri.push(tmp); element.valore = tmp; //vado a verificare il validatore, a cui sara riservato il massimo della priorita, se presente non eseguirà altri controlli oltre a lui if (element.Validatore) { const rit = element.Validatore(tmp); if (rit.approvato == false) { rit.terminale = element; ritorno.errori.push(rit); } } else { // se il validatore non c'è lo vado a verificare if (element.Verifica() == false) { ritorno.errori.push({ approvato: false, terminale: element, messaggio: 'Attenzione parametro: ' + element.nomeVariante + ', parametro non convertibile.' }); } else if (tmp == undefined && element.obbligatorio == true) { ritorno.errori.push({ approvato: false, terminale: element, messaggio: 'Attenzione parametro: ' + element.nomeVariante + ', segnato come obbligatorio. Messaggio auto creato.' }); } } if (element.autenticatore == true && tmp == undefined) { throw new ErroreMio_1.ErroreMio({ codiceErrore: 401, messaggio: 'Attenzione autenticazione mancante.' }); } } return ritorno; } GetAutenticatore() { const ritorno = []; for (let index = 0; index < this.length; index++) { const element = this[index]; if (element.autenticatore == true) { ritorno.push(element); } } if (ritorno.length > 0) { if (ritorno.length == 1) { return ritorno[0]; } else { return ritorno; } } else { return undefined; } } } exports.ListaExpressParametro = ListaExpressParametro; //# sourceMappingURL=parametro.express.js.map