🚀 feat(API): add /api/v1 prefix to all API endpoints

All API endpoints now have the /api/v1 prefix to improve the semantics of the API and to make it easier to distinguish between the frontend and backend routes.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-14 09:28:01 -03:00
commit 83bd59dedb

View file

@ -9,7 +9,7 @@ import { FlowStyleType, FlowType } from "../../types/flow";
* @returns {Promise<AxiosResponse<APIObjectType>>} A promise that resolves to an AxiosResponse containing all the objects. * @returns {Promise<AxiosResponse<APIObjectType>>} A promise that resolves to an AxiosResponse containing all the objects.
*/ */
export async function getAll(): Promise<AxiosResponse<APIObjectType>> { export async function getAll(): Promise<AxiosResponse<APIObjectType>> {
return await axios.get(`/all`); return await axios.get(`/api/v1/all`);
} }
/** /**
@ -19,7 +19,7 @@ export async function getAll(): Promise<AxiosResponse<APIObjectType>> {
* @returns {AxiosResponse<any>} The API response. * @returns {AxiosResponse<any>} The API response.
*/ */
export async function sendAll(data: sendAllProps) { export async function sendAll(data: sendAllProps) {
return await axios.post(`/predict`, data); return await axios.post(`/api/v1/predict`, data);
} }
/** /**
@ -31,7 +31,7 @@ export async function sendAll(data: sendAllProps) {
export async function checkCode( export async function checkCode(
code: string code: string
): Promise<AxiosResponse<errorsTypeAPI>> { ): Promise<AxiosResponse<errorsTypeAPI>> {
return await axios.post("/validate/code", { code }); return await axios.post("/api/v1/validate/code", { code });
} }
/** /**
@ -43,7 +43,7 @@ export async function checkCode(
export async function checkPrompt( export async function checkPrompt(
template: string template: string
): Promise<AxiosResponse<PromptTypeAPI>> { ): Promise<AxiosResponse<PromptTypeAPI>> {
return await axios.post("/validate/prompt", { template }); return await axios.post("/api/v1/validate/prompt", { template });
} }
/** /**
@ -75,24 +75,17 @@ export async function getExamples(): Promise<FlowType[]> {
* @returns {Promise<any>} The saved flow data. * @returns {Promise<any>} The saved flow data.
* @throws Will throw an error if saving fails. * @throws Will throw an error if saving fails.
*/ */
export async function saveFlowToDatabase(newFlow: FlowType) { export async function saveFlowToDatabase(newFlow: FlowType): Promise<FlowType> {
try { try {
const response = await fetch("/flows/", { const response = await axios.post("/api/v1/flows/", {
method: "POST", name: newFlow.name,
headers: { data: newFlow.data,
accept: "application/json", description: newFlow.description,
"Content-Type": "application/json",
},
body: JSON.stringify({
name: newFlow.name,
data: newFlow.data,
description: newFlow.description,
}),
}); });
if (!response.ok) { if (response.status !== 201) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
return response.json(); return response.data;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
throw error; throw error;
@ -107,22 +100,16 @@ export async function saveFlowToDatabase(newFlow: FlowType) {
*/ */
export async function updateFlowInDatabase(updatedFlow: FlowType) { export async function updateFlowInDatabase(updatedFlow: FlowType) {
try { try {
const response = await fetch(`/flows/${updatedFlow.id}`, { const response = await axios.patch(`/api/v1/flows/${updatedFlow.id}`, {
method: "PATCH", // Or "PATCH" depending on your backend API name: updatedFlow.name,
headers: { data: updatedFlow.data,
accept: "application/json", description: updatedFlow.description,
"Content-Type": "application/json",
},
body: JSON.stringify({
name: updatedFlow.name,
data: updatedFlow.data,
description: updatedFlow.description,
}),
}); });
if (!response.ok) {
if (response.status !== 200) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
return response.json(); return response.data;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
throw error; throw error;
@ -137,11 +124,11 @@ export async function updateFlowInDatabase(updatedFlow: FlowType) {
*/ */
export async function readFlowsFromDatabase() { export async function readFlowsFromDatabase() {
try { try {
const response = await fetch("/flows/"); const response = await axios.get("/api/v1/flows/");
if (!response.ok) { if (response.status !== 200) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
return response.json(); return response.data;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
throw error; throw error;
@ -150,11 +137,11 @@ export async function readFlowsFromDatabase() {
export async function downloadFlowsFromDatabase() { export async function downloadFlowsFromDatabase() {
try { try {
const response = await fetch("/flows/download/"); const response = await axios.get("/api/v1/flows/download/");
if (!response.ok) { if (response.status !== 200) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
return response.json(); return response.data;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
throw error; throw error;
@ -163,14 +150,12 @@ export async function downloadFlowsFromDatabase() {
export async function uploadFlowsToDatabase(flows) { export async function uploadFlowsToDatabase(flows) {
try { try {
const response = await fetch(`/flows/upload/`, { const response = await axios.post(`/api/v1/flows/upload/`, flows);
method: "POST", // Or "PATCH" depending on your backend API
body: flows, if (response.status !== 201) {
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
return response.json(); return response.data;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
throw error; throw error;
@ -186,13 +171,11 @@ export async function uploadFlowsToDatabase(flows) {
*/ */
export async function deleteFlowFromDatabase(flowId: string) { export async function deleteFlowFromDatabase(flowId: string) {
try { try {
const response = await fetch(`/flows/${flowId}`, { const response = await axios.delete(`/api/v1/flows/${flowId}`);
method: "DELETE", if (response.status !== 200) {
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
return response.json(); return response.data;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
throw error; throw error;
@ -208,11 +191,11 @@ export async function deleteFlowFromDatabase(flowId: string) {
*/ */
export async function getFlowFromDatabase(flowId: number) { export async function getFlowFromDatabase(flowId: number) {
try { try {
const response = await fetch(`/flows/${flowId}`); const response = await axios.get(`/api/v1/flows/${flowId}`);
if (!response.ok) { if (response.status !== 200) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
return response.json(); return response.data;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
throw error; throw error;
@ -227,11 +210,11 @@ export async function getFlowFromDatabase(flowId: number) {
*/ */
export async function getFlowStylesFromDatabase() { export async function getFlowStylesFromDatabase() {
try { try {
const response = await fetch("/flow_styles/"); const response = await axios.get("/api/v1/flow_styles/");
if (!response.ok) { if (response.status !== 200) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
return response.json(); return response.data;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
throw error; throw error;
@ -247,18 +230,17 @@ export async function getFlowStylesFromDatabase() {
*/ */
export async function saveFlowStyleToDatabase(flowStyle: FlowStyleType) { export async function saveFlowStyleToDatabase(flowStyle: FlowStyleType) {
try { try {
const response = await fetch("/flow_styles/", { const response = await axios.post("/api/v1/flow_styles/", flowStyle, {
method: "POST",
headers: { headers: {
accept: "application/json", accept: "application/json",
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(flowStyle),
}); });
if (!response.ok) {
if (response.status !== 201) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
return response.json(); return response.data;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
throw error; throw error;
@ -271,7 +253,8 @@ export async function saveFlowStyleToDatabase(flowStyle: FlowStyleType) {
* @returns {Promise<AxiosResponse<any>>} A promise that resolves to an AxiosResponse containing the version information. * @returns {Promise<AxiosResponse<any>>} A promise that resolves to an AxiosResponse containing the version information.
*/ */
export async function getVersion() { export async function getVersion() {
return await fetch("/version"); const respnose = await axios.get("/api/v1/version");
return respnose.data;
} }
/** /**
@ -280,5 +263,5 @@ export async function getVersion() {
* @returns {Promise<AxiosResponse<any>>} A promise that resolves to an AxiosResponse containing the health status. * @returns {Promise<AxiosResponse<any>>} A promise that resolves to an AxiosResponse containing the health status.
*/ */
export async function getHealth() { export async function getHealth() {
return await fetch("/health"); return await axios.get("/health"); // Health is the only endpoint that doesn't require /api/v1
} }