refactor http service
This commit is contained in:
parent
4bda2a35b4
commit
af98005ea1
23
src/service/HttpService.ts
Normal file
23
src/service/HttpService.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { server } from "../const/config";
|
||||||
|
|
||||||
|
export interface HttpService {
|
||||||
|
get: (route: string) => Promise<Response>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class HttpServiceImpl implements HttpService {
|
||||||
|
public serverAdress: string;
|
||||||
|
|
||||||
|
constructor(serverAdress: string) {
|
||||||
|
this.serverAdress = serverAdress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async get(route: string): Promise<Response> {
|
||||||
|
const url = server.serverAdress + route;
|
||||||
|
const requestOptions = {
|
||||||
|
method: 'GET',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
};
|
||||||
|
const response = await fetch(url, requestOptions);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,30 +0,0 @@
|
|||||||
export type Server = {
|
|
||||||
serverAdress: string;
|
|
||||||
wsServerAdress: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const server: Server = {
|
|
||||||
serverAdress: "https://segin.one/mancala-backend-beta",
|
|
||||||
wsServerAdress: "wss://segin.one/mancala-backend-beta/",
|
|
||||||
};
|
|
||||||
|
|
||||||
const useLocal = false;
|
|
||||||
|
|
||||||
if (useLocal) {
|
|
||||||
server.serverAdress = "http://localhost:5000";
|
|
||||||
server.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 {}
|
|
||||||
}
|
|
||||||
53
src/store/KeyStore.ts
Normal file
53
src/store/KeyStore.ts
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import { HttpService } from "../service/HttpService"
|
||||||
|
|
||||||
|
const user_key = "user_key"
|
||||||
|
|
||||||
|
export interface UserKeyStore {
|
||||||
|
getUserKey: () => Promise<string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class UserKeyStoreImpl implements UserKeyStore {
|
||||||
|
private httpService: HttpService;
|
||||||
|
private keyStoreHttp: UserKeyStore;
|
||||||
|
private keyStoreLocalStorage = new UserKeyStoreLocalStorage()
|
||||||
|
|
||||||
|
constructor(props: { httpService: HttpService }) {
|
||||||
|
this.httpService = props.httpService;
|
||||||
|
this.keyStoreHttp = new UserKeyStoreLocalHttp({ httpService: this.httpService });
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getUserKey(): Promise<string> {
|
||||||
|
const maybeUserKey = await this.keyStoreLocalStorage.getUserKey();
|
||||||
|
if (maybeUserKey === undefined) {
|
||||||
|
const userKey = await this.keyStoreHttp.getUserKey()
|
||||||
|
this.keyStoreLocalStorage.storeUserKey(userKey)
|
||||||
|
return Promise.resolve(userKey);
|
||||||
|
} else {
|
||||||
|
return Promise.resolve(maybeUserKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class UserKeyStoreLocalHttp implements UserKeyStore {
|
||||||
|
httpService: HttpService;
|
||||||
|
|
||||||
|
constructor(params: { httpService: HttpService }) {
|
||||||
|
this.httpService = params.httpService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getUserKey(): Promise<string> {
|
||||||
|
const response = await this.httpService.get("/register/")
|
||||||
|
return response.text();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class UserKeyStoreLocalStorage {
|
||||||
|
public getUserKey(): Promise<string | undefined> {
|
||||||
|
const userKey = localStorage.getItem(user_key)
|
||||||
|
return Promise.resolve(userKey === null ? undefined : userKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
public storeUserKey(userKey: string): void {
|
||||||
|
localStorage.setItem(user_key, userKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,51 +0,0 @@
|
|||||||
import { server } 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 = server.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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user