Skip to content

Instantly share code, notes, and snippets.

@DanielAmah
Last active September 9, 2020 22:43
Show Gist options
  • Select an option

  • Save DanielAmah/494982a698209ac2dfc0a6e1aed05a1a to your computer and use it in GitHub Desktop.

Select an option

Save DanielAmah/494982a698209ac2dfc0a6e1aed05a1a to your computer and use it in GitHub Desktop.
Code review for Konga expo react native app
// 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