You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
3.9 KiB
156 lines
3.9 KiB
import { ApiConfig } from "../constants/defaultValues"; |
|
import axios from "axios"; |
|
import AsyncStorage from "@react-native-async-storage/async-storage"; |
|
import { useNavigation } from "@react-navigation/native"; |
|
|
|
let { baseUrl } = ApiConfig; |
|
let access; |
|
() => { |
|
access = AsyncStorage.getItem("access"); |
|
}; |
|
|
|
baseUrl = baseUrl + "/"; |
|
|
|
const Axios = axios.create({ |
|
withCredentials: true, |
|
validateStatus: null, |
|
baseURL: baseUrl, |
|
//headers: access ? { Authorization: `Bearer ${access}` } : {}, |
|
}); |
|
class Proxy { |
|
get = (url, params, opt, data) => { |
|
try { |
|
this.check( |
|
url, |
|
opt, |
|
() => Axios.get(url, { params, ...opt }), |
|
data || params |
|
); |
|
} catch (error) { |
|
} |
|
}; |
|
|
|
post = (url, params, opt, data) => { |
|
try { |
|
this.check(url, opt, () => Axios.post(url, params, opt), data || params); |
|
} catch (error) { |
|
} |
|
}; |
|
|
|
put = (url, params, opt, data) => { |
|
try { |
|
this.check(url, opt, () => Axios.put(url, params, opt), data || params); |
|
} catch (error) { |
|
} |
|
}; |
|
|
|
delete = (url, params, opt, data) => { |
|
try { |
|
this.check( |
|
url, |
|
opt, |
|
() => Axios.delete(url, { ...opt, data: params }), |
|
data || params |
|
); |
|
} catch (error) { |
|
} |
|
}; |
|
|
|
check = async (url, { dispatch }, fetch, params) => { |
|
try { |
|
dispatch = dispatch || (() => {}); |
|
dispatch({ type: url.split("/")[0] + "/" + "loading" }); |
|
let response = await fetch(); |
|
switch (response.status) { |
|
case 200: |
|
dispatch({ type: url, data: response.data.data, params }); |
|
return response.data.data; |
|
case 401: |
|
if (this.refresh()) { |
|
let response = fetch(); |
|
dispatch({ type: url, data: response.data.data, params }); |
|
return response.data.data; |
|
} |
|
break; |
|
default: |
|
dispatch({ |
|
type: url.split("/")[0] + "/" + "error", |
|
data: response.data, |
|
params, |
|
}); |
|
} |
|
return false; |
|
} catch (error) { |
|
} |
|
}; |
|
|
|
refresh = () => { |
|
try { |
|
const navigation = useNavigation(); |
|
let refresh = AsyncStorage.getItem("refresh"); |
|
if (!refresh) navigation.push("Root"); |
|
let login = this.login( |
|
"user/login", |
|
{}, |
|
{ headers: { Authorization: `Bearer ${refresh}` } } |
|
); |
|
return login ? true : false; |
|
} catch (error) { |
|
} |
|
}; |
|
|
|
login = (url, params, { dispatch }) => { |
|
try { |
|
this.post(url, params, { |
|
dispatch: (obj) => { |
|
let login = obj.data; |
|
if (!login || !login.refreshToken){ |
|
return false; |
|
} |
|
const refresh = ["refresh", login.refreshToken]; |
|
const access = ["access", login.accessToken]; |
|
const userData = ["userData", JSON.stringify(login.profile)]; |
|
AsyncStorage.multiSet([refresh, access, userData]); |
|
delete login.refreshToken; |
|
delete login.accessToken; |
|
dispatch(obj); |
|
}, |
|
}); |
|
} catch (error) { |
|
} |
|
}; |
|
|
|
logout = (url, params, { dispatch }) => { |
|
try { |
|
this.post(url, params, { |
|
dispatch: (obj) => { |
|
const keys = ["refresh", "access", "userData"]; |
|
AsyncStorage.multiRemove(keys); |
|
dispatch(obj); |
|
}, |
|
}); |
|
} catch (error) { |
|
} |
|
}; |
|
status = async () => { |
|
try { |
|
let refresh = await AsyncStorage.getItem("refresh"); |
|
let userData = await AsyncStorage.getItem("userData"); |
|
if (!refresh) return false; |
|
if (refresh == "undefined") { |
|
const keys = ["refresh", "access", "userData"]; |
|
AsyncStorage.multiRemove(keys); |
|
return false; |
|
} |
|
return userData || null; |
|
} catch (error) { |
|
return nulll; |
|
} |
|
}; |
|
getStatus = async ({ dispatch }) => { |
|
let status = await this.status(); |
|
dispatch({ type: "user/getStatus", data: {status}}) |
|
} |
|
} |
|
const _proxy = new Proxy(); |
|
export default _proxy;
|
|
|