Skip to content

Instantly share code, notes, and snippets.

@onedebos
Created April 28, 2021 22:56
Show Gist options
  • Save onedebos/98d45ac1d613230a78e20cb574782d8e to your computer and use it in GitHub Desktop.
Save onedebos/98d45ac1d613230a78e20cb574782d8e to your computer and use it in GitHub Desktop.
methods that handle getting the post, author and featured image from our server
import axios from 'axios';
import { POSTS_API_URL, AUTHORS_API_URL, MEDIA_API_URL } from './constants';
export const getAllPostsFromServer = async () => {
// get all posts from Server
try {
const { data } = await axios.get(POSTS_API_URL);
return data;
} catch (error) {
console.log(error);
}
};
export const getAuthor = async (id) => {
try {
const {
data: { name },
} = await axios.get(`${AUTHORS_API_URL}/${id}`);
return name;
} catch (error) {
console.log(error);
}
};
export const getFeaturedImage = async (id) => {
try {
const res = await axios.get(`${MEDIA_API_URL}/${id}`);
return res.data.guid.rendered;
} catch (error) {
console.log(error);
return '';
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment