UNPKG

@victorequena22/conneccion

Version:

Utilidades para mi uso que pongo a dispocion

120 lines (119 loc) 5.21 kB
"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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); 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;