Farmbot-Web-App/webpack/session.ts

88 lines
2.8 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import { AuthState } from "./auth/interfaces";
import { box } from "boxed_value";
2017-08-30 15:18:44 -06:00
import { get, isNumber, isBoolean } from "lodash";
import { BooleanSetting, NumericSetting } from "./session_keys";
2017-06-29 12:54:02 -06:00
2017-08-27 09:19:23 -06:00
/** The `Session` namespace is a wrapper for `localStorage`.
* Use this to avoid direct access of `localStorage` where possible.
*
* Problems this namespace aims to solve:
* - Avoid duplication of localStorage key names.
* - Avoid duplication of de-serialization logic.
* - Avoid type errors by explicitly naming keys as (Boolean|Numeric)Setting
* - Create an upgrade path for the eventual server side storage
*/
2017-06-29 12:54:02 -06:00
export namespace Session {
2017-08-27 09:19:23 -06:00
/** Key that holds the user's JWT */
2017-06-29 12:54:02 -06:00
const KEY = "session";
/** Replace the contents of session storage. */
export function replace(nextState: AuthState) {
2017-06-29 12:54:02 -06:00
localStorage[KEY] = JSON.stringify(nextState);
}
/** Fetch the previous session. */
export function getAll(): AuthState | undefined {
2017-06-29 12:54:02 -06:00
try {
2017-08-27 18:53:30 -06:00
const v: AuthState = JSON.parse(localStorage[KEY]);
2017-06-29 12:54:02 -06:00
if (box(v).kind === "object") {
return v;
} else {
throw new Error("Expected object or undefined");
}
} catch (error) {
return undefined;
2017-08-02 09:14:08 -06:00
}
2017-06-29 12:54:02 -06:00
}
/** Clear localstorage and sessionstorage. */
export function clear() {
2017-06-29 12:54:02 -06:00
localStorage.clear();
sessionStorage.clear();
window.location.href = window.location.origin;
}
2017-08-30 15:18:44 -06:00
/** Fetch a *boolean* value from localstorage. Returns `undefined` when
* none are found.*/
export function getBool(key: BooleanSetting): boolean | undefined {
const output = JSON.parse(localStorage.getItem(key) || "null");
return (isBoolean(output)) ? output : undefined;
}
2017-08-27 09:19:23 -06:00
/** Store a boolean value in `localStorage` */
2017-09-27 17:21:11 -06:00
export function setBool(key: BooleanSetting, val: boolean): boolean {
localStorage.setItem(key, JSON.stringify(val));
2017-09-27 17:21:11 -06:00
return val;
}
2017-09-27 17:21:11 -06:00
export function invertBool(key: BooleanSetting): boolean {
return Session.setBool(key, !Session.getBool(key));
2017-08-27 18:53:30 -06:00
}
2017-08-27 09:19:23 -06:00
/** Extract numeric settings from `localStorage`. Returns `undefined` when
* none are found. */
export function getNum(key: NumericSetting): number | undefined {
2017-08-27 18:53:30 -06:00
const output = JSON.parse(get(localStorage, key, "null"));
return (isNumber(output)) ? output : undefined;
}
2017-08-27 09:19:23 -06:00
/** Set a numeric value in `localStorage`. */
export function setNum(key: NumericSetting, val: number): void {
localStorage.setItem(key, JSON.stringify(val));
}
2017-06-29 12:54:02 -06:00
}
2017-08-30 15:18:44 -06:00
const isBooleanSetting =
// tslint:disable-next-line:no-any
(x: any): x is BooleanSetting => {
return !!BooleanSetting[x];
};
export function safeBooleanSettting(name: string): BooleanSetting {
if (isBooleanSetting(name)) {
return name;
} else {
throw new Error(`Expected BooleanSetting but got '${name}'`);
}
}