added user key store

This commit is contained in:
jhalitaksoy 2021-06-27 19:28:47 +03:00
parent 19d1d8982b
commit c3d8f5377d
2 changed files with 69 additions and 0 deletions

View File

@ -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 {
}
}

51
src/store/key_store.ts Normal file
View File

@ -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)
}
}