Get app working against deployed service

This commit is contained in:
John Lyon-Smith
2018-04-08 21:25:17 -07:00
parent d674f5e7eb
commit ee836def93
9 changed files with 141 additions and 86 deletions

View File

@@ -2,21 +2,14 @@ import EventEmitter from "eventemitter3"
import io from "socket.io-client"
import { AsyncStorage } from "react-native"
const authTokenName = "AuthToken"
const backendName = "backendName"
const authTokenKeyName = "AuthToken"
const backendKeyName = "BackendName"
let baseURL = null
let apiPath = null
//
if (__DEV__) {
const localIPAddr = process.env.LOCAL_IP_ADDR
baseURL = `http://${localIPAddr || "localhost"}:3001`
apiPath = ""
} else {
baseURL = "https://dar.kss.us.com"
apiPath = "/api"
}
// if (__DEV__) {
// const localIPAddr = process.env.LOCAL_IP_ADDR
// baseURL = `http://${localIPAddr || "localhost"}:3001`
// apiPath = ""
// }
class NetworkError extends Error {
constructor(message) {
@@ -48,16 +41,20 @@ class API extends EventEmitter {
super()
this.user = { pending: true }
AsyncStorage.getItem(backendName)
.then((backend) => {
this.backend = backend
})
.catch((err) => {
this.backend = "normal"
AsyncStorage.setItem(backendName, this.backend)
})
// AsyncStorage.getItem(backendKeyName)
// .then((backend) => {
// this.backend = backend
// })
// .catch((err) => {
// this.backend = "normal"
// AsyncStorage.setItem(backendKeyName, this.backend)
// })
AsyncStorage.getItem(authTokenName)
this.backendName = "normal"
this.baseURL = "https://dar.kss.us.com"
this.apiPath = "/api"
AsyncStorage.getItem(authTokenKeyName)
.then((token) => {
if (!token) {
return Promise.reject()
@@ -72,7 +69,7 @@ class API extends EventEmitter {
this.emit("login")
})
.catch((err) => {
AsyncStorage.removeItem(authTokenName)
AsyncStorage.removeItem(authTokenKeyName)
this.token = null
this.user = {}
this.socket = null
@@ -81,8 +78,8 @@ class API extends EventEmitter {
}
connectSocket() {
this.socket = io(baseURL, {
path: apiPath + "/socketio",
this.socket = io(this.baseURL, {
path: this.apiPath + "/socketio",
query: {
auth_token: this.token,
},
@@ -117,14 +114,14 @@ class API extends EventEmitter {
}
get backend() {
return this.backend
return this.backendName
}
makeImageUrl(id, size) {
if (id) {
return apiPath + "/assets/" + id + "?access_token=" + this.token
return this.apiPath + "/assets/" + id + "?access_token=" + this.token
} else if (size && size.width && size.height) {
return `${apiPath}/placeholders/${size.width}x${
return `${this.apiPath}/placeholders/${size.width}x${
size.height
}?access_token=${this.token}`
} else {
@@ -133,7 +130,9 @@ class API extends EventEmitter {
}
makeAssetUrl(id) {
return id ? apiPath + "/assets/" + id + "?access_token=" + this.token : null
return id
? this.apiPath + "/assets/" + id + "?access_token=" + this.token
: null
}
static makeParams(params) {
@@ -169,7 +168,7 @@ class API extends EventEmitter {
}
}
fetchOptions.headers = headers
fetch(this.apiURL + path, fetchOptions)
fetch(this.baseURL + this.apiPath + path, fetchOptions)
.then((res) => {
return Promise.all([
Promise.resolve(res),
@@ -221,7 +220,7 @@ class API extends EventEmitter {
}
if (remember) {
AsyncStorage.setItem(authTokenName, token)
AsyncStorage.setItem(authTokenKeyName, token)
}
this.token = token
this.user = response.body
@@ -237,7 +236,7 @@ class API extends EventEmitter {
logout() {
let cb = () => {
// Regardless of response, always logout in the client
AsyncStorage.removeItem(authTokenName)
AsyncStorage.removeItem(authTokenKeyName)
this.token = null
this.user = {}
this.disconnectSocket()