Last active
April 12, 2023 00:58
-
-
Save coryhouse/09fb49dd0c13ca20cca6cc0fe2438f3e to your computer and use it in GitHub Desktop.
API Wrapper example
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
/* This API wrapper is useful because it: | |
1. Centralizes our Axios default configuration. | |
2. Abstracts away the logic for determining the baseURL. | |
3. Provides a clear, easily consumable list of JavaScript functions | |
for interacting with the API. This keeps API calls short and consistent. | |
*/ | |
import axios from 'axios'; | |
let api = null; | |
function getInitializedApi() { | |
if (api) return api; // return initialized api if already initialized. | |
return (api = axios.create({ | |
baseURL: getBaseUrl(), | |
responseType: 'json', | |
withCredentials: true | |
})); | |
} | |
// Helper functions | |
function getBaseUrl() { | |
// Insert logic here to get the baseURL by either: | |
// 1. Sniffing the URL to determine the environment we're running in. | |
// 2. Looking for an environment variable as part of the build process. | |
} | |
function get(url) { | |
return getInitializedApi().get(url); | |
} | |
function post(url, data) { | |
return getInitializedApi().post(url, data); | |
} | |
// Public functions | |
// Note how short these are due to the centralized config and helpers above. 😎 | |
export function getUserById(id) { | |
return get(`user/${id}`); | |
} | |
export function saveUser(user) { | |
return post(`user/${user.id}`, {user: user}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oops, fixed. Thanks Keith!