From af98005ea1c147936556769aa0e6fba925cb3f88 Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Sat, 30 Jul 2022 16:19:07 +0300 Subject: [PATCH] refactor http service --- src/service/HttpService.ts | 23 ++++++++++++++++ src/service/http_service.ts | 30 --------------------- src/store/KeyStore.ts | 53 +++++++++++++++++++++++++++++++++++++ src/store/key_store.ts | 51 ----------------------------------- 4 files changed, 76 insertions(+), 81 deletions(-) create mode 100644 src/service/HttpService.ts delete mode 100644 src/service/http_service.ts create mode 100644 src/store/KeyStore.ts delete mode 100644 src/store/key_store.ts diff --git a/src/service/HttpService.ts b/src/service/HttpService.ts new file mode 100644 index 0000000..c07c2f7 --- /dev/null +++ b/src/service/HttpService.ts @@ -0,0 +1,23 @@ +import { server } from "../const/config"; + +export interface HttpService { + get: (route: string) => Promise; +} + +export class HttpServiceImpl implements HttpService { + public serverAdress: string; + + constructor(serverAdress: string) { + this.serverAdress = serverAdress; + } + + public async get(route: string): Promise { + const url = server.serverAdress + route; + const requestOptions = { + method: 'GET', + headers: { 'Content-Type': 'application/json' }, + }; + const response = await fetch(url, requestOptions); + return response; + } +} diff --git a/src/service/http_service.ts b/src/service/http_service.ts deleted file mode 100644 index fd7d5eb..0000000 --- a/src/service/http_service.ts +++ /dev/null @@ -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 {} -} diff --git a/src/store/KeyStore.ts b/src/store/KeyStore.ts new file mode 100644 index 0000000..6923ff6 --- /dev/null +++ b/src/store/KeyStore.ts @@ -0,0 +1,53 @@ +import { HttpService } from "../service/HttpService" + +const user_key = "user_key" + +export interface UserKeyStore { + getUserKey: () => Promise; +} + +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 { + 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 { + const response = await this.httpService.get("/register/") + return response.text(); + } +} + +export class UserKeyStoreLocalStorage { + public getUserKey(): Promise { + const userKey = localStorage.getItem(user_key) + return Promise.resolve(userKey === null ? undefined : userKey) + } + + public storeUserKey(userKey: string): void { + localStorage.setItem(user_key, userKey) + } +} diff --git a/src/store/key_store.ts b/src/store/key_store.ts deleted file mode 100644 index a5784e7..0000000 --- a/src/store/key_store.ts +++ /dev/null @@ -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) - } -}