Last active
September 9, 2020 22:43
-
-
Save DanielAmah/494982a698209ac2dfc0a6e1aed05a1a to your computer and use it in GitHub Desktop.
Code review for Konga expo react native app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Move axios calls to a single file. Say in the utils folder. You can name it api-client.js and add the following line | |
| import axios from "axios"; | |
| import { BASE_URL } from "react-native-dotenv"; | |
| import AsyncStorage from "@react-native-community/async-storage"; | |
| const axiosInstance = axios.create({ baseURL: BASE_URL }); | |
| export const storeData = async (key, value) => { | |
| try { | |
| await AsyncStorage.setItem(key, value); | |
| } catch (error) { | |
| console.error(error); | |
| } | |
| }; | |
| export default class api { | |
| static async get(url, params, conf) { | |
| const token = await AsyncStorage.getItem("auth_token"); | |
| const headers = { headers: { Authorization: `Bearer ${token}` } }; | |
| const config = { ...headers, params, ...conf }; | |
| return axiosInstance.get(url, config); | |
| } | |
| static async post(url, data, conf) { | |
| const token = await AsyncStorage.getItem("auth_token"); | |
| const headers = { headers: { Authorization: `Bearer ${token}` } }; | |
| const config = { ...headers, ...conf }; | |
| return axiosInstance.post(url, data, config); | |
| } | |
| static async put(url, data, conf) { | |
| const token = await AsyncStorage.getItem("auth_token"); | |
| const headers = { headers: { Authorization: `Bearer ${token}` } }; | |
| const config = { ...headers, ...conf }; | |
| return axiosInstance.put(url, data, config); | |
| } | |
| static async delete(url, conf) { | |
| const token = await AsyncStorage.getItem("auth_token"); | |
| const headers = { headers: { Authorization: `Bearer ${token}` } }; | |
| const config = { ...headers, ...conf }; | |
| return axiosInstance.delete(url, config); | |
| } | |
| } | |
| // import api from "../utils/api-client"; | |
| // api.get('url').then(res => res.data) | |
| // Use like this | |
| // Move the baseURL to an .env file. You don't have to manually change the base url in the codebase. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment