kea-react
Version:
Componentes comunes de react
125 lines (108 loc) • 3.91 kB
text/typescript
type StorageType = "cookies" | "localstorage" | "indexeddb";
/**local storage service interface */
export interface CookieService {
Set(name: string, value: any);
Get(name: string): any;
Remove(name: string);
Clear();
}
function tryParse(json: string): any {
try {
return JSON.parse(json);
} catch (e) {
return null;
}
}
/**
* Servicio para almacenar datos en los cookies o en el localstorage
*/
export class CookieLocalStorageClass implements CookieService {
/**
* @param localstorage True para usar el localstorage, false para usar los cookies
* @param serialize True para serializar el value
*/
constructor(private localstorage: boolean, private serialize: boolean) {
}
/**Limpia todos los valores */
Clear() {
if(this.localstorage) {
window.localStorage.clear();
} else {
document.cookie = "";
}
}
/**Store a key-value pair */
Set(name: string, value: any) {
name = encodeURIComponent(name);
if (this.localstorage) {
window.localStorage.setItem(name, this.serialize ? JSON.stringify(value) : value);
} else {
var expireDays = 360;
var d = new Date();
d.setTime(d.getTime() + (expireDays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
if (this.serialize) {
value = encodeURIComponent(JSON.stringify(value));
}
document.cookie = name + "=" + value + "; " + expires;
}
}
/**Get a value by its key. Returns null if the value is not found */
Get(name: string): any {
name = encodeURIComponent(name);
if (this.localstorage) {
const r = window.localStorage.getItem(name);
return r && (this.serialize ? tryParse(r) : r);
} else {
var name = name + "=";
var ca = document.cookie.split(';');
var value: string | null = null;
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
value = c.substring(name.length, c.length);
}
}
if (!value)
return null;
if (this.serialize) {
return tryParse(decodeURIComponent(value));
}
}
return null;
}
/**Remove a value by its key */
Remove(name: string) {
if (this.localstorage) {
window.localStorage.removeItem(name);
} else {
var d = new Date();
d.setTime(d.getTime() + (-1 * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = name + "=" + "" + "; " + expires;
}
}
}
/**Servicio de cookies por default */
export let Cookies = new CookieLocalStorageClass(window.localStorage != null, true);
/**Memoriza una función en los cookies */
export function cookieNetworkMemoize<T extends (...args: any[]) => Promise<any>>(func: T, cookieKey: string): T {
return (function () {
const argArray: any[] = [];
for (let i = 0; i < arguments.length; i++) argArray.push(arguments[i]);
const key = "cookieMemoize_" + cookieKey + "_" + JSON.stringify(argArray);
const r = func(...argArray).then((result) => {
Cookies.Set(key, result);
return result;
}, error => {
if (error == 0 || error == 504)
return Cookies.Get(key);
else
throw error;
});
return r;
}) as any as T;
}