From c3d8f5377d417cad9a3380a6d32d44b95a99fca2 Mon Sep 17 00:00:00 2001 From: jhalitaksoy Date: Sun, 27 Jun 2021 19:28:47 +0300 Subject: [PATCH] added user key store --- src/service/http_service.ts | 18 +++++++++++++ src/store/key_store.ts | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/service/http_service.ts create mode 100644 src/store/key_store.ts diff --git a/src/service/http_service.ts b/src/service/http_service.ts new file mode 100644 index 0000000..98a7a52 --- /dev/null +++ b/src/service/http_service.ts @@ -0,0 +1,18 @@ +export const serverAdress = "http://localhost:5000" +export const wsServerAdress = "ws://localhost:5000" + +interface HttpService { + get: (route: string, succes: () => any, error: () => any) => any +} + +class HttpServiceImpl implements HttpService{ + public serverAdress : string + + constructor(serverAdress : string){ + this.serverAdress = serverAdress + } + + public get(route: string, succes: () => any, error: () => any) : any { + + } +} \ No newline at end of file diff --git a/src/store/key_store.ts b/src/store/key_store.ts new file mode 100644 index 0000000..489574d --- /dev/null +++ b/src/store/key_store.ts @@ -0,0 +1,51 @@ +import { serverAdress } from "../service/http_service" + +const key = "user_key" + +export type OnUserKeyFound = (userKey: string) => any + +export interface UserKeyStore { + getUserKey: (callback: OnUserKeyFound) => any +} + +export class UserKeyStoreImpl implements UserKeyStore { + private keyStoreHttp = new UserKeyStoreLocalHttp() + private keyStoreLocalStoreage = new UserKeyStoreLocalStoreage() + + public getUserKey(callback: (userKey: string) => any): any { + this.keyStoreLocalStoreage.getUserKey((userKey)=>{ + if(userKey){ + callback(userKey) + }else{ + this.keyStoreHttp.getUserKey((userKey)=>{ + this.keyStoreLocalStoreage.storeUserKey(userKey) + callback(userKey) + }) + } + }) + } +} + +export class UserKeyStoreLocalHttp implements UserKeyStore { + public getUserKey(callback: (userKey: string) => any): any { + const url = serverAdress + "/register/" + const requestOptions = { + method: 'GET', + headers: { 'Content-Type': 'application/json' }, + }; + fetch(url, requestOptions) + .then(response => response.text()) + .then(data => callback(data)); + } +} + +export class UserKeyStoreLocalStoreage implements UserKeyStore { + public getUserKey(callback: (userKey: string) => any): any { + const userKey = localStorage.getItem(key) + callback(userKey) + } + + public storeUserKey(userKey : string) : any { + localStorage.setItem(key, userKey) + } +}