@victorequena22/conneccion
Version:
Utilidades para mi uso que pongo a dispocion
214 lines (213 loc) • 8.67 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setCambio = exports.conCommos = exports.ConexionAvanzada = exports.ConexionCompleta = exports.ConexionBase = void 0;
const OnLineCheker_1 = require("./OnLineCheker");
const getURL_1 = require("./getURL");
class ConexionBase {
constructor() {
this.clase = '';
this.state = '';
this.init = true;
this.getCache = (url, setData) => {
const cache = localStorage.getItem(url);
if (cache !== null)
setData(JSON.parse(cache));
};
this.setCache = (path, data) => {
localStorage.removeItem(path);
localStorage.setItem(path, JSON.stringify(data));
};
this.getProxi = (path) => __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
const token = localStorage.getItem('Authorization');
const url = getURL_1.don + '/api/' + this.clase + path;
try {
const response = yield fetch(url, { headers: { 'Authorization': 'Bearer ' + token } });
const data = yield response.json();
if (data.error)
reject(data.error);
resolve(data);
}
catch (err) {
resolve(err);
}
}));
});
this.getProxiCache = (path, setData, setError) => __awaiter(this, void 0, void 0, function* () {
const url = getURL_1.don + '/api/' + this.clase + path;
this.getCache(url, setData);
const token = localStorage.getItem('Authorization');
try {
const response = yield fetch(url, { headers: { 'Authorization': 'Bearer ' + token } });
const data = yield response.json();
if (data.error && setError)
setError(data.error);
setData(data);
this.setCache(url, data);
}
catch (err) {
if (setError)
setError(err);
}
});
this.get = (url, setData, setError) => __awaiter(this, void 0, void 0, function* () {
const token = localStorage.getItem('Authorization');
try {
const response = yield fetch(url, { headers: { 'Authorization': 'Bearer ' + token } });
const data = yield response.json();
if (data.error && setError)
setError(data.error);
setData(data);
}
catch (err) {
if (setError)
setError(err);
}
});
this.postProxi = (path = '', obj = {}, success = (d) => { }, error = (d) => { }) => {
const url = getURL_1.don + '/api/' + this.clase + path;
this.post(url, obj, success, error);
};
this.post = (url = '', obj = {}, success = (d) => { }, error = (d) => { }) => {
let token = localStorage.getItem('Authorization');
let xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open("POST", url);
if (token !== null)
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.onreadystatechange = (e) => {
if (xhr.readyState !== 4) {
return;
}
if (xhr.status === 200) {
if (xhr.response.errores)
error(xhr.response.errores);
else
success(xhr.response);
}
else {
if ((0, OnLineCheker_1.isLine)())
error(xhr);
}
};
xhr.send(this.FormDataFotmat(obj));
};
this.FormDataFotmat = (o) => {
const formData = new FormData();
Object.keys(o).forEach(key => {
switch (typeof o[key]) {
case 'object':
formData.append(key, JSON.stringify(o[key]));
break;
default:
formData.append(key, o[key]);
}
});
return formData;
};
}
}
exports.ConexionBase = ConexionBase;
class ConexionCompleta extends ConexionBase {
constructor() {
super(...arguments);
this.cambios = (setData, setNot) => {
this.getProxiCache('/cambios/' + this.state.replace(' ', '/'), data => {
if (data.data.length !== 0) {
this.state = data.fecha;
setData(data.data);
}
if (setNot)
setNot();
}, setNot);
};
this.lista = (setData) => __awaiter(this, void 0, void 0, function* () {
try {
const data = yield this.getProxi('/lista');
if (Array.isArray(yield data))
setData(yield data);
}
catch (e) {
if ((0, OnLineCheker_1.isLine)())
console.log(e);
}
});
this.detalle = (codigo, setData) => __awaiter(this, void 0, void 0, function* () {
try {
const data = yield this.getProxi('/detalles/' + codigo);
setData(yield data);
}
catch (err) {
if ((0, OnLineCheker_1.isLine)())
console.log(err);
}
});
this.cancelar = (codigo, setData) => __awaiter(this, void 0, void 0, function* () {
try {
const data = yield this.getProxi('/cancelar/' + codigo);
if (setData)
setData(data);
}
catch (err) {
if ((0, OnLineCheker_1.isLine)())
console.log(err);
}
});
this.nuevo = (data, setData, setError) => {
this.post(getURL_1.don + '/api/' + this.clase + '/nuevo', data, setData, setError);
};
this.actualizar = (codigo, data, setData, setError) => {
this.post(getURL_1.don + '/api/' + this.clase + '/actualizar/' + codigo, data, setData, setError);
};
}
}
exports.ConexionCompleta = ConexionCompleta;
class ConexionAvanzada extends ConexionCompleta {
constructor() {
super(...arguments);
this.cambiosData = (data, setData, setNot) => {
this.postProxi('/cambios/' + this.state.replace(' ', '/'), { data }, setData, setNot);
};
this.listaData = (data, setData, setNot) => {
this.postProxi('/lista', { data }, setData, setNot);
};
this.detalleData = (codigo, data, setData, setNot) => {
this.postProxi('/detalles/' + codigo, { data }, setData, setNot);
};
}
}
exports.ConexionAvanzada = ConexionAvanzada;
class Commos extends ConexionBase {
constructor() {
super(...arguments);
this.clase = 'Commons';
this.tags = (setData) => __awaiter(this, void 0, void 0, function* () {
this.get('/api/Commons/tags', setData);
});
}
}
function conCommos() {
return new Commos();
}
exports.conCommos = conCommos;
function setCambio(data, c) {
const push = data.filter(d => {
const i = c.findIndex(c => c.id === d.id);
if (i > -1) {
c[i] = d;
return false;
}
return true;
});
return [...c, ...push];
}
exports.setCambio = setCambio;