commit 32e4a94d2e349224e19c731004b67c2147b89801
parent b09e301e96561b8e79d7550f65b06499235d9416
Author: MTRNord <mtrnord1@gmail.com>
Date: Tue, 8 Feb 2022 02:12:31 +0100
Make store a little safer and use correct types
Diffstat:
4 files changed, 35 insertions(+), 14 deletions(-)
diff --git a/helpers/matrix_client.ts b/helpers/matrix_client.ts
@@ -31,7 +31,7 @@ export default class MatrixClient {
this.serverUrl = this.storage.getItem("serverUrl");
this._userId = this.storage.getItem("userId");
this._accessToken = this.storage.getItem("accessToken");
- this._isGuest = this.storage.getItem("isGuest");
+ this._isGuest = this.storage.getItem("isGuest") === undefined ? undefined : (this.storage.getItem("isGuest") === "true");
this.serverName = this.storage.getItem("serverName");
this._profileRoomId = this.storage.getItem("profileRoomId");
}
@@ -44,7 +44,7 @@ export default class MatrixClient {
this.storage.setOrDelete("userId", this._userId);
this.storage.setOrDelete("accessToken", this._accessToken);
this.storage.setOrDelete("serverName", this.serverName);
- this.storage.setOrDelete("isGuest", this._isGuest);
+ this.storage.setOrDelete("isGuest", this._isGuest?.toString());
}
private generateToken(len: number) {
diff --git a/helpers/storage.ts b/helpers/storage.ts
@@ -22,7 +22,11 @@ export default class Storage {
if (typeof window !== "undefined") {
version = window.localStorage.getItem("version");
} else {
- version = this.nodeLocalStorage?.getItem("version");
+ try {
+ version = this.nodeLocalStorage?.getItem("version");
+ } catch {
+ //No-op
+ }
}
if (version === undefined || version === null || version !== STORAGE_VERSION.toString()) {
if (typeof window !== "undefined") {
@@ -35,35 +39,51 @@ export default class Storage {
}
}
- getItem(key: string): any {
+ getItem(key: string): string | undefined {
this.ensureVersion();
if (typeof window !== "undefined") {
- return window.localStorage.getItem(this.prefix + key);
+ const item = window.localStorage.getItem(this.prefix + key);
+ if (item === null) {
+ return undefined;
+ }
+ return item;
} else {
- return this.nodeLocalStorage?.getItem(this.prefix + key);
+ try {
+ return this.nodeLocalStorage?.getItem(this.prefix + key);
+ } catch {
+ //No-op
+ }
}
}
- setItem(key: string, value: any): any {
+ setItem(key: string, value: string): void {
this.ensureVersion();
if (typeof window !== "undefined") {
return window.localStorage.setItem(this.prefix + key, value);
} else {
- return this.nodeLocalStorage?.setItem(this.prefix + key, value);
+ try {
+ return this.nodeLocalStorage?.setItem(this.prefix + key, value);
+ } catch {
+ //No-op
+ }
}
}
- removeItem(key: string): any {
+ removeItem(key: string): void {
this.ensureVersion();
if (typeof window !== "undefined") {
return window.localStorage.removeItem(this.prefix + key);
} else {
- return this.nodeLocalStorage?.removeItem(this.prefix + key);
+ try {
+ return this.nodeLocalStorage?.removeItem(this.prefix + key);
+ } catch {
+ //No-op
+ }
}
}
- setOrDelete(key: string, value: any) {
- if (value !== null || value !== undefined) {
+ setOrDelete(key: string, value: string | undefined): void {
+ if (value) {
this.setItem(key, value);
} else {
this.removeItem(key);
diff --git a/pages/_app.tsx b/pages/_app.tsx
@@ -30,7 +30,8 @@ function MyApp({ Component, pageProps }: AppProps) {
guest_client: guest_client,
is_generating_guest: false,
}}
- ><div className='min-h-full flex flex-col justify-between bg-[#f8f8f8] dark:bg-[#06070D]'>
+ >
+ <div className='min-h-full flex flex-col justify-between bg-[#f8f8f8] dark:bg-[#06070D]'>
<HeaderNoSSR></HeaderNoSSR>
<main className='mb-auto lg:pt-20 pt-52 z-0'>
<Component {...pageProps} />
diff --git a/tsconfig.json b/tsconfig.json
@@ -1,6 +1,6 @@
{
"compilerOptions": {
- "target": "ES2015",
+ "target": "es2017",
"lib": [
"dom",
"dom.iterable",