From 8be27e9154b704e6cbd3dec47171c56a351f4e87 Mon Sep 17 00:00:00 2001 From: guerler Date: Sat, 2 Nov 2024 11:51:38 +0300 Subject: [PATCH] Switch visualization api calls to interface --- src/api/client.js | 24 ++++++++++++++++++++---- src/api/datasets.js | 9 +++------ src/api/visualizations.js | 9 +++------ 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/api/client.js b/src/api/client.js index ad02fb2..d0c5d93 100644 --- a/src/api/client.js +++ b/src/api/client.js @@ -1,13 +1,13 @@ import { rethrowSimple } from "@/utilities/simpleError"; import { useConfigStore } from "@/store/configStore"; -export async function fetchApi(path, options) { +async function fetchApi(path, options) { const configStore = useConfigStore(); const routedPath = `${configStore.getRoot()}${path.substring(1)}`; try { const response = await fetch(routedPath, { credentials: process.env.GALAXY_KEY ? "omit" : "include", - method: "GET", + headers: { "Content-Type": "application/json" }, ...options, }); const data = await response.json(); @@ -18,10 +18,26 @@ export async function fetchApi(path, options) { } export function GalaxyApi() { - async function GET(path, options) { - return fetchApi(path, options); + async function GET(path) { + return fetchApi(path, { + method: "GET", + }); + } + async function POST(path, options) { + return fetchApi(path, { + body: JSON.stringify(options), + method: "POST", + }); + } + async function PUT(path, options) { + return fetchApi(path, { + body: JSON.stringify(options), + method: "PUT", + }); } return { GET, + POST, + PUT, }; } diff --git a/src/api/datasets.js b/src/api/datasets.js index 5a526ed..6a4e673 100644 --- a/src/api/datasets.js +++ b/src/api/datasets.js @@ -1,15 +1,13 @@ -import { fetchApi } from "@/api/client"; -import { useConfigStore } from "@/store/configStore"; +import { GalaxyApi } from "@/api/client"; export async function datasetsGetColumns(datasetId, columnList) { try { - const url = `/api/datasets/${datasetId}`; const params = new URLSearchParams({ data_type: "raw_data", provider: "dataset-column", indeces: columnList.toString(), }).toString(); - const data = await fetchApi(`${url}?${params}`); + const data = await GalaxyApi().GET(`/api/datasets/${datasetId}?${params}`); const columnLength = columnList.length; const results = new Array(columnLength); for (let i = 0; i < results.length; i++) { @@ -30,6 +28,5 @@ export async function datasetsGetColumns(datasetId, columnList) { } export function datasetsGetUrl(datasetId) { - const configStore = useConfigStore(); - return `${configStore.getRoot()}api/datasets/${datasetId}/display`; + return `/api/datasets/${datasetId}/display`; } diff --git a/src/api/visualizations.js b/src/api/visualizations.js index 8c95412..d2d2348 100644 --- a/src/api/visualizations.js +++ b/src/api/visualizations.js @@ -1,11 +1,9 @@ -import axios from "axios"; import { rethrowSimple } from "@/utilities/simpleError"; -import { useConfigStore } from "@/store/configStore"; +import { GalaxyApi } from "@/api/client"; export async function visualizationsCreate(type, title, config) { - const configStore = useConfigStore(); try { - const { data } = await axios.post(`${configStore.getRoot()}api/visualizations`, { + const { data } = await GalaxyApi().POST("/api/visualizations", { type, title, config, @@ -17,9 +15,8 @@ export async function visualizationsCreate(type, title, config) { } export async function visualizationsSave(id, title, config) { - const configStore = useConfigStore(); try { - const response = await axios.put(`${configStore.getRoot()}api/visualizations/${id}`, { + const response = await GalaxyApi().PUT(`/api/visualizations/${id}`, { title, config, });