import { RdLib } from '../base/rdLib'; import { AppComponent } from '../app.component'; import { AESUtil } from "./aesUtil"; const xhr = new XMLHttpRequest(); // @dynamic export class ServiceCall { static Get(url: string): Promise { return new Promise((resolve, reject) => { window["fetch"](url, { headers: { "Accept": "application/json" }, credentials: 'include' }) .then((response) => { if (response.status === 200) return response.json(); else return Promise.reject(response.statusText); }) .then((data) => resolve(data)) .catch((error) => reject(error)); }); } static Post(url: string, data): Promise { // console.log("onPost", AppComponent.user); // if (!AESUtil.checkConstEncryptionAPIs(url) && !AppComponent.user.loggedIn) { // RdLib.screenOperations.toastr.warning(RdLib.localization.translateEn('Session Expired. Please Login Again')); // return Promise.reject(new Response(new ReadableStream(), { status: 401, statusText: "Session Expired" })); // } // else { // } return new Promise((resolve, reject) => { window["fetch"](url, { method: "POST", headers: { "Accept": "application/json", "Content-Type": "application/json" }, body: AESUtil.encrypt(JSON.stringify(data), url), credentials: 'include' }) .then((response) => { switch (response.status) { case 200: if (AESUtil.getSessionKey(url)) return response.text(); // encrypted else return response.json(); // unencrypted case 401: AppComponent.user.loggedIn = false; RdLib.screenOperations.toastr.warning(RdLib.localization.translateEn('Session Expired. Please Login Again')) return Promise.reject(response); case 503: window.location.replace("park.html"); break; default: return Promise.reject(response); } }) .then((data) => resolve(AESUtil.decrypt(data, url))) .catch((error) => reject(error)); }); } static xmlHttpGet(url: string): Promise { return new Promise((resolve, reject) => { xhr.open("GET", url); xhr.send(); xhr.onload = () => { if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) resolve(xhr.response); else reject({ status: xhr.status, statusText: xhr.statusText }); }; xhr.onerror = () => reject({ status: xhr.status, statusText: xhr.statusText }); }) } static xmlHttpPost(url: string, body): Promise { return new Promise((resolve, reject) => { xhr.open("POST", url); xhr.send(body); xhr.onload = () => { if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) resolve(xhr.response); else reject({ status: xhr.status, statusText: xhr.statusText }); }; xhr.onerror = () => reject({ status: xhr.status, statusText: xhr.statusText }); }) } }