From 5b3f4d19ec02b46d4c49ce52aa3ec686b7e0e2ab Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Fri, 12 Mar 2021 16:34:29 +0100 Subject: [PATCH] Moved api functions to relevant folders --- src/api/le-systeme-solaire/index.ts | 30 +++++++++++++++++++++++++++++ src/api/wikipedia/index.ts | 0 src/plugins/methods/index.ts | 2 +- src/views/celestials/Celestial.vue | 18 +++-------------- src/views/celestials/Celestials.vue | 14 ++------------ 5 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 src/api/le-systeme-solaire/index.ts create mode 100644 src/api/wikipedia/index.ts diff --git a/src/api/le-systeme-solaire/index.ts b/src/api/le-systeme-solaire/index.ts new file mode 100644 index 0000000..c5e29ba --- /dev/null +++ b/src/api/le-systeme-solaire/index.ts @@ -0,0 +1,30 @@ +import axios from "axios"; + +/** + * Fetches the API for all celestial bodies + * @returns Array of celestials in the API + */ +export const fetchCelestials = (): Promise => { + return axios + .get("https://api.le-systeme-solaire.net/rest/bodies/") + .then(res => { + return res.data.bodies; + }); +}; + +/** + * Fetches the API for all celestial bodies + * @returns Array of celestials in the API + */ +export const fetchCelestial = (slug: string): Promise => { + return axios + .get(`https://api.le-systeme-solaire.net/rest/bodies/${slug}`) + .then(res => { + return res.data; + }); +}; + +export default { + fetchCelestials, + fetchCelestial +}; diff --git a/src/api/wikipedia/index.ts b/src/api/wikipedia/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/plugins/methods/index.ts b/src/plugins/methods/index.ts index 57f7d9e..acdde32 100644 --- a/src/plugins/methods/index.ts +++ b/src/plugins/methods/index.ts @@ -28,4 +28,4 @@ export const addCelestialsType = (celestials: any) => { export default { addCelestialType, addCelestialsType -}; \ No newline at end of file +}; diff --git a/src/views/celestials/Celestial.vue b/src/views/celestials/Celestial.vue index 332d796..576553e 100644 --- a/src/views/celestials/Celestial.vue +++ b/src/views/celestials/Celestial.vue @@ -19,7 +19,8 @@ import { defineComponent } from "vue"; // API -import axios from "axios"; +import { fetchCelestial } from "@/api/le-systeme-solaire"; + // Global methods import { addCelestialType } from "@/plugins/methods"; import NestLoader from "@/components/NestLoader.vue"; @@ -47,7 +48,7 @@ export default defineComponent({ mounted() { // Fetches from API... - this.fetchCelestial() + fetchCelestial(this.slug) .then(res => { // Adds type after fake loading (it's just to showcase the spinner tbh) setTimeout(() => { @@ -58,19 +59,6 @@ export default defineComponent({ .catch(() => { this.error = "Impossible de récupérer l'astre demandé."; }); - }, - - methods: { - /** - * Fetches celestial body from API - */ - fetchCelestial(): Promise { - return axios - .get(`https://api.le-systeme-solaire.net/rest/bodies/${this.slug}`) - .then(res => { - return res.data; - }); - } } }); diff --git a/src/views/celestials/Celestials.vue b/src/views/celestials/Celestials.vue index 10e20b9..57685a1 100644 --- a/src/views/celestials/Celestials.vue +++ b/src/views/celestials/Celestials.vue @@ -19,7 +19,8 @@ import { defineComponent, onMounted, ref } from "vue"; // API -import axios from "axios"; +import { fetchCelestials } from "@/api/le-systeme-solaire"; + // Global methods import { addCelestialsType } from "@/plugins/methods"; @@ -41,17 +42,6 @@ export default defineComponent({ setup() { const celestials = ref(Array()); - /** - * Fetches celestial bodies from API - */ - const fetchCelestials = (): Promise => { - return axios - .get("https://api.le-systeme-solaire.net/rest/bodies/") - .then(res => { - return res.data.bodies; - }); - }; - const getCelestials = async () => { celestials.value = await fetchCelestials(); celestials.value = addCelestialsType(celestials.value);