/ Fixed the scope of custom celestial type functions

This commit is contained in:
Alexis
2021-03-28 18:35:26 +02:00
parent e27e9a2e9e
commit 9dbeb23644

View File

@@ -1,26 +1,36 @@
import axios from 'axios'
import { addCelestialType, addCelestialsType } from '@/plugins/methods'
/**
* Fetches the API for all celestial bodies
* Fetches the API for all celestial bodies and adds its type
* @returns Array of celestials in the API
*/
export const fetchCelestials = () => {
return axios
.get('https://api.le-systeme-solaire.net/rest/bodies/')
.then((res) => {
return res.data.bodies
return addCelestialsType(res.data.bodies)
})
}
/**
* Fetches the API for all celestial bodies
* Fetches the API for all celestial bodies and adds its type
* @returns Array of celestials in the API
*/
export const fetchCelestial = (slug) => {
return axios
.get(`https://api.le-systeme-solaire.net/rest/bodies/${slug}`)
.then((res) => {
return res.data
const celestial = addCelestialType(res.data)
const moons = []
if (celestial.moons) {
celestial.moons.forEach(async (moon) => {
const moonRes = await axios.get(moon.rel)
moons.push(addCelestialType(moonRes.data))
})
celestial.moons = moons
}
return celestial
})
}